From 23f6f7b44c746d793ad1bc3e9f10d7b4ccdab09f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 24 Oct 2026 12:19:10 +0200 Subject: [PATCH] [frontend] Prevent stored XSS in markdown directive (field hints and markdown links) (#1352) Co-authored-by: claude[bot] --- .../angular/markdown.directive.spec.ts | 16 +++++++++ .../framework/angular/markdown.directive.ts | 36 +++++++++---------- .../src/app/framework/utils/markdown.spec.ts | 25 +++++++++++++ frontend/src/app/framework/utils/markdown.ts | 21 ++++++++++- 4 files changed, 79 insertions(+), 19 deletions(-) diff --git a/frontend/src/app/framework/angular/markdown.directive.spec.ts b/frontend/src/app/framework/angular/markdown.directive.spec.ts index 97af10df2..4f513c418 100644 --- a/frontend/src/app/framework/angular/markdown.directive.spec.ts +++ b/frontend/src/app/framework/angular/markdown.directive.spec.ts @@ -44,6 +44,22 @@ describe('MarkdownDirective', () => { verifyTextRender('**bold**'); }); + it('should render HTML as text if optional', () => { + markdownDirective.optional = true; + markdownDirective.markdown = ''; + markdownDirective.ngOnChanges(); + + verifyTextRender(''); + }); + + it('should render HTML escaped if optional with exclamation', () => { + markdownDirective.optional = true; + markdownDirective.markdown = '!'; + markdownDirective.ngOnChanges(); + + verifyHtmlRender('<img src="x" onerror="alert(1)">'); + }); + it('should render if optional with exclamation', () => { markdownDirective.optional = true; markdownDirective.markdown = '!**bold**'; diff --git a/frontend/src/app/framework/angular/markdown.directive.ts b/frontend/src/app/framework/angular/markdown.directive.ts index 59cd94c47..9a4388037 100644 --- a/frontend/src/app/framework/angular/markdown.directive.ts +++ b/frontend/src/app/framework/angular/markdown.directive.ts @@ -31,31 +31,31 @@ export class MarkdownDirective { } public ngOnChanges() { - let html = ''; - - let markdown = this.markdown; - if (markdown) { - const hasExclamation = markdown.indexOf('!') === 0; - - if (hasExclamation && this.optional) { - markdown = markdown.substring(1); - } - - if (!markdown) { - html = markdown; - } else if (this.optional && !hasExclamation) { - html = markdown; - } else if (this.markdown) { - html = markdownRender(markdown, this.inline, this.trusted); - } + let markdown = this.markdown || ''; + + const hasExclamation = markdown.indexOf('!') === 0; + + // Optional markdown without exclamation is plain text and must never be interpreted as HTML. + if (this.optional && !hasExclamation) { + this.renderText(markdown); + return; + } + + if (this.optional) { + markdown = markdown.substring(1); } + const html = markdownRender(markdown, this.inline, this.trusted); const hasHtml = html.indexOf('<') >= 0 || html.indexOf('&') >= 0; if (hasHtml) { this.renderer.setProperty(this.element.nativeElement, 'innerHTML', html); } else { - this.renderer.setProperty(this.element.nativeElement, 'textContent', html); + this.renderText(html); } } + + private renderText(text: string) { + this.renderer.setProperty(this.element.nativeElement, 'textContent', text); + } } diff --git a/frontend/src/app/framework/utils/markdown.spec.ts b/frontend/src/app/framework/utils/markdown.spec.ts index 9fb0e40fe..78206e9ec 100644 --- a/frontend/src/app/framework/utils/markdown.spec.ts +++ b/frontend/src/app/framework/utils/markdown.spec.ts @@ -56,6 +56,31 @@ describe('Markdown', () => { expect(result).toEqual('squidex '); }); + it('should render relative link', () => { + const md = '[page](/app/page?a=1&b=2)'; + + const result = markdownRender(md, true); + + expect(result).toContain('href="/app/page?a=1&b=2"'); + }); + + [ + 'javascript:alert(1)', + 'JaVaScRiPt:alert(1)', + 'java\tscript:alert(1)', + 'javascript:alert(1)', + 'data:text/html,test', + 'vbscript:msgbox(1)', + ].forEach(href => { + it(`should not render link with unsafe url ${href}`, () => { + const md = `[click](${href})`; + + const result = markdownRender(md, true); + + expect(result).not.toContain(' { const md = '![{name}](https://localhost:5001/ai-images/dall-e/ea68c867-6472-4d77-a526-7c9d9c4698fe)'; diff --git a/frontend/src/app/framework/utils/markdown.ts b/frontend/src/app/framework/utils/markdown.ts index 0f199c91c..ee090f7ec 100644 --- a/frontend/src/app/framework/utils/markdown.ts +++ b/frontend/src/app/framework/utils/markdown.ts @@ -8,12 +8,31 @@ import { marked, Renderer, Tokens } from 'marked'; import { MathHelper } from './math-helper'; +const SAFE_SCHEME = /^(https?|mailto):/i; +const ANY_SCHEME = /^[^/?#]*[:&]/; + +function isUnsafeUrl(url: string) { + // Browsers ignore whitespace and control characters in the scheme, e.g. "java\tscript:". + const normalized = Array.from(url).filter(c => c.charCodeAt(0) > 32).join(''); + + if (SAFE_SCHEME.test(normalized)) { + return false; + } + + // Also treat entities as a scheme, because "javascript:" is decoded by the browser. + return ANY_SCHEME.test(normalized); +} + class CustomRenderer extends Renderer { public static readonly INSTANCE = new CustomRenderer(); public link({ href, tokens }: Tokens.Link): string { const text = this.parser.parseInline(tokens); - if (href && href.startsWith('mailto')) { + if (!href || isUnsafeUrl(href)) { + return text; + } + + if (href.startsWith('mailto')) { return `${text}`; } else { return `${text} `;