fix(frontend): open chat reference links in new tabs
Prevent in-chat navigation from replacing the active conversation by opening knowledge-base and citation links in a new tab with noopener/noreferrer, and apply the same policy to sanitized markdown links.
This commit is contained in:
@@ -3,6 +3,7 @@ import test from 'node:test';
|
||||
import {
|
||||
domPurifyAllowedUriRegexp,
|
||||
markdownDomPurifyConfig,
|
||||
markdownDomPurifySecurityHooks,
|
||||
} from './markdownDomPurify.ts';
|
||||
|
||||
test('markdownDomPurifyConfig FORBID_TAGS includes script', () => {
|
||||
@@ -15,3 +16,19 @@ test('ALLOWED_URI_REGEXP allows s3:// and rejects javascript:', () => {
|
||||
assert.match('s3://bucket/key', re);
|
||||
assert.doesNotMatch('javascript:alert(1)', re);
|
||||
});
|
||||
|
||||
test('chat markdown links always open in a new tab', () => {
|
||||
const attributes = new Map([['href', '/platform/knowledge-bases/kb-1']]);
|
||||
const anchor = {
|
||||
tagName: 'A',
|
||||
getAttribute: (name) => attributes.get(name) ?? null,
|
||||
setAttribute: (name, value) => attributes.set(name, value),
|
||||
hasAttribute: (name) => attributes.has(name),
|
||||
removeAttribute: (name) => attributes.delete(name),
|
||||
};
|
||||
|
||||
markdownDomPurifySecurityHooks.afterSanitizeElements(anchor);
|
||||
|
||||
assert.equal(attributes.get('target'), '_blank');
|
||||
assert.equal(attributes.get('rel'), 'noopener noreferrer');
|
||||
});
|
||||
|
||||
@@ -20,27 +20,45 @@ export const domPurifySecurityOptions = {
|
||||
|
||||
/** Shared DOMPurify hooks: strip scripts/event attrs; noopener links; alt on images. */
|
||||
export const domPurifySecurityHooks = {
|
||||
beforeSanitizeElements: (currentNode: Element) => {
|
||||
if (currentNode.tagName === 'SCRIPT') {
|
||||
currentNode.remove();
|
||||
return null;
|
||||
beforeSanitizeElements: (currentNode: Node) => {
|
||||
if (!('tagName' in currentNode) || !('hasAttribute' in currentNode)) return;
|
||||
const element = currentNode as Element;
|
||||
if (element.tagName === 'SCRIPT') {
|
||||
element.remove();
|
||||
return;
|
||||
}
|
||||
domPurifyForbidAttr.forEach((attr) => {
|
||||
if (currentNode.hasAttribute(attr)) {
|
||||
currentNode.removeAttribute(attr);
|
||||
if (element.hasAttribute(attr)) {
|
||||
element.removeAttribute(attr);
|
||||
}
|
||||
});
|
||||
},
|
||||
afterSanitizeElements: (currentNode: Element) => {
|
||||
if (currentNode.tagName === 'A') {
|
||||
const href = currentNode.getAttribute('href');
|
||||
afterSanitizeElements: (currentNode: Node) => {
|
||||
if (!('tagName' in currentNode) || !('getAttribute' in currentNode)) return;
|
||||
const element = currentNode as Element;
|
||||
if (element.tagName === 'A') {
|
||||
const href = element.getAttribute('href');
|
||||
if (href && href.startsWith('http')) {
|
||||
currentNode.setAttribute('rel', 'noopener noreferrer');
|
||||
currentNode.setAttribute('target', '_blank');
|
||||
element.setAttribute('rel', 'noopener noreferrer');
|
||||
element.setAttribute('target', '_blank');
|
||||
}
|
||||
}
|
||||
if (currentNode.tagName === 'IMG' && !currentNode.getAttribute('alt')) {
|
||||
currentNode.setAttribute('alt', '');
|
||||
if (element.tagName === 'IMG' && !element.getAttribute('alt')) {
|
||||
element.setAttribute('alt', '');
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
/** Chat markdown links must never replace the conversation page. */
|
||||
export const markdownDomPurifySecurityHooks = {
|
||||
...domPurifySecurityHooks,
|
||||
afterSanitizeElements: (currentNode: Node) => {
|
||||
domPurifySecurityHooks.afterSanitizeElements(currentNode);
|
||||
if (!('tagName' in currentNode) || !('getAttribute' in currentNode)) return;
|
||||
const element = currentNode as Element;
|
||||
if (element.tagName === 'A' && element.getAttribute('href')) {
|
||||
element.setAttribute('rel', 'noopener noreferrer');
|
||||
element.setAttribute('target', '_blank');
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -79,5 +97,4 @@ export const markdownDomPurifyConfig = {
|
||||
],
|
||||
USE_PROFILES: { html: true, svg: true, mathMl: true },
|
||||
...domPurifySecurityOptions,
|
||||
HOOKS: domPurifySecurityHooks,
|
||||
};
|
||||
|
||||
@@ -3,16 +3,37 @@
|
||||
*/
|
||||
|
||||
import DOMPurify from 'dompurify';
|
||||
import type { Config } from 'dompurify';
|
||||
import type { Config, NodeHook } from 'dompurify';
|
||||
import {
|
||||
domPurifySecurityHooks,
|
||||
domPurifySecurityOptions,
|
||||
markdownDomPurifyConfig,
|
||||
markdownDomPurifySecurityHooks,
|
||||
} from './markdownDomPurify.ts';
|
||||
|
||||
const PROVIDER_IMAGE_PLACEHOLDER = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
|
||||
const PROVIDER_FILE_SCHEME_RE = /^(local|minio|cos|tos|s3|oss|ks3|obs):\/\/\S+$/i;
|
||||
|
||||
type SecurityHooks = {
|
||||
beforeSanitizeElements: NodeHook;
|
||||
afterSanitizeElements: NodeHook;
|
||||
};
|
||||
|
||||
function sanitizeWithSecurityHooks(
|
||||
html: string,
|
||||
config: Config,
|
||||
hooks: SecurityHooks,
|
||||
): string {
|
||||
DOMPurify.addHook('beforeSanitizeElements', hooks.beforeSanitizeElements);
|
||||
DOMPurify.addHook('afterSanitizeElements', hooks.afterSanitizeElements);
|
||||
try {
|
||||
return DOMPurify.sanitize(html, config);
|
||||
} finally {
|
||||
DOMPurify.removeHook('afterSanitizeElements', hooks.afterSanitizeElements);
|
||||
DOMPurify.removeHook('beforeSanitizeElements', hooks.beforeSanitizeElements);
|
||||
}
|
||||
}
|
||||
|
||||
// 配置 DOMPurify 的安全策略
|
||||
const DOMPurifyConfig = {
|
||||
// 允许的标签
|
||||
@@ -54,7 +75,6 @@ const DOMPurifyConfig = {
|
||||
],
|
||||
USE_PROFILES: { html: true, svg: true, mathMl: true },
|
||||
...domPurifySecurityOptions,
|
||||
HOOKS: domPurifySecurityHooks,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -69,7 +89,11 @@ export function sanitizeHTML(html: string): string {
|
||||
|
||||
try {
|
||||
const preparedHTML = protectProviderImageSrcInHTML(html);
|
||||
return DOMPurify.sanitize(preparedHTML, DOMPurifyConfig as unknown as Config);
|
||||
return sanitizeWithSecurityHooks(
|
||||
preparedHTML,
|
||||
DOMPurifyConfig as unknown as Config,
|
||||
domPurifySecurityHooks,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('HTML sanitization failed:', error);
|
||||
// 如果清理失败,返回转义的纯文本
|
||||
@@ -85,7 +109,11 @@ export function sanitizeMarkdownHTML(html: string): string {
|
||||
|
||||
try {
|
||||
const preparedHTML = protectProviderImageSrcInHTML(html);
|
||||
return DOMPurify.sanitize(preparedHTML, markdownDomPurifyConfig as unknown as Config);
|
||||
return sanitizeWithSecurityHooks(
|
||||
preparedHTML,
|
||||
markdownDomPurifyConfig as unknown as Config,
|
||||
markdownDomPurifySecurityHooks,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Markdown HTML sanitization failed:', error);
|
||||
return escapeHTML(html);
|
||||
|
||||
Reference in New Issue
Block a user