{"record":{"id":"4c13a508b2631a0a","repo":"Tencent/WeKnora","slug":"unexpected-signing-method-v","errorCode":null,"errorMessage":"unexpected signing method: %v","messagePattern":"unexpected signing method: (.+?)","errorType":"validation","errorClass":null,"httpStatus":401,"severity":"error","filePath":"internal/middleware/auth.go","lineNumber":628,"sourceCode":"\nfunc verifyExternalUserJWT(tokenString string, tenantID uint64, secret string) (string, error) {\n\ttokenString = strings.TrimSpace(tokenString)\n\tsecret = strings.TrimSpace(secret)\n\tif tokenString == \"\" {\n\t\treturn \"\", errors.New(\"missing external user token\")\n\t}\n\tif secret == \"\" {\n\t\treturn \"\", errors.New(\"external user token secret is not configured\")\n\t}\n\tclaims := jwt.MapClaims{}\n\tparser := jwt.NewParser(\n\t\tjwt.WithAudience(\"weknora\"),\n\t\tjwt.WithExpirationRequired(),\n\t\tjwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}),\n\t)\n\ttoken, err := parser.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {\n\t\tif _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {\n\t\t\treturn nil, fmt.Errorf(\"unexpected signing method: %v\", token.Header[\"alg\"])\n\t\t}\n\t\treturn []byte(secret), nil\n\t})\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif token == nil || !token.Valid {\n\t\treturn \"\", errors.New(\"invalid external user token\")\n\t}\n\texp, err := claims.GetExpirationTime()\n\tif err != nil || exp == nil {\n\t\treturn \"\", errors.New(\"missing expiration\")\n\t}\n\tif time.Until(exp.Time) > maxExternalUserTokenTTL {\n\t\treturn \"\", fmt.Errorf(\"token lifetime exceeds %s\", maxExternalUserTokenTTL)\n\t}\n\tif nbf, nbfErr := claims.GetNotBefore(); nbfErr == nil && nbf != nil && time.Now().Before(nbf.Time) {\n\t\treturn \"\", errors.New(\"token not yet valid\")","sourceCodeStart":610,"sourceCodeEnd":646,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/middleware/auth.go#L610-L646","documentation":"verifyExternalUserJWT in internal/middleware/auth.go:628 rejects tokens whose alg header is not an HMAC method, even though the parser already restricts valid methods to HS256. This is a defense-in-depth check against algorithm-confusion attacks (e.g. RS256/none alg confusion where an attacker supplies a public key as the HMAC secret).","triggerScenarios":"A signed-token external user presents a JWT whose header alg is anything other than an HMAC method (RS256, ES256, none, etc.); the keyfunc returns this error before a key is used.","commonSituations":"Client library auto-selecting RS256 because it has a key pair; a token minted by a different service using asymmetric signing; crafted tokens probing for alg confusion.","solutions":["Issue the token with alg=HS256 signed with the tenant's HMACSecret.","If your issuer only supports asymmetric algorithms, it is incompatible with this endpoint — use a pre-shared HMAC secret.","Never attempt to bypass by modifying server-side method checks."],"exampleFix":"// before\ntok := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)\n// after\ntok := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)\ns, _ := tok.SignedString([]byte(hmacSecret))","handlingStrategy":"try-catch","validationCode":"parts := strings.Split(token, \".\")\nif len(parts) == 3 {\n    hdr, _ := base64.RawURLEncoding.DecodeString(parts[0])\n    if !strings.Contains(string(hdr), \"\\\"HS256\\\"\") { // re-sign with HS256 first\n    }\n}","typeGuard":null,"tryCatchPattern":"if err != nil {\n    if strings.Contains(err.Error(), \"unexpected signing method\") {\n        // re-issue the token with jwt.SigningMethodHS256\n    }\n}","preventionTips":["Pin jwt.SigningMethodHS256 at issuance, matching the server's WithValidMethods.","Never use asymmetric tokens against HMAC-only endpoints.","Treat alg-confusion rejections as security signals, not bugs to bypass."],"tags":["jwt","security","algorithm-confusion"],"backgroundTag":"jwt-unexpected-signing-method","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}