fix(router): restrict KB-scoped file proxy to exports/ paths

Borrowers with shared-KB read access could previously request any
owner-tenant storage path via /knowledge-bases/:id/files. Limit the
proxy to exports/ embedded images; raw knowledge uploads continue to
use /knowledge/:id/download.
This commit is contained in:
wizardchen
2026-07-13 12:12:09 +08:00
committed by lyingbug
parent 800cea0826
commit def92bb74f
4 changed files with 91 additions and 3 deletions
+2 -2
View File
@@ -1642,8 +1642,8 @@ func newKBScopedFileServeHandler(
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",
if err := secutils.ValidateKBScopedStoragePath(filePath, ownerTenantID); err != nil {
logger.Warnf(ctx, "[Router] /knowledge-bases/:id/files denied path not allowed for KB proxy: 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
+29
View File
@@ -266,6 +266,35 @@ func TestKBScopedFilesRejectsPathNotOwnedByKBTenant(t *testing.T) {
}
}
// KB-scoped proxy is for embedded exports/ images only; raw knowledge uploads
// must use /knowledge/:id/download even when the tenant matches.
func TestKBScopedFilesRejectsNonExportsPath(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 non-exports 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 non-exports path, got %q", filePath)
return nil, nil
}},
)
req := httptest.NewRequest(http.MethodGet,
"/knowledge-bases/kb-1/files?file_path="+url.QueryEscape("local://10008/knowledge-id/123.pdf"), 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)