Skip to content

feat(api): the App's tokens, and the repositories its installations reach (part of #213) - #467

Merged
samuelmbabhazi merged 22 commits into
mainfrom
feat/213-github-tokens
Nov 25, 2026
Merged

samuelmbabhazi merged 22 commits into
mainfrom
feat/213-github-tokens

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

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 Secrets.
  • GitHub's answers are untrusted input: each entry is checked before it reaches a caller.

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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 18 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 2ee78ff and 9036f29.

Files ignored due to path filters: 3

  • apps/api/openapi.json is excluded by !apps/api/openapi.json
  • docs/generated/route-role-matrix.md is excluded by !**/generated/**, !docs/generated/**
  • packages/api-client/src/gen/openapi.ts is excluded by !**/gen/**, !packages/api-client/src/gen/**
Files selected for processing: 14

  • apps/api/src/modules/git/application/github-repositories.ts
  • apps/api/src/modules/git/application/ports.ts
  • apps/api/src/modules/git/domain/github-api.spec.ts
  • apps/api/src/modules/git/domain/github-api.ts
  • apps/api/src/modules/git/git.module.ts
  • apps/api/src/modules/git/github-repositories-http.integration.spec.ts
  • apps/api/src/modules/git/infrastructure/github-api.client.spec.ts
  • apps/api/src/modules/git/infrastructure/github-api.client.ts
  • apps/api/src/modules/git/infrastructure/github-http.ts
  • apps/api/src/modules/git/infrastructure/github-manifest.client.ts
  • apps/api/src/modules/git/infrastructure/prisma-github-app.store.ts
  • apps/api/src/modules/git/interface/github-app.controller.ts
  • apps/api/src/modules/git/interface/github-app.dto.ts
  • docs/SPEC.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

An independent review of this PR (high effort) raised six points. All six held up, and 8fa4077 fixes them:

  • acme/.. passed as a repository. Names are now checked as owner and name apart, and . and .. are refused. branchHead encodes each segment, and refuses an invalid name before any request is made. The test covers acme/.., acme/. and a branch named ., and checks that no request leaves.
  • One odd default branch made the whole list fail. Branch names now follow git check-ref-format, so développement, release+2 and fix#12 pass, while .., @{, .lock, a component starting with a dot or a control character do not. An entry that still fails is left out of its page; the page is not refused.
  • One stale installation made the whole list fail. An installation GitHub does not answer for is reported in a new unreachable field, and the others are listed. Only when none answers is the route a 503. Tested with an installation that GitHub refuses.
  • A branch named . is refused by the same rules.
  • Cost and deadline. A listing is reused for a minute while the organization's installations stay the same: an installation added or removed through a webhook gives a fresh listing. Each installation gets its own JWT, so a long listing never outlives one. The test checks that a second call asks GitHub nothing. I did not add an overall deadline: each call already has its own 10 s timeout, and the cache stops polling from multiplying them.
  • truncated could be a false positive. It is now set only when installations remain after the bound, and SPEC and OpenAPI describe it as "stopped at 500 with pages or installations left".

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
@samuelmbabhazi
samuelmbabhazi changed the base branch from feat/213-github-webhooks to main November 25, 2026 11:50
Signed-off-by: samuel mbabhazi <[email protected]>

# Conflicts:
#	apps/api/src/modules/git/application/ports.ts
#	apps/api/src/modules/git/git.module.ts
#	docs/SPEC.md
samuelmbabhazi added a commit that referenced this pull request Nov 25, 2026
…k held still (#469)

`exec-sessions.spec.ts` fails on CI now and then. It just failed #467's
TypeScript job: `queues the agent action for the running replica…`
expected an exact `attachBy`.

## Why

c4c4842 (#456) made `ExecSessions.open` measure the time its database
work takes, on the monotonic clock (`performance.now()`), so that
freshness and deadlines are decided when they are. The test still
expected `attachBy` to be exactly the request time plus 60 s. On a fast
machine the work takes under a millisecond, which a `Date` drops, so the
test passed. On a slower runner it takes a millisecond or more, and the
deadline moves by that much.

## Fix

The `opening an exec session` tests hold `performance` still
(`vi.useFakeTimers({ toFake: ['performance'] })`), so deadlines are
exact on any machine. The test that checks a slow database already moves
the clock itself, and is unchanged. No production code changes.

## How it was tested

- I reproduced CI's failure locally: 5 ms of work inside the fake
database, without this change, fail the test with CI's error (`expected
{ …(11) } to match object { …(8) }`). With this change, it passes.
- The file passes five runs in a row, and `nx typecheck lint` passes for
the API.

Signed-off-by: samuel mbabhazi <[email protected]>
@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi
samuelmbabhazi merged commit e6f4db4 into main Nov 25, 2026
25 of 27 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/213-github-tokens branch November 25, 2026 13:00
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.

1 participant