Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Link extension's commands not respecting XSS prevention via unallowed protocols #5945

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-seals-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/extension-link": patch
---

Added checks for allowed protocols in link commands & exported isValidUri helper for manual checks outside of the extension
8 changes: 6 additions & 2 deletions demos/src/Marks/Link/React/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ export default () => {
}

// update link
editor.chain().focus().extendMarkRange('link').setLink({ href: url })
.run()
try {
editor.chain().focus().extendMarkRange('link').setLink({ href: url })
.run()
} catch (e) {
alert(e.message)
}
}, [editor])

if (!editor) {
Expand Down
14 changes: 13 additions & 1 deletion packages/extension-link/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ declare module '@tiptap/core' {
// eslint-disable-next-line no-control-regex
const ATTR_WHITESPACE = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g

function isAllowedUri(uri: string | undefined, protocols?: LinkOptions['protocols']) {
export function isAllowedUri(uri: string | undefined, protocols?: LinkOptions['protocols']) {
const allowedProtocols: string[] = [
'http',
'https',
Expand Down Expand Up @@ -322,11 +322,23 @@ export const Link = Mark.create<LinkOptions>({
return {
setLink:
attributes => ({ chain }) => {
const { href } = attributes

if (!isAllowedUri(href, this.options.protocols)) {
throw new Error('Invalid protocol')
}
bdbch marked this conversation as resolved.
Show resolved Hide resolved

return chain().setMark(this.name, attributes).setMeta('preventAutolink', true).run()
},

toggleLink:
attributes => ({ chain }) => {
const { href } = attributes

if (!isAllowedUri(href, this.options.protocols)) {
throw new Error('Invalid protocol')
}
bdbch marked this conversation as resolved.
Show resolved Hide resolved

return chain()
.toggleMark(this.name, attributes, { extendEmptyMarkRange: true })
.setMeta('preventAutolink', true)
Expand Down
Loading