@devries-cameron20 / t3code Mirror · read-only / commits / 6f05fbf45f
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

fix(server): secret names can no longer leave the secrets directory (#39483)

Julius Marminge committed Nov 19, 2026 at 19:46 UTC 6f05fbf45f4c3d7f3a63975d3c723bd5d11708fa
4 files changed +99 -37
apps/server/src/auth/ServerSecretStore.test.ts
+18
@@ -174,6 +174,24 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("ServerSecretStor
174 }).pipe(Effect.provide(layerServerSecretStore())),
175 );
176
177 + it.effect("refuses a name that would leave the secrets directory", () =>
178 + Effect.gen(function* () {
179 + const secretStore = yield* ServerSecretStore.ServerSecretStore;
180 + const value = new TextEncoder().encode("planted");
181 + for (const name of ["marker-x/../session-signing-key", "marker-x\\..\\session-signing-key"]) {
182 + for (const write of [secretStore.create, secretStore.set]) {
183 + assert.equal((yield* write(name, value).pipe(Effect.flip))._tag, "SecretStoreNameError");
184 + }
185 + assert.equal((yield* secretStore.get(name).pipe(Effect.flip))._tag, "SecretStoreNameError");
186 + assert.equal(
187 + (yield* secretStore.remove(name).pipe(Effect.flip))._tag,
188 + "SecretStoreNameError",
189 + );
190 + }
191 + assert.isTrue(Option.isNone(yield* secretStore.get("session-signing-key")));
192 + }).pipe(Effect.provide(layerServerSecretStore())),
193 + );
194 +
195 it.effect("replaces an empty persisted secret with a fresh random one", () =>
196 Effect.gen(function* () {
197 const secretStore = yield* ServerSecretStore.ServerSecretStore;
apps/server/src/auth/ServerSecretStore.ts
+59 -31
@@ -28,6 +28,17 @@ export class SecretStoreSecureError extends Schema.TaggedError<SecretStoreSecure
28 }
29 }
30
31 +export class SecretStoreNameError extends Schema.TaggedError<SecretStoreNameError>()(
32 + "SecretStoreNameError",
33 + {
34 + resource: Schema.String,
35 + },
36 +) {
37 + override get message(): string {
38 + return `Refused ${this.resource}: a secret name cannot contain a path separator.`;
39 + }
40 +}
41 +
42 export class SecretStoreReadError extends Schema.TaggedError<SecretStoreReadError>()(
43 "SecretStoreReadError",
44 {
@@ -118,6 +129,7 @@ export class SecretStoreEncodeError extends Schema.TaggedError<SecretStoreEncode
129
130 export const SecretStoreError = Schema.Union([
131 SecretStoreSecureError,
132 + SecretStoreNameError,
133 SecretStoreReadError,
134 SecretStoreTemporaryPathError,
135 SecretStorePersistError,
@@ -169,30 +181,43 @@ export const make = Effect.gen(function* () {
181 ),
182 );
183
172 - const resolveSecretPath = (name: string) => path.join(serverConfig.secretsDir, `${name}.bin`);
184 + // Some names carry text from outside the server; one with a separator would leave the directory.
185 + const resolveSecretPath = (name: string): Effect.Effect<string, SecretStoreNameError> =>
186 + /[/\\\0]/.test(name)
187 + ? Effect.fail(new SecretStoreNameError({ resource: `secret ${JSON.stringify(name)}` }))
188 + : Effect.succeed(path.join(serverConfig.secretsDir, `${name}.bin`));
189 const emptySecretRepair = yield* Semaphore.make(1);
190 const isMissingOrNonEmpty = (secret: Option.Option<Uint8Array>) =>
191 Option.isNone(secret) || secret.value.byteLength > 0;
192
193 const get: ServerSecretStore["Service"]["get"] = (name) =>
178 - fileSystem.readFile(resolveSecretPath(name)).pipe(
179 - Effect.map((bytes) => Option.some(Uint8Array.from(bytes))),
180 - Effect.catch((cause) =>
181 - cause.reason._tag === "NotFound"
182 - ? Effect.succeedNone
183 - : Effect.fail(
184 - new SecretStoreReadError({
185 - resource: `secret ${name}`,
186 - cause,
187 - }),
188 - ),
194 + resolveSecretPath(name).pipe(
195 + Effect.flatMap((secretPath) =>
196 + fileSystem.readFile(secretPath).pipe(
197 + Effect.map((bytes) => Option.some(Uint8Array.from(bytes))),
198 + Effect.catch((cause) =>
199 + cause.reason._tag === "NotFound"
200 + ? Effect.succeedNone
201 + : Effect.fail(
202 + new SecretStoreReadError({
203 + resource: `secret ${name}`,
204 + cause,
205 + }),
206 + ),
207 + ),
208 + ),
209 ),
210 Effect.withSpan("ServerSecretStore.get"),
211 );
212
193 - const set: ServerSecretStore["Service"]["set"] = (name, value) => {
194 - const secretPath = resolveSecretPath(name);
195 - return crypto.uuidV4().pipe(
213 + const set: ServerSecretStore["Service"]["set"] = (name, value) =>
214 + resolveSecretPath(name).pipe(
215 + Effect.flatMap((secretPath) => setAt(name, secretPath, value)),
216 + Effect.withSpan("ServerSecretStore.set"),
217 + );
218 +
219 + const setAt = (name: string, secretPath: string, value: Uint8Array) =>
220 + crypto.uuidV4().pipe(
221 Effect.mapError(
222 (cause) =>
223 new SecretStoreTemporaryPathError({
@@ -223,15 +248,15 @@ export const make = Effect.gen(function* () {
248 ),
249 );
250 }),
226 - Effect.withSpan("ServerSecretStore.set"),
251 );
228 - };
252
253 // `link` publishes the fully written temp file under the final name, or fails with
254 // AlreadyExists, so a concurrent reader never observes a partially written secret.
232 - const create: ServerSecretStore["Service"]["create"] = (name, value) => {
233 - const secretPath = resolveSecretPath(name);
234 - return crypto.uuidV4().pipe(
255 + const create: ServerSecretStore["Service"]["create"] = (name, value) =>
256 + resolveSecretPath(name).pipe(Effect.flatMap((secretPath) => createAt(name, secretPath, value)));
257 +
258 + const createAt = (name: string, secretPath: string, value: Uint8Array) =>
259 + crypto.uuidV4().pipe(
260 Effect.mapError(
261 (cause) =>
262 new SecretStoreTemporaryPathError({
@@ -263,7 +288,6 @@ export const make = Effect.gen(function* () {
288 );
289 }),
290 );
266 - };
291
292 const getOrCreateRandom: ServerSecretStore["Service"]["getOrCreateRandom"] = (name, bytes) =>
293 get(name).pipe(
@@ -321,16 +345,20 @@ export const make = Effect.gen(function* () {
345 );
346
347 const remove: ServerSecretStore["Service"]["remove"] = (name) =>
324 - fileSystem.remove(resolveSecretPath(name)).pipe(
325 - Effect.catch((cause) =>
326 - cause.reason._tag === "NotFound"
327 - ? Effect.void
328 - : Effect.fail(
329 - new SecretStoreRemoveError({
330 - resource: `secret ${name}`,
331 - cause,
332 - }),
333 - ),
348 + resolveSecretPath(name).pipe(
349 + Effect.flatMap((secretPath) =>
350 + fileSystem.remove(secretPath).pipe(
351 + Effect.catch((cause) =>
352 + cause.reason._tag === "NotFound"
353 + ? Effect.void
354 + : Effect.fail(
355 + new SecretStoreRemoveError({
356 + resource: `secret ${name}`,
357 + cause,
358 + }),
359 + ),
360 + ),
361 + ),
362 ),
363 Effect.withSpan("ServerSecretStore.remove"),
364 );
apps/server/src/cloud/CloudLink.test.ts
+8 -2
@@ -297,8 +297,9 @@ it.effect("an update the client walks away from still finishes", () => {
297 });
298
299 it.effect("mints a T3 Connect credential under the host's device approval setting", () =>
300 - withService({}, ({ mintCredential, connectCredentialRequests }) =>
300 + withService({}, ({ mintCredential, connectCredentialRequests, stored }) =>
301 Effect.gen(function* () {
302 + const before = new Set(stored.keys());
303 const proof = yield* signRelayJwt({
304 privateKey: cloudMintKeys.privateKey,
305 typ: RELAY_MINT_REQUEST_TYP,
@@ -306,7 +307,8 @@ it.effect("mints a T3 Connect credential under the host's device approval settin
307 iss: "https://relay.test",
308 aud: "t3-env:environment-1",
309 sub: "cloud-user-1",
309 - jti: "mint-jti-1",
310 + // Longer than a file name can be, and not one: the marker is named by a digest.
311 + jti: "../mint-jti-1".padEnd(300, "x"),
312 iat: 0,
313 exp: 60,
314 environmentId: "environment-1",
@@ -320,6 +322,10 @@ it.effect("mints a T3 Connect credential under the host's device approval settin
322 assert.deepEqual(connectCredentialRequests, [
323 { proofKeyThumbprint: "client-jkt", requireApproval: false },
324 ]);
325 + const markers = [...stored.keys()].filter((name) => !before.has(name)).toSorted();
326 + assert.lengthOf(markers, 2);
327 + assert.match(markers[0]!, /^cloud-mint-jti-[0-9a-f]{64}$/);
328 + assert.match(markers[1]!, /^cloud-mint-nonce-[0-9a-f]{64}$/);
329 }),
330 ),
331 );
apps/server/src/cloud/CloudLink.ts
+14 -4
@@ -39,7 +39,9 @@ import {
39 } from "@t3tools/shared/relayJwt";
40 import { isSecureRelayUrl, normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
41 import * as Context from "effect/Context";
42 +import * as Digest from "effect/crypto/Digest";
43 import * as SecureRandom from "effect/crypto/SecureRandom";
44 +import * as Hex from "effect/encoding/Hex";
45 import * as Signature from "effect/crypto/Signature";
46 import * as Jwk from "effect/jose/Jwk";
47 import * as DateTime from "effect/DateTime";
@@ -520,6 +522,7 @@ const make = Effect.gen(function* () {
522 const httpServer = yield* HttpServer.HttpServer;
523 const awarenessRelay = yield* AgentAwarenessRelay.AgentAwarenessRelay;
524 const random = yield* SecureRandom.SecureRandom;
525 + const digest = yield* Digest.Digest;
526 const signature = yield* Signature.Signature;
527 const signingKey = yield* EnvironmentSigningKey;
528 const config = yield* ServerConfig.ServerConfig;
@@ -527,6 +530,13 @@ const make = Effect.gen(function* () {
530 const path = yield* Path.Path;
531 const managedPolicy = yield* ManagedPolicy.ManagedPolicy;
532
533 + // Relay-signed text must not name a file directly; a digest is also a fixed, safe length.
534 + // Untraced: it runs twice per proof inside the health and mint spans.
535 + const replayMarkerName = Effect.fnUntraced(function* (prefix: string, value: string) {
536 + const hash = yield* digest.digest(Digest.sha256, stringToBytes(value)).pipe(Effect.orDie);
537 + return `${prefix}${Hex.encode(hash)}`;
538 + });
539 +
540 const withSecrets = Effect.provideService(ServerSecretStore.ServerSecretStore, secrets);
541 const withRuntimeFiles = <A, E>(
542 effect: Effect.Effect<A, E, ServerConfig.ServerConfig | FileSystem.FileSystem | Path.Path>,
@@ -1522,8 +1532,8 @@ const make = Effect.gen(function* () {
1532 }
1533 const proof = proofOption.value;
1534
1525 - const jtiSecretName = `${CLOUD_HEALTH_JTI_PREFIX}${proof.jti}`;
1526 - const nonceSecretName = `${CLOUD_HEALTH_NONCE_PREFIX}${proof.nonce}`;
1535 + const jtiSecretName = yield* replayMarkerName(CLOUD_HEALTH_JTI_PREFIX, proof.jti);
1536 + const nonceSecretName = yield* replayMarkerName(CLOUD_HEALTH_NONCE_PREFIX, proof.nonce);
1537 const consumedReplayGuards = yield* consumeCloudReplayGuards({
1538 secrets,
1539 names: [jtiSecretName, nonceSecretName],
@@ -1625,8 +1635,8 @@ const make = Effect.gen(function* () {
1635 }
1636 const proof = proofOption.value;
1637
1628 - const jtiSecretName = `${CLOUD_MINT_JTI_PREFIX}${proof.jti}`;
1629 - const nonceSecretName = `${CLOUD_MINT_NONCE_PREFIX}${proof.nonce}`;
1638 + const jtiSecretName = yield* replayMarkerName(CLOUD_MINT_JTI_PREFIX, proof.jti);
1639 + const nonceSecretName = yield* replayMarkerName(CLOUD_MINT_NONCE_PREFIX, proof.nonce);
1640 const consumedReplayGuards = yield* consumeCloudReplayGuards({
1641 secrets,
1642 names: [jtiSecretName, nonceSecretName],