Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions apps/server/src/mcp/McpSessionRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,65 @@ it.effect("does not keep credentials of other threads alive", () =>
expect(yield* registry.resolve(token)).toBeUndefined();
}),
);

it.effect("only a trusted touch restores an expired token with its original scope", () =>
Effect.gen(function* () {
let timestamp = 1_000;
const registry = yield* makeRegistry(() => timestamp);
const threadId = ThreadId.make("thread-idle");
const issued = yield* registry.issue({
threadId,
providerInstanceId: ProviderInstanceId.make("claude"),
capabilities: new Set(["preview"]),
});
const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
const scope = yield* registry.resolve(token);
expect(scope).toBeDefined();

timestamp += 101;
expect(yield* registry.resolve(token)).toBeUndefined();
const otherThreadId = ThreadId.make("thread-active");
const other = yield* registry.issue({
threadId: otherThreadId,
providerInstanceId: ProviderInstanceId.make("codex"),
capabilities: new Set(["device"]),
});
yield* registry.touch(otherThreadId);
expect(
(yield* registry.resolve(other.config.authorizationHeader.replace(/^Bearer\s+/, "")))?.thread
.threadId,
).toBe(otherThreadId);
expect(yield* registry.resolve("unknown-token")).toBeUndefined();
expect(yield* registry.resolve(token)).toBeUndefined();

yield* registry.touch(threadId);
expect(yield* registry.resolve(token)).toEqual(scope);
timestamp += 101;
expect(yield* registry.resolve(token)).toBeUndefined();
}),
);

it.effect.each(["provider-session", "thread", "all"] as const)(
"never restores an expired token after %s revocation",
(revocation) =>
Effect.gen(function* () {
let timestamp = 1_000;
const registry = yield* makeRegistry(() => timestamp);
const threadId = ThreadId.make("thread-revoked");
const issued = yield* registry.issue({
threadId,
providerInstanceId: ProviderInstanceId.make("codex"),
capabilities: new Set(["preview"]),
});
const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");

timestamp += 101;
yield* {
"provider-session": registry.revokeProviderSession(issued.config.providerSessionId),
thread: registry.revokeThread(threadId),
all: registry.revokeAll,
}[revocation];
yield* registry.touch(threadId);
expect(yield* registry.resolve(token)).toBeUndefined();
}),
);
31 changes: 11 additions & 20 deletions apps/server/src/mcp/McpSessionRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export interface McpSessionRegistryOptions {
}

/**
* The bound matters because `/mcp` is mounted outside the environment auth
* stack and is reachable on whatever host the server binds to, so this token is
* the only thing guarding the `t3-code` toolkits on a remote-reachable server.
* `/mcp` is outside environment auth, so idle tokens stop authenticating.
* Keep expired records until explicit revocation: a trusted provider turn can
* refresh the token its subprocess still holds; MCP requests cannot.
*/
const DEFAULT_LIVENESS_WINDOW_MS = 24 * 60 * 60 * 1_000;

Expand Down Expand Up @@ -95,15 +95,6 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
.digest("SHA-256", new TextEncoder().encode(token))
.pipe(Effect.map(bytesToHex), Effect.orDie);

const pruneDead = (records: ReadonlyMap<string, CredentialRecord>, timestamp: number) => {
const next = new Map(
Array.from(records).filter(
([, record]) => timestamp - record.lastAliveAt <= livenessWindowMs,
),
);
return next.size === records.size ? records : next;
};

const issue: McpSessionRegistryShape["issue"] = Effect.fn("McpSessionRegistry.issue")(
function* (request) {
const issuedAt = yield* currentTimeMillis;
Expand All @@ -129,7 +120,7 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
issuedAt,
};
yield* SynchronizedRef.update(state, ({ records }) => {
const next = new Map(pruneDead(records, issuedAt));
const next = new Map(records);
next.set(tokenHash, { tokenHash, scope, lastAliveAt: issuedAt });
return { records: next };
});
Expand All @@ -154,10 +145,11 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
const tokenHash = yield* hashToken(rawToken);
const timestamp = yield* currentTimeMillis;
return yield* SynchronizedRef.modify(state, ({ records }) => {
const current = pruneDead(records, timestamp);
const record = current.get(tokenHash);
if (!record) return [undefined, { records: current }] as const;
const next = new Map(current);
const record = records.get(tokenHash);
if (!record || timestamp - record.lastAliveAt > livenessWindowMs) {
return [undefined, { records }] as const;
}
const next = new Map(records);
next.set(tokenHash, { ...record, lastAliveAt: timestamp });
return [record.scope, { records: next }] as const;
});
Expand All @@ -168,9 +160,8 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
function* (threadId) {
const timestamp = yield* currentTimeMillis;
yield* SynchronizedRef.update(state, ({ records }) => {
const current = pruneDead(records, timestamp);
const next = new Map(current);
for (const [tokenHash, record] of current) {
const next = new Map(records);
for (const [tokenHash, record] of records) {
if (record.scope.thread.threadId === threadId) {
next.set(tokenHash, { ...record, lastAliveAt: timestamp });
}
Expand Down
Loading