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
fix(files): serve shared KB images via KB-scoped file proxy
wizardchen800cea082657
diff --git a/frontend/src/components/doc-content.vue b/frontend/src/components/doc-content.vueindex 978480e92..806942542 100644--- a/frontend/src/components/doc-content.vue+++ b/frontend/src/components/doc-content.vue@@ -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 => {diff --git a/frontend/src/utils/security.ts b/frontend/src/utils/security.tsindex f462c767a..6e548c7b5 100644--- a/frontend/src/utils/security.ts+++ b/frontend/src/utils/security.ts@@ -430,6 +430,7 @@ function applyHydratedProtectedImage(root: ParentNode, sourceURL: string, blobURexport 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';diff --git a/frontend/src/views/knowledge/KnowledgeBase.vue b/frontend/src/views/knowledge/KnowledgeBase.vueindex 7ef591359..fa309a1e3 100644--- a/frontend/src/views/knowledge/KnowledgeBase.vue+++ b/frontend/src/views/knowledge/KnowledgeBase.vue@@ -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>diff --git a/frontend/src/views/knowledge/wiki/WikiBrowser.vue b/frontend/src/views/knowledge/wiki/WikiBrowser.vueindex 74a86b718..f13ece0ac 100644--- a/frontend/src/views/knowledge/wiki/WikiBrowser.vue+++ b/frontend/src/views/knowledge/wiki/WikiBrowser.vue@@ -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)}})}diff --git a/internal/router/router.go b/internal/router/router.goindex 5a3fe2d2e..1d36aa35f 100644--- a/internal/router/router.go+++ b/internal/router/router.go@@ -213,6 +213,10 @@ func NewRouter(params RouterParams) *gin.Engine {RegisterTenantRoutes(v1, params.TenantHandler, params.TenantMemberHandler, params.TenantInvitationHandler, params.AuditLogHandler, rbacGuards)RegisterMyInvitationRoutes(v1, params.TenantInvitationHandler)RegisterKnowledgeBaseRoutes(v1, params.KBHandler, rbacGuards)+ // KB-scoped image proxy: lets tenants render images embedded in+ // org-shared / agent-visible KB content, which the tenant-scoped+ // /files route cannot serve because it enforces same-tenant paths.+ serveKBScopedFiles(v1, rbacGuards, params.TenantService, params.FileService)RegisterKnowledgeTagRoutes(v1, params.TagHandler, rbacGuards)RegisterKnowledgeRoutes(v1, params.KnowledgeHandler, rbacGuards)RegisterFAQRoutes(v1, params.FAQHandler, rbacGuards)@@ -1569,6 +1573,142 @@ func serveFiles(r getRouteRegistrar, globalFileService interfaces.FileService) {r.GET("/files", middleware.DenyAPIKeyPrincipal(), newFileServeHandler(globalFileService))}+// serveKBScopedFiles registers the KB-scoped file proxy used to render images+// embedded in a knowledge base's content (chunks / wiki pages). Unlike the+// tenant-scoped /files route — which enforces file_path.tenant == caller.tenant+// and therefore cannot serve objects owned by another tenant — this route is+// gated by RequireKBAccess. That guard resolves org-shared / agent-visible KBs+// and rewrites the request context's tenant ID to the KB's *owner* (source)+// tenant, so images stored under the owner tenant (local://<owner>/exports/...)+// become reachable by tenants that legitimately share the KB, while still+// enforcing that the requested path belongs to that owner tenant.+//+// Route:+// - GET /api/v1/knowledge-bases/:id/files?file_path=<provider://...>+func serveKBScopedFiles(+ r *gin.RouterGroup,+ g *rbacGuards,+ tenantService interfaces.TenantService,+ globalFileService interfaces.FileService,+) {+ logger.Infof(context.Background(), "[Router] Serving KB-scoped files from /knowledge-bases/:id/files")+ // API keys are denied outright (as on /files): a signed URL's tenant/KB+ // scope cannot authorize serving an arbitrary file_path under the owner+ // tenant, and this route deliberately crosses the caller's own tenant.+ r.GET("/knowledge-bases/:id/files",+ middleware.DenyAPIKeyPrincipal(),+ g.Viewer(),+ g.KBAccessRead("id"),+ newKBScopedFileServeHandler(tenantService, globalFileService),+ )+}++// newKBScopedFileServeHandler builds the handler backing serveKBScopedFiles.+// The effective (owner) tenant is taken from the request context, which+// RequireKBAccess has already rewritten to the KB's source tenant. The owner+// tenant's storage config is loaded via TenantService so the file is fetched+// from the backend that actually holds it — the caller's own storage config is+// irrelevant here.+func newKBScopedFileServeHandler(+ tenantService interfaces.TenantService,+ globalFileService interfaces.FileService,+) gin.HandlerFunc {+ baseDir := os.Getenv("LOCAL_STORAGE_BASE_DIR")+ if baseDir == "" {+ baseDir = "/data/files"+ }+ absDir, _ := filepath.Abs(baseDir)++ return func(c *gin.Context) {+ ctx := c.Request.Context()++ filePath := strings.TrimSpace(c.Query("file_path"))+ if filePath == "" {+ c.JSON(http.StatusBadRequest, gin.H{"error": "missing required parameter: file_path"})+ return+ }+ if strings.Contains(filePath, "..") {+ c.JSON(http.StatusBadRequest, gin.H{"error": "invalid file path"})+ return+ }++ // RequireKBAccess rewrote the request context tenant ID to the KB's+ // owner (source) tenant for shared KBs; for own KBs it equals the+ // caller's tenant. Either way it is the tenant that owns this KB's+ // storage objects, so the requested path must belong to it.+ ownerTenantID, ok := types.TenantIDFromContext(ctx)+ if !ok || ownerTenantID == 0 {+ c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized: tenant context missing"})+ return+ }++ if err := secutils.ValidateStoragePathTenant(filePath, ownerTenantID); err != nil {+ logger.Warnf(ctx, "[Router] /knowledge-bases/:id/files denied path not owned by KB tenant: owner_tenant_id=%d file_path=%q err=%v",+ ownerTenantID, filePath, err)+ c.JSON(http.StatusForbidden, gin.H{"error": "forbidden: file path not accessible"})+ return+ }++ tenant, err := tenantService.GetTenantByID(ctx, ownerTenantID)+ if err != nil || tenant == nil {+ logger.Warnf(ctx, "[Router] /knowledge-bases/:id/files owner tenant lookup failed: owner_tenant_id=%d err=%v",+ ownerTenantID, err)+ c.Status(http.StatusNotFound)+ return+ }++ provider := types.ParseProviderScheme(filePath)++ var (+ fileSvc interfaces.FileService+ resolvedProvider string+ )+ if tenant.StorageEngineConfig != nil {+ fileSvc, resolvedProvider, err = filesvc.NewFileServiceFromStorageConfig(provider, tenant.StorageEngineConfig, absDir)+ } else {+ err = http.ErrMissingFile+ }+ if err != nil {+ globalStorageType := strings.ToLower(strings.TrimSpace(os.Getenv("STORAGE_TYPE")))+ if globalStorageType == "" {+ globalStorageType = "local"+ }+ if provider == globalStorageType && globalFileService != nil {+ fileSvc = globalFileService+ resolvedProvider = globalStorageType+ } else {+ logger.Warnf(ctx, "[Router] /knowledge-bases/:id/files resolve file service failed: owner_tenant_id=%d provider=%s global_storage_type=%s err=%v",+ ownerTenantID, provider, globalStorageType, err)+ c.Status(http.StatusBadRequest)+ return+ }+ }++ reader, err := fileSvc.GetFile(ctx, filePath)+ if err != nil {+ logger.Warnf(ctx, "[Router] /knowledge-bases/:id/files get file failed: owner_tenant_id=%d provider=%s path=%q err=%v",+ ownerTenantID, resolvedProvider, filePath, err)+ c.Status(http.StatusNotFound)+ return+ }+ defer reader.Close()++ contentType, inline := secutils.SafeContentTypeByFilename(filePath)+ c.Header("Content-Type", contentType)+ c.Header("X-Content-Type-Options", "nosniff")+ if !inline {+ c.Header("Content-Disposition", "attachment")+ }+ // Cross-tenant shared content — keep it private so shared proxies /+ // CDNs do not cache one tenant's view for another.+ c.Header("Cache-Control", "private, max-age=86400")+ c.Status(http.StatusOK)+ if _, err := io.Copy(c.Writer, reader); err != nil {+ logger.Warnf(ctx, "[Router] /knowledge-bases/:id/files write response failed: %v", err)+ }+ }+}+// servePresignedFiles serves files via HMAC-signed URLs without requiring authentication.// This is used by IM channels to serve images that are embedded in bot replies.//diff --git a/internal/router/router_files_test.go b/internal/router/router_files_test.goindex 77ae1a1c7..aa8a7e9f0 100644--- a/internal/router/router_files_test.go+++ b/internal/router/router_files_test.go@@ -179,6 +179,111 @@ func TestServeFilesRejectsAPIKeyPrincipal(t *testing.T) {}}+// newKBScopedFilesTestEngine wires newKBScopedFileServeHandler behind a+// middleware that injects effectiveTenantID into the request context, mirroring+// what RequireKBAccess does after resolving an org-shared KB to its source+// tenant. This lets the handler be exercised without the full RBAC stack.+func newKBScopedFilesTestEngine(+ effectiveTenantID uint64,+ tenantSvc interfaces.TenantService,+ global interfaces.FileService,+) *gin.Engine {+ engine := gin.New()+ engine.GET("/knowledge-bases/:id/files",+ func(c *gin.Context) {+ ctx := context.WithValue(c.Request.Context(), types.TenantIDContextKey, effectiveTenantID)+ c.Request = c.Request.WithContext(ctx)+ c.Next()+ },+ newKBScopedFileServeHandler(tenantSvc, global),+ )+ return engine+}++// A tenant whose owner-tenant (10008) storage objects are requested by a+// borrowing tenant via a shared KB: the effective tenant in context is the+// owner, so the path validates and the file is served.+func TestKBScopedFilesServesOwnerTenantPath(t *testing.T) {+ gin.SetMode(gin.TestMode)+ t.Setenv("STORAGE_TYPE", "local")++ const ownerTenantID = uint64(10008)+ var requestedPath string+ engine := newKBScopedFilesTestEngine(+ ownerTenantID,+ &stubTenantService{get: func(_ context.Context, id uint64) (*types.Tenant, error) {+ return &types.Tenant{ID: id}, nil+ }},+ &stubFileService{getFile: func(_ context.Context, filePath string) (io.ReadCloser, error) {+ requestedPath = filePath+ return io.NopCloser(strings.NewReader("shared-body")), nil+ }},+ )++ filePath := "local://10008/exports/img.jpg"+ req := httptest.NewRequest(http.MethodGet, "/knowledge-bases/kb-1/files?file_path="+url.QueryEscape(filePath), nil)+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, req)++ if got, want := recorder.Code, http.StatusOK; got != want {+ t.Fatalf("status = %d, want %d body=%s", got, want, recorder.Body.String())+ }+ if requestedPath != filePath {+ t.Fatalf("requested path = %q, want %q", requestedPath, filePath)+ }+ if body := recorder.Body.String(); body != "shared-body" {+ t.Fatalf("body = %q, want %q", body, "shared-body")+ }+}++// The path must belong to the effective (owner) tenant. A path pointing at a+// different tenant than the resolved KB owner is still rejected, so the guard+// cannot be used to reach arbitrary tenants' files.+func TestKBScopedFilesRejectsPathNotOwnedByKBTenant(t *testing.T) {+ gin.SetMode(gin.TestMode)+ t.Setenv("STORAGE_TYPE", "local")++ const ownerTenantID = uint64(10008)+ engine := newKBScopedFilesTestEngine(+ ownerTenantID,+ &stubTenantService{get: func(_ context.Context, id uint64) (*types.Tenant, error) {+ t.Fatalf("GetTenantByID should not be called for mismatched path, got %d", id)+ return nil, nil+ }},+ &stubFileService{getFile: func(_ context.Context, filePath string) (io.ReadCloser, error) {+ t.Fatalf("GetFile should not be called for mismatched path, got %q", filePath)+ return nil, nil+ }},+ )++ req := httptest.NewRequest(http.MethodGet,+ "/knowledge-bases/kb-1/files?file_path="+url.QueryEscape("local://9999/exports/other.jpg"), nil)+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, req)++ if got, want := recorder.Code, http.StatusForbidden; got != want {+ t.Fatalf("status = %d, want %d", got, want)+ }+}++func TestKBScopedFilesRequiresFilePath(t *testing.T) {+ gin.SetMode(gin.TestMode)++ engine := newKBScopedFilesTestEngine(+ 10008,+ &stubTenantService{},+ &stubFileService{},+ )++ req := httptest.NewRequest(http.MethodGet, "/knowledge-bases/kb-1/files", nil)+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, req)++ if got, want := recorder.Code, http.StatusBadRequest; got != want {+ t.Fatalf("status = %d, want %d", got, want)+ }+}+func TestServeFilesForcesActiveContentDownload(t *testing.T) {gin.SetMode(gin.TestMode)t.Setenv("STORAGE_TYPE", "local")