Repository navigation
Reject malformed signature headers instead of returning an unauthenticated 500 - #35
Conversation
CI failure summaryCI is failing on Test Agent with Dummy Data (Python 3.10, 3.11, 3.12). All other checks pass — including the new webhook signature tests in this PR. Failing test: tests/test_mcp.py::test_register_agent_tools_registers_all_tools Likely cause: Pre-existing MCP registry test issue on main, unrelated to the webhook signature fix. Suggested fix: Land a fix for tests/test_mcp.py on main, then rebase. This PR scope: Webhook auth changes look solid — integration, lint, security, and test_webhook_auth.py all pass. |
|
CI fix merged to |
Rebase needed — no code fixes requiredThe CI failure on this PR was caused by a pre-existing issue on Your PR code looks good. To get CI green, update your branch with latest git fetch origin
git checkout {"headRefName":"fix/webhook-signature-non-ascii"}
git rebase origin/main
git push --force-with-leaseOr use the Update branch button on this PR if GitHub offers it. After rebase, CI should pass and this will be ready to merge. Thanks for contributing! |
hmac.compare_digest refuses a str holding non-ASCII characters and raises TypeError, and header values reach the application latin-1-decoded, so any unauthenticated caller could turn signature verification into an unhandled error by sending a non-ASCII X-Hub-Signature-256 or X-Slack-Signature. The same applied to a non-UTF-8 request body, which made verify_slack_signature raise UnicodeDecodeError while building the Slack base string. Both surfaced as HTTP 500 on unauthenticated endpoints. Fail closed instead: a new module-private _signatures_equal helper returns False for anything compare_digest refuses to compare, and the Slack body decode is guarded the same way. The callers already answer False with 401, so api/server.py is unchanged. hmac.compare_digest remains the comparison primitive and neither side is sanitised, re-encoded lossily or truncated, so only a byte-for-byte match is still accepted. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Assert that /webhook/manual and /slack/action answer 401 rather than 500 for a non-ASCII signature header, and that /slack/action answers 401 for a non-UTF-8 body. These go through the real request path, so they pin the status code the caller actually observes rather than the verification function's return value. The client fixtures they use set raise_server_exceptions=False; without it an unhandled server error propagates out of the test client as an exception and a crash cannot be told apart from a rejection. The non-ASCII header is passed as bytes because httpx ASCII-encodes str header values and would fail in the client before the request is sent. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The previous parenthetical claimed compare_digest rejects non-str arguments generally, which is wrong: bytes-like arguments are legal when both are bytes-like. Rephrase to the actual rule—TypeError also occurs when only one argument is a str (the mixed str/bytes case). Co-Authored-By: Kimi K2.7 Code <[email protected]>
67d3b73 to
b017cd4
Compare
|
Rebased onto Confirmed it was the dependency float rather than anything on our side: at the old head, resolving fresh from that commit's |
Bug Description / Summary
Webhook signature verification raised
TypeErroron a non-ASCII signature header, which surfaced asan unauthenticated HTTP 500 on
/webhook/manual,/webhook/github,/webhook/alertmanagerand/slack/action.hmac.compare_digestrefuses to comparestrvalues containing non-ASCIIcharacters, and header values arrive latin-1-decoded, so any byte above 0x7F in the signature header
reached it.
Fixes #27.
There were three affected sites, not the two the issue named — both
compare_digestcall sitesplus the Slack body decode, which raised
UnicodeDecodeErroron a non-UTF-8 body for the sameunauthenticated 500.
What changed
api/webhook_auth.py— one module-private_signatures_equal(expected, provided)wrappinghmac.compare_digestintry/except TypeError: return False, used by both call sites; and atry/except UnicodeDecodeError: return Falsearound the Slack body decode.tests/test_security.py,tests/test_webhook_auth.py— unit and endpoint coverage.api/server.pyhas zero changed lines — returningFalseroutes into its existing 401 raises.compare_digestremains the primitive. No length pre-check, no normalisation, and deliberately nolossy encoding: the issue suggested
encode("latin-1", "ignore"), but that would mangle the valuebeing compared, so a malformed signature is rejected rather than repaired.
Testing
The dangerous version of this fix turns a crash into an accept, so that is what was tested hardest,
differentially against
main:mainreturns the identical bool here; every input thatraised on
mainreturnsFalse. The change is exactlyraise → False.characters that NFKC-normalise to the correct signature, modifier-letter substitutions, zero-width
space and BOM and soft-hyphen insertions, lone surrogates,
bytes/bytearray/memoryviewof thecorrect value, an object exposing
__buffer__, and astrsubclass overriding__eq__to returnTrue.toward small mutations of the correct signature) plus 20,000 bodies: zero exceptions escaped, and
all 41 accepts were byte-identical to the correct digest.
four endpoints), and an incorrect-but-well-formed one is still rejected.
byte-preserving, so real non-English traffic is unaffected.
Bite: tests were written first and run against untouched production code —
7 failed, 7 passed,the four unit failures tracing to the three sites (
TypeError×2 sites,UnicodeDecodeError×1) andthe three endpoint failures being
assert 500 == 401.Timing was measured, not assumed. Holding the string length constant at 71 and varying only the
position of the non-ASCII character, the
TypeErrorpath is flat at ~200 ns (207.0 / 209.8 / 199.4 /195.5 / 203.3 ns at indices 7/20/40/55/70), and the compare path is 89.7 / 90.7 / 94.0 ns for
late-mismatch / early-mismatch / exact match. The comparison itself is untouched and still
constant-time. The branch taken is decided by
provided.isascii()— a function of attacker-controlledinput only, since
expectedis always ASCII hex — so no secret material is involved.Gates:
pytest tests/ -v1 failed, 159 passed, 2 skippedagainst a1 failed, 145 passed, 2 skippedbaseline;
bandit -r . -llNo issues identified; flake8 selection 0.The one failure is pre-existing and is not ours:
tests/test_mcp.py::test_register_agent_tools_registers_all_toolsfails on a clean checkout because
requirements.txt:15pinsmcp[cli]>=1.9.0unbounded and mcp 2.0.0removed
mcp.server.fastmcp. Filed separately as #34. Expect this PR's CI to be red for that reasonalone.
Environment
Debian 13 (x86_64), Python 3.13.14. CI's matrix is 3.10–3.12; the
compare_digestnon-ASCIITypeErroris long-standing and version-stable, so I expect identical behaviour there, but that partis reasoned rather than measured.
Follow-ups / Known Limitations
WEBHOOK_SECRETunset,
api/server.py:199skips verification entirely, andverify_slack_signaturereturnsTruebefore reaching any of this. Measured identical on
mainand this branch. I left it alone becauseyou declined that change on security: verify the Slack signing secret before executing approved actions #24.
api/server.py:403doesparse_qs(body.decode()), which still 500s on a non-UTF-8 body when no signing secret isconfigured. It lives in
server.py, which this PR keeps at zero changed lines. Happy to file or fixit separately.
producing a non-UTF-8 body is now a silent 401 rather than a loud 500. That is the normal
fail-closed trade and the old behaviour was worse, but it is a real change in observability.
black --check .already fails onmain(67 files); this branch adds 23 further complaints, all intests/test_security.pyand all the 88-vs-100 column rule.api/webhook_auth.pyadds none. The stepis
continue-on-error, and I did not runblack .since it would rewrite 67 unrelated files.Generated by Claude Opus 5 (1M context) (brief, implementation, review), Kimi K3 (verification), Kimi K2.7 Code (implementation)