Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat: implement tenant validation for file access and enhance related tests

wizardchenbb202a203a63
diff --git a/internal/router/router.go b/internal/router/router.go
index cefa60b22..7107e3e4a 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -1227,6 +1227,12 @@ func serveFiles(r getRouteRegistrar, globalFileService interfaces.FileService) {
return
}
+ if err := secutils.ValidateStoragePathTenant(filePath, tenant.ID); err != nil {
+ logger.Warnf(context.Background(), "[Router] /files denied cross-tenant or invalid path: tenant_id=%d file_path=%q err=%v", tenant.ID, filePath, err)
+ c.JSON(http.StatusForbidden, gin.H{"error": "forbidden: file path not accessible"})
+ return
+ }
+
var (
fileSvc interfaces.FileService
resolvedProvider string
diff --git a/internal/router/router_files_test.go b/internal/router/router_files_test.go
index 19a4647db..cac45da50 100644
--- a/internal/router/router_files_test.go
+++ b/internal/router/router_files_test.go
@@ -62,7 +62,7 @@ func TestServeFilesFallsBackToGlobalFileService(t *testing.T) {
},
})
- filePath := "local://docs/example.txt"
+ filePath := "local://42/docs/example.txt"
req := httptest.NewRequest(http.MethodGet, "/files?file_path="+url.QueryEscape(filePath), nil)
req = req.WithContext(context.WithValue(req.Context(), types.TenantInfoContextKey, &types.Tenant{ID: 42}))
@@ -92,7 +92,7 @@ func TestServeFilesDoesNotFallbackWhenProviderDoesNotMatchGlobalStorage(t *testi
},
})
- req := httptest.NewRequest(http.MethodGet, "/files?file_path="+url.QueryEscape("local://docs/example.txt"), nil)
+ req := httptest.NewRequest(http.MethodGet, "/files?file_path="+url.QueryEscape("local://42/docs/example.txt"), nil)
req = req.WithContext(context.WithValue(req.Context(), types.TenantInfoContextKey, &types.Tenant{ID: 42}))
recorder := httptest.NewRecorder()
@@ -102,3 +102,49 @@ func TestServeFilesDoesNotFallbackWhenProviderDoesNotMatchGlobalStorage(t *testi
t.Fatalf("status = %d, want %d", got, want)
}
}
+
+func TestServeFilesRejectsCrossTenantPath(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ t.Setenv("STORAGE_TYPE", "local")
+
+ engine := gin.New()
+ serveFiles(engine, &stubFileService{
+ getFile: func(ctx context.Context, filePath string) (io.ReadCloser, error) {
+ t.Fatalf("GetFile should not be called for cross-tenant path, got %q", filePath)
+ return nil, nil
+ },
+ })
+
+ req := httptest.NewRequest(http.MethodGet, "/files?file_path="+url.QueryEscape("local://7/knowledge/secret.pdf"), nil)
+ req = req.WithContext(context.WithValue(req.Context(), types.TenantInfoContextKey, &types.Tenant{ID: 42}))
+
+ 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 TestServeFilesRejectsPathWithoutTenantSegment(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ t.Setenv("STORAGE_TYPE", "local")
+
+ engine := gin.New()
+ serveFiles(engine, &stubFileService{
+ getFile: func(ctx context.Context, filePath string) (io.ReadCloser, error) {
+ t.Fatalf("GetFile should not be called without tenant segment, got %q", filePath)
+ return nil, nil
+ },
+ })
+
+ req := httptest.NewRequest(http.MethodGet, "/files?file_path="+url.QueryEscape("local://docs/example.txt"), nil)
+ req = req.WithContext(context.WithValue(req.Context(), types.TenantInfoContextKey, &types.Tenant{ID: 42}))
+
+ recorder := httptest.NewRecorder()
+ engine.ServeHTTP(recorder, req)
+
+ if got, want := recorder.Code, http.StatusForbidden; got != want {
+ t.Fatalf("status = %d, want %d", got, want)
+ }
+}
diff --git a/internal/utils/presign.go b/internal/utils/presign.go
index 07a75ab2c..5662f3521 100644
--- a/internal/utils/presign.go
+++ b/internal/utils/presign.go
@@ -95,6 +95,20 @@ func VerifyFileURLSig(filePath string, tenantID uint64, expiresStr, sig string)
return hmac.Equal([]byte(expected), []byte(sig))
}
+// ValidateStoragePathTenant ensures the tenant segment embedded in a provider://
+// storage path matches the authenticated caller's tenant. Cross-tenant access
+// must use /api/v1/files/presigned with an HMAC bound to the resource owner.
+func ValidateStoragePathTenant(filePath string, tenantID uint64) error {
+ pathTenant := ParseTenantIDFromStoragePath(filePath)
+ if pathTenant == 0 {
+ return fmt.Errorf("storage path has no tenant segment")
+ }
+ if pathTenant != tenantID {
+ return fmt.Errorf("storage path tenant mismatch")
+ }
+ return nil
+}
+
// ParseTenantIDFromStoragePath extracts the tenant ID from a provider:// storage path.
// Storage paths follow the convention: {scheme}://.../{tenantID}/...
// Returns 0 if the path does not contain a valid tenant ID.
diff --git a/internal/utils/presign_test.go b/internal/utils/presign_test.go
index 794623f3d..5ceb0ea97 100644
--- a/internal/utils/presign_test.go
+++ b/internal/utils/presign_test.go
@@ -86,6 +86,12 @@ func TestVerifyFileURLSig_NoKey(t *testing.T) {
assert.False(t, VerifyFileURLSig("local://1/img.png", 1, "99999999999", "abc"))
}
+func TestValidateStoragePathTenant(t *testing.T) {
+ assert.NoError(t, ValidateStoragePathTenant("local://42/knowledge/file.pdf", 42))
+ assert.Error(t, ValidateStoragePathTenant("local://7/knowledge/file.pdf", 42))
+ assert.Error(t, ValidateStoragePathTenant("local://docs/example.txt", 42))
+}
+
func TestParseTenantIDFromStoragePath(t *testing.T) {
tests := []struct {
path string