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 = '';
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} `;