-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add no-hmac-equality-compare ast-grep rule #15
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,22 @@ | ||
| id: no-hmac-equality-compare | ||
| message: "Use constant_time_eq() for HMAC comparison, not == or !=. Non-constant-time comparison leaks timing information." | ||
| severity: error | ||
| language: rust | ||
| note: | | ||
| HMAC challenge IDs must be compared using constant_time_eq() to prevent | ||
| timing side-channel attacks. Using == or != leaks information about how | ||
| many leading bytes match, which can allow an attacker to forge valid HMACs. | ||
|
|
||
| Replace: | ||
| if credential.challenge.id != expected_id { ... } | ||
| With: | ||
| if !constant_time_eq(&credential.challenge.id, &expected_id) { ... } | ||
|
|
||
| To disable this rule: | ||
| - Line: add `// ast-grep-ignore: no-hmac-equality-compare` with justification | ||
| rule: | ||
| any: | ||
| - pattern: $A == expected_id | ||
| - pattern: $A != expected_id | ||
| - pattern: expected_id == $A | ||
| - pattern: expected_id != $A | ||
45 changes: 45 additions & 0 deletions
45
src/rust/tests/__snapshots__/no-hmac-equality-compare-snapshot.yml
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,45 @@ | ||
| id: no-hmac-equality-compare | ||
| snapshots: | ||
| ? | | ||
| if credential.challenge.id != expected_id { | ||
| return Err("mismatch"); | ||
| } | ||
| : labels: | ||
| - source: credential.challenge.id != expected_id | ||
| style: primary | ||
| start: 3 | ||
| end: 41 | ||
| ? | | ||
| if credential.challenge.id == expected_id { | ||
| return Ok(()); | ||
| } | ||
| : labels: | ||
| - source: credential.challenge.id == expected_id | ||
| style: primary | ||
| start: 3 | ||
| end: 41 | ||
| ? | | ||
| if expected_id != credential.challenge.id { | ||
| return Err("mismatch"); | ||
| } | ||
| : labels: | ||
| - source: expected_id != credential.challenge.id | ||
| style: primary | ||
| start: 3 | ||
| end: 41 | ||
| ? | | ||
| if expected_id == credential.challenge.id { | ||
| return Ok(()); | ||
| } | ||
| : labels: | ||
| - source: expected_id == credential.challenge.id | ||
| style: primary | ||
| start: 3 | ||
| end: 41 | ||
| ? | | ||
| let matches = self.id == expected_id; | ||
| : labels: | ||
| - source: self.id == expected_id | ||
| style: primary | ||
| start: 14 | ||
| end: 36 |
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,32 @@ | ||
| id: no-hmac-equality-compare | ||
| valid: | ||
| - | | ||
| if !constant_time_eq(&credential.challenge.id, &expected_id) { | ||
| return Err("mismatch"); | ||
| } | ||
| - | | ||
| constant_time_eq(&self.id, &expected_id) | ||
| - | | ||
| let expected_id = compute_challenge_id(secret, realm, method, intent, request, None, None, None); | ||
| - | | ||
| let is_valid = hmac_verify(&id, &expected_id); | ||
|
|
||
| invalid: | ||
| - | | ||
| if credential.challenge.id != expected_id { | ||
| return Err("mismatch"); | ||
| } | ||
| - | | ||
| if credential.challenge.id == expected_id { | ||
| return Ok(()); | ||
| } | ||
| - | | ||
| if expected_id != credential.challenge.id { | ||
| return Err("mismatch"); | ||
| } | ||
| - | | ||
| if expected_id == credential.challenge.id { | ||
| return Ok(()); | ||
| } | ||
| - | | ||
| let matches = self.id == expected_id; |
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.
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.
The rule only matches comparisons where the RHS/LHS is the bare identifier
expected_id, so it misses common Rust forms likefoo == &expected_id,&foo == expected_id_ref, orfoo == *expected_idwhenexpected_idis stored as a reference. In those cases the non-constant-time comparison still happens, but this lint will not fire, which undermines the stated goal of preventing HMAC timing-regression patterns from slipping into PRs.Useful? React with 👍 / 👎.