Repository navigation
fix(auth): keep the full length difference in ApiKey::matches - #6
Merged
Merged
Conversation
The length difference was narrowed to u8, so it folded to zero whenever the two lengths differed by a multiple of 256. The modulo indexing then let the key repeated that many times match on every byte: a generated 64-byte key presented five or nine times over authenticated. Keep the difference as a usize and widen each byte's XOR into it. The loop still reads every byte of the longer input, so the timing is unchanged. Fixes #4 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VhxjRgZo4zeCeNA8rZXp7v
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #4.
The bug
ApiKey::matchesnarrowed the length difference tou8, so it became zero whenever the lengths differed by a multiple of 256. Because the loop indexes both inputs modulo their lengths, the key repeated that many times then matched on every byte. A generated 64-byte key sent five or nine times over authenticated.The existing test only tried one repeat (
abcdefabcdef). A length difference of 6 does not fold to zero, so the test passed.The fix
usize, and each byte's XOR is widened into it withusize::from.Tests
a_repeated_key_does_not_match_at_any_length: a generated key repeated 2 to 9 times, and"ab"repeated 129 times (258 bytes).key x5 matched, and passes with the fix.cargo test --workspace,cargo clippy --workspace --all-targetsandcargo fmtare all clean.🤖 Generated with Claude Code
https://claude.ai/code/session_01VhxjRgZo4zeCeNA8rZXp7v