fix(codebase): answer 401 rather than 500 on a non-ASCII webhook secret header - #1524
Merged
Conversation
…et header hmac.compare_digest raises TypeError when either str argument holds a non-ASCII character, and WSGI/ASGI header values reach Django latin-1 decoded. An unauthenticated caller could therefore turn either webhook endpoint into a 500 (and a Sentry error event) with a one-byte header change. Both validators now compare UTF-8 bytes, which stays constant-time and makes the documented 401 hold for every possible header value. The GitHub validator has the same shape: its sha256= prefix check passes on a header whose remainder is non-ASCII, and the remainder is what gets compared. Leaves untouched the separate, deliberate choice to return True when no secret is configured.
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.
Both webhook validators compared the incoming secret against the configured one using
hmac.compare_digestonstrvalues. That raisesTypeErrorwhen either argument holds a non-ASCII character:WSGI/ASGI header values reach Django latin-1-decoded, so any non-ASCII byte in the secret header becomes a non-ASCII
str. An unauthenticated caller could therefore turn either webhook endpoint into a 500 — and a Sentry error event — with a one-byte header change, instead of the documented 401.Both clients, not just GitLab
GitLab compares the
X-Gitlab-Tokenheader directly, so the path is immediate.GitHub is less obvious and was worth checking: its
startswith("sha256=")guard passes on a header whose remainder is non-ASCII, and that remainder (signature[7:]) is exactly what reachescompare_digest. SoX-Hub-Signature-256: sha256=<0xE9>crashes it the same way.The fix
Encode both sides to UTF-8 before comparing. This stays constant-time —
compare_digestaccepts bytes-like arguments and is designed for exactly this — and makes the 401 hold for every possible header value.Unchanged: both validators still return
Truewhen no secret is configured. That is a separate, deliberate design choice and is out of scope here.Tests
Three, each written failing first and each reproducing the real
TypeErrorat the real call site before the fix:test_a_non_ascii_token_is_rejected_rather_than_raising— GitLab validator returnsFalserather than raisingtest_a_non_ascii_signature_is_rejected_rather_than_raising— GitHub validator, sametest_gitlab_callback_non_ascii_token_is_401_not_500— HTTP level, asserts the endpoint answers 401; before the fix this test surfaced the crash rather than a status code, which is the user-visible half of the bugGitHub has no view-level webhook test harness (its
test_callbacks.pyis unit-level), so no HTTP-level counterpart was invented for it; the function-level test covers the same defect.make test→ 4562 passing on this branch, ruff clean.Provenance
Found while reviewing the Telegram notification channel (#1523), which hit the identical bug in its own webhook route and fixed it there. This backports the same fix to the two pre-existing validators.