Skip to content

Commit 6ecc74d

Browse files
fix: disable open link button for unsafe link
Signed-off-by: Luka Trovic <[email protected]>
1 parent 1e47bd1 commit 6ecc74d

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/components/Link/LinkBubbleView.vue

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
</div>
1313
<!-- open link -->
1414
<NcButton
15-
:title="t('text', 'Open link')"
16-
:aria-label="t('text', 'Open link')"
15+
:disabled="!isSafeHref"
16+
:title="openLinkTitle"
17+
:aria-label="openLinkTitle"
1718
variant="tertiary"
1819
@click="openLink(href)">
1920
<template #icon>
@@ -98,6 +99,7 @@ import OpenInNewIcon from 'vue-material-design-icons/OpenInNew.vue'
9899
import PencilOutlineIcon from 'vue-material-design-icons/PencilOutline.vue'
99100
100101
import { useOpenLinkHandler } from '../../composables/useOpenLinkHandler.ts'
102+
import { PROTOCOLS_TO_LINK_TO } from '../../marks/Link.ts'
101103
import PreviewOptions from '../Editor/PreviewOptions.vue'
102104
103105
const PROTOCOLS_WITH_PREVIEW = ['http:', 'https:']
@@ -170,6 +172,27 @@ export default {
170172
return false
171173
}
172174
},
175+
176+
isSafeHref() {
177+
try {
178+
const url = new URL(this.href, window.location)
179+
return !!this.href && PROTOCOLS_TO_LINK_TO.includes(url.protocol)
180+
} catch {
181+
return false
182+
}
183+
},
184+
185+
openLinkTitle() {
186+
if (this.isSafeHref) {
187+
return t('text', 'Open link')
188+
}
189+
190+
if (!this.href) {
191+
return t('text', 'No link available to open')
192+
}
193+
194+
return t('text', 'Cannot open links with unsafe protocols')
195+
},
173196
},
174197
175198
watch: {

src/marks/Link.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { defaultMarkdownSerializer } from 'prosemirror-markdown'
1313
import { domHref, parseHref } from '../helpers/links.js'
1414
import { linkClicking } from '../plugins/links'
1515

16-
const PROTOCOLS_TO_LINK_TO = ['http:', 'https:', 'mailto:', 'tel:']
16+
export const PROTOCOLS_TO_LINK_TO = ['http:', 'https:', 'mailto:', 'tel:']
1717

1818
const extractHrefFromMatch = (match: ExtendedRegExpMatchArray) => {
1919
return { href: match.groups?.href }

0 commit comments

Comments
 (0)