Skip to content

Commit 751ca8f

Browse files
committed
fix: harden filesystem safety regressions
1 parent 9856bde commit 751ca8f

22 files changed

Lines changed: 955 additions & 107 deletions

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ dist/
22
node_modules/
33
coverage/
44
*.tsbuildinfo
5+
.clawpatch/
56
.deepsec/
67
.vscode

‎CHANGELOG.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
## Unreleased
44

5+
### Security and Correctness
6+
7+
- Reject Windows drive-letter paths on POSIX secure-file reads, missing path fallbacks under symlinked parents, swapped archive destination roots, broad domain ACL principals, and hardlinked sync store reads.
8+
- Preserve top-level JSON `null` values in JSON stores and reject non-document JSON values such as top-level `undefined` before replacing files.
9+
- Fix timeout error factories, root-relative path resolution at filesystem roots, dot-prefixed child names in root path helpers, durable queue directory modes, and pinned helper stale-worker events.
10+
11+
### Build
12+
13+
- Clean `dist` before package builds and make prepack fail deterministically when dependencies are missing instead of resolving new toolchain versions at publish time.
14+
15+
### Tests
16+
17+
- Added Clawpatch regression coverage for secure reads, path fallback validation, sync stores, archive destination races, Windows ACL classification, JSON writes/stores, durable queue modes, root create no-clobber behavior, and pinned helper worker replacement.
18+
519
## 0.2.4 - 2026-05-14
620

