security: require secret env vars at startup, remove dev fallbacks#41
security: require secret env vars at startup, remove dev fallbacks#41
Conversation
Replace hardcoded development secret fallbacks with a requireEnv() helper that throws immediately if AUTH_SESSION_SECRET or EPDS_CALLBACK_SECRET are missing. This prevents services from silently running with well-known secrets in production. Also removes the unused csrfSecret config field and parameter — the CSRF middleware already uses crypto.randomBytes and ignores it. Closes #37
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughThis PR removes hardcoded development secret fallbacks from the auth service and PDS core, requiring Changes
Estimated Code Review Effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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 |
|
🚅 Deployed to the ePDS-pr-41 environment in ePDS
|
|
Pull Request Test Coverage Report for Build 23618738448Details
💛 - Coveralls |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/auth-service/src/index.ts`:
- Around line 124-133: requireEnv currently accepts whitespace-only values
(e.g., " ") as valid, so update the function requireEnv to reject values that
are empty after trimming: retrieve process.env[name], check if value is
undefined or value.trim().length === 0, and throw the existing Error if so; keep
the same error message but ensure values consisting only of whitespace are
treated as missing. Use the function name requireEnv to locate and modify the
check.
🪄 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: defaults
Review profile: CHILL
Plan: Pro
Run ID: efd7433b-d33c-47c0-9248-b6fa2724a6f4
📒 Files selected for processing (11)
.env.examplepackages/auth-service/.env.examplepackages/auth-service/src/__tests__/consent.test.tspackages/auth-service/src/__tests__/csrf.test.tspackages/auth-service/src/context.tspackages/auth-service/src/index.tspackages/auth-service/src/middleware/csrf.tspackages/pds-core/.env.examplepackages/pds-core/src/index.tspackages/shared/src/__tests__/types.test.tspackages/shared/src/types.ts
💤 Files with no reviewable changes (4)
- packages/auth-service/src/tests/consent.test.ts
- packages/auth-service/src/context.ts
- packages/shared/src/types.ts
- packages/shared/src/tests/types.test.ts
| function requireEnv(name: string): string { | ||
| const value = process.env[name] | ||
| if (!value) { | ||
| throw new Error( | ||
| `Missing required environment variable: ${name}. ` + | ||
| `Generate a value with: openssl rand -hex 32`, | ||
| ) | ||
| } | ||
| return value | ||
| } |
There was a problem hiding this comment.
Reject whitespace-only required env values.
requireEnv() currently treats values like ' ' as valid. That can still boot with an effectively empty secret.
🔧 Proposed fix
function requireEnv(name: string): string {
- const value = process.env[name]
+ const value = process.env[name]?.trim()
if (!value) {
throw new Error(
`Missing required environment variable: ${name}. ` +
`Generate a value with: openssl rand -hex 32`,
)
}
return value
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| function requireEnv(name: string): string { | |
| const value = process.env[name] | |
| if (!value) { | |
| throw new Error( | |
| `Missing required environment variable: ${name}. ` + | |
| `Generate a value with: openssl rand -hex 32`, | |
| ) | |
| } | |
| return value | |
| } | |
| function requireEnv(name: string): string { | |
| const value = process.env[name]?.trim() | |
| if (!value) { | |
| throw new Error( | |
| `Missing required environment variable: ${name}. ` + | |
| `Generate a value with: openssl rand -hex 32`, | |
| ) | |
| } | |
| return value | |
| } |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/auth-service/src/index.ts` around lines 124 - 133, requireEnv
currently accepts whitespace-only values (e.g., " ") as valid, so update the
function requireEnv to reject values that are empty after trimming: retrieve
process.env[name], check if value is undefined or value.trim().length === 0, and
throw the existing Error if so; keep the same error message but ensure values
consisting only of whitespace are treated as missing. Use the function name
requireEnv to locate and modify the check.



Summary
'dev-*-change-me'fallbacks forAUTH_SESSION_SECRETandEPDS_CALLBACK_SECRETwith arequireEnv()helper that throws at startup if the variable is missingpds-core/src/index.tsforEPDS_CALLBACK_SECRETcsrfSecretconfig field and unused_secretparameter incsrfProtection()— the middleware usescrypto.randomBytesand ignores it.env.examplefiles withREQUIREDannotations and removeAUTH_CSRF_SECRETMotivation
If these env vars are not set in production, services previously ran silently with well-known secrets, allowing an attacker to forge session cookies (
AUTH_SESSION_SECRET) or HMAC callback signatures (EPDS_CALLBACK_SECRET).Breaking Change
Services will now refuse to start without
AUTH_SESSION_SECRETandEPDS_CALLBACK_SECRETset. Existing deployments that rely on the dev defaults must set these env vars before upgrading.Generate values with:
openssl rand -hex 32Closes #37
Summary by CodeRabbit
Bug Fixes
Chores