Files
kuentagym/api/test/credential.test.js
T
AlexandClaude Opus 5 49be4e0626 Coach: the Claude provider, and an AI runtime you only carry if you ask
The Agent SDK adapter, its dependency, and the packaging that keeps both out
of the image an owner gets when they never wanted the Coach.

api/Dockerfile builds two targets. The default one runs `npm ci --omit=optional`
and ships no AI runtime at all (~158 MB); `--target coach` drops that flag and
adds @anthropic-ai/claude-agent-sdk (~455 MB). The SDK's runtime ships as a
per-platform package, so on this Alpine base the lockfile resolves the musl
build — the kind of thing that only fails on somebody else's machine, which is
why the new api-image CI job builds *and runs* both targets rather than merely
building them.

Both targets now create the unprivileged `coach` user. That is not a Claude
detail: canDropPrivileges() fails closed, so without it every job is refused —
including the fixture provider that exists so the loop can be walked before an
account is connected. The shipped image could not run one.

The adapter imports the SDK lazily, so an absent runtime stays an ordinary
reportable state rather than a boot crash: check() says so, isConnected() goes
false, and /api/config carries no coach key.

Four SDK options are what make this safe next to ./data — tools: [] (no Read,
no Bash, no Grep), settingSources: [], skills: [], strictMcpConfig. They are
one frozen exported object, asserted by value, so re-enabling one is a red
build rather than a quiet capability grant.

Verified against the SDK's own type definitions rather than from memory. Two
things the fork's draft had wrong: SDKResultError carries no `errors` field, so
`message.errors?.join()` was dead code and the failure reason now comes from
`subtype` and `stop_reason`; and the client-app version string was hardcoded at
1.2.3 two releases after it stopped being true, so it is read from package.json.

The credential connect/disconnect routes land here as promised in 2/6 — they
serve a credential, and until this PR there was no provider that had one.
authMode and the per-profile pair still wait for the side that consumes them.

api 72/72 on macOS and on Linux as uid 65534 with the SDK absent; both image
targets built and run; library in sync at 1324.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 10:46:28 +01:00

152 lines
6.5 KiB
JavaScript