721
### Features

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
},
100100
"scripts": {
101101
"benchmark": "node scripts/benchmark.mjs",
102-
"build": "tsc -p tsconfig.json",
102+
"build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json",
103103
"lint:file-size": "node scripts/check-file-size.mjs",
104104
"lint:fs-boundary": "node scripts/check-fs-boundary-primitives.mjs",
105105
"prepack": "node scripts/prepack-build.mjs",

‎scripts/check-file-size.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import path from "node:path";
33

44
const DEFAULT_MAX_LINES = 500;
55
const LINE_BUDGETS = new Map([
6-
["src/file-store.ts", 580],
6+
["src/file-store.ts", 585],
77
["src/permissions.ts", 566],
8-
["src/pinned-python.ts", 655],
8+
["src/pinned-python.ts", 750],
99
["src/root-impl.ts", 1750],
1010
["src/root-path.ts", 862],
1111
["test/api-coverage.test.ts", 983],

‎scripts/prepack-build.mjs‎

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
import { existsSync } from "node:fs";
2+
import { existsSync, rmSync } from "node:fs";
33
import { spawnSync } from "node:child_process";
44
import { createRequire } from "node:module";
55

@@ -26,25 +26,8 @@ function run(command, args, env = {}) {
2626
let tscBin = resolveTypeScriptCompiler();
2727

2828
if (!tscBin || !existsSync(tscBin)) {
29-
run(
30-
"pnpm",
31-
[
32-
"add",
33-
"--save-dev",
34-
"typescript@^5.8.3",
35-
"@types/node@^22.15.19",
36-
"--ignore-scripts",
37-
"--lockfile=false",
38-
],
39-
{
40-
npm_config_lockfile_only: "false",
41-
PNPM_CONFIG_LOCKFILE_ONLY: "false",
42-
},
43-
);
44-
tscBin = resolveTypeScriptCompiler();
45-
if (!tscBin || !existsSync(tscBin)) {
46-
throw new Error("TypeScript compiler is unavailable after installing dev dependencies");
47-
}
29+
throw new Error("TypeScript compiler is unavailable; run pnpm install before packing");
4830
}
4931

32+
rmSync("dist", { recursive: true, force: true });
5033
run(process.execPath, [tscBin, "-p", "tsconfig.json"]);

‎src/archive-staging.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,23 @@ export async function prepareArchiveDestinationDir(destDir: string): Promise<str
7373
"archive destination is not a directory",
7474
);
7575
}
76-
return await fs.realpath(destDir);
76+
const realPath = await fs.realpath(destDir);
77+
const realStat = await fs.stat(realPath);
78+
const postStat = await fs.lstat(destDir);
79+
if (
80+
realStat.dev !== stat.dev ||
81+
realStat.ino !== stat.ino ||
82+
postStat.isSymbolicLink() ||
83+
!postStat.isDirectory() ||
84+
postStat.dev !== stat.dev ||
85+
postStat.ino !== stat.ino
86+
) {
87+
throw new ArchiveSecurityError(
88+
"destination-symlink-traversal",
89+
"archive destination changed during extraction",
90+
);
91+
}
92+
return realPath;
7793
}
7894

7995
async function assertNoSymlinkTraversal(params: {

‎src/file-store.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from "./file-store-boundary.js";
2121
import { readFileStoreCopySource } from "./file-store-source.js";
2222
import { createJsonStore, type JsonFileStoreOptions, type JsonStore } from "./json-document-store.js";
23+
import { stringifyJsonDocument } from "./json-stringify.js";
2324
import { isPathInside, resolveSafeRelativePath } from "./path.js";
2425
import { root, type OpenResult, type ReadResult, type Root, type RootReadOptions } from "./root.js";
2526
import { DEFAULT_ROOT_MAX_BYTES } from "./root-impl.js";
@@ -352,7 +353,7 @@ export function fileStore(options: FileStoreOptions): FileStore {
352353
exists: async (relativePath) => await (await openRoot()).exists(assertRelativePath(relativePath)),
353354
writeText: async (relativePath, data, writeOptions) => await write(relativePath, data, writeOptions),
354355
writeJson: async (relativePath, data, writeOptions) => {
355-
const json = JSON.stringify(data, null, 2);
356+
const json = stringifyJsonDocument(data, null, 2);
356357
return await write(
357358
relativePath,
358359
writeOptions?.trailingNewline === false ? json : `${json}\n`,
@@ -369,15 +370,15 @@ export function fileStore(options: FileStoreOptions): FileStore {
369370
return await (await openRoot()).readJson<T>(assertRelativePath(relativePath));
370371
} catch (error) {
371372
if (isNotFound(error)) {
372-
return null;
373+
return undefined;
373374
}
374375
throw error;
375376
}
376377
},
377378
readRequired: async () =>
378379
await (await openRoot()).readJson<T>(assertRelativePath(relativePath)),
379380
write: async (value, options) => {
380-
const json = JSON.stringify(value, null, 2);
381+
const json = stringifyJsonDocument(value, null, 2);
381382
await write(
382383
relativePath,
383384
options?.trailingNewline === false ? json : `${json}\n`,
@@ -546,7 +547,7 @@ export function fileStoreSync(options: FileStoreOptions): FileStoreSync {
546547
absolutePath: targetPath,
547548
rootPath: rootDir,
548549
boundaryLabel: "store root",
549-
rejectHardlinks: privateMode,
550+
rejectHardlinks: true,
550551
});
551552
if (!opened.ok) {
552553
return handleSyncStoreReadOpenFailure(opened);
@@ -568,7 +569,7 @@ export function fileStoreSync(options: FileStoreOptions): FileStoreSync {
568569
write,
569570
writeText: (relativePath, data, writeOptions) => write(relativePath, data, writeOptions),
570571
writeJson: (relativePath, data, writeOptions) => {
571-
const json = JSON.stringify(data, null, 2);
572+
const json = stringifyJsonDocument(data, null, 2);
572573
return write(
573574
relativePath,
574575
writeOptions?.trailingNewline === false ? json : `${json}\n`,

‎src/json-document-store.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export type JsonStore<T> = {
2828

2929
export type JsonStoreAdapter<T> = {
3030
filePath: string;
31-
readIfExists(): Promise<T | null>;
31+
readIfExists(): Promise<T | undefined>;
3232
readRequired(): Promise<T>;
3333
write(value: T, options?: { trailingNewline?: boolean }): Promise<void>;
3434
};
@@ -66,11 +66,12 @@ export function createJsonStore<T>(
6666
const locks = lockOptions ? createSidecarLockManager(lockOptions.managerKey) : null;
6767

6868
async function read(): Promise<T | undefined> {
69-
return (await adapter.readIfExists()) ?? undefined;
69+
return await adapter.readIfExists();
7070
}
7171

7272
async function readOr(fallback: T): Promise<T> {
73-
return (await read()) ?? cloneFallback(fallback);
73+
const current = await read();
74+
return current === undefined ? cloneFallback(fallback) : current;
7475
}
7576

7677
async function write(value: T): Promise<void> {
@@ -115,7 +116,8 @@ export function createJsonStore<T>(
115116
}),
116117
updateOr: async (fallback, run) =>
117118
await withOptionalLock(async () => {
118-
const next = await run((await read()) ?? cloneFallback(fallback));
119+
const current = await read();
120+
const next = await run(current === undefined ? cloneFallback(fallback) : current);
119121
await write(next);
120122
return next;
121123
}),

0 commit comments

Comments
 (0)