fix: require explicit JWT secret configuration#301
Conversation
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 57 minutes and 30 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughJWT secret configuration now enforces strict validation at startup. The change removes fallback default secret behavior, adds mandatory Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~15 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
base/src/main/java/com/tinyengine/it/login/utils/JwtUtil.java (1)
62-74: Consider caching the derivedSecretKey.
getSecretKey()is invoked on every token generation/parse/validation call, each time hittingSystem.getenvand rebuilding the HMAC key. SinceSECRET_STRINGis validated once at startup and is not expected to change at runtime, you can compute theSecretKeyonce (e.g., invalidateSecretConfiguration()into aprivate SecretKey secretKeyfield, or astatic finallazily initialized holder) and reuse it. Minor, but removes repeated allocation/env lookups on a hot path.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@base/src/main/java/com/tinyengine/it/login/utils/JwtUtil.java` around lines 62 - 74, getSecretKey() currently calls getSecretString() on every use which repeats System.getenv and rebuilds the HMAC key; compute and cache the SecretKey once and reuse it: add a private static final (or a private static volatile with lazy holder) SecretKey field and initialize it either at class load or inside validateSecretConfiguration() after validating SECRET_ENV_NAME, then have getSecretKey() return that cached field instead of recreating it each call.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@base/src/main/java/com/tinyengine/it/login/utils/JwtUtil.java`:
- Around line 49-60: validateSecretConfiguration() in JwtUtil now throws on
missing SECRET_STRING/SECRET_ENV_NAME which will break existing containers;
update deployment manifests and docs to inject this env var and document the
breaking change. Modify the tiny-engine-back service environment in
docker-compose.yml and the Dockerfile (and any sample .env or README) to define
SECRET_STRING (or set SECRET_ENV_NAME to the correct variable), ensure
JwtUtil.getSecretKey() can read it at container start, and add a clear note in
the PR/release notes that SECRET_STRING is required before upgrading.
- Around line 72-74: The current getSecretKey() uses
Keys.hmacShaKeyFor(getSecretString().getBytes(...)) which will throw
WeakKeyException for secrets under 32 bytes and, combined with the
`@PostConstruct` validateSecretConfiguration check, will block startup; update
validateSecretConfiguration (and any startup check) to explicitly validate the
SECRET_STRING length in UTF-8 bytes (require >=32 bytes for HS256) and throw a
clear, distinct IllegalStateException messages for missing vs too-short/weak
secrets (e.g., "SECRET_STRING is not set" vs "SECRET_STRING is too weak: must be
at least 32 bytes/characters in UTF-8"); alternatively catch WeakKeyException in
getSecretKey() and rethrow a clearer exception referencing SECRET_STRING; also
update README/deployment docs to state the minimum 32-byte requirement for
SECRET_STRING.
---
Nitpick comments:
In `@base/src/main/java/com/tinyengine/it/login/utils/JwtUtil.java`:
- Around line 62-74: getSecretKey() currently calls getSecretString() on every
use which repeats System.getenv and rebuilds the HMAC key; compute and cache the
SecretKey once and reuse it: add a private static final (or a private static
volatile with lazy holder) SecretKey field and initialize it either at class
load or inside validateSecretConfiguration() after validating SECRET_ENV_NAME,
then have getSecretKey() return that cached field instead of recreating it each
call.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: a619d375-d993-4bc6-958a-50004cc48487
📒 Files selected for processing (1)
base/src/main/java/com/tinyengine/it/login/utils/JwtUtil.java
Summary
SECRET_STRINGTesting
mvn -pl base -DskipTests compilecould not start becauseJAVA_HOMEis not configured in this environmentSummary by CodeRabbit
Release Notes