Appearance
safeLinkUrl ​
Overview ​
safeLinkUrl turns a link stored in portal content (dashboard tiles and banners, entered by workspace admins) into an href that can only open a web page. A value with any scheme other than http or https (for example javascript: or data:) gets no href at all, so clicking it does nothing.
Added in commit df54f0c on the security audit branch (PR #144). Before that the tile and banner links bound the stored value directly (:href="tile.url", :href="banner.url").
File Information ​
- Path:
app/utils/safeLinkUrl.ts - Export:
safeLinkUrl(url?: string | null): string | undefined - Auto-import: yes (
utils/**is inimports.dirs)
Rules ​
Checked in this order:
| # | Rule | Result |
|---|---|---|
| 1 | trimmed = (url ?? '').trim(); empty | undefined |
| 2 | compact = trimmed with every character of code 32 or lower removed (spaces, tabs, newlines, other control characters) | used only for the next two checks |
| 3 | compact matches /^https?:\/\//i | trimmed, unchanged |
| 4 | compact matches /^[a-z][a-z0-9+-]*:/i (any other scheme) | undefined |
| 5 | anything else | `https://${trimmed}` |
Removing control characters before the scheme test means a value such as java + tab + script:... is still seen as a javascript: scheme and dropped.
Examples ​
| Stored value | href |
|---|---|
https://example.com/guide | https://example.com/guide |
HTTP://example.com | HTTP://example.com |
www.example.com/page | https://www.example.com/page |
example.com:8080/path | https://example.com:8080/path (the . stops the scheme match) |
javascript:alert(1) | undefined |
mailto:[email protected], tel:123 | undefined (only web links are kept) |
localhost:3000 | undefined (looks like a scheme) |
/acme/folders | https:///acme/folders (relative paths are not supported) |
"", null | undefined |
Callers ​
| File | Element |
|---|---|
app/components/dashboard/Tile.vue | <a :href="safeLinkUrl(tile.url)" target="_blank" rel="noopener noreferrer"> |
app/pages/[brand_name]/index.vue | banner slide <a :href="safeLinkUrl(banner.url)" target="_blank" rel="noopener noreferrer"> |
Both links open in a new tab with rel="noopener noreferrer", so the opened page gets no window.opener and no referrer.