fix(api-keys): allow scoped file serve and ingest batch routes

Let tenant-wide retrieve keys serve tenant-bounded /files paths while
denying KB-restricted keys, declare knowledge batch write routes for
ingest-capable keys, and short-circuit ownership checks for API-key
principals already authorized by the gate and KB allow-list.
This commit is contained in:
wizardchen
2026-07-20 19:47:42 +08:00
committed by lyingbug
parent 23e606f570
commit d1cab68e3d
7 changed files with 271 additions and 36 deletions
+31
View File
@@ -161,6 +161,37 @@ func DenyAPIKeyPrincipal() gin.HandlerFunc {
}
}
// AllowFileServeAPIKey guards the tenant-scoped file-proxy routes (/files and
// the KB-scoped image proxy) for X-API-Key callers. Those routes serve an
// arbitrary storage path that only carries a tenant segment — there is no KB
// id in the path a KB-restricted key's allow-list could be checked against —
// so a KB-restricted key is denied outright (it must download KB content via
// the KB-scoped routes such as /knowledge/:id/download, which DO enforce the
// allow-list). A key passes when it is full-access, or when it is NOT
// KB-restricted and carries the retrieve capability: that is exactly the class
// of key that can already read any of the tenant's KB content, so exposing the
// tenant-bounded raw file path (the handler still enforces
// ValidateStoragePathTenant / the KB owner tenant) grants it nothing new. JWT
// sessions carry no API-key scope and pass straight through.
func AllowFileServeAPIKey() gin.HandlerFunc {
return func(c *gin.Context) {
scope, ok := types.TenantAPIKeyScopeFromContext(c.Request.Context())
if !ok {
c.Next()
return
}
if scope.FullAccess ||
(!scope.IsKnowledgeBaseRestricted() &&
scope.HasCapability(types.APIKeyCapabilityRetrieve)) {
c.Next()
return
}
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": "Forbidden: API key scope does not allow this operation",
})
}
}
// normalizeRoutePath collapses duplicate slashes and trims a trailing slash so
// helper-built paths (BasePath()+rel) match gin's c.FullPath() exactly.
func normalizeRoutePath(p string) string {