-
Notifications
You must be signed in to change notification settings - Fork 24
Add validation for special characters in SERVER_SECRET_KEY and fix test infrastructure #185
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5c48e27
Initial plan
Copilot a9afb1f
Add regex validation for SERVER_SECRET_KEY to reject special characters
Copilot de9ef2c
Fix test case with incorrect key length for secret key validation
Copilot 566a4b7
Update test to use actual parseConfig function instead of isolated sc…
Copilot 65141ff
Fix formatting issues in secret key validation test
Copilot 345417d
Fix TypeScript errors in secret key validation test
Copilot 27792cf
Fix formatting issues in secret_key_validation_test.ts
Copilot a67452c
Remove unused assertRejects import to fix linting error
Copilot b0c1e2f
Fix test hanging issue by disabling PO token generation and handling …
Copilot 6ffb343
Fix config environment variable evaluation and secret key validation …
Copilot 3d4159f
revert useless changes
unixfox 60e9531
revert again
unixfox 0c69a7e
Merge branch 'master' into copilot/fix-140
unixfox fbb9cd8
fix: no need for new ),
unixfox 0a146c3
chore: move back down secret_key for better diff
unixfox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/** | ||
* Test for secret key validation in the config schema | ||
* This test verifies that SERVER_SECRET_KEY validation properly rejects special characters | ||
*/ | ||
import { assert, assertEquals } from "./deps.ts"; | ||
import { z } from "zod"; | ||
|
||
// Extract the exact validation logic we implemented | ||
const SecretKeySchema = z.string().length(16).regex( | ||
/^[a-zA-Z0-9]+$/, | ||
"SERVER_SECRET_KEY contains invalid characters. Only alphanumeric characters (a-z, A-Z, 0-9) are allowed. Please generate a valid key using 'pwgen 16 1' or ensure your key contains only letters and numbers.", | ||
); | ||
|
||
Deno.test("Secret key validation in config schema", async (t) => { | ||
await t.step("accepts valid alphanumeric keys", () => { | ||
const validKeys = [ | ||
"aaaaaaaaaaaaaaaa", // all lowercase | ||
"AAAAAAAAAAAAAAAA", // all uppercase | ||
"1234567890123456", // all numbers | ||
"Aa1Bb2Cc3Dd4Ee5F", // mixed case | ||
"ABC123DEF456789A", // mixed letters and numbers | ||
]; | ||
|
||
for (const key of validKeys) { | ||
const result = SecretKeySchema.safeParse(key); | ||
assert( | ||
result.success, | ||
`Key "${key}" should be valid but was rejected`, | ||
); | ||
if (result.success) { | ||
assertEquals(result.data, key); | ||
} | ||
} | ||
}); | ||
|
||
await t.step("rejects keys with special characters", () => { | ||
const invalidKeys = [ | ||
"my#key!123456789", // Contains # and ! | ||
"test@key12345678", // Contains @ (fixed length) | ||
"key-with-dashes1", // Contains - | ||
"key_with_under_s", // Contains _ | ||
"key with spaces1", // Contains spaces (fixed length to 16) | ||
"key$with$dollar$", // Contains $ | ||
"key+with+plus+12", // Contains + | ||
"key=with=equals=", // Contains = | ||
"key(with)parens1", // Contains () | ||
"key[with]bracket", // Contains [] | ||
]; | ||
|
||
for (const key of invalidKeys) { | ||
const result = SecretKeySchema.safeParse(key); | ||
assert( | ||
!result.success, | ||
`Key "${key}" should be invalid but was accepted`, | ||
); | ||
if (!result.success) { | ||
const errorMessage = result.error.issues[0].message; | ||
assert( | ||
errorMessage.includes( | ||
"SERVER_SECRET_KEY contains invalid characters", | ||
), | ||
`Error message should mention invalid characters, got: ${errorMessage}`, | ||
); | ||
assert( | ||
errorMessage.includes("alphanumeric characters"), | ||
`Error message should mention alphanumeric, got: ${errorMessage}`, | ||
); | ||
assert( | ||
errorMessage.includes("pwgen"), | ||
`Error message should suggest pwgen, got: ${errorMessage}`, | ||
); | ||
} | ||
} | ||
}); | ||
|
||
await t.step("rejects keys with wrong length", () => { | ||
const wrongLengthKeys = [ | ||
"short", // Too short | ||
"thiskeyistoolongtobevalid", // Too long | ||
"", // Empty | ||
"a", // Single character | ||
"exactly15chars", // 15 chars | ||
"exactly17charss", // 17 chars | ||
]; | ||
|
||
for (const key of wrongLengthKeys) { | ||
const result = SecretKeySchema.safeParse(key); | ||
assert( | ||
!result.success, | ||
`Key "${key}" (length ${key.length}) should be invalid but was accepted`, | ||
); | ||
} | ||
}); | ||
|
||
await t.step("validates error message content", () => { | ||
// Test that special character validation provides the right error | ||
const specialCharResult = SecretKeySchema.safeParse("my#key!123456789"); | ||
assert(!specialCharResult.success); | ||
|
||
if (!specialCharResult.success) { | ||
const errorMessage = specialCharResult.error.issues[0].message; | ||
|
||
// Check that the error message contains all expected elements | ||
assert( | ||
errorMessage.includes("SERVER_SECRET_KEY contains invalid characters"), | ||
"Should mention SERVER_SECRET_KEY and invalid characters", | ||
); | ||
assert( | ||
errorMessage.includes("Only alphanumeric characters (a-z, A-Z, 0-9) are allowed"), | ||
"Should specify allowed character set", | ||
); | ||
assert( | ||
errorMessage.includes("pwgen 16 1"), | ||
"Should suggest pwgen command", | ||
); | ||
} | ||
|
||
// Test that length validation still works and provides clear message | ||
const lengthResult = SecretKeySchema.safeParse("short"); | ||
assert(!lengthResult.success); | ||
|
||
if (!lengthResult.success) { | ||
const lengthMessage = lengthResult.error.issues[0].message; | ||
assert( | ||
lengthMessage.includes("exactly 16 character"), | ||
`Should mention 16 characters: ${lengthMessage}`, | ||
); | ||
} | ||
}); | ||
|
||
await t.step("validates precedence - length vs character validation", () => { | ||
// When both length and character validation fail, length should be checked first | ||
// This is the default Zod behavior | ||
const result = SecretKeySchema.safeParse("bad#"); | ||
assert(!result.success); | ||
|
||
if (!result.success) { | ||
const errorMessage = result.error.issues[0].message; | ||
// Should get length error since it's checked first | ||
assert( | ||
errorMessage.includes("exactly 16 character"), | ||
`Should get length error first: ${errorMessage}`, | ||
); | ||
} | ||
}); | ||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.