Skip to content

Commit e0b1940

Browse files
t3dotggclaude
andauthored
feat(server): request_secret can save a CLI token to a private file (pingdotgg#24809)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 5398be0 commit e0b1940

13 files changed

Lines changed: 264 additions & 13 deletions

File tree

‎apps/mobile/src/features/threads/SecretRequestCard.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
22
SECRET_REQUEST_DEFAULT_PLACEHOLDER,
3-
SECRET_REQUEST_PRIVACY_NOTE,
43
secretRequestAnswerInput,
54
secretRequestDisplay,
65
secretRequestFailureMessage,
6+
secretRequestPrivacyNote,
77
type SecretRequestItem,
88
} from "@t3tools/client-runtime/secret-request";
99
import {
@@ -144,7 +144,7 @@ function PendingSecretRequestForm(props: {
144144
type="monochrome"
145145
/>
146146
<Text className="flex-1 font-sans text-xs text-foreground-muted">
147-
{SECRET_REQUEST_PRIVACY_NOTE}
147+
{secretRequestPrivacyNote(item)}
148148
</Text>
149149
</View>
150150
<Button

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,6 +1969,7 @@ const make = Effect.gen(function* () {
19691969
label: input.label,
19701970
reason: input.reason,
19711971
...(input.placeholder === undefined ? {} : { placeholder: input.placeholder }),
1972+
...(input.delivery === "file" ? { delivery: "file" as const } : {}),
19721973
secretStatus,
19731974
})
19741975
.pipe(
@@ -2047,6 +2048,12 @@ const make = Effect.gen(function* () {
20472048
yield* Effect.annotateCurrentSpan({ "secret_request.status": status });
20482049
yield* Metrics.increment(Metrics.secretRequestsTotal, { status });
20492050
if (status !== "saved") return { status };
2051+
if (input.delivery === "file") {
2052+
const file = yield* secretRequests
2053+
.saveToFile({ threadId, turnItemId })
2054+
.pipe(Effect.mapError((error) => failure("orchestration_error", error.message)));
2055+
return { status, file };
2056+
}
20502057
const secretRef = yield* secretRequests.savedRef({ threadId: threadId, turnItemId });
20512058
if (Option.isNone(secretRef)) {
20522059
return yield* failure(

‎apps/server/src/mcp/toolkits/orchestrator/tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ const DeleteScheduledTaskTool = Tool.make("delete_scheduled_task", {
189189

190190
const RequestSecretTool = Tool.make("request_secret", {
191191
description:
192-
"Ask the user for a secret (a token, API key, signing secret, password) through a private card in this thread, and wait for them to answer. The value is kept by the app and NEVER returned to you or shown in the transcript. When saved, the result carries a secretRef: pass it to a tool that accepts one (e.g. schedule_task's signature.secretRef). It works once. Never ask for secrets in chat, and never invent one.",
192+
"Ask the user for a secret (a token, API key, signing secret, password) through a private card in this thread, and wait for them to answer. The value is NEVER returned to you or shown in the transcript. By default the result carries a one-use secretRef for a tool that accepts one (e.g. schedule_task's signature.secretRef). For a shell CLI token, set delivery to file. Never ask for secrets in chat, and never invent one.",
193193
parameters: OrchestratorMcpRequestSecretInput,
194194
success: OrchestratorMcpRequestSecretResult,
195195
failure: OrchestratorMcpFailure,

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7749,6 +7749,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
77497749
label: command.label,
77507750
reason: command.reason,
77517751
...(command.placeholder === undefined ? {} : { placeholder: command.placeholder }),
7752+
...(command.delivery === undefined ? {} : { delivery: command.delivery }),
77527753
secretStatus: command.secretStatus,
77537754
};
77547755
yield* emit(

‎apps/server/src/secrets/SecretRequests.test.ts‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { assert, it } from "@effect/vitest";
4+
import * as HostProcess from "@t3tools/shared/HostProcess";
45
import {
56
ProjectId,
67
type OrchestrationV2ServerCommand,
8+
SecretRef,
79
ThreadId,
810
TurnItemId,
911
} from "@t3tools/contracts";
1012
import * as Effect from "effect/Effect";
13+
import * as FileSystem from "effect/FileSystem";
1114
import * as Layer from "effect/Layer";
1215
import * as Metric from "effect/Metric";
1316
import * as Tracer from "effect/Tracer";
1417
import * as Option from "effect/Option";
18+
import * as Path from "effect/Path";
1519
import * as PlatformError from "effect/PlatformError";
20+
import * as TestClock from "effect/testing/TestClock";
1621

1722
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
1823
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
@@ -30,6 +35,8 @@ const withService = <A, E>(
3035
}) => Effect.Effect<A, E>,
3136
options: {
3237
readonly threadId?: ThreadId;
38+
readonly delivery?: "file";
39+
readonly directory?: string;
3340
readonly runStatus?: string;
3441
readonly removeFails?: boolean;
3542
readonly closedFirst?: boolean;
@@ -48,6 +55,7 @@ const withService = <A, E>(
4855
Layer.succeed(
4956
ServerSecretStore.ServerSecretStore,
5057
ServerSecretStore.ServerSecretStore.of({
58+
...(options.directory === undefined ? {} : { directory: options.directory }),
5159
get: (name) => Effect.yieldNow.pipe(Effect.as(Option.fromNullishOr(stored.get(name)))),
5260
set: (name, value) => Effect.sync(() => void stored.set(name, value)),
5361
create: (name, value) =>
@@ -98,6 +106,7 @@ const withService = <A, E>(
98106
type: "secret_request",
99107
label: "GitHub token",
100108
reason: "Used as GH_TOKEN.",
109+
...(options.delivery === undefined ? {} : { delivery: options.delivery }),
101110
secretStatus,
102111
},
103112
],
@@ -338,3 +347,113 @@ it.effect("a used value that cannot be deleted is not handed out", () =>
338347
{ removeFails: true },
339348
),
340349
);
350+
351+
const SECRET_FILE_TTL_MS = 24 * 60 * 60 * 1000;
352+
353+
it.effect("a file request moves its value to an owner-only file, once", () =>
354+
Effect.gen(function* () {
355+
const fs = yield* FileSystem.FileSystem;
356+
const path = yield* Path.Path;
357+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "secret-files-test-" });
358+
const file = yield* withService(
359+
({ service, stored, dispatched }) =>
360+
Effect.gen(function* () {
361+
yield* service.answer({
362+
threadId,
363+
turnItemId,
364+
answer: { type: "save", secret: "cf_token'$(id)" },
365+
});
366+
assert.include(dispatched[0] as object, { secretStatus: "saved", delivery: "file" });
367+
const file = yield* service.saveToFile({ threadId, turnItemId });
368+
assert.equal(yield* fs.readFileString(file), "cf_token'$(id)");
369+
assert.deepEqual(valuesOf(stored), []);
370+
if ((yield* HostProcess.Platform) !== "win32") {
371+
assert.equal((yield* fs.stat(file)).mode & 0o777, 0o600);
372+
assert.equal((yield* fs.stat(path.dirname(file))).mode & 0o777, 0o700);
373+
}
374+
// A retried request_secret gets the same file; the value is never a ref again.
375+
assert.equal(yield* service.saveToFile({ threadId, turnItemId }), file);
376+
const ref = SecretRef.make(`secret-ref:${path.basename(file)}`);
377+
const reused = yield* service.consume({ ref, projectId }).pipe(Effect.flip);
378+
assert.equal(reused.reason, "ref_unavailable");
379+
return file;
380+
}),
381+
{ delivery: "file", directory },
382+
);
383+
384+
const { mtime } = yield* fs.stat(file);
385+
yield* TestClock.setTime(Option.getOrThrow(mtime).getTime() + SECRET_FILE_TTL_MS);
386+
yield* withService(() => Effect.void, { directory });
387+
assert.isTrue(yield* fs.exists(file));
388+
yield* TestClock.setTime(Option.getOrThrow(mtime).getTime() + SECRET_FILE_TTL_MS + 1);
389+
yield* withService(() => Effect.void, { directory });
390+
assert.isFalse(yield* fs.exists(file));
391+
}).pipe(Effect.provide(NodeServices.layer)),
392+
);
393+
394+
it.effect("a failed file write keeps the value, so the request can retry", () =>
395+
Effect.gen(function* () {
396+
const fs = yield* FileSystem.FileSystem;
397+
const path = yield* Path.Path;
398+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "secret-files-test-" });
399+
yield* withService(
400+
({ service }) =>
401+
Effect.gen(function* () {
402+
yield* service.answer({
403+
threadId,
404+
turnItemId,
405+
answer: { type: "save", secret: "cf_token" },
406+
});
407+
const ref = Option.getOrThrow(yield* service.savedRef({ threadId, turnItemId }));
408+
const name = ref.slice("secret-ref:".length);
409+
// A non-empty directory where the partial file goes makes the write fail.
410+
const partial = path.join(directory, "files", `${name}.partial`);
411+
yield* fs.makeDirectory(path.join(partial, "block"), { recursive: true });
412+
const failed = yield* service.saveToFile({ threadId, turnItemId }).pipe(Effect.flip);
413+
assert.equal(failed.reason, "store_failed");
414+
yield* fs.remove(partial, { recursive: true });
415+
const file = yield* service.saveToFile({ threadId, turnItemId });
416+
assert.equal(yield* fs.readFileString(file), "cf_token");
417+
}),
418+
{ delivery: "file", directory },
419+
);
420+
}).pipe(Effect.provide(NodeServices.layer)),
421+
);
422+
423+
it.effect("a file is not delivered while its stored copy cannot be removed", () =>
424+
Effect.gen(function* () {
425+
const fs = yield* FileSystem.FileSystem;
426+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "secret-files-test-" });
427+
yield* withService(
428+
({ service }) =>
429+
Effect.gen(function* () {
430+
yield* service.answer({
431+
threadId,
432+
turnItemId,
433+
answer: { type: "save", secret: "cf_token" },
434+
});
435+
const first = yield* service.saveToFile({ threadId, turnItemId }).pipe(Effect.flip);
436+
assert.equal(first.reason, "consume_failed");
437+
// A retry finds the file but still will not hand it out until the removal works.
438+
const retried = yield* service.saveToFile({ threadId, turnItemId }).pipe(Effect.flip);
439+
assert.equal(retried.reason, "consume_failed");
440+
}),
441+
{ delivery: "file", directory, removeFails: true },
442+
);
443+
}).pipe(Effect.provide(NodeServices.layer)),
444+
);
445+
446+
it.effect("a secret the card promised to keep from the agent is never written to a file", () =>
447+
withService(({ service, stored }) =>
448+
Effect.gen(function* () {
449+
yield* service.answer({
450+
threadId,
451+
turnItemId,
452+
answer: { type: "save", secret: "ghp_secret" },
453+
});
454+
const refused = yield* service.saveToFile({ threadId, turnItemId }).pipe(Effect.flip);
455+
assert.equal(refused.reason, "not_a_file_request");
456+
assert.isTrue(valuesOf(stored).some((value) => value.includes("ghp_secret")));
457+
}),
458+
),
459+
);

‎apps/server/src/secrets/SecretRequests.ts‎

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Answers stay in the secret store behind one-use refs; orchestration records only request status.
22
// Consuming tools keep values out of transcripts, projections, clients and model context.
3+
// A `file` request instead moves its value to an owner-only file for the agent's own shell.
34
import {
45
CommandId,
56
SecretRef,
@@ -17,6 +18,7 @@ import * as Effect from "effect/Effect";
1718
import * as FileSystem from "effect/FileSystem";
1819
import * as Layer from "effect/Layer";
1920
import * as Option from "effect/Option";
21+
import * as Path from "effect/Path";
2022
import * as Schedule from "effect/Schedule";
2123
import * as Schema from "effect/Schema";
2224
import * as Semaphore from "effect/Semaphore";
@@ -30,6 +32,7 @@ const storeName = (ref: SecretRef) => `secret-request-${ref.slice(SECRET_REF_PRE
3032
const REF_PATTERN = /^secret-ref:[0-9a-f]{32}$/;
3133
const SECRET_REF_TTL_MS = 24 * 60 * 60 * 1000;
3234
const STORE_NAME_PATTERN = /^(secret-request-[0-9a-f]{32})\.bin$/;
35+
const FILE_NAME_PATTERN = /^[0-9a-f]{32}(\.partial)?$/;
3336

3437
const refFor = (salt: string, threadId: ThreadId, turnItemId: string) =>
3538
SecretRef.make(
@@ -57,13 +60,21 @@ export class SecretRequests extends Context.Service<
5760
readonly ref: SecretRef;
5861
readonly projectId: ProjectId;
5962
}) => Effect.Effect<string, SecretRequestError>;
63+
/** Moves a saved `file` request's value to an owner-only file and returns its path. */
64+
readonly saveToFile: (input: {
65+
readonly threadId: ThreadId;
66+
readonly turnItemId: string;
67+
}) => Effect.Effect<string, SecretRequestError>;
6068
}
6169
>()("t3/secrets/SecretRequests") {}
6270

6371
const make = Effect.gen(function* () {
6472
const store = yield* ServerSecretStore.ServerSecretStore;
6573
const fileSystem = yield* FileSystem.FileSystem;
74+
const path = yield* Path.Path;
6675
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
76+
// Beside the store's own files so the same owner-only directory and cleanup rules apply.
77+
const filesDir = store.directory === undefined ? undefined : path.join(store.directory, "files");
6778

6879
const salt = Buffer.from(
6980
yield* store.getOrCreateRandom("secret-request-salt", 32).pipe(Effect.orDie),
@@ -124,6 +135,7 @@ const make = Effect.gen(function* () {
124135
label: item.label,
125136
reason: item.reason,
126137
...(item.placeholder === undefined ? {} : { placeholder: item.placeholder }),
138+
...(item.delivery === undefined ? {} : { delivery: item.delivery }),
127139
secretStatus,
128140
})
129141
.pipe(
@@ -183,7 +195,8 @@ const make = Effect.gen(function* () {
183195
Effect.withSpan("SecretRequests.consume"),
184196
);
185197

186-
const consumeRef = Effect.fn("SecretRequests.consumeRef")(function* (input: {
198+
/** A live ref's value, left in the store. */
199+
const readRef = Effect.fn("SecretRequests.readRef")(function* (input: {
187200
readonly ref: SecretRef;
188201
readonly projectId: ProjectId;
189202
}) {
@@ -202,12 +215,72 @@ const make = Effect.gen(function* () {
202215
yield* removeLogged(storeName(input.ref));
203216
return yield* new SecretRequestError({ reason: "ref_expired" });
204217
}
218+
return decoded.value.value;
219+
});
220+
221+
const consumeRef = Effect.fn("SecretRequests.consumeRef")(function* (input: {
222+
readonly ref: SecretRef;
223+
readonly projectId: ProjectId;
224+
}) {
225+
const value = yield* readRef(input);
205226
// One use: the value moves into whatever consumed it. If it cannot be
206227
// deleted, it is not handed out, so a ref is never used twice.
207228
if (!(yield* removeLogged(storeName(input.ref)))) {
208229
return yield* new SecretRequestError({ reason: "consume_failed" });
209230
}
210-
return decoded.value.value;
231+
return value;
232+
});
233+
234+
const saveToFile: SecretRequests["Service"]["saveToFile"] = Effect.fn(
235+
"SecretRequests.saveToFile",
236+
)(function* (input) {
237+
const records = yield* threadManagement
238+
.getThreadRecords(input.threadId, ["turnItems"], {
239+
turnItemTypes: ["secret_request"],
240+
messageRoles: [],
241+
})
242+
.pipe(Effect.mapError((cause) => new SecretRequestError({ reason: "load_failed", cause })));
243+
const item = records.turnItems.find((candidate) => candidate.id === input.turnItemId);
244+
// Only a card that told the user the agent's commands can read the value may write it out.
245+
if (
246+
item?.type !== "secret_request" ||
247+
item.secretStatus !== "saved" ||
248+
item.delivery !== "file"
249+
) {
250+
return yield* new SecretRequestError({ reason: "not_a_file_request" });
251+
}
252+
if (filesDir === undefined) return yield* new SecretRequestError({ reason: "store_failed" });
253+
const ref = refFor(salt, input.threadId, item.id);
254+
const file = path.join(filesDir, ref.slice(SECRET_REF_PREFIX.length));
255+
return yield* Effect.gen(function* () {
256+
// One use: the file counts as delivered only once the stored copy is gone. A retry with the
257+
// same clientRequestId gets the file the first call wrote and finishes that removal.
258+
const removeStored = removeLogged(storeName(ref)).pipe(
259+
Effect.flatMap((removed) =>
260+
removed ? Effect.succeed(file) : new SecretRequestError({ reason: "consume_failed" }),
261+
),
262+
);
263+
if (yield* fileSystem.exists(file).pipe(Effect.orElseSucceed(() => false))) {
264+
return yield* removeStored;
265+
}
266+
// The stored value goes only after the file is complete, so a failed write can retry.
267+
const value = yield* readRef({ ref, projectId: records.thread.projectId });
268+
const partial = `${file}.partial`;
269+
yield* fileSystem.makeDirectory(filesDir, { recursive: true, mode: 0o700 }).pipe(
270+
Effect.andThen(fileSystem.chmod(filesDir, 0o700)),
271+
Effect.andThen(fileSystem.remove(partial, { force: true })),
272+
// `wx` never follows or replaces an existing path.
273+
Effect.andThen(
274+
fileSystem.writeFile(partial, new TextEncoder().encode(value), {
275+
flag: "wx",
276+
mode: 0o600,
277+
}),
278+
),
279+
Effect.andThen(fileSystem.rename(partial, file)),
280+
Effect.mapError((cause) => new SecretRequestError({ reason: "store_failed", cause })),
281+
);
282+
return yield* removeStored;
283+
}).pipe(consumeLock.withPermits(1));
211284
});
212285

213286
const sweepExpired = Effect.gen(function* () {
@@ -231,14 +304,32 @@ const make = Effect.gen(function* () {
231304
Effect.catchCause((cause) => Effect.logWarning("Could not sweep expired secret refs", cause)),
232305
Effect.withSpan("SecretRequests.sweepExpired"),
233306
);
234-
yield* sweepExpired;
235-
yield* sweepExpired.pipe(
307+
const sweepExpiredFiles = Effect.gen(function* () {
308+
if (filesDir === undefined || !(yield* fileSystem.exists(filesDir))) return;
309+
const now = yield* Clock.currentTimeMillis;
310+
let removed = 0;
311+
for (const name of yield* fileSystem.readDirectory(filesDir)) {
312+
if (!FILE_NAME_PATTERN.test(name)) continue;
313+
const file = path.join(filesDir, name);
314+
const { mtime } = yield* fileSystem.stat(file);
315+
if (Option.exists(mtime, (time) => now - time.getTime() <= SECRET_REF_TTL_MS)) continue;
316+
yield* fileSystem.remove(file);
317+
removed += 1;
318+
}
319+
yield* Effect.annotateCurrentSpan({ "secret_request.expired_files_removed": removed });
320+
}).pipe(
321+
Effect.catchCause((cause) => Effect.logWarning("Could not sweep expired secret files", cause)),
322+
Effect.withSpan("SecretRequests.sweepExpiredFiles"),
323+
);
324+
const sweep = Effect.andThen(sweepExpired, sweepExpiredFiles);
325+
yield* sweep;
326+
yield* sweep.pipe(
236327
Effect.delay("1 hour"),
237328
Effect.repeat(Schedule.spaced("1 hour")),
238329
Effect.forkScoped,
239330
);
240331

241-
return SecretRequests.of({ answer, savedRef, consume });
332+
return SecretRequests.of({ answer, savedRef, consume, saveToFile });
242333
});
243334

244335
export const layer = Layer.effect(SecretRequests, make);

0 commit comments

Comments
 (0)