Fixes 9 auth middleware findings from the A-3 recon audit. F-A3-11 CRITICAL: Removed JWT secret from WebAuthMiddleware log output. Replaced emoji-prefixed fmt.Printf with ETHOS-compliant log.Printf. No secret values in any log output. F-A3-7 CRITICAL: Config download now requires WebAuthMiddleware. GET /downloads/config/:agent_id is admin-only (agents never call it). F-A3-6 HIGH: Update package download now requires AuthMiddleware. GET /downloads/updates/:package_id requires valid agent JWT. F-A3-10 HIGH: Scheduler stats changed from AuthMiddleware to WebAuthMiddleware. Agent JWTs can no longer view scheduler internals. F-A3-13 LOW: RequireAdmin() middleware implemented. 7 security settings routes re-enabled (GET/PUT/POST under /security/settings). security_settings.go.broken renamed to .go, API mismatches fixed. F-A3-12 MEDIUM: JWT issuer claims added for token type separation. Agent tokens: issuer=redflag-agent, Web tokens: issuer=redflag-web. AuthMiddleware rejects tokens with wrong issuer. Grace period: tokens with no issuer still accepted (backward compat). F-A3-2 MEDIUM: /auth/verify now has WebAuthMiddleware applied. Endpoint returns 200 with valid=true for valid admin tokens. F-A3-9 MEDIUM: Agent self-unregister (DELETE /:id) now rate-limited using the same agent_reports rate limiter as other agent routes. F-A3-14 LOW: CORS origin configurable via REDFLAG_CORS_ORIGIN env var. Defaults to http://localhost:3000 for development. Added PATCH method and agent-specific headers to CORS config. All 27 server tests pass. All 14 agent tests pass. No regressions. See docs/A3_Fix_Implementation.md and docs/Deviations_Report.md (DEV-020 through DEV-022). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
145 lines
5.2 KiB
Go
145 lines
5.2 KiB
Go
package middleware_test
|
|
|
|
// token_confusion_test.go — Pre-fix tests for cross-type JWT token confusion.
|
|
//
|
|
// BUG F-A3-12 MEDIUM: Agent and web JWTs share the same signing secret with
|
|
// no issuer/audience differentiation. Cross-type token confusion is possible.
|
|
//
|
|
// The shared secret is set at main.go:166. Both AuthMiddleware (agent JWT)
|
|
// and WebAuthMiddleware (admin JWT) use the same HMAC key. Without issuer
|
|
// or audience claims, a JWT valid for one context may pass signature
|
|
// validation in the other.
|
|
//
|
|
// Run: cd aggregator-server && go test ./internal/api/middleware/... -v -run TestToken
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Fimeg/RedFlag/aggregator-server/internal/api/handlers"
|
|
"github.com/Fimeg/RedFlag/aggregator-server/internal/api/middleware"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// makeWebJWT creates a valid web/admin JWT with correct issuer for testing
|
|
func makeWebJWT(t *testing.T, secret string) string {
|
|
t.Helper()
|
|
claims := handlers.UserClaims{
|
|
UserID: "1",
|
|
Username: "admin",
|
|
Role: "admin",
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
Issuer: "redflag-web",
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
|
},
|
|
}
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
signed, err := token.SignedString([]byte(secret))
|
|
if err != nil {
|
|
t.Fatalf("failed to sign web JWT: %v", err)
|
|
}
|
|
return signed
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test 4.1 — Web token SHOULD be rejected by agent AuthMiddleware
|
|
//
|
|
// Category: Verify actual behavior — PASS or FAIL depending on claims parsing
|
|
//
|
|
// BUG F-A3-12: Shared JWT secret allows cross-type token use. A web token
|
|
// passes signature validation on agent middleware. The question is whether
|
|
// claims parsing (AgentClaims expecting AgentID uuid.UUID) rejects the
|
|
// web token that has UserID string instead.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestWebTokenRejectedByAgentAuthMiddleware(t *testing.T) {
|
|
// POST-FIX (F-A3-12): Web token with issuer=redflag-web is rejected by
|
|
// agent AuthMiddleware which validates issuer=redflag-agent.
|
|
sharedSecret := "shared-secret-confusion-test"
|
|
middleware.JWTSecret = sharedSecret
|
|
|
|
router := gin.New()
|
|
router.Use(middleware.AuthMiddleware())
|
|
router.GET("/agent-route", func(c *gin.Context) {
|
|
agentID, exists := c.Get("agent_id")
|
|
if !exists {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "no agent_id"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"agent_id": agentID})
|
|
})
|
|
|
|
// Web JWT with issuer=redflag-web
|
|
webToken := makeWebJWT(t, sharedSecret)
|
|
|
|
req := httptest.NewRequest("GET", "/agent-route", nil)
|
|
req.Header.Set("Authorization", "Bearer "+webToken)
|
|
rec := httptest.NewRecorder()
|
|
router.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusUnauthorized && rec.Code != http.StatusForbidden {
|
|
t.Errorf("[ERROR] [server] [auth] web JWT accepted by agent AuthMiddleware (got %d, expected 401)", rec.Code)
|
|
}
|
|
t.Logf("[INFO] [server] [auth] F-A3-12 FIXED: web JWT rejected by agent middleware (%d)", rec.Code)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test 4.2 — Agent token SHOULD be rejected by WebAuthMiddleware
|
|
//
|
|
// Category: Verify actual behavior — PASS or FAIL depending on claims parsing
|
|
//
|
|
// BUG F-A3-12: An agent token (AgentClaims with AgentID uuid.UUID) may pass
|
|
// signature validation on WebAuthMiddleware. The question is whether the
|
|
// UserClaims parsing rejects it.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestAgentTokenRejectedByWebAuthMiddleware(t *testing.T) {
|
|
// POST-FIX (F-A3-12): Agent token with issuer=redflag-agent is rejected by
|
|
// WebAuthMiddleware which validates issuer=redflag-web.
|
|
sharedSecret := "shared-secret-confusion-test-2"
|
|
middleware.JWTSecret = sharedSecret
|
|
|
|
authHandler := handlers.NewAuthHandler(sharedSecret, nil)
|
|
|
|
router := gin.New()
|
|
router.Use(authHandler.WebAuthMiddleware())
|
|
router.GET("/admin-route", func(c *gin.Context) {
|
|
userID, exists := c.Get("user_id")
|
|
if !exists {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "no user_id"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"user_id": userID})
|
|
})
|
|
|
|
// Agent JWT with issuer=redflag-agent
|
|
agentClaims := middleware.AgentClaims{
|
|
AgentID: uuid.New(),
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
Issuer: middleware.JWTIssuerAgent,
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
|
},
|
|
}
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, agentClaims)
|
|
agentToken, err := token.SignedString([]byte(sharedSecret))
|
|
if err != nil {
|
|
t.Fatalf("failed to sign agent JWT: %v", err)
|
|
}
|
|
|
|
req := httptest.NewRequest("GET", "/admin-route", nil)
|
|
req.Header.Set("Authorization", "Bearer "+agentToken)
|
|
rec := httptest.NewRecorder()
|
|
router.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusUnauthorized && rec.Code != http.StatusForbidden {
|
|
t.Errorf("[ERROR] [server] [auth] agent JWT accepted by WebAuthMiddleware (got %d, expected 401)", rec.Code)
|
|
}
|
|
t.Logf("[INFO] [server] [auth] F-A3-12 FIXED: agent JWT rejected by web middleware (%d)", rec.Code)
|
|
}
|