mirror of
https://github.com/garrytan/gstack.git
synced 2026-09-17 02:15:31 +02:00
feat(browse): tunnel revoke/agents CLI with post-revoke verification
`$B tunnel revoke <name>` was documented in the instruction block, pair-agent/SKILL.md, and REMOTE_BROWSER_ACCESS.md but implemented nowhere: the CLI forwarded it to the daemon as Unknown command 'tunnel', and nothing in the repo called DELETE /token/:clientId or GET /agents. New pre-server short-circuit (#2254 pattern: tokens are memory-only, never boot a daemon to revoke against it). `tunnel revoke <name>` DELETEs the token, prints the deleted count ("(count unknown)" for old daemons that answer {revoked} without tokens_deleted), then RE-READS GET /agents to prove the agent is gone. The still-listed branch is the version-skew net: a new CLI against a still-running old daemon with the first-match revoke bug exits 1 and says to re-run (each old-daemon call deletes the next match) or stop. An alive pid with an unreachable port reports "Could not reach daemon" (exit 1), never a false "no daemon". `tunnel agents` lists sessions plus pending (unexchanged) setup keys, which GET /agents now exposes via listTokens({includeSetup}) — without them the revocation view was blind to a paired-but-never-connected agent. Setup-key tokens never leave the server. DELETE /token/ now decodeURIComponents the clientId (400 on malformed encoding) so CLI-encoded names round-trip. Tests: subprocess CLI coverage (usage paths, no-daemon exit 0 without spawning, live pair/connect/revoke loop, pending-key listing), stub-daemon pins for the skew and unreachable branches, and e2e pins for revoke-all semantics, percent-encoded ids, and the second-DELETE-is-404 regression. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
b9eb108f16
commit
f28bfd0158
@@ -194,6 +194,120 @@ describe('pair-agent flow end-to-end (HTTP only, no ngrok)', () => {
|
||||
expect(Array.isArray(scopes)).toBe(true);
|
||||
});
|
||||
|
||||
// ─── Revocation e2e: revoke-all + the /agents verification surface ────
|
||||
|
||||
test('DELETE /token revokes session AND setup keys; agent leaves /agents; token 401s; re-connect fails', async () => {
|
||||
const pair = async () => {
|
||||
const resp = await fetch(`${daemon.baseUrl}/pair`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${daemon.token}` },
|
||||
body: JSON.stringify({ clientId: 'revoke-e2e' }),
|
||||
});
|
||||
return (await resp.json() as any).setup_key as string;
|
||||
};
|
||||
const key1 = await pair();
|
||||
const connectResp = await fetch(`${daemon.baseUrl}/connect`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ setup_key: key1 }),
|
||||
});
|
||||
const { token: scopedToken } = await connectResp.json() as any;
|
||||
|
||||
// A second, UNSPENT setup key for the same clientId (the re-grant hole).
|
||||
const key2 = await pair();
|
||||
|
||||
const pre = await fetch(`${daemon.baseUrl}/command`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${scopedToken}` },
|
||||
body: JSON.stringify({ command: 'status', args: [] }),
|
||||
});
|
||||
expect(pre.status).not.toBe(401);
|
||||
|
||||
// /agents lists the session AND the pending setup key, never the token.
|
||||
const agentsPre = await (await fetch(`${daemon.baseUrl}/agents`, {
|
||||
headers: { Authorization: `Bearer ${daemon.token}` },
|
||||
})).json() as any;
|
||||
expect(agentsPre.agents.some((a: any) => a.clientId === 'revoke-e2e' && !a.pending)).toBe(true);
|
||||
expect(agentsPre.agents.some((a: any) => a.clientId === 'revoke-e2e' && a.pending)).toBe(true);
|
||||
for (const a of agentsPre.agents) expect(a.token).toBeUndefined();
|
||||
|
||||
// Regression: pre-fix this deleted only the spent setup key and returned
|
||||
// a false 200 while the session survived. Count covers session + spent
|
||||
// key + pending key.
|
||||
const del = await fetch(`${daemon.baseUrl}/token/revoke-e2e`, {
|
||||
method: 'DELETE',
|
||||
headers: { Authorization: `Bearer ${daemon.token}` },
|
||||
});
|
||||
expect(del.status).toBe(200);
|
||||
const delBody = await del.json() as any;
|
||||
expect(delBody.revoked).toBe('revoke-e2e');
|
||||
expect(delBody.tokens_deleted).toBe(3);
|
||||
|
||||
// Assert per-clientId absence, NOT list-empty: this file shares one
|
||||
// daemon and other tests' agents remain listed.
|
||||
const agentsPost = await (await fetch(`${daemon.baseUrl}/agents`, {
|
||||
headers: { Authorization: `Bearer ${daemon.token}` },
|
||||
})).json() as any;
|
||||
expect(agentsPost.agents.some((a: any) => a.clientId === 'revoke-e2e')).toBe(false);
|
||||
|
||||
const post = await fetch(`${daemon.baseUrl}/command`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${scopedToken}` },
|
||||
body: JSON.stringify({ command: 'status', args: [] }),
|
||||
});
|
||||
expect(post.status).toBe(401);
|
||||
|
||||
// The leftover unspent key is dead too (re-grant hole closed).
|
||||
const reconnect = await fetch(`${daemon.baseUrl}/connect`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ setup_key: key2 }),
|
||||
});
|
||||
expect(reconnect.status).toBe(401);
|
||||
});
|
||||
|
||||
test('second DELETE /token for the same clientId returns 404, not a false 200', async () => {
|
||||
// Regression: pre-fix, consecutive DELETEs both returned 200 — the first
|
||||
// consumed the spent setup key, the second the session. Depends on the
|
||||
// previous test having revoked 'revoke-e2e' (bun runs file tests in order).
|
||||
const del = await fetch(`${daemon.baseUrl}/token/revoke-e2e`, {
|
||||
method: 'DELETE',
|
||||
headers: { Authorization: `Bearer ${daemon.token}` },
|
||||
});
|
||||
expect(del.status).toBe(404);
|
||||
});
|
||||
|
||||
test('DELETE /token decodes percent-encoded clientIds', async () => {
|
||||
const pairResp = await fetch(`${daemon.baseUrl}/pair`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${daemon.token}` },
|
||||
body: JSON.stringify({ clientId: 'space agent' }),
|
||||
});
|
||||
const { setup_key } = await pairResp.json() as any;
|
||||
await fetch(`${daemon.baseUrl}/connect`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ setup_key }),
|
||||
});
|
||||
const del = await fetch(`${daemon.baseUrl}/token/${encodeURIComponent('space agent')}`, {
|
||||
method: 'DELETE',
|
||||
headers: { Authorization: `Bearer ${daemon.token}` },
|
||||
});
|
||||
expect(del.status).toBe(200);
|
||||
const agents = await (await fetch(`${daemon.baseUrl}/agents`, {
|
||||
headers: { Authorization: `Bearer ${daemon.token}` },
|
||||
})).json() as any;
|
||||
expect(agents.agents.some((a: any) => a.clientId === 'space agent')).toBe(false);
|
||||
});
|
||||
|
||||
test('DELETE /token with malformed percent-encoding returns 400', async () => {
|
||||
const del = await fetch(`${daemon.baseUrl}/token/%E0%A4%A`, {
|
||||
method: 'DELETE',
|
||||
headers: { Authorization: `Bearer ${daemon.token}` },
|
||||
});
|
||||
expect(del.status).toBe(400);
|
||||
});
|
||||
|
||||
test('POST /command with no auth returns 401', async () => {
|
||||
const resp = await fetch(`${daemon.baseUrl}/command`, {
|
||||
method: 'POST',
|
||||
|
||||
Reference in New Issue
Block a user