Skip to content

fix: replace hardcoded JWT keys and add auth middleware (CWE-798, CWE-862)#188

Open
saaa99999999 wants to merge 3 commits into
cloudwego:mainfrom
saaa99999999:fix/jwt-hardcoded-keys
Open

fix: replace hardcoded JWT keys and add auth middleware (CWE-798, CWE-862)#188
saaa99999999 wants to merge 3 commits into
cloudwego:mainfrom
saaa99999999:fix/jwt-hardcoded-keys

Conversation

@saaa99999999
Copy link
Copy Markdown
Contributor

@saaa99999999 saaa99999999 commented May 23, 2026

Three demo applications in this repo ship with hardcoded JWT signing keys and unprotected CRUD endpoints. Developers who copy these official examples get these problems directly in their own code.

1. hertz_jwt — JWT key hardcoded as "secret key"

bizdemo/hertz_jwt/biz/mw/jwt.go:39:

JwtMiddleware, err = jwt.New(&jwt.HertzJWTMiddleware{
    Realm:         "test zone",
    Key:           []byte("secret key"),
    Timeout:       time.Hour,
    // ...
})

The string "secret key" is the HMAC signing key for all JWT tokens in this demo. Anyone who reads the repo can forge tokens for any identity.

import jwt
token = jwt.encode({"identity": "admin", "exp": 9999999999}, "secret key", algorithm="HS256")
# This token passes all auth checks

Fixed by reading from env var, exiting cleanly if not set:

func getJWTKey() []byte {
    key := os.Getenv("JWT_SECRET_KEY")
    if key == "" {
        fmt.Fprintf(os.Stderr, "fatal: JWT_SECRET_KEY is not set. Generate one with: openssl rand -base64 32
")
        os.Exit(1)
    }
    return []byte(key)
}
// ...
Key: getJWTKey(),

2. tiktok_demo — JWT key hardcoded as "tiktok secret key"

bizdemo/tiktok_demo/biz/mw/jwt/jwt.go:47:

JwtMiddleware, _ = jwt.New(&jwt.HertzJWTMiddleware{
    Key:         []byte("tiktok secret key"),
    TokenLookup: "query:token,form:token",
    // ...
})

Same pattern. The key is in the public repo. Plus TokenLookup allows tokens via URL query parameters, which means tokens leak into browser history and server logs.

Fixed the same way — reads from TIKTOK_JWT_SECRET_KEY env var, exits cleanly if missing.

3. hertz_gorm — user CRUD endpoints have no authentication

bizdemo/hertz_gorm/biz/router/user_gorm/middleware.go:

func _createuserMw() []app.HandlerFunc { return nil }
func _deleteuserMw() []app.HandlerFunc { return nil }
func _queryuserMw()  []app.HandlerFunc { return nil }
func _updateuserMw() []app.HandlerFunc { return nil }

All four middleware stubs return nil. These protect POST endpoints:

  • /v1/user/create/
  • /v1/user/update/:user_id
  • /v1/user/delete/:user_id
  • /v1/user/query/

Without auth middleware, anyone can create, read, update, or delete any user in the database.

Fixed by applying JWT middleware to all four endpoints:

func _createuserMw() []app.HandlerFunc {
    return []app.HandlerFunc{jwtMiddleware.MiddlewareFunc()}
}
// same for _deleteuserMw, _queryuserMw, _updateuserMw

CWE-798 / CWE-862

@saaa99999999 saaa99999999 requested review from a team as code owners May 23, 2026 05:34
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 23, 2026

CLA assistant check
All committers have signed the CLA.

Replace hardcoded JWT keys ("secret key" in hertz_jwt, "tiktok secret key"
in tiktok_demo) with environment variable lookups that panic on empty value,
preventing silent insecure defaults.

Also adds JWT authentication middleware to hertz_gorm user CRUD endpoints
which previously had no authentication.

CWE-798: Use of Hard-coded Credentials
CWE-862: Missing Authorization

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@saaa99999999 saaa99999999 force-pushed the fix/jwt-hardcoded-keys branch from 65641b2 to fbe88d3 Compare May 23, 2026 07:56
The sonic v1.14.1 and other dependencies use GoMapIterator which
was removed in Go 1.23+. Pin to 1.22 to avoid build failures.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants