fix(security): block refresh tokens as bearer and sandbox MCP uploads

Reject refresh JWTs in ValidateToken, revoke all user sessions on logout,
and restrict create_knowledge_from_file to allowed directories on network MCP transports.
This commit is contained in:
wizardchen
2026-07-02 15:26:43 +08:00
committed by lyingbug
parent cd65665894
commit 040504447e
7 changed files with 180 additions and 3 deletions
+47
View File
@@ -858,11 +858,18 @@ func (s *userService) ValidateToken(ctx context.Context, tokenString string) (*t
return nil, 0, errors.New("invalid user ID in token")
}
if isRefreshTokenClaims(claims) {
return nil, 0, errors.New("refresh token cannot be used as access token")
}
// Check if token is revoked
tokenRecord, err := s.tokenRepo.GetTokenByValue(ctx, tokenString)
if err != nil || tokenRecord == nil || tokenRecord.IsRevoked {
return nil, 0, errors.New("token is revoked")
}
if tokenRecord.TokenType == "refresh_token" {
return nil, 0, errors.New("refresh token cannot be used as access token")
}
user, err := s.userRepo.GetUserByID(ctx, userID)
if err != nil {
@@ -877,6 +884,34 @@ func (s *userService) ValidateToken(ctx context.Context, tokenString string) (*t
return user, activeTenantID, nil
}
func isRefreshTokenClaims(claims jwt.MapClaims) bool {
tokenType, ok := claims["type"].(string)
return ok && tokenType == "refresh"
}
func userIDFromSignedToken(tokenString string) (string, error) {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(getJwtSecret()), nil
}, jwt.WithoutClaimsValidation())
if err != nil || token == nil || !token.Valid {
return "", errors.New("invalid token")
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return "", errors.New("invalid token claims")
}
userID, ok := claims["user_id"].(string)
if !ok || strings.TrimSpace(userID) == "" {
return "", errors.New("invalid user ID in token")
}
return userID, nil
}
// tenantIDFromClaims pulls the active tenant ID out of a parsed JWT
// claim map. Returns fallback when the claim is missing or has an
// unrecognised type. Extracted as a free function so it can be unit
@@ -958,6 +993,18 @@ func (s *userService) RefreshToken(
return s.GenerateTokens(ctx, user)
}
// Logout invalidates every outstanding session for the user identified by
// the presented JWT. Access and refresh tokens are both accepted so clients
// can end the session without refreshing first; expired tokens are allowed
// so logout still works after the access token TTL.
func (s *userService) Logout(ctx context.Context, tokenString string) error {
userID, err := userIDFromSignedToken(tokenString)
if err != nil {
return err
}
return s.tokenRepo.RevokeTokensByUserID(ctx, userID)
}
// RevokeToken revokes a token
func (s *userService) RevokeToken(ctx context.Context, tokenString string) error {
tokenRecord, err := s.tokenRepo.GetTokenByValue(ctx, tokenString)
@@ -6,6 +6,25 @@ import (
"github.com/golang-jwt/jwt/v5"
)
func TestIsRefreshTokenClaims(t *testing.T) {
cases := []struct {
name string
claims jwt.MapClaims
want bool
}{
{name: "refresh", claims: jwt.MapClaims{"type": "refresh"}, want: true},
{name: "access", claims: jwt.MapClaims{"type": "access"}, want: false},
{name: "missing type", claims: jwt.MapClaims{"user_id": "u1"}, want: false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := isRefreshTokenClaims(tc.claims); got != tc.want {
t.Fatalf("isRefreshTokenClaims(%v) = %v, want %v", tc.claims, got, tc.want)
}
})
}
}
// tenantIDFromClaims is the JWT->tenant-id projection used by
// userService.ValidateToken. It must:
// - prefer the claim when present (so /auth/switch-tenant takes effect)