refactor: simplify image handling in _resolve_images function

- Removed the image storage handling from the `_resolve_images` function, which now only decodes images and returns them as inline bytes.
- Updated the function's docstring to clarify that image persistence is managed by the Go App, and the return value for `image_dir_path` is always empty.
- Adjusted related gRPC and protobuf files to remove the `ConvertToPDF` method and its associated types, streamlining the API.
- Enhanced security measures in the frontend by implementing a placeholder for provider images and ensuring proper hydration of protected file images.

These changes improve the clarity and efficiency of image processing and API interactions within the application.
This commit is contained in:
wizardchen
2026-03-03 21:30:27 +08:00
committed by lyingbug
parent 894d2c060a
commit 4192cfd072
26 changed files with 835 additions and 897 deletions
+136 -6
View File
@@ -4,6 +4,8 @@
import DOMPurify from 'dompurify';
const PROVIDER_IMAGE_PLACEHOLDER = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
// 配置 DOMPurify 的安全策略
const DOMPurifyConfig = {
// 允许的标签
@@ -21,7 +23,7 @@ const DOMPurifyConfig = {
],
// 允许的属性
ALLOWED_ATTR: [
'href', 'title', 'alt', 'src', 'class', 'id', 'style',
'href', 'title', 'alt', 'src', 'class', 'id', 'style', 'data-protected-src',
'target', 'rel', 'width', 'height',
// Mermaid SVG 支持的属性
'd', 'fill', 'stroke', 'stroke-width', 'stroke-linecap', 'stroke-linejoin',
@@ -39,7 +41,7 @@ const DOMPurifyConfig = {
'display', 'pointer-events', 'cursor', 'data-emit', 'direction'
],
// 允许的协议
ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i,
ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp):|(?:local|minio|cos|tos):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i,
// 禁止的标签和属性
FORBID_TAGS: ['script', 'object', 'embed', 'form', 'input', 'button'],
FORBID_ATTR: ['onerror', 'onload', 'onclick', 'onmouseover', 'onfocus', 'onblur'],
@@ -99,7 +101,8 @@ export function sanitizeHTML(html: string): string {
}
try {
return DOMPurify.sanitize(html, DOMPurifyConfig);
const preparedHTML = protectProviderImageSrcInHTML(html);
return DOMPurify.sanitize(preparedHTML, DOMPurifyConfig);
} catch (error) {
console.error('HTML sanitization failed:', error);
// 如果清理失败,返回转义的纯文本
@@ -107,6 +110,24 @@ export function sanitizeHTML(html: string): string {
}
}
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(
/<img\b([^>]*?)\ssrc=(["'])(local|minio|cos|tos):(?:\/\/|&#x2f;&#x2f;|&#47;&#47;)([^"']+)\2([^>]*)>/gi,
(_m, before, quote, provider, restPathRaw, after) => {
const restPath = decodeProviderURL(restPathRaw);
const protectedSrc = `${provider}://${restPath}`;
return `<img${before} src=${quote}${PROVIDER_IMAGE_PLACEHOLDER}${quote} data-protected-src=${quote}${protectedSrc}${quote}${after}>`;
},
);
}
/**
* 转义 HTML 特殊字符
* @param text 需要转义的文本
@@ -140,14 +161,23 @@ export function isValidURL(url: string): boolean {
if (!url || typeof url !== 'string') {
return false;
}
const trimmed = url.trim();
if (!trimmed) {
return false;
}
// 允许以 / 开头的站内相对路径(如本地存储 /files/images/xxx.jpg)
if (url.startsWith('/') && !url.startsWith('//')) {
if (trimmed.startsWith('/') && !trimmed.startsWith('//')) {
return true;
}
// 允许 provider:// 形式,由前端后续鉴权拉取并替换为 blob URL
if (/^(local|minio|cos|tos):\/\/\S+$/i.test(trimmed)) {
return true;
}
try {
const urlObj = new URL(url);
const urlObj = new URL(trimmed);
return ['http:', 'https:'].includes(urlObj.protocol);
} catch {
return false;
@@ -220,9 +250,109 @@ export function createSafeImage(src: string, alt: string = '', title: string = '
return '';
}
const safeSrc = escapeHTML(src);
// src is validated by isValidImageURL; keep URL structure unchanged.
// Only escape quotes to avoid breaking attributes.
const safeSrc = src.replace(/"/g, '&quot;');
const safeAlt = escapeHTML(alt);
const safeTitle = escapeHTML(title);
return `<img src="${safeSrc}" alt="${safeAlt}" title="${safeTitle}" class="markdown-image" style="max-width: 100%; height: auto;">`;
}
const protectedFileBlobCache = new Map<string, string>();
function getProtectedFileRequestHeaders(): Record<string, string> {
const headers: Record<string, string> = {};
try {
const token = (localStorage.getItem('weknora_token') || '').trim();
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
const selectedTenantId = (localStorage.getItem('weknora_selected_tenant_id') || '').trim();
const tenantRaw = localStorage.getItem('weknora_tenant');
if (selectedTenantId) {
try {
const tenant = tenantRaw ? JSON.parse(tenantRaw) : null;
const defaultTenantId = tenant?.id ? String(tenant.id) : '';
if (selectedTenantId !== defaultTenantId) {
headers['X-Tenant-ID'] = selectedTenantId;
}
} catch {
// ignore tenant parse error and skip X-Tenant-ID
}
}
} catch {
// ignore localStorage read errors
}
return headers;
}
/**
* 将 Markdown 里通过 /files 代理的图片,改为用带鉴权 Header 的 fetch 拉取后再显示。
* 用于避免在 URL 中暴露 token。
*/
export async function hydrateProtectedFileImages(root: ParentNode | null | undefined): Promise<void> {
if (!root || typeof window === 'undefined') {
return;
}
const images = root.querySelectorAll<HTMLImageElement>(
'img[data-protected-src], img[src^="local://"], img[src^="minio://"], img[src^="cos://"], img[src^="tos://"]',
);
if (!images.length) {
return;
}
const headers = getProtectedFileRequestHeaders();
await Promise.all(Array.from(images).map(async (img) => {
const protectedSrc = (img.getAttribute('data-protected-src') || '').trim();
const src = (img.getAttribute('src') || '').trim();
const sourceURL = protectedSrc || src;
if (!sourceURL) {
return;
}
if (img.dataset.authHydrated === '1') {
return;
}
img.dataset.authHydrated = '1';
const isProviderScheme = /^(local|minio|cos|tos):\/\//.test(sourceURL);
const requestURL = isProviderScheme
? `/files?${new URLSearchParams({ file_path: sourceURL }).toString()}`
: sourceURL;
if (!requestURL.startsWith('/files?') || !requestURL.includes('file_path=')) {
img.dataset.authHydrated = '0';
return;
}
const cachedBlobURL = protectedFileBlobCache.get(requestURL);
if (cachedBlobURL) {
img.src = cachedBlobURL;
return;
}
try {
const resp = await fetch(requestURL, {
method: 'GET',
headers,
credentials: 'include',
});
if (!resp.ok) {
throw new Error(`HTTP ${resp.status}`);
}
const blob = await resp.blob();
const blobURL = URL.createObjectURL(blob);
protectedFileBlobCache.set(requestURL, blobURL);
img.src = blobURL;
if (protectedSrc) {
img.removeAttribute('data-protected-src');
}
} catch (error) {
console.warn('[security] hydrateProtectedFileImages failed:', error);
img.dataset.authHydrated = '0';
}
}));
}
@@ -227,6 +227,7 @@ import { MessagePlugin } from 'tdesign-vue-next';
import { useUIStore } from '@/stores/ui';
import { useI18n } from 'vue-i18n';
import { openMermaidFullscreen } from '@/utils/mermaidViewer';
import { hydrateProtectedFileImages } from '@/utils/security';
const router = useRouter();
const uiStore = useUIStore();
@@ -278,7 +279,7 @@ const DOMPurifyConfig = {
],
ALLOWED_ATTR: [
'href', 'title', 'target', 'rel', 'data-tooltip', 'data-url', 'data-kb-id',
'data-chunk-id', 'data-doc', 'class', 'role', 'tabindex', 'src', 'alt',
'data-chunk-id', 'data-doc', 'class', 'role', 'tabindex', 'src', 'alt', 'data-protected-src',
'width', 'height', 'style', 'id',
// Mermaid SVG 支持的属性
'd', 'fill', 'stroke', 'stroke-width', 'stroke-linecap', 'stroke-linejoin',
@@ -294,7 +295,9 @@ const DOMPurifyConfig = {
'filterunits', 'primitiveunits', 'xmlns', 'xmlns:xlink', 'xlink:href',
'version', 'baseprofile', 'enable-background', 'overflow', 'visibility',
'display', 'pointer-events', 'cursor', 'data-emit', 'direction'
]
],
// Allow provider:// URLs so they can be hydrated later.
ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp):|(?:local|minio|cos|tos):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i
};
const TOOL_NAME_I18N: Record<string, string> = {
@@ -460,12 +463,13 @@ watch(eventStream, (stream) => {
}
});
// 只在会话完成后渲染 Mermaid 图表
if (props.session?.is_completed) {
nextTick(() => {
nextTick(async () => {
await hydrateProtectedFileImages(rootElement.value);
// 只在会话完成后渲染 Mermaid 图表
if (props.session?.is_completed) {
renderMermaidDiagrams();
});
}
}
});
}, { immediate: true, deep: true });
// State for intermediate steps collapse
@@ -1096,7 +1100,7 @@ const onRootKeydown = (e: KeyboardEvent) => {
onMounted(() => {
// 使用 nextTick 确保 DOM 已渲染
nextTick(() => {
nextTick(async () => {
const root = rootElement.value;
if (!root) return;
root.addEventListener('click', onRootClick, true);
@@ -1109,6 +1113,7 @@ onMounted(() => {
root.addEventListener('mouseout', onHoverOut, true);
window.addEventListener('scroll', scheduleFloatClose, true);
window.addEventListener('resize', scheduleFloatClose, true);
await hydrateProtectedFileImages(rootElement.value);
});
});
@@ -1148,17 +1153,9 @@ const parseTagAttributes = (attrString: string): Record<string, string> => {
const preprocessMarkdown = (contentStr: string): string => {
if (!contentStr.trim()) return '';
// Handle streaming image syntax to prevent flickering
const lastImgStart = contentStr.lastIndexOf('![');
if (lastImgStart !== -1) {
const potentialImgTag = contentStr.slice(lastImgStart);
const hasClosingParen = potentialImgTag.includes(')');
const hasClosingBracket = potentialImgTag.includes(']');
if (!hasClosingBracket || !hasClosingParen) {
contentStr = contentStr.slice(0, lastImgStart);
}
}
// Replace incomplete streaming image markdown with an in-place loading placeholder.
// This avoids showing a half-baked provider:// URL while keeping layout stable.
contentStr = replaceIncompleteImageWithPlaceholder(contentStr);
// Preprocess custom citation tags
return contentStr
@@ -1220,6 +1217,26 @@ const preprocessMarkdown = (contentStr: string): string => {
);
};
const replaceIncompleteImageWithPlaceholder = (content: string): string => {
const lastImgStart = content.lastIndexOf('![');
if (lastImgStart < 0) return content;
const tail = content.slice(lastImgStart);
const hasImageOpen = tail.startsWith('![');
const hasBracketClose = tail.includes(']');
const hasParenOpen = tail.includes('(');
const hasParenClose = tail.includes(')');
if (!hasImageOpen) return content;
// Incomplete image syntax at stream tail, e.g. ![alt](local://...
if (!hasBracketClose || (hasParenOpen && !hasParenClose)) {
const placeholder = `<span class="streaming-image-loading"><span class="streaming-image-loading__skeleton"></span></span>`;
return content.slice(0, lastImgStart) + placeholder;
}
return content;
};
// Get tokens from markdown content
const getTokens = (content: any) => {
const contentStr = typeof content === 'string' ? content : String(content || '');
@@ -1250,7 +1267,8 @@ agentRenderer.code = function(code, infostring) {
const getTokenHTML = (token: any): string => {
try {
const html = marked.parser([token], { renderer: agentRenderer });
return DOMPurify.sanitize(html, DOMPurifyConfig);
const protectedHTML = protectProviderImageSrcInHTML(html);
return DOMPurify.sanitize(protectedHTML, DOMPurifyConfig);
} catch (e) {
console.error('Token rendering error:', e);
return '';
@@ -1267,13 +1285,26 @@ const renderMarkdown = (content: any): string => {
const html = marked.parse(processed, { renderer: agentRenderer }) as string;
if (!html) return '';
return DOMPurify.sanitize(html, DOMPurifyConfig);
const protectedHTML = protectProviderImageSrcInHTML(html);
return DOMPurify.sanitize(protectedHTML, DOMPurifyConfig);
} catch (e) {
console.error('Markdown rendering error:', e, 'Content:', contentStr.substring(0, 100));
return contentStr.replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
};
const protectProviderImageSrcInHTML = (html: string): string => {
if (!html) return html;
const placeholder = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
return html.replace(
/<img\b([^>]*?)\ssrc=(["'])(local|minio|cos|tos):\/\/([^"']+)\2([^>]*)>/gi,
(_m, before, quote, provider, restPath, after) => {
const src = `${provider}://${restPath}`;
return `<img${before} src=${quote}${placeholder}${quote} data-protected-src=${quote}${src}${quote}${after}>`;
},
);
};
// 已渲染的 mermaid 元素 ID 集合
const renderedMermaidIds = new Set<string>();
@@ -3216,10 +3247,103 @@ const handleAddToKnowledge = (answerEvent: any) => {
}
}
:deep(.streaming-image-loading) {
display: inline-block;
position: relative;
width: clamp(150px, 30vw, 260px);
max-width: 100%;
aspect-ratio: 4 / 3;
border-radius: 10px;
border: 1px solid rgba(175, 190, 210, 0.55);
background: linear-gradient(
145deg,
rgba(245, 249, 255, 0.72) 0%,
rgba(228, 236, 248, 0.62) 45%,
rgba(214, 225, 242, 0.58) 100%
);
box-shadow:
inset 0 1px 0 rgba(255, 255, 255, 0.75),
inset 0 -1px 0 rgba(168, 182, 206, 0.28),
0 8px 20px rgba(100, 121, 152, 0.12);
backdrop-filter: blur(3px) saturate(115%);
-webkit-backdrop-filter: blur(3px) saturate(115%);
overflow: hidden;
vertical-align: middle;
animation: streamingImageBreath 2.2s ease-in-out infinite;
}
:deep(.streaming-image-loading__skeleton) {
position: absolute;
inset: 0;
background: linear-gradient(
110deg,
rgba(234, 239, 246, 0.95) 8%,
rgba(248, 250, 252, 0.98) 18%,
rgba(234, 239, 246, 0.95) 33%
);
background-size: 220% 100%;
animation: streamingImageShimmer 1.4s linear infinite;
}
:deep(.streaming-image-loading)::before {
content: '';
position: absolute;
inset: 0;
border-radius: inherit;
border: 1px solid rgba(255, 255, 255, 0.45);
pointer-events: none;
}
:deep(.streaming-image-loading)::after {
content: '';
position: absolute;
left: -35%;
top: -55%;
width: 62%;
height: 210%;
background: linear-gradient(
120deg,
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 0.38) 46%,
rgba(255, 255, 255, 0) 100%
);
transform: rotate(16deg);
animation: streamingImageMirror 2.8s ease-in-out infinite;
pointer-events: none;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@keyframes streamingImageShimmer {
to {
background-position-x: -220%;
}
}
@keyframes streamingImageMirror {
0%, 100% {
left: -38%;
opacity: 0.35;
}
50% {
left: 110%;
opacity: 0.75;
}
}
@keyframes streamingImageBreath {
0%, 100% {
transform: translateY(0) scale(1);
filter: saturate(1);
}
50% {
transform: translateY(-0.5px) scale(1.01);
filter: saturate(1.06);
}
}
</style>
+124 -8
View File
@@ -53,7 +53,7 @@ import docInfo from './docInfo.vue';
import deepThink from './deepThink.vue';
import AgentStreamDisplay from './AgentStreamDisplay.vue';
import picturePreview from '@/components/picture-preview.vue';
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL } from '@/utils/security';
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL, hydrateProtectedFileImages } from '@/utils/security';
import { openMermaidFullscreen } from '@/utils/mermaidViewer';
import { useI18n } from 'vue-i18n';
import { MessagePlugin } from 'tdesign-vue-next';
@@ -172,14 +172,35 @@ const markdownTokens = computed(() => {
if (!text || typeof text !== 'string') {
return [];
}
const processed = replaceIncompleteImageWithPlaceholder(text);
// 首先对 Markdown 内容进行安全处理
const safeMarkdown = safeMarkdownToHTML(text);
const safeMarkdown = safeMarkdownToHTML(processed);
// 使用 marked.lexer 分词
return marked.lexer(safeMarkdown);
});
const replaceIncompleteImageWithPlaceholder = (content) => {
const lastImgStart = content.lastIndexOf('![');
if (lastImgStart < 0) return content;
const tail = content.slice(lastImgStart);
const hasImageOpen = tail.startsWith('![');
const hasBracketClose = tail.includes(']');
const hasParenOpen = tail.includes('(');
const hasParenClose = tail.includes(')');
if (!hasImageOpen) return content;
// Incomplete image syntax at stream tail, e.g. ![alt](local://...
if (!hasBracketClose || (hasParenOpen && !hasParenClose)) {
const placeholder = `<span class="streaming-image-loading"><span class="streaming-image-loading__skeleton"></span></span>`;
return content.slice(0, lastImgStart) + placeholder;
}
return content;
};
// 计算属性:判断是否有实际内容(非空且不只是空白)
const hasActualContent = computed(() => {
const text = props.content || props.session?.content || '';
@@ -354,20 +375,22 @@ const bindMermaidClickEvents = () => {
// 监听内容变化并渲染 Mermaid - 只在会话完成后渲染
watch(() => [props.content, props.session?.content, props.session?.is_completed], () => {
// 只在会话完成后渲染 mermaid
if (props.session?.is_completed) {
nextTick(() => {
nextTick(async () => {
await hydrateProtectedFileImages(parentMd.value);
// 只在会话完成后渲染 mermaid
if (props.session?.is_completed) {
renderMermaidDiagrams();
});
}
}
});
}, { immediate: true });
onMounted(async () => {
// 为 markdown-content 中的图片添加点击事件
nextTick(() => {
nextTick(async () => {
if (parentMd.value) {
parentMd.value.addEventListener('click', handleMarkdownImageClick, true);
}
await hydrateProtectedFileImages(parentMd.value);
// 初始渲染 Mermaid 图表
renderMermaidDiagrams();
});
@@ -687,6 +710,99 @@ onBeforeUnmount(() => {
border-radius: 8px;
}
:deep(.streaming-image-loading) {
display: inline-block;
position: relative;
width: clamp(150px, 30vw, 260px);
max-width: 100%;
aspect-ratio: 4 / 3;
border-radius: 10px;
border: 1px solid rgba(175, 190, 210, 0.55);
background: linear-gradient(
145deg,
rgba(245, 249, 255, 0.72) 0%,
rgba(228, 236, 248, 0.62) 45%,
rgba(214, 225, 242, 0.58) 100%
);
box-shadow:
inset 0 1px 0 rgba(255, 255, 255, 0.75),
inset 0 -1px 0 rgba(168, 182, 206, 0.28),
0 8px 20px rgba(100, 121, 152, 0.12);
backdrop-filter: blur(3px) saturate(115%);
-webkit-backdrop-filter: blur(3px) saturate(115%);
overflow: hidden;
vertical-align: middle;
animation: streamingImageBreath 2.2s ease-in-out infinite;
}
:deep(.streaming-image-loading__skeleton) {
position: absolute;
inset: 0;
background: linear-gradient(
110deg,
rgba(234, 239, 246, 0.95) 8%,
rgba(248, 250, 252, 0.98) 18%,
rgba(234, 239, 246, 0.95) 33%
);
background-size: 220% 100%;
animation: streamingImageShimmer 1.4s linear infinite;
}
:deep(.streaming-image-loading)::before {
content: '';
position: absolute;
inset: 0;
border-radius: inherit;
border: 1px solid rgba(255, 255, 255, 0.45);
pointer-events: none;
}
:deep(.streaming-image-loading)::after {
content: '';
position: absolute;
left: -35%;
top: -55%;
width: 62%;
height: 210%;
background: linear-gradient(
120deg,
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 0.38) 46%,
rgba(255, 255, 255, 0) 100%
);
transform: rotate(16deg);
animation: streamingImageMirror 2.8s ease-in-out infinite;
pointer-events: none;
}
@keyframes streamingImageShimmer {
to {
background-position-x: -220%;
}
}
@keyframes streamingImageMirror {
0%, 100% {
left: -38%;
opacity: 0.35;
}
50% {
left: 110%;
opacity: 0.75;
}
}
@keyframes streamingImageBreath {
0%, 100% {
transform: translateY(0) scale(1);
filter: saturate(1);
}
50% {
transform: translateY(-0.5px) scale(1.01);
filter: saturate(1.06);
}
}
:deep(.t-loading__gradient-conic) {
background: conic-gradient(from 90deg at 50% 50%, #fff 0deg, #676767 360deg) !important;