Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(frontend): keep target="_blank" on sanitized markdown links (#3818)

rill9a2cd2006373
diff --git a/frontend/src/utils/markdownDomPurify.test.mjs b/frontend/src/utils/markdownDomPurify.test.mjs
index cb82a35cb..61f73cdef 100644
--- a/frontend/src/utils/markdownDomPurify.test.mjs
+++ b/frontend/src/utils/markdownDomPurify.test.mjs
@@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
import test from 'node:test';
import {
domPurifyAllowedUriRegexp,
+ domPurifySecurityHooks,
markdownDomPurifyConfig,
markdownDomPurifySecurityHooks,
} from './markdownDomPurify.ts';
@@ -17,6 +18,50 @@ test('ALLOWED_URI_REGEXP allows s3:// and rejects javascript:', () => {
assert.doesNotMatch('javascript:alert(1)', re);
});
+test('markdownDomPurifyConfig keeps hook-added target attribute', () => {
+ // USE_PROFILES replaces ALLOWED_ATTR, so target must come from ADD_ATTR.
+ assert.ok(markdownDomPurifyConfig.ADD_ATTR.includes('target'));
+});
+
+test('author-supplied target never survives; only the hook adds _blank with noopener', () => {
+ // ADD_ATTR allows target globally, so the before hook must drop any
+ // author value. SVG <a> (lowercase tagName), <area> and non-"http" hrefs
+ // never reach the after hook's rewrite and would otherwise keep it.
+ const cases = [
+ { tagName: 'a', href: 'https://evil.example' },
+ { tagName: 'AREA', href: 'https://evil.example' },
+ { tagName: 'A', href: '//evil.example' },
+ { tagName: 'A', href: 'HTTPS://evil.example' },
+ { tagName: 'A', href: ' https://evil.example' },
+ { tagName: 'A', href: '/relative' },
+ { tagName: 'DIV', href: null },
+ ];
+ const hookSets = {
+ base: domPurifySecurityHooks,
+ markdown: markdownDomPurifySecurityHooks,
+ };
+ for (const [name, hooks] of Object.entries(hookSets)) {
+ for (const { tagName, href } of cases) {
+ const attributes = new Map([['target', '_top'], ['rel', 'opener']]);
+ if (href !== null) attributes.set('href', href);
+ const el = {
+ tagName,
+ getAttribute: (n) => attributes.get(n) ?? null,
+ setAttribute: (n, v) => attributes.set(n, v),
+ hasAttribute: (n) => attributes.has(n),
+ removeAttribute: (n) => attributes.delete(n),
+ };
+ hooks.beforeSanitizeElements(el);
+ hooks.afterSanitizeElements(el);
+ const label = `${name} ${tagName} ${JSON.stringify(href)}`;
+ if (attributes.has('target')) {
+ assert.equal(attributes.get('target'), '_blank', label);
+ assert.equal(attributes.get('rel'), 'noopener noreferrer', label);
+ }
+ }
+ }
+});
+
test('chat markdown links always open in a new tab', () => {
const attributes = new Map([['href', '/platform/knowledge-bases/kb-1']]);
const anchor = {
diff --git a/frontend/src/utils/markdownDomPurify.ts b/frontend/src/utils/markdownDomPurify.ts
index 5c37edd7d..7438d8b7d 100644
--- a/frontend/src/utils/markdownDomPurify.ts
+++ b/frontend/src/utils/markdownDomPurify.ts
@@ -32,6 +32,12 @@ export const domPurifySecurityHooks = {
element.removeAttribute(attr);
}
});
+ // target is allowed only so the afterSanitizeElements hook can add
+ // target="_blank" together with rel="noopener noreferrer"; never keep an
+ // author-supplied value (SVG <a>, <area>, non-http hrefs skip the hook).
+ if (element.hasAttribute('target')) {
+ element.removeAttribute('target');
+ }
},
afterSanitizeElements: (currentNode: Node) => {
if (!('tagName' in currentNode) || !('getAttribute' in currentNode)) return;
@@ -102,5 +108,9 @@ export const markdownDomPurifyConfig = {
'mathvariant', 'encoding', 'aria-hidden',
],
USE_PROFILES: { html: true, svg: true, mathMl: true },
+ // USE_PROFILES 会整体替换 ALLOWED_ATTR,而 html profile 默认不含 target,
+ // 导致 afterSanitizeElements hook 加上的 target="_blank" 在随后的
+ // _sanitizeAttributes 中被剥离。用 ADD_ATTR 在 profile 之上追加该属性。
+ ADD_ATTR: ['target'],
...domPurifySecurityOptions,
};
diff --git a/frontend/src/utils/security.test.ts b/frontend/src/utils/security.test.ts
index 5df0dd4d2..7ab26e4ae 100644
--- a/frontend/src/utils/security.test.ts
+++ b/frontend/src/utils/security.test.ts
@@ -1,7 +1,7 @@
import assert from 'node:assert/strict'
import test from 'node:test'
-import { protectProviderImageSrcInHTML } from './security.ts'
+import { documentPreviewDomPurifyConfig, protectProviderImageSrcInHTML } from './security.ts'
test('protectProviderImageSrcInHTML uses a placeholder src for provider images', () => {
const html = '<p><img alt="preview" src="local://10000/exports/a.jpg"></p>'
@@ -36,3 +36,11 @@ test('protectProviderImageSrcInHTML uses a placeholder src for resource referenc
/data-protected-src="resource:\/\/AbCdEfGhIjKlMnOpQrStUv"/,
)
})
+
+test('document preview sanitizer keeps target on links alongside image attributes', () => {
+ // USE_PROFILES replaces ALLOWED_ATTR, so target must come from ADD_ATTR or
+ // the hook-added target="_blank" is stripped again.
+ for (const attr of ['target', 'loading', 'decoding', 'fetchpriority']) {
+ assert.ok(documentPreviewDomPurifyConfig.ADD_ATTR.includes(attr), attr)
+ }
+})
diff --git a/frontend/src/utils/security.ts b/frontend/src/utils/security.ts
index de42c6ef7..4e58d3495 100644
--- a/frontend/src/utils/security.ts
+++ b/frontend/src/utils/security.ts
@@ -93,6 +93,10 @@ const DOMPurifyConfig = {
'mathvariant', 'encoding', 'aria-hidden'
],
USE_PROFILES: { html: true, svg: true, mathMl: true },
+ // USE_PROFILES 会整体替换 ALLOWED_ATTR,而 html profile 默认不含 target,
+ // 导致 afterSanitizeElements hook 加上的 target="_blank" 在随后的
+ // _sanitizeAttributes 中被剥离。用 ADD_ATTR 在 profile 之上追加该属性。
+ ADD_ATTR: ['target'],
...domPurifySecurityOptions,
};
@@ -129,9 +133,10 @@ export function applyDocumentPreviewImageAttributes(currentNode: Node): void {
element.setAttribute('fetchpriority', 'low');
}
-const documentPreviewDomPurifyConfig = {
+export const documentPreviewDomPurifyConfig = {
...DOMPurifyConfig,
- ADD_ATTR: [...DOCUMENT_PREVIEW_IMAGE_ATTRS],
+ // 在基础 ADD_ATTR 上追加,而不是覆盖,否则链接的 target="_blank" 又会被剥掉。
+ ADD_ATTR: [...DOMPurifyConfig.ADD_ATTR, ...DOCUMENT_PREVIEW_IMAGE_ATTRS],
};
const documentPreviewSecurityHooks: SecurityHooks = {