/* Whose account pays for a job, and what happens when the answer is "somebody else's".
*
* The refusal in instance mode is the point of this file. An instance-level credential is one
* person's account; the moment a second profile would spend it, the safe answer is no job at
* all. Warning-and-continuing would put the decision on whoever clicks past the warning, and
* the decision is about somebody's personal subscription.
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { tempData } from './helpers.mjs';
const dir = tempData();
const cfg = await import('../coach/config.js');
function connectInstance(token = 'tok-instance') {
cfg.reset();
cfg.save({
enabled: true, provider: 'claude', authMode: 'instance', boundUid: null,
auth: { type: 'cli-token', account: 'owner@example.test', data: cfg.encrypt({ token }) }
});
}
test('instance mode: the first profile to spend the credential binds it', () => {
connectInstance();
const first = cfg.credentialFor('alice');
assert.equal(first.ok, true);
assert.equal(first.auth.token, 'tok-instance');
assert.equal(cfg.load().boundUid, null, 'resolving alone must not bind — only spending does');
cfg.bindInstanceCredential('alice');
assert.equal(cfg.load().boundUid, 'alice');
});
test('instance mode: a second profile is refused, not warned', () => {
connectInstance();
cfg.bindInstanceCredential('alice');
const second = cfg.credentialFor('bob');
assert.equal(second.ok, false);
assert.equal(second.reason, 'shared-account');
assert.equal(second.message, cfg.SHARED_ACCOUNT_REFUSAL);
assert.equal(second.auth, undefined, 'a refused profile is never handed the token');
});
test('instance mode: the bound profile keeps working', () => {
connectInstance();
cfg.bindInstanceCredential('alice');
assert.equal(cfg.credentialFor('alice').ok, true);
});
test('per-profile mode: each profile answers for itself', () => {
cfg.reset();
cfg.save({ enabled: true, provider: 'claude', authMode: 'profile', auth: null, boundUid: null });
assert.equal(cfg.credentialFor('alice').ok, false, 'nobody is signed in yet');
assert.equal(cfg.credentialFor('alice').reason, 'no-credential');
cfg.saveProfileAuth('alice', { type: 'cli-token', account: 'alice@example.test', data: cfg.encrypt({ token: 'tok-alice' }) });
const a = cfg.credentialFor('alice');
assert.equal(a.ok, true);
assert.equal(a.auth.token, 'tok-alice');
assert.equal(a.account, 'alice@example.test');
// Bob signing in later must not reach Alice's token, and vice versa.
assert.equal(cfg.credentialFor('bob').ok, false);
cfg.saveProfileAuth('bob', { type: 'cli-token', account: 'bob@example.test', data: cfg.encrypt({ token: 'tok-bob' }) });
assert.equal(cfg.credentialFor('bob').auth.token, 'tok-bob');
assert.equal(cfg.credentialFor('alice').auth.token, 'tok-alice');
});
test('per-profile mode never refuses for sharing — that is what it is for', () => {
cfg.reset();
cfg.save({ enabled: true, provider: 'claude', authMode: 'profile', boundUid: 'alice' });
cfg.saveProfileAuth('bob', { type: 'cli-token', data: cfg.encrypt({ token: 'tok-bob' }) });
const b = cfg.credentialFor('bob');
assert.equal(b.ok, true, 'a stale boundUid from a previous instance-mode config must not leak in');
});
test('a profile credential lives in its own file, not in synced state', () => {
cfg.reset();
cfg.saveProfileAuth('alice', { type: 'cli-token', data: cfg.encrypt({ token: 'tok-alice' }) });
const file = cfg.profileAuthFile('alice');
assert.match(file, /coach-auth-alice\.json$/);
assert.ok(!file.includes('state-'), 'must not ride along in the state blob the client syncs');
assert.equal(fs.statSync(file).mode & 0o777, 0o600);
});
test('a profile id cannot escape the data directory', () => {
assert.throws(() => cfg.profileAuthFile('../../etc/passwd'), /bad profile id/);
assert.throws(() => cfg.profileAuthFile('a/b'), /bad profile id/);
assert.throws(() => cfg.profileAuthFile(''), /bad profile id/);
});
test('clearing a profile credential removes it', () => {
cfg.reset();
cfg.saveProfileAuth('alice', { type: 'cli-token', data: cfg.encrypt({ token: 't' }) });
assert.equal(cfg.credentialFor.length >= 1, true);
cfg.clearProfileAuth('alice');
cfg.save({ enabled: true, provider: 'claude', authMode: 'profile' });
assert.equal(cfg.credentialFor('alice').ok, false);
});
test('the fixture needs no account at all', () => {
cfg.reset();
cfg.save({ enabled: true, provider: 'fixture', authMode: 'instance', auth: null, boundUid: 'someone-else' });
const c = cfg.credentialFor('anybody');
assert.equal(c.ok, true, 'the fixture is how an instance is exercised without an AI account');
});
test('accountFor names the account without revealing the token', () => {
connectInstance('secret-token-value');
const a = cfg.accountFor('alice');
assert.equal(a.connected, true);
assert.equal(a.account, 'owner@example.test');
assert.equal(a.mode, 'instance');
assert.equal(JSON.stringify(a).includes('secret-token-value'), false);
});
test('accountFor carries the refusal so the screen can state it', () => {
connectInstance();
cfg.bindInstanceCredential('alice');
const b = cfg.accountFor('bob');
assert.equal(b.connected, false);
assert.equal(b.reason, 'shared-account');
assert.equal(b.message, cfg.SHARED_ACCOUNT_REFUSAL);
});
test('jobEnv only ever carries the credential it was handed', () => {
connectInstance('tok-for-env');
const resolved = cfg.credentialFor('alice');
const env = cfg.jobEnv('/tmp/job', resolved);
assert.equal(env.HOME, '/tmp/job');
assert.equal(env.TMPDIR, '/tmp/job');
// Built from nothing: no RP_ID, no ADMIN_UIDS, no VAPID material, nothing this server holds.
// The one permitted addition is the configured provider's own credential variable — read off
// the row rather than hardcoded, so this keeps testing the contract if that name ever changes.
// connectInstance() files a `cli-token`, which jobEnv injects under the provider's oauthEnv.
const expected = [cfg.providerMeta(cfg.load()).oauthEnv].filter(Boolean);
assert.deepEqual(
Object.keys(env).filter(k => !['PATH', 'HOME', 'TMPDIR'].includes(k)).sort(),
expected,
'only the credential the job was handed may appear, under its provider\'s own variable'
);
if (expected.length) assert.equal(env[expected[0]], 'tok-for-env');
const none = cfg.jobEnv('/tmp/job', { ok: false });
assert.equal(Object.values(none).some(v => String(v).includes('tok-for-env')), false);
});