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>
102 lines
3.6 KiB
JavaScript
102 lines
3.6 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
// Static imports are hoisted above any other top-level statement in an ES
|
|
// module, so http.mjs (and the agentTokens.mjs DATA_DIR const it pulls in
|
|
// transitively) would already be evaluated before a plain `process.env...`
|
|
// assignment placed above it in this file ever ran. A dynamic import
|
|
// after setting the env avoids that trap.
|
|
const DATA_DIR = mkdtempSync(join(tmpdir(), "rithub-mcp-http-test-"));
|
|
process.env.RITHUB_MCP_DATA_DIR = DATA_DIR;
|
|
process.env.RITHUB_MCP_TOKEN_KEY = "33".repeat(32);
|
|
test.after(() => rmSync(DATA_DIR, { recursive: true, force: true }));
|
|
|
|
const { createHttpServer } = await import("../src/http.mjs");
|
|
|
|
function listen(server) {
|
|
return new Promise((resolve) => server.listen(0, "127.0.0.1", () => resolve(server.address().port)));
|
|
}
|
|
|
|
test("healthz responds ok without auth", async () => {
|
|
const server = createHttpServer({ baseUrl: "https://example.invalid" });
|
|
const port = await listen(server);
|
|
try {
|
|
const res = await fetch(`http://127.0.0.1:${port}/healthz`);
|
|
assert.equal(res.status, 200);
|
|
const body = await res.json();
|
|
assert.equal(body.ok, true);
|
|
} finally {
|
|
server.close();
|
|
}
|
|
});
|
|
|
|
test("/mcp without a Bearer token is rejected with 401", async () => {
|
|
const server = createHttpServer({ baseUrl: "https://example.invalid" });
|
|
const port = await listen(server);
|
|
try {
|
|
const res = await fetch(`http://127.0.0.1:${port}/mcp`, { method: "POST", headers: { "content-type": "application/json" }, body: "{}" });
|
|
assert.equal(res.status, 401);
|
|
} finally {
|
|
server.close();
|
|
}
|
|
});
|
|
|
|
test("/mcp with an unknown agent-scoped token is rejected with 401 before any Gitea call", async () => {
|
|
const server = createHttpServer({ baseUrl: "https://example.invalid" });
|
|
const port = await listen(server);
|
|
try {
|
|
const res = await fetch(`http://127.0.0.1:${port}/mcp`, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json", authorization: "Bearer rtat_doesnotexist" },
|
|
body: "{}",
|
|
});
|
|
assert.equal(res.status, 401);
|
|
const body = await res.json();
|
|
assert.match(body.error.message, /invalid, revoked, or expired/);
|
|
} finally {
|
|
server.close();
|
|
}
|
|
});
|
|
|
|
test("GET /agent-tokens with no query serves the settings page without auth", async () => {
|
|
const server = createHttpServer({ baseUrl: "https://example.invalid" });
|
|
const port = await listen(server);
|
|
try {
|
|
const res = await fetch(`http://127.0.0.1:${port}/agent-tokens`);
|
|
assert.equal(res.status, 200);
|
|
const html = await res.text();
|
|
assert.match(html, /New agent token/);
|
|
} finally {
|
|
server.close();
|
|
}
|
|
});
|
|
|
|
test("GET /agent-tokens?owner=&repo= without a Bearer token is rejected with 401", async () => {
|
|
const server = createHttpServer({ baseUrl: "https://example.invalid" });
|
|
const port = await listen(server);
|
|
try {
|
|
const res = await fetch(`http://127.0.0.1:${port}/agent-tokens?owner=a&repo=b`);
|
|
assert.equal(res.status, 401);
|
|
} finally {
|
|
server.close();
|
|
}
|
|
});
|
|
|
|
test("POST /agent-tokens without a Bearer token is rejected with 401", async () => {
|
|
const server = createHttpServer({ baseUrl: "https://example.invalid" });
|
|
const port = await listen(server);
|
|
try {
|
|
const res = await fetch(`http://127.0.0.1:${port}/agent-tokens`, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ owner: "a", repo: "b", scope: "pr" }),
|
|
});
|
|
assert.equal(res.status, 401);
|
|
} finally {
|
|
server.close();
|
|
}
|
|
});
|