-
Notifications
You must be signed in to change notification settings - Fork 20
fix(core): accept URL-safe base64 in ENCRYPTION_KEY validator (prod hotfix) #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,21 @@ function getEncryptionKey(): Buffer { | |
| } | ||
| } | ||
|
|
||
| // Try as URL-safe base64 (alphabet [A-Za-z0-9_-], no padding). Historically | ||
| // some keys were generated as `openssl rand -base64 32 | tr +/ -_` and stored | ||
| // in this form; same 32 bytes, just a different alphabet. Apply the same | ||
| // round-trip check so typos still get rejected. | ||
| if (/^[A-Za-z0-9_-]+$/.test(key)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For URL-safe keys generated by the documented historical command Useful? React with 👍 / 👎. |
||
| const urlsafeBuffer = Buffer.from(key, "base64url"); | ||
| if ( | ||
| urlsafeBuffer.length === 32 && | ||
| urlsafeBuffer.toString("base64url") === key | ||
| ) { | ||
| cachedKey = urlsafeBuffer; | ||
| return urlsafeBuffer; | ||
| } | ||
| } | ||
|
Comment on lines
+37
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the validation error text to mention URL-safe base64. Line 66 still says only canonical base64 or hex are accepted, which is now outdated and can mislead incident triage. Proposed patch- throw new Error(
- "ENCRYPTION_KEY must be a canonical base64 or hex encoded 32-byte key. " +
- "Generate a valid key with: openssl rand -base64 32 (or openssl rand -hex 32)"
- );
+ throw new Error(
+ "ENCRYPTION_KEY must be a canonical base64, URL-safe base64 (unpadded), or hex encoded 32-byte key. " +
+ "Generate a valid key with: openssl rand -base64 32, openssl rand -base64 32 | tr +/ -_, or openssl rand -hex 32"
+ );Also applies to: 65-68 🤖 Prompt for AI Agents |
||
|
|
||
| // Try as hex (must be exactly 64 hex characters for 32 bytes), again | ||
| // verifying the round-trip so partially-valid input is rejected. | ||
| if (/^[0-9a-fA-F]+$/.test(key) && key.length % 2 === 0) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regression can pass without exercising the URL-safe parsing path.
Because
getEncryptionKey()caches the first valid key, this test may reuse the previous test’s base64 key instead of the URL-safe key set on Line 49.Proposed patch
🤖 Prompt for AI Agents