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
+105
View File
@@ -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")