railyard/apps/rithub-mcp/test/scopeGuard.test.mjs
caelum0x 2a842f24a7 feat(rithub-mcp): agent-scoped tokens + the agent PR loop (wedge #2)
Repo owners can now mint a token limited to ONE repo and a chosen scope
(read / pr / full) instead of handing an agent their full account token --
via one MCP tool call (mint_agent_token) or the plain /agent-tokens
settings page. scopeGuard.mjs enforces the fence on every request before
it reaches the real Rithub API; a pr-scope token can open branches/PRs/
issues/comments but never merge, touch another repo, or mint itself a
broader token (the 5 token-management tools are ownerOnly and refused
outright for an agent-scoped session).

enable_agent_on_repo turns on the second half: labelling any open issue
"agent" makes bin/agent-worker.mjs (a separate pm2 poller) read the issue,
call the Anthropic Messages API for a structured fix, commit it on a new
branch, and open a pull request that closes the issue -- commenting on the
issue when it starts and when it opens the PR. The worker uses the same
scope-guarded client a human-held token would be, so it can't exceed the
fence either.

Verified end to end against production: a throwaway mail.tm user, a real
one-line bug, mint/list/revoke (plus a confirmed-blocked self-escalation
attempt), enable_agent_on_repo, an agent-labelled issue, one real
pollOnce() run against the live Anthropic API that produced a correct
one-line fix, a PR that closed the issue on merge, and the REST
/agent-tokens mint/list routes -- all cleaned up after (repo deleted, user
purged, token deleted, mail.tm inbox deleted). 27/27 unit tests pass.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-19 02:08:50 +03:00

72 lines
3.7 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { checkScope, wrapClient, ScopeError } from "../src/scopeGuard.mjs";
const binding = { repoOwner: "alice", repoName: "demo", scope: "pr" };
test("GET anywhere inside the bound repo is always allowed", () => {
assert.doesNotThrow(() => checkScope(binding, "GET", "/repos/alice/demo/issues"));
assert.doesNotThrow(() => checkScope(binding, "GET", "/repos/alice/demo/git/blobs/abc123"));
});
test("GET /user is allowed regardless of binding (resolving the caller's own login)", () => {
assert.doesNotThrow(() => checkScope(binding, "GET", "/user"));
});
test("any method outside the bound repo is rejected", () => {
assert.throws(() => checkScope(binding, "GET", "/repos/other/repo/issues"), ScopeError);
assert.throws(() => checkScope(binding, "GET", "/user/repos"), ScopeError);
assert.throws(() => checkScope(binding, "POST", "/repos/migrate"), ScopeError);
});
test("read scope rejects every write", () => {
const readBinding = { ...binding, scope: "read" };
assert.throws(() => checkScope(readBinding, "POST", "/repos/alice/demo/issues"), ScopeError);
assert.throws(() => checkScope(readBinding, "POST", "/repos/alice/demo/pulls"), ScopeError);
});
test("pr scope allows opening branches/PRs/issues/comments but not merging", () => {
assert.doesNotThrow(() => checkScope(binding, "POST", "/repos/alice/demo/contents"));
assert.doesNotThrow(() => checkScope(binding, "POST", "/repos/alice/demo/pulls"));
assert.doesNotThrow(() => checkScope(binding, "POST", "/repos/alice/demo/issues"));
assert.doesNotThrow(() => checkScope(binding, "POST", "/repos/alice/demo/issues/3/comments"));
assert.throws(() => checkScope(binding, "POST", "/repos/alice/demo/pulls/3/merge"), ScopeError);
assert.throws(() => checkScope(binding, "DELETE", "/repos/alice/demo/git/refs/heads/main"), ScopeError);
});
test("full scope allows merging but never deletes/transfers/settings-changes the repo itself", () => {
const fullBinding = { ...binding, scope: "full" };
assert.doesNotThrow(() => checkScope(fullBinding, "POST", "/repos/alice/demo/pulls/3/merge"));
assert.doesNotThrow(() => checkScope(fullBinding, "DELETE", "/repos/alice/demo/issues/3/labels"));
assert.throws(() => checkScope(fullBinding, "DELETE", "/repos/alice/demo"), ScopeError);
assert.throws(() => checkScope(fullBinding, "POST", "/repos/alice/demo/transfer"), ScopeError);
assert.throws(() => checkScope(fullBinding, "PATCH", "/repos/alice/demo/settings"), ScopeError);
});
test("wrapClient enforces the same rules as checkScope before forwarding to the underlying client", async () => {
const calls = [];
const fakeApi = {
get: async (path) => { calls.push(["GET", path]); return { ok: true }; },
post: async (path) => { calls.push(["POST", path]); return { ok: true }; },
put: async () => { throw new Error("should not be called"); },
patch: async () => { throw new Error("should not be called"); },
del: async () => { throw new Error("should not be called"); },
raw: async (path) => { calls.push(["RAW", path]); return "diff text"; },
};
const client = wrapClient(fakeApi, binding);
await client.get("/repos/alice/demo/issues");
await client.post("/repos/alice/demo/pulls", { title: "x" });
await client.raw("/repos/alice/demo/pulls/1.diff");
assert.deepEqual(calls, [
["GET", "/repos/alice/demo/issues"],
["POST", "/repos/alice/demo/pulls"],
["RAW", "/repos/alice/demo/pulls/1.diff"],
]);
await assert.rejects(() => client.post("/repos/alice/demo/pulls/1/merge"), ScopeError);
await assert.rejects(() => client.del("/repos/alice/demo"), ScopeError);
await assert.rejects(() => client.get("/repos/other/repo"), ScopeError);
});