Skip to content

JWT improvements FMSG_API_JWT_SECRET starting with base64: decoded as base64#5

Merged
markmnl merged 3 commits intomainfrom
jwt-improvements
Apr 18, 2026
Merged

JWT improvements FMSG_API_JWT_SECRET starting with base64: decoded as base64#5
markmnl merged 3 commits intomainfrom
jwt-improvements

Conversation

@markmnl
Copy link
Copy Markdown
Owner

@markmnl markmnl commented Apr 18, 2026

No description provided.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates JWT secret handling so FMSG_API_JWT_SECRET can optionally be provided as base64-encoded bytes (via a base64: prefix), enabling non-text/binary-safe HMAC keys to be configured via environment variables.

Changes:

  • Change middleware.SetupJWT to accept HMAC key bytes ([]byte) instead of a string.
  • Add parseSecret in main.go to decode FMSG_API_JWT_SECRET when prefixed with base64:.
  • Update JWT middleware test and README to reflect the new behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/middleware/jwt_test.go Updates test callsite to pass key bytes into SetupJWT.
src/middleware/jwt.go Changes SetupJWT signature to accept []byte and uses it directly as the middleware key.
src/main.go Adds base64-prefixed secret parsing and passes derived key bytes into SetupJWT.
README.md Documents base64: prefix support for FMSG_API_JWT_SECRET.

Comment thread src/main.go
Comment on lines +101 to +104
b, err := base64.StdEncoding.DecodeString(s[len(prefix):])
if err != nil {
log.Fatalf("FMSG_API_JWT_SECRET has base64: prefix but is not valid base64: %v", err)
}
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseSecret can return an empty HMAC key when FMSG_API_JWT_SECRET is set to just base64: (or base64 that decodes to an empty byte slice). That would silently run JWT validation with an empty secret, which is a security misconfiguration. Consider validating that the decoded key length is > 0 (and failing fast) before returning it; similarly consider trimming whitespace around the base64 payload to avoid surprising decode failures from env formatting.

Suggested change
b, err := base64.StdEncoding.DecodeString(s[len(prefix):])
if err != nil {
log.Fatalf("FMSG_API_JWT_SECRET has base64: prefix but is not valid base64: %v", err)
}
payload := strings.TrimSpace(s[len(prefix):])
b, err := base64.StdEncoding.DecodeString(payload)
if err != nil {
log.Fatalf("FMSG_API_JWT_SECRET has base64: prefix but is not valid base64: %v", err)
}
if len(b) == 0 {
log.Fatalf("FMSG_API_JWT_SECRET must not decode to an empty key")
}

Copilot uses AI. Check for mistakes.
@markmnl markmnl merged commit bf6c22c into main Apr 18, 2026
1 check passed
@markmnl markmnl deleted the jwt-improvements branch April 18, 2026 03:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants