fix: replace hardcoded JWT keys and add auth middleware (CWE-798, CWE-862)#188
Open
saaa99999999 wants to merge 3 commits into
Open
fix: replace hardcoded JWT keys and add auth middleware (CWE-798, CWE-862)#188saaa99999999 wants to merge 3 commits into
saaa99999999 wants to merge 3 commits into
Conversation
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>
65641b2 to
fbe88d3
Compare
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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.
Fixed by reading from env var, exiting cleanly if not set:
2. tiktok_demo — JWT key hardcoded as "tiktok secret key"
bizdemo/tiktok_demo/biz/mw/jwt/jwt.go:47:
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:
All four middleware stubs return nil. These protect POST endpoints:
Without auth middleware, anyone can create, read, update, or delete any user in the database.
Fixed by applying JWT middleware to all four endpoints:
CWE-798 / CWE-862