Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
feat(security): enhance image source protection for storage backends
wizardchen6349fff00264
diff --git a/frontend/src/utils/markdownDomPurify.ts b/frontend/src/utils/markdownDomPurify.tsindex 80e3be9ad..8b97d9591 100644--- a/frontend/src/utils/markdownDomPurify.ts+++ b/frontend/src/utils/markdownDomPurify.ts@@ -2,7 +2,7 @@ export const domPurifyForbidTags = ['script', 'style', 'object', 'embed', 'form'export const domPurifyForbidAttr = ['onerror', 'onload', 'onclick', 'onmouseover', 'onfocus', 'onblur'] as const;export const domPurifyAllowedUriRegexp =- /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp|blob):|(?:local|minio|cos|tos|s3|oss|ks3|obs):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i;+ /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp|blob):|(?:storage|local|minio|cos|tos|s3|oss|ks3|obs):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i;/** Shared DOMPurify security options (FORBID_*, URI scheme, DOM flags). */export const domPurifySecurityOptions = {diff --git a/frontend/src/utils/security.test.ts b/frontend/src/utils/security.test.tsindex 5ccf254a7..743c10bf8 100644--- a/frontend/src/utils/security.test.ts+++ b/frontend/src/utils/security.test.ts@@ -11,3 +11,16 @@ test('protectProviderImageSrcInHTML uses a placeholder src for provider images',assert.match(renderedSrc || '', /^data:image\/gif;base64,/)assert.match(sanitized, /data-protected-src="local:\/\/10000\/exports\/a\.jpg"/)})++test('protectProviderImageSrcInHTML uses a placeholder src for storage-backend images', () => {+ const html =+ '<p><img alt="preview" src="storage://c0d93536-702c-4977-aa5e-fe670073c3cb/local://10000/exports/a.png"></p>'+ const sanitized = protectProviderImageSrcInHTML(html)+ const renderedSrc = sanitized.match(/<img[^>]*\ssrc="([^"]+)"/)?.[1]++ assert.match(renderedSrc || '', /^data:image\/gif;base64,/)+ assert.match(+ sanitized,+ /data-protected-src="storage:\/\/c0d93536-702c-4977-aa5e-fe670073c3cb\/local:\/\/10000\/exports\/a\.png"/,+ )+})diff --git a/frontend/src/utils/security.ts b/frontend/src/utils/security.tsindex d26d6c3b2..ee1f8251e 100644--- a/frontend/src/utils/security.ts+++ b/frontend/src/utils/security.ts@@ -12,7 +12,20 @@ import {} 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;+const PROVIDER_SCHEME_PATTERN = 'local|minio|cos|tos|s3|oss|ks3|obs';+const PROVIDER_FILE_SCHEME_RE = new RegExp(`^(${PROVIDER_SCHEME_PATTERN}):\\/\\/\\S+$`, 'i');+const STORAGE_BACKEND_FILE_SCHEME_RE = new RegExp(+ `^storage:\\/\\/[0-9A-Za-z_-]+\\/(${PROVIDER_SCHEME_PATTERN}):\\/\\/\\S+$`,+ 'i',+);+const PROVIDER_IMG_SRC_RE = new RegExp(+ `<img\\b([^>]*?)\\ssrc=(["'])(${PROVIDER_SCHEME_PATTERN}):(?:\\/\\/|//|//)([^"']+)\\2([^>]*)>`,+ 'gi',+);+const STORAGE_BACKEND_IMG_SRC_RE = new RegExp(+ `<img\\b([^>]*?)\\ssrc=(["'])storage:\\/\\/([0-9A-Za-z_-]+)\\/(${PROVIDER_SCHEME_PATTERN}):(?:\\/\\/|//|//)([^"']+)\\2([^>]*)>`,+ 'gi',+);type SecurityHooks = {beforeSanitizeElements: NodeHook;@@ -120,30 +133,50 @@ export function sanitizeMarkdownHTML(html: string): string {}}+function buildProtectedImageTag(+ before: string,+ quote: string,+ protectedSrc: string,+ after: string,+): string {+ // A definitive 404 should not leave a skeleton behind. Streaming+ // re-renders call this function repeatedly, so remember the missing+ // source until the explicit end-of-stream retry clears the cache.+ if (protectedFileMissingSources.has(protectedSrc)) {+ return '';+ }+ // Reuse the already-hydrated blob if we have one, so repeated re-renders+ // (typewriter streaming) keep the same stable image instead of flashing+ // back to the placeholder every frame.+ const cachedBlobURL = protectedFileBlobBySource.get(protectedSrc);+ if (cachedBlobURL) {+ return `<img${before} src=${quote}${cachedBlobURL}${quote} data-protected-src=${quote}${protectedSrc}${quote}${after}>`;+ }+ // Not hydrated yet: render the 1x1 placeholder but tag it so CSS can give+ // it a stable skeleton box. Otherwise width:auto/height:auto collapse the+ // 1x1 gif to a ~1px line that violently jumps to full size once loaded.+ return `<img${before} src=${quote}${PROVIDER_IMAGE_PLACEHOLDER}${quote} data-protected-src=${quote}${protectedSrc}${quote} data-img-loading=${quote}1${quote}${after}>`;+}+export function protectProviderImageSrcInHTML(html: string): string {if (!html) return html;- return html.replace(- /<img\b([^>]*?)\ssrc=(["'])(local|minio|cos|tos|s3|oss|ks3|obs):(?:\/\/|//|//)([^"']+)\2([^>]*)>/gi,+ const withProviderImages = html.replace(+ PROVIDER_IMG_SRC_RE,(_m, before, quote, provider, restPathRaw, after) => {const restPath = decodeProviderURL(restPathRaw);- const protectedSrc = `${provider}://${restPath}`;- // A definitive 404 should not leave a skeleton behind. Streaming- // re-renders call this function repeatedly, so remember the missing- // source until the explicit end-of-stream retry clears the cache.- if (protectedFileMissingSources.has(protectedSrc)) {- return '';- }- // Reuse the already-hydrated blob if we have one, so repeated re-renders- // (typewriter streaming) keep the same stable image instead of flashing- // back to the placeholder every frame.- const cachedBlobURL = protectedFileBlobBySource.get(protectedSrc);- if (cachedBlobURL) {- return `<img${before} src=${quote}${cachedBlobURL}${quote} data-protected-src=${quote}${protectedSrc}${quote}${after}>`;- }- // Not hydrated yet: render the 1x1 placeholder but tag it so CSS can give- // it a stable skeleton box. Otherwise width:auto/height:auto collapse the- // 1x1 gif to a ~1px line that violently jumps to full size once loaded.- return `<img${before} src=${quote}${PROVIDER_IMAGE_PLACEHOLDER}${quote} data-protected-src=${quote}${protectedSrc}${quote} data-img-loading=${quote}1${quote}${after}>`;+ return buildProtectedImageTag(before, quote, `${provider}://${restPath}`, after);+ },+ );+ return withProviderImages.replace(+ STORAGE_BACKEND_IMG_SRC_RE,+ (_m, before, quote, backendID, provider, restPathRaw, after) => {+ const restPath = decodeProviderURL(restPathRaw);+ return buildProtectedImageTag(+ before,+ quote,+ `storage://${backendID}/${provider}://${restPath}`,+ after,+ );},);}@@ -158,7 +191,8 @@ function decodeProviderURL(raw: string): string {}function isProviderFileURL(url: string): boolean {- return PROVIDER_FILE_SCHEME_RE.test(url.trim());+ const trimmed = url.trim();+ return PROVIDER_FILE_SCHEME_RE.test(trimmed) || STORAGE_BACKEND_FILE_SCHEME_RE.test(trimmed);}function providerSourceFromImageSrc(src: string): string | null {@@ -465,7 +499,7 @@ export async function hydrateProtectedFileImages(}const images = root.querySelectorAll<HTMLImageElement>(- 'img[data-protected-src], img[src^="local://"], img[src^="minio://"], img[src^="cos://"], img[src^="tos://"], img[src^="s3://"], img[src^="oss://"], img[src^="ks3://"], img[src^="obs://"]',+ 'img[data-protected-src], img[src^="storage://"], img[src^="local://"], img[src^="minio://"], img[src^="cos://"], img[src^="tos://"], img[src^="s3://"], img[src^="oss://"], img[src^="ks3://"], img[src^="obs://"]',);if (!images.length) {return;