feat(api-keys): implement DenyAPIKeyPrincipal middleware and related tests
Add DenyAPIKeyPrincipal middleware to explicitly block API-key principals from accessing certain routes, ensuring security for endpoints outside the /api/v1 group. Introduce tests to validate that API keys are correctly denied access while allowing JWT sessions to pass through. This enhances the overall authorization model by reinforcing the default-deny approach for API keys.
This commit is contained in:
@@ -135,6 +135,25 @@ func (a *APIKeyRouteAuthorizer) authorize(scope types.TenantAPIKeyScope, method,
|
||||
return nil
|
||||
}
|
||||
|
||||
// DenyAPIKeyPrincipal returns a middleware that rejects any X-API-Key
|
||||
// principal outright. Use it on routes registered directly on the engine
|
||||
// (outside the /api/v1 group) where the APIKeyRouteAuthorizer.Middleware
|
||||
// gate does NOT run — the JWT role guards (RequireRole / RequireSystemAdmin
|
||||
// / RequireOwnershipOrRole) short-circuit API-key principals on the
|
||||
// assumption the gate already authorized them, so an ungated route would
|
||||
// otherwise let any valid key through. JWT sessions pass straight through.
|
||||
func DenyAPIKeyPrincipal() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if _, ok := types.TenantAPIKeyScopeFromContext(c.Request.Context()); ok {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
|
||||
"error": "Forbidden: API keys cannot access this endpoint",
|
||||
})
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func isSafeHTTPMethod(method string) bool {
|
||||
switch method {
|
||||
case http.MethodGet, http.MethodHead, http.MethodOptions:
|
||||
|
||||
@@ -151,6 +151,50 @@ func TestGateKBScopeDoesNotBlockDataPlane(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// runDenyAPIKey mounts DenyAPIKeyPrincipal ahead of a handler and reports
|
||||
// whether the request reached the handler.
|
||||
func runDenyAPIKey(t *testing.T, scope *types.TenantAPIKeyScope) (reached bool, status int) {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
engine := gin.New()
|
||||
engine.Use(func(c *gin.Context) {
|
||||
if scope != nil {
|
||||
c.Request = c.Request.WithContext(types.WithTenantAPIKeyScope(c.Request.Context(), *scope))
|
||||
}
|
||||
c.Next()
|
||||
})
|
||||
engine.GET("/api/v1/files/presigned-preview", DenyAPIKeyPrincipal(), func(c *gin.Context) {
|
||||
reached = true
|
||||
c.Status(http.StatusOK)
|
||||
})
|
||||
w := httptest.NewRecorder()
|
||||
engine.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/files/presigned-preview", nil))
|
||||
return reached, w.Code
|
||||
}
|
||||
|
||||
// TestDenyAPIKeyPrincipalBlocksAPIKeys guards the gate-bypass class of bug:
|
||||
// engine-root routes (outside /api/v1) that rely on RequireRole must not be
|
||||
// reachable by API-key principals, since RequireRole short-circuits them.
|
||||
func TestDenyAPIKeyPrincipalBlocksAPIKeys(t *testing.T) {
|
||||
// Even an Owner-role API key must be rejected outright.
|
||||
reached, status := runDenyAPIKey(t, &types.TenantAPIKeyScope{Role: types.TenantRoleOwner})
|
||||
if reached {
|
||||
t.Fatal("API-key principal must not reach a DenyAPIKeyPrincipal-guarded handler")
|
||||
}
|
||||
if status != http.StatusForbidden {
|
||||
t.Fatalf("expected 403 for API-key principal, got %d", status)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDenyAPIKeyPrincipalAllowsJWT confirms JWT sessions (no API-key scope)
|
||||
// pass straight through.
|
||||
func TestDenyAPIKeyPrincipalAllowsJWT(t *testing.T) {
|
||||
reached, status := runDenyAPIKey(t, nil)
|
||||
if !reached || status != http.StatusOK {
|
||||
t.Fatalf("JWT session should pass DenyAPIKeyPrincipal: reached=%v status=%d", reached, status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeRoutePath(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"/api/v1//models": "/api/v1/models",
|
||||
|
||||
@@ -1668,7 +1668,12 @@ func servePresignedPreview(r *gin.Engine, cfg *config.Config) {
|
||||
}
|
||||
absDir, _ := filepath.Abs(baseDir)
|
||||
|
||||
// This route is registered on the engine root, NOT the /api/v1 group,
|
||||
// so the APIKeyGate never runs for it. RequireRole short-circuits
|
||||
// API-key principals (deferring to that absent gate), which would let
|
||||
// any valid key past the Admin check. Deny API keys explicitly first.
|
||||
r.GET("/api/v1/files/presigned-preview",
|
||||
middleware.DenyAPIKeyPrincipal(),
|
||||
middleware.RequireRole(types.TenantRoleAdmin, cfg),
|
||||
func(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
Reference in New Issue
Block a user