diff --git a/frontend/src/utils/security.test.ts b/frontend/src/utils/security.test.ts
new file mode 100644
index 00000000..5ccf254a
--- /dev/null
+++ b/frontend/src/utils/security.test.ts
@@ -0,0 +1,13 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+
+import { protectProviderImageSrcInHTML } from './security.ts'
+
+test('protectProviderImageSrcInHTML uses a placeholder src for provider images', () => {
+ const html = '

'
+ const sanitized = protectProviderImageSrcInHTML(html)
+ const renderedSrc = sanitized.match(/
]*\ssrc="([^"]+)"/)?.[1]
+
+ assert.match(renderedSrc || '', /^data:image\/gif;base64,/)
+ assert.match(sanitized, /data-protected-src="local:\/\/10000\/exports\/a\.jpg"/)
+})
diff --git a/frontend/src/utils/security.ts b/frontend/src/utils/security.ts
index 48557439..0cfc37e7 100644
--- a/frontend/src/utils/security.ts
+++ b/frontend/src/utils/security.ts
@@ -8,7 +8,10 @@ import {
domPurifySecurityHooks,
domPurifySecurityOptions,
markdownDomPurifyConfig,
-} from '@/utils/markdownDomPurify';
+} 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;
// 配置 DOMPurify 的安全策略
const DOMPurifyConfig = {
@@ -91,23 +94,69 @@ export function sanitizeMarkdownHTML(html: string): string {
export function protectProviderImageSrcInHTML(html: string): string {
if (!html) return html;
- const decodeProviderURL = (raw: string): string =>
- raw
- .replace(///gi, '/')
- .replace(///g, '/')
- .replace(/&/g, '&')
- .replace(/"/g, '"');
return html.replace(
/
]*?)\ssrc=(["'])(local|minio|cos|tos|s3|oss|ks3|obs):(?:\/\/|//|//)([^"']+)\2([^>]*)>/gi,
(_m, before, quote, provider, restPathRaw, after) => {
const restPath = decodeProviderURL(restPathRaw);
const protectedSrc = `${provider}://${restPath}`;
- const fileProxyURL = `/files?${new URLSearchParams({ file_path: protectedSrc }).toString()}`;
- return `
`;
+ return `
`;
},
);
}
+function decodeProviderURL(raw: string): string {
+ return raw
+ .trim()
+ .replace(///gi, '/')
+ .replace(///g, '/')
+ .replace(/&/g, '&')
+ .replace(/"/g, '"');
+}
+
+function isProviderFileURL(url: string): boolean {
+ return PROVIDER_FILE_SCHEME_RE.test(url.trim());
+}
+
+function providerSourceFromImageSrc(src: string): string | null {
+ const decodedSrc = decodeProviderURL(src);
+ if (isProviderFileURL(decodedSrc)) {
+ return decodedSrc;
+ }
+
+ try {
+ const baseURL = typeof window !== 'undefined' ? window.location.origin : 'http://localhost';
+ const url = new URL(decodedSrc, baseURL);
+ const isFileProxy =
+ url.pathname === '/files' ||
+ /^\/api\/v1\/embed\/[^/]+\/files$/.test(url.pathname);
+ if (!isFileProxy) {
+ return null;
+ }
+
+ const filePath = (url.searchParams.get('file_path') || '').trim();
+ return isProviderFileURL(filePath) ? filePath : null;
+ } catch {
+ return null;
+ }
+}
+
+function normalizeProtectedImageElement(img: HTMLImageElement): string | null {
+ const protectedSrc = providerSourceFromImageSrc(
+ img.getAttribute('data-protected-src') || '',
+ );
+ const src = img.getAttribute('src') || '';
+ const sourceURL = protectedSrc || providerSourceFromImageSrc(src);
+ if (!sourceURL) {
+ return null;
+ }
+
+ img.setAttribute('data-protected-src', sourceURL);
+ if (!src.trim().startsWith('blob:')) {
+ img.setAttribute('src', PROVIDER_IMAGE_PLACEHOLDER);
+ }
+ return sourceURL;
+}
+
/**
* 转义 HTML 特殊字符
* @param text 需要转义的文本
@@ -152,7 +201,7 @@ export function isValidURL(url: string): boolean {
}
// 允许 provider:// 形式,由前端后续鉴权拉取并替换为 blob URL
- if (/^(local|minio|cos|tos|s3|oss|ks3|obs):\/\/\S+$/i.test(trimmed)) {
+ if (isProviderFileURL(trimmed)) {
return true;
}
@@ -308,9 +357,10 @@ export async function hydrateProtectedFileImages(
: getProtectedFileRequestHeaders();
await Promise.all(Array.from(images).map(async (img) => {
+ const normalizedSourceURL = normalizeProtectedImageElement(img);
const protectedSrc = (img.getAttribute('data-protected-src') || '').trim();
const src = (img.getAttribute('src') || '').trim();
- const sourceURL = protectedSrc || src;
+ const sourceURL = normalizedSourceURL || protectedSrc || src;
if (!sourceURL) {
return;
}
@@ -319,7 +369,7 @@ export async function hydrateProtectedFileImages(
}
img.dataset.authHydrated = '1';
- const isProviderScheme = /^(local|minio|cos|tos|s3|oss|ks3|obs):\/\//.test(sourceURL);
+ const isProviderScheme = isProviderFileURL(sourceURL);
const fileProxyBase = embed
? `/api/v1/embed/${embed.channelId}/files`
: '/files';
diff --git a/frontend/src/views/knowledge/wiki/WikiBrowser.vue b/frontend/src/views/knowledge/wiki/WikiBrowser.vue
index 539f0c00..ffbd3208 100644
--- a/frontend/src/views/knowledge/wiki/WikiBrowser.vue
+++ b/frontend/src/views/knowledge/wiki/WikiBrowser.vue
@@ -650,7 +650,7 @@ import { MessagePlugin } from 'tdesign-vue-next'
// height (title + 2-line summary + meta + padding) which keeps recycle
// mode cheap — no measurement overhead per item.
import { RecycleScroller } from 'vue-virtual-scroller'
-import { hydrateProtectedFileImages } from '@/utils/security'
+import { hydrateProtectedFileImages, sanitizeMarkdownHTML } from '@/utils/security'
import picturePreview from '@/components/picture-preview.vue'
import { createSessions } from '@/api/chat'
import ChatView from '@/views/chat/index.vue'
@@ -1263,8 +1263,10 @@ function renderMarkdown(content: string): string {
return `${display}`
})
- // Use marked to render the markdown to HTML
- return marked.parse(preprocessed, { breaks: true, async: false }) as string
+ // Use the shared markdown sanitizer before v-html inserts the content, so
+ // provider-backed images start as placeholders and hydrate into blob URLs.
+ const html = marked.parse(preprocessed, { breaks: true, async: false }) as string
+ return sanitizeMarkdownHTML(html)
}
async function openGraphDrawer(slug: string) {