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>
This commit is contained in:
@@ -33,6 +33,7 @@ import {
|
||||
} from './reply-language-guard.js';
|
||||
import { buildClaudeReadonlySandboxSettings } from './reviewer-runtime.js';
|
||||
import {
|
||||
createCredentialsGuardHook,
|
||||
createPreCompactHook,
|
||||
createReviewerBashGuardHook,
|
||||
createSanitizeBashHook,
|
||||
@@ -272,8 +273,12 @@ class ClaudeQueryRunner {
|
||||
{
|
||||
matcher: 'Bash',
|
||||
hooks: readonlyReviewerRuntime
|
||||
? [createReviewerBashGuardHook(), createSanitizeBashHook()]
|
||||
: [createSanitizeBashHook()],
|
||||
? [
|
||||
createCredentialsGuardHook(),
|
||||
createReviewerBashGuardHook(),
|
||||
createSanitizeBashHook(),
|
||||
]
|
||||
: [createCredentialsGuardHook(), createSanitizeBashHook()],
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -284,6 +284,43 @@ export function createSanitizeBashHook(): HookCallback {
|
||||
};
|
||||
}
|
||||
|
||||
const CREDENTIALS_FILE_PATTERN = /\.credentials\.json/;
|
||||
const CREDENTIALS_COPY_PATTERN =
|
||||
/(?:^|[\s;&|(`])(?:cp|mv|ln|rsync|scp|install|tee)\s|copyFile|writeFileSync|>\s*\S*\.credentials\.json/;
|
||||
|
||||
/**
|
||||
* A copy of the Claude OAuth credentials carries the refresh token. A Claude
|
||||
* CLI run against that copy (a "harmless" test) refreshes it, which rotates
|
||||
* the token family and revokes the token EJClaw and every room use: all rooms
|
||||
* fail with "401 OAuth access token has been revoked" until a manual re-login
|
||||
* (2026-11-02). Reading the file stays allowed; copying or writing one is not.
|
||||
*/
|
||||
export function isClaudeCredentialsCopyCommand(command: string): boolean {
|
||||
return (
|
||||
CREDENTIALS_FILE_PATTERN.test(command) &&
|
||||
CREDENTIALS_COPY_PATTERN.test(command)
|
||||
);
|
||||
}
|
||||
|
||||
export function createCredentialsGuardHook(): HookCallback {
|
||||
return async (input, _toolUseId, _context) => {
|
||||
const preInput = input as PreToolUseHookInput;
|
||||
const command = (preInput.tool_input as { command?: string })?.command;
|
||||
if (!command || !isClaudeCredentialsCopyCommand(command)) return {};
|
||||
return {
|
||||
hookSpecificOutput: {
|
||||
hookEventName: 'PreToolUse',
|
||||
permissionDecision: 'deny',
|
||||
permissionDecisionReason:
|
||||
'EJClaw blocks copying or writing Claude .credentials.json files: a ' +
|
||||
'Claude CLI using a copy refreshes the shared login and logs out every ' +
|
||||
'room. Test with fake tokens in a fresh dir instead, and never give a ' +
|
||||
'test the real refresh token.',
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export function createReviewerBashGuardHook(): HookCallback {
|
||||
return async (input, _toolUseId, _context) => {
|
||||
const preInput = input as PreToolUseHookInput;
|
||||
|
||||
69
runners/agent-runner/test/credentials-guard.test.ts
Normal file
69
runners/agent-runner/test/credentials-guard.test.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
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({});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user