Repository navigation
#783: the Git webhook receiver, with signature verification and delivery de-duplication - #791
Merged
Merged
Conversation
…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.
This was referenced Dec 1, 2026
…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
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.
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.
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; enqueuessync_pr_state"), and story #19's duplicate-delivery test (criterion 2).
Verification.
X-Hub-Signature-256must besha256=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_checksjob (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 thehost, and polling stays the fallback. Read events:
pull_requestclosed, reopened, synchronize;workflow_runcompletedof a
pull_requestrun; only the configured repository and a head in it. Every other event is 200ignored.De-duplication.
X-GitHub-Deliverygoes into the new tablewebhook_deliverieswithINSERT ... ON CONFLICT DO NOTHING, first, in the transaction that moves the job. The same ID again, even at the same moment, is 200duplicateand 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: 404webhook-not-configured, and the worker polls as before.The secret. Secret
cmapi-git-webhook(keysecret), mounted into the API alone as an optionalsecretKeyRef, in theplain manifests and the chart.
deploy/secrets.sh --git-webhookwrites it once (cases 13a-13c insecrets-test.sh,mutants AF and AG); the chart's
git.webhook.generatedoes the same withresource-policy: keep. docs/13 "The Gitwebhook" 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's0014_approval_decision, merged as 03cda30 and merged into thisbranch);
REQUIRED_REVISION0015. 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 abody model's);
cmapi/contract/openapi.json, samples, docs/04, the portal's generated types, the portal map counts (51operations, 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, thesignature), 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 updatedry run, which changes at each recording of the samples (measured 0.125, 0.062 and 0.312 for
capacity.cpu); it now readsthem 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
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.
webhook-id(=Idempotency-Key, "consistent across webhook retries"), notX-Gitlab-Event-UUID,which GitLab documents only as shared by recursive webhooks. GitLab is designed (the
RECEIVERSseam), not built.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(onepr_checksjob, onegit_webhookevent perdelivery ID, one
pr_failed),test_a_delivery_for_a_request_past_pr_open_does_not_move_it_back(a synced request, three latedeliveries: no event, no state change), and
test_two_deliveries_of_one_id_at_the_same_moment_move_the_check_once. Run againsta 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'srun_aftermoved).After merging main 03cda30 (#785), one file at a time on the same database:
test_git_webhook_78315,test_data_model_doc1,test_models_match_migrations15,test_review_c_authz13,test_api_standard177,test_problem_type_docs16,test_portal_map6,test_plugins_switch42,test_api_contract7,test_api_docs26,test_api_samples3,test_usage_verdicts_31012,test_migrate_log_6149,test_second_pass_converged17: 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, databasecmapi_t1819), macOS, 2026-12-01:test_git_webhook_78315 passed;test_data_model_doc1,test_models_match_migrations15,test_review_c_authz13,test_api_standard177,test_problem_type_docs16,test_portal_map6,test_plugins_switch42,test_api_contract7,test_api_docs26,test_api_samples3,test_usage_verdicts_31012,test_token_renew_39889,test_migrate_log_6149,test_second_pass_converged17,test_idempotency_release2_60313,test_second_pass29,test_capacity_reservations_62_review9,
test_operations_2621,test_operations_review_77420: all passed.contract/lint.sh: no results;lint-test.py: 37 cases,0 failed.
deploy/tests/mutate-secrets.pyon the stub: base passes, 34 mutants, 0 wrong (AF and AG new). shellcheck ofsecrets.shandsecrets-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
algorithm. The docs/13
gh api .../hooksstep has not been run.