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
7 changes: 7 additions & 0 deletions .changeset/server-trace-store-private-temp.md
Original file line number Diff line number Diff line change
@@ -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-<user id>-<hash of the working directory>`, created with owner-only permissions, so its path changes once from the earlier `shivaedev-server-<hash>` 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.
2 changes: 1 addition & 1 deletion packages/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
36 changes: 31 additions & 5 deletions packages/server/src/adapters/defaultTraceStore.ts
Original file line number Diff line number Diff line change
@@ -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 };
});
5 changes: 4 additions & 1 deletion packages/server/src/localTracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
50 changes: 48 additions & 2 deletions packages/server/src/test-support/traceStoreLocation.ts
Original file line number Diff line number Diff line change
@@ -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 (?<path>\S+traces\.db),/u;
const REFUSED = /and (?<folder>\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() {
Expand All @@ -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;
Expand All @@ -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;
Expand Down
82 changes: 76 additions & 6 deletions packages/server/src/traceStoreLocation.spec.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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]],
]);
},
);
Loading