-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid rendering JavaScript URLs as clickable links
To avoid running JavaScript taken from JSON files downloaded from the internet in the client we now try to render URLs only as clickable links if the protocol of the URL is safe, which more concretely means HTTP or HTTPS. Other URLs are rendered as plain text. This commit only covers links that are obviously treated as links to external resources. To avoid duplication, this introduces a new component, SafeLink, that takes the URL and optionally id and target attributes and renders a suitable a-element if the protocol is safe and text otherwise.
- Loading branch information
1 parent
5115e53
commit 1f72bae
Showing
4 changed files
with
45 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!-- | ||
This file is Free Software under the MIT License | ||
without warranty, see README.md and LICENSES/MIT.txt for details. | ||
SPDX-License-Identifier: MIT | ||
SPDX-FileCopyrightText: 2024 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de> | ||
Software-Engineering: 2024 Intevation GmbH <https://intevation.de> | ||
--> | ||
|
||
<!-- | ||
Component that renders a URL as a clickable if the URL is safe to click. | ||
Safe to click here means that it uses one of the following protocols: | ||
http, https | ||
Other URLs are renders a plain text. | ||
--> | ||
|
||
<script lang="ts"> | ||
export let url = undefined | ||
export let id = undefined | ||
export let target = undefined | ||
// Protocols that are considered safe for URLs that should be | ||
// clickable. | ||
const safeProtocols = ["https:", "http:"] | ||
let protocol = undefined | ||
if (URL.canParse(url)) { | ||
protocol = new URL(url).protocol | ||
} | ||
</script> | ||
|
||
{#if safeProtocols.includes(protocol)} | ||
<a id={id} target={target} href={url}>{url}</a> | ||
{:else} | ||
{url} | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters