Skip to content

Fix webhook signature validation crashes on malformed headers - #1245

Open
knowprasad wants to merge 1 commit into
CCExtractor:masterfrom
knowprasad:fix/webhook-signature-validation
Open

knowprasad wants to merge 1 commit into
CCExtractor:masterfrom
knowprasad:fix/webhook-signature-validation

Conversation

@knowprasad

Copy link
Copy Markdown

Summary

In utility.py, is_valid_signature(x_hub_signature, data, private_key) previously unpacked x_hub_signature.split('=', 1) and accessed hashlib.__dict__.get(hash_algorithm) without input and type guards.

If a webhook request arrives with a missing signature header, a header lacking =, an unknown hash algorithm, or a non-callable attribute, is_valid_signature would crash with unhandled exceptions (AttributeError, ValueError, TypeError), causing a 500 server error instead of returning False for the caller to abort gracefully.

Changes

  1. Added guards for x_hub_signature string type and presence of = before splitting.
  2. Verified hash_algorithm in hashlib.algorithms_available and verified callable(getattr(hashlib, hash_algorithm, None)).
  3. Handled string and bytes encoding safely for data and private_key.
  4. Wrapped hmac.new in try-except for (ValueError, TypeError) as a fallback guard.
  5. Added unit tests in tests/test_utility.py covering valid signatures, digest mismatches, missing/empty headers, malformed signatures without =, unknown algorithms, and non-callable attribute names.

Signed-off-by: prsdx <prsdx.dev@gmail.com>
@sonarqubecloud

Copy link
Copy Markdown

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.

1 participant