Repository navigation
feat(api): GitHub webhooks, signed, recorded once, with installations (part of #213) - #466
Merged
Merged
Conversation
…oviders Signed-off-by: samuel mbabhazi <[email protected]>
…rt of #213) Signed-off-by: samuel mbabhazi <[email protected]>
…say session only Signed-off-by: samuel mbabhazi <[email protected]>
… (part of #213) Signed-off-by: samuel mbabhazi <[email protected]>
… whole Signed-off-by: samuel mbabhazi <[email protected]>
Signed-off-by: samuel mbabhazi <[email protected]>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 23 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
…ly when newer Signed-off-by: samuel mbabhazi <[email protected]>
Collaborator
Author
|
An independent review of this PR (high effort, while CodeRabbit is rate limited) raised seven points. All seven held up, and the latest commit fixes them:
Unit tests (20) and both GitHub integration files pass. |
Signed-off-by: samuel mbabhazi <[email protected]>
Signed-off-by: samuel mbabhazi <[email protected]>
samuelmbabhazi
changed the base branch from
feat/213-github-app-manifest
to
main
November 25, 2026 11:12
Signed-off-by: samuel mbabhazi <[email protected]>
Signed-off-by: samuel mbabhazi <[email protected]> # Conflicts: # apps/api/openapi.json # apps/api/prisma/schema.prisma # apps/api/src/modules/git/application/ports.ts # apps/api/src/modules/git/git.module.ts # apps/api/src/platform/prisma/tenant-database-role.ts # deploy/postgres/runtime-role.sql # docs/PLAN.md # docs/SPEC.md
This was referenced Nov 25, 2026
samuelmbabhazi
added a commit
that referenced
this pull request
Nov 25, 2026
…each (part of #213) (#467) Part of #213 (M3.1), as ADR 0031 decides: the organization's App calls GitHub as itself, and lists the repositories it can reach. This is what choosing an application's repository needs, and what a push will use to check the branch head. Stacked on #466; retarget to `main` once #466 is merged. ## What changed - **`GithubApiClient`** (`modules/git/infrastructure`): - **The App's JWT.** RS256, signed with the App's private key; issued a minute back for clock drift, nine minutes long. - **Installation tokens.** Asked with the repositories and permissions a call needs, as ADR 0031 requires (`POST /app/installations/{id}/access_tokens`). - **Repositories.** `GET /installation/repositories`, page by page, up to a limit. - **A branch head.** `GET /repos/{owner}/{repo}/branches/{branch}`, with the branch URL-encoded; an unknown branch is `undefined`. - **Revoking a token.** `DELETE /installation/token`, best effort. - Only `api.github.com`, no redirect, a 10 s timeout and answers bounded to 2 MiB. Any failure is a 503 that does not repeat GitHub's text. - **The answers are checked field by field** (`domain/github-api.ts`). Repository names allow `owner/name` only, branches refuse `..`, `//` and a leading or trailing `/`, and commits must be 40 hex characters. A page with one bad entry is refused whole. - **`GET …/github-app/repositories`** (`organization.read`, API tokens allowed): - it lists the repositories of the installations that are not suspended; - each installation gets a token with `metadata: read` only, revoked once read; - at most 500 are listed, sorted by name, with `truncated` beyond that; - without an App the list is empty, as `GET …/github-app` answers `{app: null}`. - **The store** decrypts the App's private key. If it no longer decrypts, the answer is 409 `github_app_unreadable` ("connect it again"). The store also lists the active installations. - **Shared helpers.** The bounded reader is now shared with the manifest client (`github-http.ts`). ## How it was tested - Domain: - the JWT claims; - installation tokens, valid or not; - repository pages, including a traversal in a name, a branch with `..` or `//`, and an entry missing a field; - branch heads and name validation. - Client: - a real RSA key pair generated in the test checks the JWT's header, claims and RS256 signature; - the token request's URL, headers and scoped body; - pagination and the limit; - an encoded branch, and 404 as an unknown branch; - a 500, a redirect, an invalid entry, an oversized body or a network error gives a 503 without GitHub's text; - revocation never fails the caller. - Integration with PostgreSQL, the runtime role and a fake GitHub: - the list is empty before the App exists; - with two installations, one suspended, only the active one is listed, signed with the stored key, with metadata read only, and its token revoked; - a member can read the list, an outsider gets 404; - no token, JWT or key reaches the answer. - The route matrix in unit and integration tests covers the route. ## Security considerations - Tokens are as narrow as each call: metadata read to list. Repository contents read, for builds and head checks, comes later. Every token is revoked as soon as it has served. - The private key is decrypted only for the JWT, and is never logged or returned. The JWT and tokens are `Secret`s. - GitHub's answers are untrusted input: each entry is checked before it reaches a caller. --------- Signed-off-by: samuel mbabhazi <[email protected]>
samuelmbabhazi
added a commit
that referenced
this pull request
Nov 27, 2026
## What changed A verified GitHub push deploys its commit (#213, ADR 0031). This pull request is stacked on #526: its base is that branch until #526 merges. - **Webhook (`git` module):** - A `push` to a branch is recorded with a job of the new `github-push` pg-boss queue, in one transaction, and answered 204 at once. - A tag, a deleted branch or a malformed field queues nothing (`pushOf`). - The job carries identifiers, the branch, the commit and the pusher's login only. - **Worker (`deployments` module):** `PushWorker` works the queue, started by `main.ts`. It checks each payload again (`pushJob`) and hands it to `PushDeployments`. For each Git application that follows that branch of that repository through that installation (at most 20): 1. GitHub is asked for the branch head. Only while the pushed commit is that head does anything happen, so a replayed or overtaken push deploys nothing. 2. The `shipkeep.yml` there becomes the application's configuration, as the system (`ApplicationCommands.applyRepositoryConfig`). It is audited as `application.updated` with the commit. The application's name cannot change, and domains are claimed as for any configuration. 3. `DeploymentCommands.deployPush` queues a deployment with the trigger `webhook` and no actor, and its release keeps `commit_sha`. `release.created` and `deployment.queued` are the system's, with `commitSha` and `githubPusher`. - In a protected environment (the default, SPEC 7) or during maintenance, nothing deploys: `deployment.push_held`. - When the newest release is already this commit's, nothing deploys twice. - **Failures:** - A configuration that cannot deploy is audited as `deployment.push_refused` with the error's stable code. - While a deployment is under way, or GitHub cannot be reached, the push is tried again a minute later for the applications left, 30 times at most. - A database error fails the job, and pg-boss retries it. - **Refactor:** `claimDomains` and the application audit take an `Actor`, so the system can use them. - **Docs:** SPEC (GitHub App section) and a note in ADR 0031: the delivery is recorded with the job rather than with the deployment, so that the webhook answers within GitHub's ten seconds. ## Why Acceptance of #213: "A verified push webhook enqueues a deploy request job with the commit and branch; a forged or replayed webhook is rejected." Forged deliveries were already refused (#466). This adds the job, the branch head check against replays, and the deployment. ## How it was tested - **`push-deployments.integration.spec.ts`** (PostgreSQL, the real webhook route, a fake GitHub): - a signed push queues exactly one job with the expected payload; GitHub's redelivery and the same body under another identifier queue nothing; a tag and a deleted branch queue nothing; - a protected environment: the configuration follows, `push_held`, no deployment; - an unprotected environment: an overtaken push deploys nothing; the head deploys once with trigger `webhook`, a null actor and the commit; handled again it does not deploy twice; while that deployment runs, the next push is answered "try again", then deploys; - invalid YAML and a renamed application are refused with their codes; GitHub unavailable means "try again". - **Mutation check:** without the branch head check, two of these cases fail. - **Unit tests:** `pushOf` (tags, deletions, zero commit, upper case, bad ids, branch tricks) and `pushJob` (every field of the payload, retries). - `api` lint, typecheck and unit tests pass: 945 tests. The HTTP pipeline test fakes the new queue. - Full integration suite: 455 pass, plus the same environment-only failures as on #526 (container runtime; PostgreSQL 18's 23001). ## Security considerations - Replays deploy nothing: the delivery identifier and body are recorded as before, and only the current branch head deploys. - A push deploys only Git applications of the organization whose installation, repository identifier and branch all match. - Protected environments are never deployed by a push. Audit events carry codes and identifiers, never configuration or GitHub text. - No new external service, and no change to the agent or its action list. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * GitHub push events now trigger deployments when the pushed commit is the current branch head and the application configuration is valid. * Configuration updates from pushes are applied automatically. Pushes to protected environments or during maintenance update configuration without deploying. * Busy deployments and temporary GitHub or processing failures are retried; pushes that cannot be deployed are recorded for review. * **Documentation** * Updated GitHub integration guidance to describe push-triggered deployments, holds, and retries. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: samuel mbabhazi <[email protected]>
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.
Part of #213 (M3.1), as ADR 0031 decides: GitHub's webhooks for an organization's App. Stacked on #465; retarget to
mainonce #465 is merged.What changed
POST /api/v1/webhooks/github/{organizationId}, the URL the App's manifest names.X-Hub-Signature-256is checked in constant time, over those bytes, with the App's decrypted webhook secret, before anything is parsed.github_webhook_rejected. Malformed headers or payloads answer 400.AllowSignedBody(), a new route declaration: anonymous, for a route that a server calls rather than a browser. It spares only that route theOrigincheck of anonymous mutations, because GitHub sends noOrigin. The route checks its signature itself. The guard has a test that the plain anonymous mutation still needs itsOrigin.github_deliveries). EachX-GitHub-Deliveryis recorded in the transaction that applies the delivery, and kept 30 days; older ones are forgotten on the way.github_installations).installationeventscreated,deleted,suspendandunsuspendkeep the account, the repository selection and the suspension, for installations of the organization's own App only.github.installation_added,_removed,_suspendedand_unsuspended.pushdeliveries are recorded and deploy nothing yet. Turning a push into a deployment comes next. Per ADR 0031, it will check that the pushed commit is still the head of the branch.TENANT_TABLESand runtime grants. SPEC 9 describes the route.How it was tested
AllowSignedBody.sha1signature, a ping of another App, and the other organization's URL.Security considerations
AllowSignedBodyis explicit and narrow: it removes the Origin check for that route only, and a route that uses it must check a signature itself.