fix(files): serve shared KB images via KB-scoped file proxy

Org-shared and agent-visible knowledge bases store images under the owner
tenant's storage path, which the tenant-scoped /files route rejects for
borrowing tenants. Add GET /api/v1/knowledge-bases/:id/files gated by
RequireKBAccess and route frontend image hydration through it when kbId
is known.
This commit is contained in:
wizardchen
2026-07-13 11:29:37 +08:00
committed by lyingbug
parent abcecc870a
commit 800cea0826
6 changed files with 262 additions and 8 deletions
+2 -2
View File
@@ -84,7 +84,7 @@ mermaid.initialize({
topPadding: 50
}
});
const props = defineProps(["visible", "details", "knowledgeType", "sourceInfo", "canEditKB", "parse_status"]);
const props = defineProps(["visible", "details", "knowledgeType", "sourceInfo", "canEditKB", "parse_status", "kbId"]);
const emit = defineEmits(["closeDoc", "getDoc", "questionDeleted"]);
const hasTimelineSpans = ref(false);
@@ -618,7 +618,7 @@ const runMarkdownPostRenderPipeline = async () => {
if (!renderRoot) {
return;
}
await hydrateProtectedFileImages(renderRoot);
await hydrateProtectedFileImages(renderRoot, undefined, props.kbId);
const images = renderRoot?.querySelectorAll?.('img.markdown-image') as NodeListOf<HTMLImageElement> | undefined;
if (images) {
images.forEach(async item => {
+10 -1
View File
@@ -430,6 +430,7 @@ function applyHydratedProtectedImage(root: ParentNode, sourceURL: string, blobUR
export async function hydrateProtectedFileImages(
root: ParentNode | null | undefined,
embed?: { channelId: string; token: string },
kbId?: string,
): Promise<void> {
if (!root || typeof window === 'undefined') {
return;
@@ -466,9 +467,16 @@ export async function hydrateProtectedFileImages(
img.dataset.authHydrated = '1';
const isProviderScheme = isProviderFileURL(sourceURL);
// When a KB context is known, route through the KB-scoped proxy. It is
// authorized via RequireKBAccess (own / org-shared / agent-visible) and
// serves objects owned by the KB's source tenant — so images in a shared
// KB (local://<owner-tenant>/...) load for the borrowing tenant, which the
// tenant-scoped /files route rejects as a cross-tenant path.
const fileProxyBase = embed
? `/api/v1/embed/${embed.channelId}/files`
: '/files';
: kbId
? `/api/v1/knowledge-bases/${encodeURIComponent(kbId)}/files`
: '/files';
const requestURL = isProviderScheme
? `${fileProxyBase}?${new URLSearchParams({ file_path: sourceURL }).toString()}`
: sourceURL;
@@ -476,6 +484,7 @@ export async function hydrateProtectedFileImages(
const isProxyRequest =
requestURL.includes('file_path=') &&
(requestURL.startsWith('/files?') ||
/^\/api\/v1\/knowledge-bases\/[^/]+\/files\?/.test(requestURL) ||
/^\/api\/v1\/embed\/[^/]+\/files\?/.test(requestURL));
if (!isProxyRequest) {
img.dataset.authHydrated = '0';
@@ -2291,7 +2291,7 @@ async function createNewSession(value: string): Promise<void> {
</template>
<!-- DocContent drawer (shared by documents tab and wiki source refs) -->
<DocContent ref="docContentRef" :visible="isCardDetails" :details="details" :canEditKB="canEdit"
<DocContent ref="docContentRef" :visible="isCardDetails" :details="details" :canEditKB="canEdit" :kbId="kbId"
@closeDoc="closeDoc" @getDoc="getDoc">
</DocContent>
</div>
@@ -1385,7 +1385,7 @@ function closeImagePreview() {
watch(graphDrawerContent, async () => {
await nextTick()
if (drawerBodyRef.value) {
await hydrateProtectedFileImages(drawerBodyRef.value)
await hydrateProtectedFileImages(drawerBodyRef.value, undefined, props.knowledgeBaseId)
}
})
@@ -1896,7 +1896,7 @@ const indexHasMore = computed(() => {
watch(renderedContent, async () => {
await nextTick()
if (readerBodyRef.value) {
await hydrateProtectedFileImages(readerBodyRef.value)
await hydrateProtectedFileImages(readerBodyRef.value, undefined, props.knowledgeBaseId)
}
})
@@ -1906,7 +1906,7 @@ watch(renderedContent, async () => {
watch(renderedIndexMarkdown, async () => {
await nextTick()
if (indexBodyRef.value) {
await hydrateProtectedFileImages(indexBodyRef.value)
await hydrateProtectedFileImages(indexBodyRef.value, undefined, props.knowledgeBaseId)
}
})
@@ -4449,7 +4449,7 @@ watch(() => props.view, (v) => {
} else if (v === 'browser') {
nextTick(async () => {
if (readerBodyRef.value && renderedContent.value) {
await hydrateProtectedFileImages(readerBodyRef.value)
await hydrateProtectedFileImages(readerBodyRef.value, undefined, props.knowledgeBaseId)
}
})
}