From 1e45a1b1062d3f5fa69ac2ebdb80938f94dfd9a0 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 16:04:35 +0000 Subject: [PATCH] The development trace store's temporary folder is private to its user A working directory without node_modules records traces under the system's temporary directory, in a folder named only after a hash of the working directory and created with default permissions. On a shared temporary directory another user could make that folder or a link first and read or redirect the traces. The folder's name now carries the user's id, it is created with mode 0700, and it is used only when it is a real folder owned by this user with no access for others. Otherwise the host records in a folder made by mkdtemp, and the one startup log line names both. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01VjCSiB3Dk7BRBmjic6wFM5 --- .changeset/server-trace-store-private-temp.md | 7 ++ packages/server/README.md | 2 +- .../server/src/adapters/defaultTraceStore.ts | 36 ++++++-- packages/server/src/localTracing.ts | 5 +- .../src/test-support/traceStoreLocation.ts | 50 ++++++++++- .../server/src/traceStoreLocation.spec.ts | 82 +++++++++++++++++-- 6 files changed, 167 insertions(+), 15 deletions(-) create mode 100644 .changeset/server-trace-store-private-temp.md diff --git a/.changeset/server-trace-store-private-temp.md b/.changeset/server-trace-store-private-temp.md new file mode 100644 index 000000000..9e436278b --- /dev/null +++ b/.changeset/server-trace-store-private-temp.md @@ -0,0 +1,7 @@ +--- +"@shivaedev/server": patch +--- + +### Fixed + +- The development trace store that a working directory without `node_modules` gets in the system's temporary directory is now in a folder only its user can reach. The folder is `shivaedev-server--`, created with owner-only permissions, so its path changes once from the earlier `shivaedev-server-` and then stays the same across restarts. When that name is held by a link, by another user's folder or by a folder open to others, the host records in a newly made private folder and the startup log line names it. Before, another user of a shared temporary directory could make the folder or a link first and read or redirect the traces. diff --git a/packages/server/README.md b/packages/server/README.md index ef6063bbf..061003b6c 100644 --- a/packages/server/README.md +++ b/packages/server/README.md @@ -312,7 +312,7 @@ For existing Railway services, enable manual tracing per service and environment Set `browserErrors: { log: true, includeUserId: true, requireSession: true }` on `appHost` for the recommended signed-in application setting. `log` writes one Error log with the masked message and stack per accepted report, including when OTLP export is off. `includeUserId` resolves the session and adds `user.id` to the HTTP span and error report when signed in. `requireSession` rejects signed-out reports with 401; a session-provider outage returns 503. Session lookups use the host's session and origin policy. With no session policy, requiring a session rejects reports. All three options default to false, preserving reports before sign-in without session lookups or an extra log line. `browserErrorRoute(trustedProxy?, options?)` accepts the same options when composed directly. -With `NODE_ENV=development` and no OTLP endpoint, `appHost` and `otlpTracing` record to `node_modules/.cache/@shivaedev/server/traces.db` in the working directory using Node's SQLite support, so the store stays out of Git through the ignore entry the application already has for `node_modules`. `runDevelopment` sets this mode for its child processes. A working directory without a `node_modules` folder gets none: the store is then `traces.db` in a folder of the system's temporary directory, and a log line at startup names the file. Set `PLATFORM_TRACE_STORE` to choose another file and `PLATFORM_TRACE_INDEXED_ATTRIBUTES=orderId,projectId` to index application attributes. Indexed values use the same redaction as the stored spans. An explicit OTLP endpoint takes precedence; other modes without an endpoint keep export disabled. +With `NODE_ENV=development` and no OTLP endpoint, `appHost` and `otlpTracing` record to `node_modules/.cache/@shivaedev/server/traces.db` in the working directory using Node's SQLite support, so the store stays out of Git through the ignore entry the application already has for `node_modules`. `runDevelopment` sets this mode for its child processes. A working directory without a `node_modules` folder gets none: the store is then `traces.db` in a folder of the system's temporary directory, and a log line at startup names the file. The folder's name carries the user's id and a hash of the working directory, so it stays the same across restarts, and it is created for its owner alone. When that name is taken by a link, by another user's folder or by a folder that others can reach, the host records in a newly made private folder instead and the log line names both. Set `PLATFORM_TRACE_STORE` to choose another file and `PLATFORM_TRACE_INDEXED_ATTRIBUTES=orderId,projectId` to index application attributes. Indexed values use the same redaction as the stored spans. An explicit OTLP endpoint takes precedence; other modes without an endpoint keep export disabled. Records flush every second on a live maintenance clock and when their scope closes. Application clock jumps do not replay flush intervals. The store retains the latest five completed recorder runs, deleting older spans, logs and attribute indexes when a recorder closes. Active recorders remain writable until their scopes close. Logs use the existing `effect.log` diagnostic spans; the `logs` view exposes their parent span ID and trace ID. Open the file with any SQLite viewer, or inspect it directly: diff --git a/packages/server/src/adapters/defaultTraceStore.ts b/packages/server/src/adapters/defaultTraceStore.ts index 58eab1983..fec130698 100644 --- a/packages/server/src/adapters/defaultTraceStore.ts +++ b/packages/server/src/adapters/defaultTraceStore.ts @@ -1,17 +1,43 @@ import { createHash } from "node:crypto"; -import { existsSync } from "node:fs"; +import { existsSync, lstatSync, mkdirSync, mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import process from "node:process"; import { Effect } from "effect"; -export const defaultTraceStore = Effect.sync(() => { +const PREFIX = "shivaedev-server-"; + +function createPrivately(folder: string) { + try { + mkdirSync(folder, { mode: 0o700 }); + } catch (cause) { + if (!(cause instanceof Error && "code" in cause && cause.code === "EEXIST")) { + throw cause; + } + } +} + +function isPrivate(folder: string, user: number | undefined) { + const found = lstatSync(folder); + return found.isDirectory() && (user === undefined || (found.uid === user && (found.mode & 0o077) === 0)); +} + +function temporaryFolder(workingDirectory: string) { + // Other users of a shared temporary directory can make a folder or a link under any name first. Windows has no user ids and gives each user a temporary directory of their own. + const user = process.getuid?.(); + const name = createHash("sha256").update(workingDirectory).digest("hex").slice(0, 12); + const stable = join(tmpdir(), `${PREFIX}${user === undefined ? "" : `${user}-`}${name}`); + createPrivately(stable); + return isPrivate(stable, user) ? { folder: stable, refused: undefined } : { folder: mkdtempSync(join(tmpdir(), PREFIX)), refused: stable }; +} + +export const defaultTraceStore = Effect.try(() => { const workingDirectory = process.cwd(); const installed = join(workingDirectory, "node_modules"); if (existsSync(installed)) { - return { path: join(installed, ".cache", "@shivaedev", "server", "traces.db"), temporary: false, workingDirectory }; + return { path: join(installed, ".cache", "@shivaedev", "server", "traces.db"), refused: undefined, temporary: false, workingDirectory }; } // No node_modules folder is created here: an ignore rule anchored at a repository's root would not cover it, and the store could be committed. - const folder = `shivaedev-server-${createHash("sha256").update(workingDirectory).digest("hex").slice(0, 12)}`; - return { path: join(tmpdir(), folder, "traces.db"), temporary: true, workingDirectory }; + const { folder, refused } = temporaryFolder(workingDirectory); + return { path: join(folder, "traces.db"), refused, temporary: true, workingDirectory }; }); diff --git a/packages/server/src/localTracing.ts b/packages/server/src/localTracing.ts index 8797288a4..59827e38e 100644 --- a/packages/server/src/localTracing.ts +++ b/packages/server/src/localTracing.ts @@ -21,7 +21,10 @@ const storePath = Effect.fnUntraced(function* (chosen: string | undefined) { } const store = yield* defaultTraceStore; if (store.temporary) { - yield* Effect.logInfo(`Development traces are recorded to ${store.path}, because ${store.workingDirectory} has no node_modules folder.`); + const refused = store.refused === undefined ? "" : ` and ${store.refused} is not a folder that only this user can reach`; + yield* Effect.logInfo( + `Development traces are recorded to ${store.path}, because ${store.workingDirectory} has no node_modules folder${refused}.`, + ); } return store.path; }); diff --git a/packages/server/src/test-support/traceStoreLocation.ts b/packages/server/src/test-support/traceStoreLocation.ts index 1fc4cc9a8..dba2122fb 100644 --- a/packages/server/src/test-support/traceStoreLocation.ts +++ b/packages/server/src/test-support/traceStoreLocation.ts @@ -1,12 +1,34 @@ -import { join, relative } from "node:path"; +import { dirname, join, relative } from "node:path"; import process from "node:process"; import { fileURLToPath } from "node:url"; import * as NodeServices from "@effect/platform-node/NodeServices"; -import { Effect, FileSystem, Stream } from "effect"; +import { Effect, FileSystem, Option, Stream } from "effect"; import { ChildProcess, ChildProcessSpawner } from "effect/process"; import { storyKit } from "@shivaedev/test-story/server/storyKit.ts"; const ANNOUNCED = /recorded to (?\S+traces\.db),/u; +const REFUSED = /and (?\S+) is not a folder that only this user can reach/u; +const ANOTHER_USER = 65_534; + +// Only root can hand a folder to another user. +export const runsAsRoot = process.getuid?.() === 0; + +function announcedFolders(output: readonly string[]) { + return output.flatMap((line) => ANNOUNCED.exec(line)?.groups?.path ?? []).map((path) => dirname(path)); +} + +function latestFolder(output: readonly string[]) { + const folder = announcedFolders(output).at(-1); + return folder === undefined ? Effect.die(new Error("The application has announced no trace store yet")) : Effect.succeed(folder); +} + +function described(found: FileSystem.File.Info) { + if (found.type !== "Directory") { + return found.type === "SymbolicLink" ? "a link" : `a ${found.type}`; + } + const owner = Option.contains(found.uid, process.getuid?.()) ? "this user's" : "another user's"; + return `${owner} folder, mode ${(found.mode & 0o777).toString(8)}`; +} export const traceStoreLocation = storyKit({ *create() { @@ -21,6 +43,8 @@ export const traceStoreLocation = storyKit({ name: "development trace store location", stages: ["files"], verbs: ({ output, root }, story) => ({ + announcedStoreFolders: () => + Effect.flatMap(FileSystem.FileSystem, (fs) => Effect.forEach(announcedFolders(output), (folder) => Effect.map(fs.lstat(folder), described))), announcedStores: () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -29,7 +53,29 @@ export const traceStoreLocation = storyKit({ Effect.map(fs.exists(path), (exists) => (exists ? relative(root, path) : `no file at ${path}`)), ); }), + anotherUserTakesStoreFolder: () => + Effect.gen(function* () { + story.tell("the announced store's folder is handed to another user"); + yield* (yield* FileSystem.FileSystem).chown(yield* latestFolder(output), ANOTHER_USER, ANOTHER_USER); + }), applicationFolder: () => Effect.flatMap(FileSystem.FileSystem, (fs) => fs.readDirectory(join(root, "application"))), + folderBehindLink: () => Effect.flatMap(FileSystem.FileSystem, (fs) => fs.readDirectory(join(root, "elsewhere"))), + linkTakesStoreFolderPlace: () => + Effect.gen(function* () { + story.tell("the announced store's folder is removed and a link to another folder takes its name"); + const fs = yield* FileSystem.FileSystem; + const folder = yield* latestFolder(output); + yield* fs.remove(folder, { recursive: true }); + yield* fs.makeDirectory(join(root, "elsewhere")); + yield* fs.symlink(join(root, "elsewhere"), folder); + }), + othersGainStoreFolderAccess: () => + Effect.gen(function* () { + story.tell("the announced store's folder is opened to other users"); + yield* (yield* FileSystem.FileSystem).chmod(yield* latestFolder(output), 0o755); + }), + refusedStoreFolders: () => + Effect.sync(() => output.flatMap((line) => REFUSED.exec(line)?.groups?.folder ?? []).map((folder) => relative(root, folder))), *runsInDevelopment() { story.tell("the application runs in development from its folder, records a span and exits"); const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; diff --git a/packages/server/src/traceStoreLocation.spec.ts b/packages/server/src/traceStoreLocation.spec.ts index 38d1064a5..c17b2523f 100644 --- a/packages/server/src/traceStoreLocation.spec.ts +++ b/packages/server/src/traceStoreLocation.spec.ts @@ -1,8 +1,11 @@ import { expect } from "vitest"; import { outcomes } from "@shivaedev/test-story/server/outcomes.ts"; -import { dependenciesInstalled, traceStoreLocation } from "#test/traceStoreLocation.ts"; +import { dependenciesInstalled, runsAsRoot, traceStoreLocation } from "#test/traceStoreLocation.ts"; -const IN_THE_TEMPORARY_FOLDER = expect.stringMatching(/^temporary\/shivaedev-server-[0-9a-f]{12}\/traces\.db$/u); +const IN_ITS_TEMPORARY_FOLDER = expect.stringMatching(/^temporary\/shivaedev-server-\d+-[0-9a-f]{12}\/traces\.db$/u); +const IN_A_FRESH_FOLDER = expect.stringMatching(/^temporary\/shivaedev-server-[0-9A-Za-z]{6}\/traces\.db$/u); +const ITS_TEMPORARY_FOLDER = expect.stringMatching(/^temporary\/shivaedev-server-\d+-[0-9a-f]{12}$/u); +const PRIVATE = "this user's folder, mode 700"; traceStoreLocation.it( "a development application that chose no trace store records under its node_modules cache folder", @@ -15,15 +18,82 @@ traceStoreLocation.it( ); traceStoreLocation.it( - "without a node_modules folder, the application records in the temporary folder, says where and writes nothing to its own folder", + "without a node_modules folder, the application records in a temporary folder only its user can reach, says where and writes nothing to its own folder", [], - function* ({ announcedStores, applicationFolder, runsInDevelopment, traceStores }) { + function* ({ announcedStoreFolders, announcedStores, applicationFolder, refusedStoreFolders, runsInDevelopment, traceStores }) { yield* runsInDevelopment(); yield* outcomes([ - ["trace stores", () => traceStores(), [IN_THE_TEMPORARY_FOLDER]], - ["announced stores", () => announcedStores(), [IN_THE_TEMPORARY_FOLDER]], + ["trace stores", () => traceStores(), [IN_ITS_TEMPORARY_FOLDER]], + ["announced stores", () => announcedStores(), [IN_ITS_TEMPORARY_FOLDER]], + ["announced store folders", () => announcedStoreFolders(), [PRIVATE]], + ["refused store folders", () => refusedStoreFolders(), []], ["application folder", () => applicationFolder(), []], ]); }, ); + +traceStoreLocation.it( + "a second start from the same folder records to the same temporary store", + [], + function* ({ announcedStores, runsInDevelopment }) { + yield* runsInDevelopment(); + const [first] = yield* announcedStores(); + + yield* runsInDevelopment(); + + expect(yield* announcedStores()).toEqual([first, first]); + }, +); + +traceStoreLocation.it( + "after its temporary folder is opened to other users, the next start records in a fresh private folder and names both folders", + [], + function* ({ announcedStoreFolders, announcedStores, othersGainStoreFolderAccess, refusedStoreFolders, runsInDevelopment }) { + yield* runsInDevelopment(); + yield* othersGainStoreFolderAccess(); + + yield* runsInDevelopment(); + + yield* outcomes([ + ["announced stores", () => announcedStores(), [IN_ITS_TEMPORARY_FOLDER, IN_A_FRESH_FOLDER]], + ["announced store folders", () => announcedStoreFolders(), ["this user's folder, mode 755", PRIVATE]], + ["refused store folders", () => refusedStoreFolders(), [ITS_TEMPORARY_FOLDER]], + ]); + }, +); + +traceStoreLocation.it( + "when a link to another folder holds its temporary folder's name, the next start records in a fresh private folder, names both folders and writes nothing behind the link", + [], + function* ({ announcedStoreFolders, folderBehindLink, linkTakesStoreFolderPlace, refusedStoreFolders, runsInDevelopment, traceStores }) { + yield* runsInDevelopment(); + yield* linkTakesStoreFolderPlace(); + + yield* runsInDevelopment(); + + yield* outcomes([ + ["trace stores", () => traceStores(), [IN_A_FRESH_FOLDER]], + ["announced store folders", () => announcedStoreFolders(), ["a link", PRIVATE]], + ["refused store folders", () => refusedStoreFolders(), [ITS_TEMPORARY_FOLDER]], + ["folder behind the link", () => folderBehindLink(), []], + ]); + }, +); + +traceStoreLocation.it.runIf(runsAsRoot)( + "after its temporary folder is handed to another user, the next start records in a fresh private folder and names both folders", + [], + function* ({ announcedStoreFolders, announcedStores, anotherUserTakesStoreFolder, refusedStoreFolders, runsInDevelopment }) { + yield* runsInDevelopment(); + yield* anotherUserTakesStoreFolder(); + + yield* runsInDevelopment(); + + yield* outcomes([ + ["announced stores", () => announcedStores(), [IN_ITS_TEMPORARY_FOLDER, IN_A_FRESH_FOLDER]], + ["announced store folders", () => announcedStoreFolders(), ["another user's folder, mode 700", PRIVATE]], + ["refused store folders", () => refusedStoreFolders(), [ITS_TEMPORARY_FOLDER]], + ]); + }, +);