Skip to content

fix(security): stop lending the server GitHub credential to any agent token - #964

Merged
lilyshen0722 merged 3 commits into
mainfrom
fix/github-credential-exposure
Aug 17, 2026
Merged

lilyshen0722 merged 3 commits into
mainfrom
fix/github-credential-exposure

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Summary

Three GitHub routes were reachable by any cm_agent_* token — which every agent, installed by any user on the instance, holds — and each of them spent our shared server-side GITHUB_PAT:

route what a caller could do
POST /api/github/token receive process.env.GITHUB_PAT in plaintext, or mint an App installation token for a repo of their choosing
GET /api/github/pulls/:number/diff read the diff of any repo that PAT could reach, including private ones
POST /api/github/pulls/:number/review post a review — including APPROVE — onto any such repo

owner/repo were caller-supplied; Team-Commonly/commonly were only defaults. So the caller chose the target and our credential supplied the authority. The rate limiter on the /pulls routes bounded volume, not authority.

Why nothing happened, and why that matters

The PAT has been 401-dead for weeks. Every call failed upstream, so the broken credential was doing the access control. That is also why this stayed invisible: it was filed as an ops annoyance (AX #9 flagged the credential as unverified) rather than an authorisation gap, because a failing call and a forbidden one look identical from outside.

The practical consequence: rotating the PAT would have re-armed all three at once. This lands first for that reason.

What changed

  • Deleted all three routes and the commonly_pr_diff / commonly_pr_review MCP tools they backed.
  • Pinned the issue routes to Team-Commonly/commonly server-side. They had the same caller-supplied-target shape.
  • Retired the VALID_NAME regex on those inputs — validating a caller's repo name made it well-formed, never authorised, and there is no caller-supplied value left to interpolate.
  • Redirected the agent-facing guidance (heartbeat preset + bundled github skill) to gh instead of recommending the removed tools.

No capability is lost

Every runtime that used these has a shell and its own GitHub identity:

gh is also strictly more capable here: it supports line-level review comments, which commonly_pr_review never did (it took only {event, body}).

The tools existed for ergonomics — docs/audits/ui-smoke-2026-05-23 records agents having to "know the gh CLI is available". That is a real complaint, and it justified building a credential proxy without anyone re-asking whose authority it spent.

Deliberately left open

A shell-less runtime — native Tier-1, or the hosted Code Reviewer persona in ADR-022's v1 cast — still has no GitHub path. That case needs per-user GitHub App auth, because reviewing a user's repository under our identity is wrong even when it works. A shared PAT cannot express it, so no amount of gating on these routes would have delivered it.

Verification

backend/__tests__/unit/routes/github.upstreamErrorRoutes.test.js — 10 passing:

  • each removed route asserted unroutable against the mounted app, with isPatConfigured: true (the strongest configuration for the defect — precisely when /token used to answer with the secret)
  • separately asserted that no GitHub service call is reached for any of them, so a 404 arriving for an unrelated reason cannot satisfy the test
  • issue pinning asserted against a caller-supplied {owner: 'attacker', repo: 'private-repo'}
  • the existing source-level route-contract check still passes over the remaining 5 routes

commonly-mcp — 46 passing. Tool count 29 → 27, plus a by-name assertion that the two tools are absent, because a future tool would restore the count and silently re-satisfy a bare length check.

npx tsc --noEmit clean.

Note: backend/__tests__/unit/routes/github.upstreamErrors.test.js fails to run on this branch — it also fails identically on main (verified by restoring the pristine file and re-running; the stack differs only by the line number of an import). Pre-existing, untouched here.

Follow-ups not in this PR

  1. Do not rotate the PAT until this merges. Rotation is a privilege-escalation event while these routes exist.
  2. The issue routes remain reachable by any agent token — now only against our own repo, but POST /issues/:number/close is still a write any agent can perform. Worth an explicit authorisation decision.
  3. AX audit entry 25 records the rules earned, including "a dead credential is not a closed door, and it hides one."

🤖 Generated with Claude Code

https://claude.ai/code/session_01XeUH4HVDsDHYPsHJthXjB8

lilyshen0722 and others added 2 commits August 16, 2026 16:42
… token

Three routes let any `cm_agent_*` token — held by an agent installed by any
user on the instance — reach GitHub under our shared PAT:

- `POST /api/github/token` returned `process.env.GITHUB_PAT` in plaintext to
  the caller, or minted an App installation token for a caller-chosen repo.
- `GET /api/github/pulls/:number/diff` and `POST /:number/review` took
  `owner`/`repo` from the caller (ours were only defaults) and executed under
  that PAT — so any agent could read diffs from, and post reviews including
  APPROVE onto, any repository the credential reached.

Nothing exploited this because the PAT has been 401-dead for weeks: the broken
credential was doing the access control, and rotating it would have re-armed
all three at once. That is why this lands before any rotation.

Removed all three, plus the `commonly_pr_diff` / `commonly_pr_review` MCP tools
they backed. No runtime loses a capability — every runtime that used them has a
shell and its own GitHub identity (local wrappers use the operator's `gh`
keyring; cluster runtimes get `gh` plus a credential helper at image build), and
`gh` additionally supports the line-level review comments these never did. The
agent-facing preset and bundled skill now say so instead of recommending them.

Issue routes are pinned server-side to Team-Commonly/commonly. They kept the
same shape — caller-supplied `owner`/`repo` — so the target is no longer
caller-chosen. This retires the `VALID_NAME` regex on those inputs: validating a
caller's repo name made it well-formed, never authorised.

Left deliberately unsolved: a shell-LESS runtime (native Tier-1, or a hosted
Code Reviewer persona) still has no GitHub path. That case needs per-user
GitHub App auth, because reviewing a user's repository under our identity is
wrong even when it works.

Tests: the removed routes are asserted unroutable against the mounted app, with
a PAT configured — the strongest configuration for the defect — and separately
asserted never to reach a GitHub service call, so a 404 arriving for some other
reason cannot pass. Issue pinning is asserted against a caller-supplied
attacker-controlled target. AX audit entry 25 records the rules earned.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XeUH4HVDsDHYPsHJthXjB8
…d number

Fourth instance of the same defect, reached through a surface that names no
GitHub route: the task board.

`githubIssueNumber` is caller-supplied on task creation and validated only as a
positive integer (`tasksApi.ts:155`), so it can name ANY issue in the
repository. Two writes trusted it, both under the server's shared PAT and both
reachable by any `cm_agent_*` token:

- completing a task auto-closed that issue, with `prUrl` — also caller-supplied
  — interpolated into the closing comment
- creating a sub-task commented on the parent's issue, with `title` as the body

So an agent could create a task naming someone else's issue, complete it, and
have our server close that issue as us with attacker-chosen text.

Gated on a new `githubIssueOwned` flag, set only where this server itself opened
the issue via `createGithubIssue`. The number stays as display metadata; it is
no longer authority to write.

Legacy tasks predate the flag and so no longer auto-close. That is the correct
direction: a write we cannot prove we own should fail closed.

Same pre-rotation gate as the rest of this PR — inert today only because the PAT
is 401-dead, and re-armed the moment it is rotated.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XeUH4HVDsDHYPsHJthXjB8
@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Added a fourth instance found while sweeping for siblings — worth calling out because it isn't a GitHub route and wouldn't be found by grepping for one.

The task board. githubIssueNumber is caller-supplied on task creation and validated only as a positive integer (tasksApi.ts:155), so it can name any issue in the repo. Two writes trusted it, both under the shared PAT and both reachable by any agent token:

  • completing a task auto-closed that issue, with the caller-supplied prUrl interpolated into the closing comment
  • creating a sub-task commented on the parent's issue, with title as the body

So an agent could create a task naming someone else's issue, complete it, and have the server close it as us with attacker-chosen text.

Gated on a new githubIssueOwned flag, set only where this server itself opened the issue. The number remains display metadata; it is no longer authority to write. Legacy tasks predate the flag and no longer auto-close — a write we can't prove we own should fail closed.

Note the search that found it: grepping route files for process.env.*TOKEN came back clean. The credential use was service-mediated (tasksApi → GitHubAppService → _apiHeaders), which is the shape worth remembering — the direct-env grep is the one that gives false comfort.

CodeQL flagged js/missing-rate-limiting on the complete handler — a
pre-existing gap, surfaced only because this PR touched that line. Create
and claim were limited; complete, updates and patch were not, though all
five write to Mongo behind the same auth.

Fixing the flagged line alone would have left two siblings open for the
next diff to rediscover, so all three get the shared limiter, keyed per
token like the existing two rather than per IP.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XeUH4HVDsDHYPsHJthXjB8
@lilyshen0722
lilyshen0722 merged commit 823944e into main Aug 17, 2026
11 checks passed
@lilyshen0722
lilyshen0722 deleted the fix/github-credential-exposure branch August 17, 2026 09:21
lilyshen0722 added a commit that referenced this pull request Aug 18, 2026
…source

npm @commonlyai/mcp@0.3.0 and main's commonly-mcp@0.3.0 are DIFFERENT CODE.

#964 deleted `commonly_pr_diff`/`commonly_pr_review` from the tool list
(their routes spent the server's shared GitHub PAT on a caller-chosen repo) but
did not bump the version. So the correction reached the repo and never reached
a single seat: every MCP agent installed from npm still advertises two tools
that now 404, and the model plans around a capability that fails at the call.

Verified by unpacking the published tarball rather than reading this tree —
0.3.0 on npm declares both names; main declares neither and carries a comment
saying why. Same version string, two different tool surfaces.

A version number that maps to two different artifacts is the part worth fixing
beyond this release: it defeats the only check anyone can run from outside.

This commit is the bump alone. The code correction is already on main.
Publishing needs npm auth, which is Sam's to run.
lilyshen0722 added a commit that referenced this pull request Aug 18, 2026
…source (#979)

npm @commonlyai/mcp@0.3.0 and main's commonly-mcp@0.3.0 are DIFFERENT CODE.

#964 deleted `commonly_pr_diff`/`commonly_pr_review` from the tool list
(their routes spent the server's shared GitHub PAT on a caller-chosen repo) but
did not bump the version. So the correction reached the repo and never reached
a single seat: every MCP agent installed from npm still advertises two tools
that now 404, and the model plans around a capability that fails at the call.

Verified by unpacking the published tarball rather than reading this tree —
0.3.0 on npm declares both names; main declares neither and carries a comment
saying why. Same version string, two different tool surfaces.

A version number that maps to two different artifacts is the part worth fixing
beyond this release: it defeats the only check anyone can run from outside.

This commit is the bump alone. The code correction is already on main.
Publishing needs npm auth, which is Sam's to run.
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