Skip to content

Commit

Permalink
[0.10] Ban javascript URLs in @lexical/link (#4342)
Browse files Browse the repository at this point in the history
  • Loading branch information
acywatson committed Apr 18, 2023
1 parent 6a239b6 commit 6017410
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/lexical-link/src/__tests__/unit/LexicalLinkNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ describe('LexicalLinkNode tests', () => {
});
});

test('LinkNode.createDOM() sanitizes javascript: URLs', async () => {
const {editor} = testEnv;

await editor.update(() => {
// eslint-disable-next-line no-script-url
const linkNode = new LinkNode('javascript:alert(0)');
expect(linkNode.createDOM(editorConfig).outerHTML).toBe(
'<a href="about:blank" class="my-link-class"></a>',
);
});
});

test('LinkNode.updateDOM()', async () => {
const {editor} = testEnv;

Expand Down
23 changes: 22 additions & 1 deletion packages/lexical-link/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ export type SerializedLinkNode = Spread<
Spread<LinkAttributes, SerializedElementNode>
>;

const SUPPORTED_URL_PROTOCOLS = new Set([
'http:',
'https:',
'mailto:',
'sms:',
'tel:',
]);

/** @noInheritDoc */
export class LinkNode extends ElementNode {
/** @internal */
Expand Down Expand Up @@ -78,7 +86,7 @@ export class LinkNode extends ElementNode {

createDOM(config: EditorConfig): HTMLAnchorElement {
const element = document.createElement('a');
element.href = this.__url;
element.href = this.sanitizeUrl(this.__url);
if (this.__target !== null) {
element.target = this.__target;
}
Expand Down Expand Up @@ -154,6 +162,19 @@ export class LinkNode extends ElementNode {
return node;
}

sanitizeUrl(url: string): string {
try {
const parsedUrl = new URL(url);
// eslint-disable-next-line no-script-url
if (!SUPPORTED_URL_PROTOCOLS.has(parsedUrl.protocol)) {
return 'about:blank';
}
} catch (e) {
return 'https://';
}
return url;
}

exportJSON(): SerializedLinkNode | SerializedAutoLinkNode {
return {
...super.exportJSON(),
Expand Down

2 comments on commit 6017410

@vercel
Copy link

@vercel vercel bot commented on 6017410 Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical-git-main-fbopensource.vercel.app
lexical.dev
lexical-fbopensource.vercel.app
lexicaljs.com
www.lexical.dev
lexicaljs.org

@vercel
Copy link

@vercel vercel bot commented on 6017410 Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

lexical-playground-git-main-fbopensource.vercel.app
lexical-playground.vercel.app
playground.lexical.dev
lexical-playground-fbopensource.vercel.app

Please sign in to comment.