Files
EJClaw/runners/agent-runner/test/credentials-guard.test.ts
Codex 3f2e364c73 fix: stop silent Claude 401 stalls and token-family revocations
- Refreshing the OAuth token revokes the old access token, and agents hold it
  in CLAUDE_CODE_OAUTH_TOKEN (Claude Code keeps env tokens on 401). Refresh
  early only while no run is active; keep the 1h hard deadline.
- forceRefreshToken (after a 401) now probes /api/oauth/profile: a revoked
  token keeps its expiresAt, so it was handed back unchanged forever. A
  rejected token is refreshed; if that fails, re-login is flagged at once and
  held until a different token appears.
- Classify "OAuth access token has been revoked" as auth-expired. When the
  login is gone, close the paired task and tell the room to re-login instead
  of looping owner/arbiter retries with suppressed output.
- Only suppress the CLI's own short 401 banner, not an answer quoting it
  (a sketchup-app TASK_DONE report was swallowed this way).
- Runner denies Bash commands that copy or write .credentials.json: a CLI test
  against a copied login refreshed the real token family on 2026-11-02 and
  logged out every room.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 10:45:21 +09:00

70 lines
2.2 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
createCredentialsGuardHook,
isClaudeCredentialsCopyCommand,
} from '../src/runner-hooks.js';
describe('isClaudeCredentialsCopyCommand', () => {
it('blocks the 2026-11-02 test commands that copied the real login', () => {
expect(
isClaudeCredentialsCopyCommand(
'mkdir -p oauthtest/a && cp /home/claude/EJClaw/data/claude/.credentials.json oauthtest/a/.credentials.json',
),
).toBe(true);
expect(
isClaudeCredentialsCopyCommand(
`bun -e 'const c=JSON.parse(fs.readFileSync("data/claude/.credentials.json","utf8")); fs.writeFileSync("b/.credentials.json",JSON.stringify(c))'`,
),
).toBe(true);
expect(
isClaudeCredentialsCopyCommand(
'cat data/claude/.credentials.json > /tmp/x/.credentials.json',
),
).toBe(true);
});
it('allows reading or inspecting the credentials file', () => {
expect(
isClaudeCredentialsCopyCommand(
'ls -la data/sessions/x/services/claude/.claude/.credentials.json',
),
).toBe(false);
expect(
isClaudeCredentialsCopyCommand(
`bun -e 'const a=JSON.parse(require("fs").readFileSync("data/claude/.credentials.json","utf8")); console.log(a.claudeAiOauth.expiresAt)'`,
),
).toBe(false);
expect(isClaudeCredentialsCopyCommand('cp a.txt b.txt')).toBe(false);
});
});
describe('createCredentialsGuardHook', () => {
it('denies a copy and passes other commands through', async () => {
const hook = createCredentialsGuardHook();
const signal = new AbortController().signal;
const denied = await hook(
{
hook_event_name: 'PreToolUse',
tool_name: 'Bash',
tool_input: { command: 'cp data/claude/.credentials.json /tmp/c/' },
} as never,
undefined,
{ signal },
);
expect(denied).toMatchObject({
hookSpecificOutput: { permissionDecision: 'deny' },
});
const allowed = await hook(
{
hook_event_name: 'PreToolUse',
tool_name: 'Bash',
tool_input: { command: 'git status' },
} as never,
undefined,
{ signal },
);
expect(allowed).toEqual({});
});
});