Skip to content

Reject malformed signature headers instead of returning an unauthenticated 500 - #35

Merged
OkiriGabriel merged 3 commits into
open-devops-agent:mainfrom
Nitjsefnie-OSC:fix/webhook-signature-non-ascii
Aug 2, 2026
Merged

OkiriGabriel merged 3 commits into
open-devops-agent:mainfrom
Nitjsefnie-OSC:fix/webhook-signature-non-ascii

Conversation

@Nitjsefnie

Copy link
Copy Markdown
Contributor

Bug Description / Summary

Webhook signature verification raised TypeError on a non-ASCII signature header, which surfaced as
an unauthenticated HTTP 500 on /webhook/manual, /webhook/github, /webhook/alertmanager and
/slack/action. hmac.compare_digest refuses to compare str values containing non-ASCII
characters, 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_digest call sites
plus the Slack body decode, which raised UnicodeDecodeError on a non-UTF-8 body for the same
unauthenticated 500.

What changed

  • api/webhook_auth.py — one module-private _signatures_equal(expected, provided) wrapping
    hmac.compare_digest in try/except TypeError: return False, used by both call sites; and a
    try/except UnicodeDecodeError: return False around the Slack body decode.
  • tests/test_security.py, tests/test_webhook_auth.py — unit and endpoint coverage.

api/server.py has zero changed lines — returning False routes into its existing 401 raises.

compare_digest remains the primitive. No length pre-check, no normalisation, and deliberately no
lossy encoding: the issue suggested encode("latin-1", "ignore"), but that would mangle the value
being 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:

  • Every input that returned a bool on main returns the identical bool here; every input that
    raised on main returns False.
    The change is exactly raise → False.
  • Attempts that specifically tried to get a wrong signature accepted, all rejected: fullwidth
    characters that NFKC-normalise to the correct signature, modifier-letter substitutions, zero-width
    space and BOM and soft-hyphen insertions, lone surrogates, bytes/bytearray/memoryview of the
    correct value, an object exposing __buffer__, and a str subclass overriding __eq__ to return
    True.
  • A 200,000-signature fuzz (alphabet U+0000–U+02FF plus BOM, emoji and a lone surrogate, biased
    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.
  • The two regressions that matter both hold: a correct signature is still accepted (200 on all
    four endpoints), and an incorrect-but-well-formed one is still rejected.
  • Non-ASCII but valid UTF-8 Slack bodies still verify — the decode/re-encode round trip is
    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) and
the 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 TypeError path 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-controlled
input only, since expected is always ASCII hex — so no secret material is involved.

Gates: pytest tests/ -v 1 failed, 159 passed, 2 skipped against a 1 failed, 145 passed, 2 skipped
baseline; bandit -r . -ll No 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_tools
fails on a clean checkout because requirements.txt:15 pins mcp[cli]>=1.9.0 unbounded and mcp 2.0.0
removed mcp.server.fastmcp. Filed separately as #34. Expect this PR's CI to be red for that reason
alone.

Environment

Debian 13 (x86_64), Python 3.13.14. CI's matrix is 3.10–3.12; the compare_digest non-ASCII
TypeError is long-standing and version-stable, so I expect identical behaviour there, but that part
is reasoned rather than measured.

Follow-ups / Known Limitations

  • The unset-secret fail-open is untouched and this PR does not widen it. With WEBHOOK_SECRET
    unset, api/server.py:199 skips verification entirely, and verify_slack_signature returns True
    before reaching any of this. Measured identical on main and this branch. I left it alone because
    you declined that change on security: verify the Slack signing secret before executing approved actions #24.
  • One residual, reachable only through that fail-open: api/server.py:403 does
    parse_qs(body.decode()), which still 500s on a non-UTF-8 body when no signing secret is
    configured. It lives in server.py, which this PR keeps at zero changed lines. Happy to file or fix
    it separately.
  • No logging on the new reject paths — the module has no logger. A genuine infrastructure fault
    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 on main (67 files); this branch adds 23 further complaints, all in
    tests/test_security.py and all the 88-vs-100 column rule. api/webhook_auth.py adds none. The step
    is continue-on-error, and I did not run black . 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)

@OkiriGabriel

Copy link
Copy Markdown
Collaborator

CI failure summary

CI 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
Error: AttributeError: module 'devops_mcp' has no attribute 'registry'

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.

Run: https://github.com/open-devops-agent/open-devops-agent/actions/runs/30650380413

@OkiriGabriel

Copy link
Copy Markdown
Collaborator

CI fix merged to main in #38 (pinned MCP SDK to <2.0.0 and fixed tests/test_mcp.py). Please rebase onto latest main — your PR checks should go green now.

@OkiriGabriel

Copy link
Copy Markdown
Collaborator

Rebase needed — no code fixes required

The CI failure on this PR was caused by a pre-existing issue on main, not your changes. That is now fixed and merged (#38).

Your PR code looks good. To get CI green, update your branch with latest main:

git fetch origin
git checkout {"headRefName":"fix/webhook-signature-non-ascii"}
git rebase origin/main
git push --force-with-lease

Or 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!

Nitjsefnie and others added 3 commits August 2, 2026 10:32
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]>
@Nitjsefnie
Nitjsefnie force-pushed the fix/webhook-signature-non-ascii branch from 67d3b73 to b017cd4 Compare August 2, 2026 08:42
@Nitjsefnie

Copy link
Copy Markdown
Contributor Author

Rebased onto main with #38 in — new head b017cd4, no conflicts.

Confirmed it was the dependency float rather than anything on our side: at the old head, resolving fresh from that commit's requirements.txt pulled mcp 2.0.0 and reproduced your exact failure (AttributeError: module 'devops_mcp' has no attribute 'registry'); post-rebase it resolves mcp 1.29.0 and the suite is 160 passed, 2 skipped locally on 3.12. Worth knowing for anyone else checking: a warm venv that already has 1.29.0 hides the bug entirely.

@OkiriGabriel
OkiriGabriel merged commit 4be1c66 into open-devops-agent:main Aug 2, 2026
10 checks passed
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.

Signature verification raises TypeError (HTTP 500) on non-ASCII signature headers

2 participants