Browse Source

[frontend] Prevent stored XSS in markdown directive (field hints and markdown links) (#1352)

Co-authored-by: claude[bot] <claude[bot]@users.noreply.github.com>
pull/1354/head
github-actions[bot] 1 month ago
committed by GitHub
parent
commit
23f6f7b44c
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 16
      frontend/src/app/framework/angular/markdown.directive.spec.ts
  2. 36
      frontend/src/app/framework/angular/markdown.directive.ts
  3. 25
      frontend/src/app/framework/utils/markdown.spec.ts
  4. 21
      frontend/src/app/framework/utils/markdown.ts

16
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 = '<img src="x" onerror="alert(1)">';
markdownDirective.ngOnChanges();
verifyTextRender('<img src="x" onerror="alert(1)">');
});
it('should render HTML escaped if optional with exclamation', () => {
markdownDirective.optional = true;
markdownDirective.markdown = '!<img src="x" onerror="alert(1)">';
markdownDirective.ngOnChanges();
verifyHtmlRender('&lt;img src=&quot;x&quot; onerror=&quot;alert(1)&quot;&gt;');
});
it('should render if optional with exclamation', () => {
markdownDirective.optional = true;
markdownDirective.markdown = '!**bold**';

36
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);
}
}

25
frontend/src/app/framework/utils/markdown.spec.ts

@ -56,6 +56,31 @@ describe('Markdown', () => {
expect(result).toEqual('<a href="https://squidex.io" target="_blank", rel="noopener">squidex <i class="icon-external-link"></i></a>');
});
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&amp;b=2"');
});
[
'javascript:alert(1)',
'JaVaScRiPt:alert(1)',
'java\tscript:alert(1)',
'javascript&#58;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('<a');
});
});
it('should render image', () => {
const md = '![{name}](https://localhost:5001/ai-images/dall-e/ea68c867-6472-4d77-a526-7c9d9c4698fe)';

21
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&#58;" 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 `<a href="${href}">${text}</a>`;
} else {
return `<a href="${href}" target="_blank", rel="noopener">${text} <i class="icon-external-link"></i></a>`;

Loading…
Cancel
Save