Skip to content

#783: the Git webhook receiver, with signature verification and delivery de-duplication - #791

Merged
ephico2real2 merged 6 commits into
mainfrom
feat/783-git-webhook
Dec 1, 2026
Merged

ephico2real2 merged 6 commits into
mainfrom
feat/783-git-webhook

Conversation

@ephico2real2

@ephico2real2 ephico2real2 commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

Closes #783
Part of #19

What this does

POST /api/v1/webhooks/git, the Git webhook receiver of docs/02 ("Git webhook receiver ... with signature check; enqueues
sync_pr_state"), and story #19's duplicate-delivery test (criterion 2).

  • Verification. X-Hub-Signature-256 must be sha256= and the HMAC-SHA256 of the raw body with the shared secret,
    compared in constant time on bytes; GitHub's own test vector is a test. Gitea sends the same headers, so one verifier
    serves both providers. A bad or missing signature is 403 webhook-signature-invalid (see "Decisions to confirm").

  • What a delivery does. It moves the request's waiting pr_checks job (sync_pr_state as built) to now
    (JobRepo.hasten). It never adds a job and never changes a request: the worker still reads the PR and its CI from the
    host, and polling stays the fallback. Read events: pull_request closed, reopened, synchronize; workflow_run completed
    of a pull_request run; only the configured repository and a head in it. Every other event is 200 ignored.

  • De-duplication. X-GitHub-Delivery goes into the new table webhook_deliveries with INSERT ... ON CONFLICT DO NOTHING, first, in the transaction that moves the job. The same ID again, even at the same moment, is 200 duplicate
    and moves nothing. Kept 7 days (GitHub redelivers "deliveries that occurred in the past 3 days"), deleted by the new
    daily job webhook_prune.

  • Off without a secret. No CMAPI_GIT_WEBHOOK_SECRET: 404 webhook-not-configured, and the worker polls as before.

  • The secret. Secret cmapi-git-webhook (key secret), mounted into the API alone as an optional secretKeyRef, in the
    plain manifests and the chart. deploy/secrets.sh --git-webhook writes it once (cases 13a-13c in secrets-test.sh,
    mutants AF and AG); the chart's git.webhook.generate does the same with resource-policy: keep. docs/13 "The Git
    webhook" sets the same value on GitHub through stdin, never an argument. It stays off on CRC: GitHub cannot reach
    apps-crc.testing.

  • Migration 0015_webhook_deliveries (revises #779, #780: a rejection takes the checks of an approval and is an approvals row #785's 0014_approval_decision, merged as 03cda30 and merged into this
    branch); REQUIRED_REVISION 0015. docs/16's schema section is written again from the migrated database.

  • Contract. DocumentedRouter.read(raw_body=...) documents a body that a route reads as bytes (its fields checked like a
    body model's); cmapi/contract/openapi.json, samples, docs/04, the portal's generated types, the portal map counts (51
    operations, 30 with no mockup action).

  • Docs. docs/02 (the receiver built, webhook_prune), docs/08, docs/13, docs/14 (who may call it: no token, the
    signature), docs/16 (the table, the event git_webhook, the rule), docs/18 (the variable, the Helm values), GLOSSARY.
    The decisions with their sources: docs/reviews/2026-12-01-issue-783/README.md.

  • Defect Portal test pins capacity percentages of a recorded sample that changes at each recording #792, found by this PR's CI and fixed here: the portal test pinned cpu 13% · memory 10% of the recorded update
    dry run, which changes at each recording of the samples (measured 0.125, 0.062 and 0.312 for capacity.cpu); it now reads
    them from the sample the stand-in serves. Whether the sample scenario should be made deterministic stays open on Portal test pins capacity percentages of a recorded sample that changes at each recording #792.

Decisions to confirm

  1. 403, not 401, for a bad signature. RFC 9110, section 15.5.2: "The server generating a 401 response MUST send a
    WWW-Authenticate header field ... containing at least one challenge applicable to the target resource"; HTTP has no
    challenge for a signed body, and cmapi's 401 handler would send Bearer. Section 15.5.4 fits a refused credential.
    The brief said 401; it is one line to change if the owner rules so.
  2. GitLab's key is webhook-id (= Idempotency-Key, "consistent across webhook retries"), not X-Gitlab-Event-UUID,
    which GitLab documents only as shared by recursive webhooks. GitLab is designed (the RECEIVERS seam), not built.
  3. The signature covers the body, not the headers. A signed body sent again under a new ID runs a waiting check once
    more, a read of the host; nothing else.

Tests

cmapi/tests/service/test_git_webhook_783.py (15 test cases). Story #19 criterion 2:
test_the_same_delivery_twice_makes_one_job_one_event_and_one_state_change (one pr_checks job, one git_webhook event per
delivery ID, one pr_failed), test_a_delivery_for_a_request_past_pr_open_does_not_move_it_back (a synced request, three late
deliveries: no event, no state change), and test_two_deliveries_of_one_id_at_the_same_moment_move_the_check_once. Run against
a receiver with the delivery-ID check removed, the duplicate test fails on the side effect: AssertionError: a duplicate does not move the waiting check (the job's run_after moved).

After merging main 03cda30 (#785), one file at a time on the same database: test_git_webhook_783 15, test_data_model_doc 1,
test_models_match_migrations 15, test_review_c_authz 13, test_api_standard 177, test_problem_type_docs 16,
test_portal_map 6, test_plugins_switch 42, test_api_contract 7, test_api_docs 26, test_api_samples 3,
test_usage_verdicts_310 12, test_migrate_log_614 9, test_second_pass_converged 17: all passed. deploy/tests/doc13-test.py:
0 cases failed, 0 mutants survived.

Earlier, before the renumbering (the migration was 0014 then, on main 390655a), on the suite's PostgreSQL 16 (127.0.0.1:55432, database cmapi_t1819), macOS, 2026-12-01:
test_git_webhook_783 15 passed; test_data_model_doc 1, test_models_match_migrations 15, test_review_c_authz 13,
test_api_standard 177, test_problem_type_docs 16, test_portal_map 6, test_plugins_switch 42, test_api_contract 7,
test_api_docs 26, test_api_samples 3, test_usage_verdicts_310 12, test_token_renew_398 89, test_migrate_log_614 9,
test_second_pass_converged 17, test_idempotency_release2_603 13, test_second_pass 29, test_capacity_reservations_62_review
9, test_operations_26 21, test_operations_review_774 20: all passed. contract/lint.sh: no results; lint-test.py: 37 cases,
0 failed. deploy/tests/mutate-secrets.py on the stub: base passes, 34 mutants, 0 wrong (AF and AG new). shellcheck of
secrets.sh and secrets-test.sh: clean. scripts/doc-citations.py --check: 0 problems.

Who wrote what

All commits and files: Opus 5.5 (high), the implementer. No reviewer has run yet; no second design was made (the delivery
loop's two designs and the adversarial review are the coordinator's next steps).

Not tested

  • On CRC or kind: nothing here ran on a cluster. The service tests ran on the suite's PostgreSQL 16 on this Mac.
  • A real delivery from GitHub or Gitea: the tests post deliveries shaped as GitHub documents them, signed with the documented
    algorithm. The docs/13 gh api .../hooks step has not been run.
  • The full service suite (CI is the gate); only the new and directly affected files ran locally, one at a time.

…ery de-duplication

POST /api/v1/webhooks/git verifies GitHub's (and Gitea's) X-Hub-Signature-256, the HMAC-SHA256 of
the raw body, in constant time, and moves the request's waiting pr_checks job to now. It never adds
a job or changes a request: the worker still reads the host, and polling stays the fallback.

Each X-GitHub-Delivery is recorded once in webhook_deliveries (migration 0014), with INSERT ... ON
CONFLICT DO NOTHING in the transaction that moves the job; the same ID again answers 200
`duplicate` and does nothing. Rows are kept 7 days (GitHub redelivers the past 3), pruned by the
daily webhook_prune. A bad or missing signature is 403 webhook-signature-invalid; with no secret the
receiver is off, 404 webhook-not-configured.

The secret is Secret cmapi-git-webhook, mounted into the API alone and optional, in the plain
manifests and the chart; deploy/secrets.sh --git-webhook writes it once. Story #19 criterion 2's
duplicate-delivery test is in tests/service/test_git_webhook_783.py. Decisions and sources:
docs/reviews/2026-12-01-issue-783/README.md.
…sion

#785 merges ahead of this branch with 0014 revising 0013. Until it is on main this branch's chain has
a gap at 0014, and the service tests cannot migrate.
…orded plan's capacity (#792)

doc13-test refuses Gitea in the runbook. The portal test pinned cpu 13% and memory 10% of the recorded
update dry run, which differs at each recording of the samples (held by shape only); it now reads them
from the sample that the stand-in serves, rounded as plan.ts rounds them.
# Conflicts:
#	cmapi/src/cmapi/core/db/__init__.py
#	docs/14-authorization.md
#	docs/16-data-model.md
#	docs/citations.lock.json
…REVISION is 0015, the contract has 51 operations

test_a_database_at_0013_is_behind_this_code reads REQUIRED_REVISION, as test_usage_verdicts_310 does;
docs/08 counts the operation this PR adds.
…cel, a delivery never hurries a check in error backoff, unstorable or over-nested payload text is ignored or 422 instead of a 500; rulings recorded
@ephico2real2
ephico2real2 merged commit 290e7f2 into main Dec 1, 2026
12 checks passed
ephico2real2 added a commit that referenced this pull request Dec 1, 2026
Conflicts were citation line numbers in docs/14, docs/16, docs/02 and
citations.lock.json. main's versions are taken, #762's doc edits are applied
again, and the citations are relocated and written with
scripts/doc-citations.py (0 problems).

Written by Opus 5.5 (high), the implementer.
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.

Task: the Git webhook receiver and duplicate-delivery de-duplication, so #19's webhook criterion can be proven

1 participant