Skip to content

🛡️ Sentinel: [MEDIUM] Add VerifyHMACSha256 for secure HMAC verification#32

Open
Laisky wants to merge 1 commit intov6-devfrom
sentinel-verify-hmac-sha256-15182121104108134464
Open

🛡️ Sentinel: [MEDIUM] Add VerifyHMACSha256 for secure HMAC verification#32
Laisky wants to merge 1 commit intov6-devfrom
sentinel-verify-hmac-sha256-15182121104108134464

Conversation

@Laisky
Copy link
Owner

@Laisky Laisky commented Mar 12, 2026

🚨 Severity: MEDIUM
💡 Vulnerability: Lack of a dedicated HMAC verification function often leads developers to use non-constant-time comparison methods (like bytes.Equal), making the system vulnerable to timing attacks.
🎯 Impact: Attackers might exploit timing differences to recover valid HMAC signatures bit by bit.
🔧 Fix: Implemented VerifyHMACSha256 in crypto/sign.go using crypto/hmac.Equal for secure, constant-time verification.
✅ Verification: Added TestVerifyHMACSha256 in crypto/sign_test.go to verify success and failure cases.


PR created automatically by Jules for task 15182121104108134464 started by @Laisky

Co-authored-by: Laisky <4532436+Laisky@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 12, 2026 03:10
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8b0c69b6-cfaf-4938-bad6-bf9bda755d27

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sentinel-verify-hmac-sha256-15182121104108134464

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a dedicated HMAC-SHA256 verification helper to the crypto package to encourage constant-time signature comparison and reduce timing-attack risk.

Changes:

  • Introduces VerifyHMACSha256 in crypto/sign.go using crypto/hmac.Equal.
  • Adds TestVerifyHMACSha256 in crypto/sign_test.go covering success and mismatch scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
crypto/sign.go Adds a new public verification helper for HMAC-SHA256 signatures.
crypto/sign_test.go Adds unit tests for the new HMAC verification helper.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +330 to +339
// VerifyHMACSha256 verify HMAC by sha256
//
// # Args:
// - key: secure key, no limit on length
// - data: raw data to verify HMAC
// - signature: HMAC signature to verify
//
// # Returns:
// - err: nil if signature match
func VerifyHMACSha256(key, signature []byte, data io.Reader) error {
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment for VerifyHMACSha256 lists args in the order key, data, signature, but the function signature is func VerifyHMACSha256(key, signature []byte, data io.Reader). Please update the comment (or reorder parameters) so the documented argument order matches the actual API and avoids misuse by callers.

Copilot uses AI. Check for mistakes.
Comment on lines +345 to +346
if !hmac.Equal(h, signature) {
return errors.New("signature not match")
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VerifyHMACSha256 accepts any signature length and relies on hmac.Equal. hmac.Equal returns immediately when lengths differ, which undermines the goal of constant-time verification and also differs from other codepaths that enforce a 32-byte HMAC (e.g., SM4 decrypt validates HMAC length). Consider explicitly validating that signature is exactly 32 bytes (sha256 size) and returning an error otherwise.

Copilot uses AI. Check for mistakes.
}

if !hmac.Equal(h, signature) {
return errors.New("signature not match")
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error string "signature not match" is both grammatically incorrect and inconsistent with other verification helpers in this package (e.g., VerifyByEd25519WithSHA512 returns "invalid signature", and SM4 HMAC verification returns "hmac not match"). Consider using a consistent message (and update tests accordingly) so callers can reliably match/handle verification failures.

Suggested change
return errors.New("signature not match")
return errors.New("hmac not match")

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants