@openclaw/matrix 2026.11.10 → 2026.12.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node_modules/@openclaw/fs-safe/CHANGELOG.md +57 -0
- package/node_modules/@openclaw/fs-safe/README.md +6 -0
- package/node_modules/@openclaw/fs-safe/dist/archive-parser.wasm +0 -0
- package/node_modules/@openclaw/fs-safe/dist/atomic-io.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/atomic-io.js +10 -0
- package/node_modules/@openclaw/fs-safe/dist/bounded-read.d.ts +1 -1
- package/node_modules/@openclaw/fs-safe/dist/bounded-read.js +15 -4
- package/node_modules/@openclaw/fs-safe/dist/directory-guard.js +11 -2
- package/node_modules/@openclaw/fs-safe/dist/drvfs-parent.d.ts +6 -0
- package/node_modules/@openclaw/fs-safe/dist/drvfs-parent.js +38 -0
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-acquisition.d.ts +1 -1
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-acquisition.js +2 -2
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-acquire.js +13 -8
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-io.js +13 -0
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-mutation.d.ts +2 -2
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-mutation.js +34 -24
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-stale-admission.js +7 -2
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync.js +20 -20
- package/node_modules/@openclaw/fs-safe/dist/file-observation.d.ts +13 -2
- package/node_modules/@openclaw/fs-safe/dist/file-observation.js +8 -4
- package/node_modules/@openclaw/fs-safe/dist/file-store-sync-write.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/file-store-sync-write.js +11 -1
- package/node_modules/@openclaw/fs-safe/dist/file-store-write-options.d.ts +2 -0
- package/node_modules/@openclaw/fs-safe/dist/file-store-write-options.js +17 -0
- package/node_modules/@openclaw/fs-safe/dist/file-store.d.ts +2 -1
- package/node_modules/@openclaw/fs-safe/dist/file-store.js +12 -16
- package/node_modules/@openclaw/fs-safe/dist/guest-native-python.js +1 -1
- package/node_modules/@openclaw/fs-safe/dist/guest.js +3 -4
- package/node_modules/@openclaw/fs-safe/dist/json-document-store.d.ts +2 -0
- package/node_modules/@openclaw/fs-safe/dist/json-document-store.js +1 -0
- package/node_modules/@openclaw/fs-safe/dist/json-store.js +1 -0
- package/node_modules/@openclaw/fs-safe/dist/json.d.ts +5 -2
- package/node_modules/@openclaw/fs-safe/dist/json.js +6 -2
- package/node_modules/@openclaw/fs-safe/dist/local-file-descriptor.js +16 -10
- package/node_modules/@openclaw/fs-safe/dist/native-binding.d.ts +6 -0
- package/node_modules/@openclaw/fs-safe/dist/native-pinned-write.js +4 -0
- package/node_modules/@openclaw/fs-safe/dist/native-staged-file-metadata.d.ts +20 -0
- package/node_modules/@openclaw/fs-safe/dist/native-staged-file-metadata.js +18 -0
- package/node_modules/@openclaw/fs-safe/dist/native-staged-file.d.ts +3 -18
- package/node_modules/@openclaw/fs-safe/dist/native-staged-file.js +48 -24
- package/node_modules/@openclaw/fs-safe/dist/opened-file-failure.js +19 -7
- package/node_modules/@openclaw/fs-safe/dist/output.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/output.js +3 -0
- package/node_modules/@openclaw/fs-safe/dist/path-policy.js +4 -1
- package/node_modules/@openclaw/fs-safe/dist/permissions-windows.js +35 -10
- package/node_modules/@openclaw/fs-safe/dist/pinned-open.d.ts +9 -2
- package/node_modules/@openclaw/fs-safe/dist/pinned-open.js +42 -23
- package/node_modules/@openclaw/fs-safe/dist/pinned-write-staged.js +85 -34
- package/node_modules/@openclaw/fs-safe/dist/pinned-write-types.d.ts +3 -0
- package/node_modules/@openclaw/fs-safe/dist/pinned-write.js +4 -0
- package/node_modules/@openclaw/fs-safe/dist/prepare-directory-mode.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/prepare-directory-mode.js +1 -1
- package/node_modules/@openclaw/fs-safe/dist/private-temp-workspace.js +2 -2
- package/node_modules/@openclaw/fs-safe/dist/publication-recovery.d.ts +24 -0
- package/node_modules/@openclaw/fs-safe/dist/publication-recovery.js +151 -0
- package/node_modules/@openclaw/fs-safe/dist/publish-copy-stage.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/publish-copy-stage.js +2 -1
- package/node_modules/@openclaw/fs-safe/dist/realpath.d.ts +1 -1
- package/node_modules/@openclaw/fs-safe/dist/realpath.js +22 -11
- package/node_modules/@openclaw/fs-safe/dist/replace-file-descriptor.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/replace-file-descriptor.js +5 -0
- package/node_modules/@openclaw/fs-safe/dist/replace-file-temp-owner.d.ts +7 -0
- package/node_modules/@openclaw/fs-safe/dist/replace-file-temp-owner.js +75 -2
- package/node_modules/@openclaw/fs-safe/dist/replace-file-types.d.ts +2 -0
- package/node_modules/@openclaw/fs-safe/dist/replace-file.js +14 -10
- package/node_modules/@openclaw/fs-safe/dist/root-context.js +28 -12
- package/node_modules/@openclaw/fs-safe/dist/root-copy.js +1 -0
- package/node_modules/@openclaw/fs-safe/dist/root-create-input.js +2 -2
- package/node_modules/@openclaw/fs-safe/dist/root-file-final-admission.d.ts +4 -0
- package/node_modules/@openclaw/fs-safe/dist/root-file-final-admission.js +53 -36
- package/node_modules/@openclaw/fs-safe/dist/root-file-open-batch-sync.js +3 -1
- package/node_modules/@openclaw/fs-safe/dist/root-file.d.ts +2 -1
- package/node_modules/@openclaw/fs-safe/dist/root-file.js +24 -13
- package/node_modules/@openclaw/fs-safe/dist/root-impl.js +17 -1
- package/node_modules/@openclaw/fs-safe/dist/root-options.d.ts +4 -2
- package/node_modules/@openclaw/fs-safe/dist/root-path.d.ts +1 -1
- package/node_modules/@openclaw/fs-safe/dist/root-path.js +2 -1
- package/node_modules/@openclaw/fs-safe/dist/root-read-admission.js +31 -9
- package/node_modules/@openclaw/fs-safe/dist/root-write-admission.js +6 -2
- package/node_modules/@openclaw/fs-safe/dist/secret-file.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/secret-file.js +2 -0
- package/node_modules/@openclaw/fs-safe/dist/sibling-staged-file.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/sibling-staged-file.js +84 -4
- package/node_modules/@openclaw/fs-safe/dist/sibling-temp.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/sibling-temp.js +2 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-acquire.js +22 -15
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-publication.d.ts +22 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-publication.js +303 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-reclaim.d.ts +2 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-reclaim.js +9 -1
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-root.d.ts +8 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-root.js +37 -3
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-staging.d.ts +14 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-staging.js +155 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-stale-admission.js +7 -2
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-write.d.ts +10 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-write.js +143 -0
- package/node_modules/@openclaw/fs-safe/dist/temp-target.js +28 -7
- package/node_modules/@openclaw/fs-safe/dist/temp-workspace-admission.d.ts +2 -2
- package/node_modules/@openclaw/fs-safe/dist/temp-workspace-admission.js +34 -18
- package/node_modules/@openclaw/fs-safe/dist/temp-workspace-child-admission.d.ts +1 -1
- package/node_modules/@openclaw/fs-safe/dist/temp-workspace-child-admission.js +13 -3
- package/node_modules/@openclaw/fs-safe/dist/temp-workspace-ownership.js +1 -1
- package/node_modules/@openclaw/fs-safe/dist/temp-workspace-types.d.ts +9 -0
- package/node_modules/@openclaw/fs-safe/dist/test-hooks.d.ts +6 -0
- package/node_modules/@openclaw/fs-safe/dist/text-atomic.d.ts +1 -1
- package/node_modules/@openclaw/fs-safe/dist/text-atomic.js +1 -0
- package/node_modules/@openclaw/fs-safe/dist/windows-owner.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/windows-owner.js +2 -1
- package/node_modules/@openclaw/fs-safe/dist/windows-security-helper.exe +0 -0
- package/node_modules/@openclaw/fs-safe/docs/advanced.md +6 -1
- package/node_modules/@openclaw/fs-safe/docs/atomic.md +64 -0
- package/node_modules/@openclaw/fs-safe/docs/contributing.md +69 -0
- package/node_modules/@openclaw/fs-safe/docs/file-store.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/json-store.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/json.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/output.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/permissions.md +20 -7
- package/node_modules/@openclaw/fs-safe/docs/reading.md +24 -9
- package/node_modules/@openclaw/fs-safe/docs/root.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/security-model.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/sidecar-lock.md +12 -7
- package/node_modules/@openclaw/fs-safe/docs/staged-file.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/temp-isolation-reference.md +73 -0
- package/node_modules/@openclaw/fs-safe/docs/temp.md +65 -0
- package/node_modules/@openclaw/fs-safe/docs/testing.md +1 -0
- package/node_modules/@openclaw/fs-safe/docs/writing.md +2 -0
- package/node_modules/@openclaw/fs-safe/package.json +25 -20
- package/node_modules/@openclaw/fs-safe-darwin-arm64/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-darwin-arm64/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-darwin-x64/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-darwin-x64/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-freebsd-arm64/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-freebsd-arm64/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-freebsd-x64/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-freebsd-x64/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-linux-arm64-musl/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-linux-arm64-musl/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-linux-x64-gnu/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-linux-x64-gnu/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-linux-x64-musl/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-linux-x64-musl/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-win32-arm64-msvc/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-win32-arm64-msvc/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-win32-x64-msvc/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-win32-x64-msvc/package.json +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import promises from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { assertSyncDirectoryGuard } from "./directory-guard.js";
|
|
5
|
+
import { createSuppressedError } from "./suppressed-error.js";
|
|
6
|
+
import { FsSafeError } from "./errors.js";
|
|
7
|
+
import { annotateReadOnlyOpenError } from "./file-open-error.js";
|
|
8
|
+
import { recordFileObservationFailure } from "./file-observation.js";
|
|
9
|
+
import { captureNativeFdClose } from "./native-binding.js";
|
|
10
|
+
import { nativeOpenFlags } from "./native-operations.js";
|
|
11
|
+
import { writePinnedInput } from "./pinned-write-input.js";
|
|
12
|
+
import { cleanupPinnedFilePath } from "./file-cleanup.js";
|
|
13
|
+
import { createSidecarFile } from "./sidecar-lock-publication.js";
|
|
14
|
+
import { isTransientLockFileDenial } from "./sidecar-lock-policy.js";
|
|
15
|
+
export class SidecarFallbackWriteError extends Error {
|
|
16
|
+
failure;
|
|
17
|
+
constructor(failure) {
|
|
18
|
+
super("sidecar fallback failed");
|
|
19
|
+
this.failure = failure;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export async function writeSidecarAtParent(params, parent, native) {
|
|
23
|
+
try {
|
|
24
|
+
return await writeAtParent(params, parent, native);
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
if (!native)
|
|
28
|
+
throw new SidecarFallbackWriteError(error);
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** The admitted Root parent stays owned by the caller until verification ends. */
|
|
33
|
+
async function writeAtParent(params, parent, native) {
|
|
34
|
+
if (params.input.kind !== "buffer")
|
|
35
|
+
throw new TypeError("sidecar payload must be buffered");
|
|
36
|
+
const target = path.join(parent.realPath, params.basename);
|
|
37
|
+
const raw = Buffer.isBuffer(params.input.data)
|
|
38
|
+
? params.input.data.toString(params.input.encoding ?? "utf8") : params.input.data;
|
|
39
|
+
const assertCurrent = () => {
|
|
40
|
+
params.assertBeforeMutation?.();
|
|
41
|
+
assertSyncDirectoryGuard(parent);
|
|
42
|
+
};
|
|
43
|
+
const onConflict = (error) => {
|
|
44
|
+
recordFileObservationFailure(error, `exclusive-create:${target}`);
|
|
45
|
+
if (error.code === "EEXIST") {
|
|
46
|
+
throw new FsSafeError("already-exists", "file already exists", { cause: error });
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const created = await createSidecarFile(target, raw, async (pathname) => {
|
|
50
|
+
assertCurrent();
|
|
51
|
+
const failedOpen = (error) => {
|
|
52
|
+
if (native)
|
|
53
|
+
annotateReadOnlyOpenError(error, pathname);
|
|
54
|
+
if (isTransientLockFileDenial(error, pathname))
|
|
55
|
+
recordFileObservationFailure(error, `sidecar-create-denial:${target}`);
|
|
56
|
+
if (pathname === target)
|
|
57
|
+
onConflict(error);
|
|
58
|
+
throw error;
|
|
59
|
+
};
|
|
60
|
+
if (!native) {
|
|
61
|
+
let handle;
|
|
62
|
+
try {
|
|
63
|
+
handle = await promises.open(pathname, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL |
|
|
64
|
+
(process.platform !== "win32" ? fs.constants.O_NOFOLLOW ?? 0 : 0), params.mode);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
return failedOpen(error);
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
fd: handle.fd,
|
|
71
|
+
close: () => handle.close(),
|
|
72
|
+
writeFile: async (data, encoding) => await writePinnedInput(handle, { kind: "buffer", data, encoding }),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const close = captureNativeFdClose(native.binding);
|
|
76
|
+
let open = true;
|
|
77
|
+
let fd;
|
|
78
|
+
try {
|
|
79
|
+
fd = native.binding.createStagedFile && process.platform !== "win32"
|
|
80
|
+
? await native.binding.createStagedFile(native.fd, path.basename(pathname))
|
|
81
|
+
: native.binding.openBeneath(native.fd, path.basename(pathname), nativeOpenFlags(fs.constants.O_RDWR | fs.constants.O_CREAT | fs.constants.O_EXCL)).fd;
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
const failure = error;
|
|
85
|
+
if (process.platform === "win32" && failure?.code === "EPERM" && failure.path === undefined) {
|
|
86
|
+
try {
|
|
87
|
+
failure.path = pathname;
|
|
88
|
+
}
|
|
89
|
+
catch { /* Keep immutable native diagnostics. */ }
|
|
90
|
+
}
|
|
91
|
+
return failedOpen(error);
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
fd,
|
|
95
|
+
async close() { if (open) {
|
|
96
|
+
open = false;
|
|
97
|
+
close(fd);
|
|
98
|
+
} },
|
|
99
|
+
async writeFile(data, encoding) {
|
|
100
|
+
await writePinnedInput(fd, { kind: "buffer", data, encoding });
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}, {
|
|
104
|
+
nativeParent: native, assertCurrent, assertCleanupCandidate: params.assertSidecarCleanupCandidate,
|
|
105
|
+
onConflict, prepare: fd => fs.fchmodSync(fd, params.mode), requireExactIdentity: true,
|
|
106
|
+
});
|
|
107
|
+
const handle = created.handle;
|
|
108
|
+
let failure;
|
|
109
|
+
try {
|
|
110
|
+
const identity = created.identity;
|
|
111
|
+
try {
|
|
112
|
+
assertCurrent();
|
|
113
|
+
await params.verifyPublished?.(handle.fd, identity, parent, native);
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (!native) {
|
|
117
|
+
try {
|
|
118
|
+
assertCurrent();
|
|
119
|
+
await cleanupPinnedFilePath({ pathname: target, handle, identity, parentGuard: parent, throwOnCleanupError: true });
|
|
120
|
+
}
|
|
121
|
+
catch (cleanupError) {
|
|
122
|
+
throw createSuppressedError(cleanupError, error, "sidecar verification and cleanup both failed");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
return { dev: identity.dev, ino: identity.ino };
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
failure = { error };
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
try {
|
|
135
|
+
await handle.close();
|
|
136
|
+
}
|
|
137
|
+
catch (closeError) {
|
|
138
|
+
if (failure)
|
|
139
|
+
throw createSuppressedError(closeError, failure.error, "sidecar write and close both failed");
|
|
140
|
+
throw closeError;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -2,9 +2,9 @@ import crypto from "node:crypto";
|
|
|
2
2
|
import fsSync from "node:fs";
|
|
3
3
|
import fs from "node:fs/promises";
|
|
4
4
|
import path from "node:path";
|
|
5
|
-
import { suffixWindowsReservedDeviceName } from "./filename.js";
|
|
5
|
+
import { fitFileNameToPortableComponent, suffixWindowsReservedDeviceName } from "./filename.js";
|
|
6
6
|
import { sameFileIdentityForCleanup } from "./file-identity.js";
|
|
7
|
-
import { assertSafePathSegment,
|
|
7
|
+
import { assertSafePathSegment, trimHyphenEdges, } from "./safe-path-segment.js";
|
|
8
8
|
import { resolveSecureTempRoot } from "./secure-temp-dir.js";
|
|
9
9
|
import { hasNodeErrorCode } from "./path.js";
|
|
10
10
|
import { registerTempPathForExit } from "./temp-cleanup.js";
|
|
@@ -60,10 +60,25 @@ function sanitizeExtension(extension) {
|
|
|
60
60
|
return token ? `.${token}` : "";
|
|
61
61
|
}
|
|
62
62
|
export function sanitizeTempFileName(fileName) {
|
|
63
|
+
// Keep Unicode filenames, but retain the historical ASCII punctuation filtering.
|
|
64
|
+
const normalized = trimHyphenEdges(path.basename(fileName).trim()
|
|
65
|
+
.replace(/\0/g, "")
|
|
66
|
+
// Some Unicode symbols decompose into characters forbidden on Windows.
|
|
67
|
+
.replace(/[\u0080-\u{10ffff}]/gu, (character) => /[<>:"/\\|?*]/u.test(character.normalize("NFD")) ? "-" : character)
|
|
68
|
+
.replace(/(?:[^A-Za-z0-9._\-\u0080-\u{10ffff}]|[\p{Cc}\p{Cf}\p{Cs}\s])+/gu, "-"));
|
|
69
|
+
if (!normalized || normalized === "." || normalized === "..")
|
|
70
|
+
return "download.bin";
|
|
63
71
|
// Suffix reserved stems before admission so CON.txt stays CON_.txt.
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
72
|
+
const fitted = trimHyphenEdges(fitFileNameToPortableComponent({
|
|
73
|
+
prefix: "",
|
|
74
|
+
fileName: suffixWindowsReservedDeviceName(normalized),
|
|
75
|
+
suffix: "",
|
|
76
|
+
}));
|
|
77
|
+
// Truncation must not expose a device name or a parent/current-directory token.
|
|
78
|
+
return fitted && fitted !== "." && fitted !== ".." &&
|
|
79
|
+
suffixWindowsReservedDeviceName(fitted) === fitted
|
|
80
|
+
? fitted
|
|
81
|
+
: "download.bin";
|
|
67
82
|
}
|
|
68
83
|
export function buildRandomTempFilePath(params) {
|
|
69
84
|
const rootDir = resolveTempRoot(params.rootDir);
|
|
@@ -111,6 +126,12 @@ function resolveTempFileCleanupSafety(value) {
|
|
|
111
126
|
return value;
|
|
112
127
|
throw new TypeError("cleanupSafety must be compatible or require-bounded");
|
|
113
128
|
}
|
|
129
|
+
async function makeTempDirectory(prefix) {
|
|
130
|
+
// Only the OS call receives an extended Windows path. Keep the admitted
|
|
131
|
+
// namespace for alias, ancestry, identity and cleanup checks.
|
|
132
|
+
const created = await fs.mkdtemp(path.toNamespacedPath(prefix));
|
|
133
|
+
return path.join(path.dirname(prefix), path.basename(created));
|
|
134
|
+
}
|
|
114
135
|
export async function createOwnedTempFile(params) {
|
|
115
136
|
const cleanupSafety = resolveTempFileCleanupSafety(params.cleanupSafety);
|
|
116
137
|
const rootDir = resolveTempRoot(params.rootDir);
|
|
@@ -136,7 +157,7 @@ export async function createOwnedTempFile(params) {
|
|
|
136
157
|
let unregisterTempDir;
|
|
137
158
|
try {
|
|
138
159
|
capability.prepareChildCreation();
|
|
139
|
-
dir = await
|
|
160
|
+
dir = await makeTempDirectory(childPrefix);
|
|
140
161
|
assertNoWindowsPathAlias(dir, "filesystem", "temp directory uses a Windows filesystem namespace alias");
|
|
141
162
|
initialPath = path.join(dir, initialFileName);
|
|
142
163
|
assertNoWindowsPathAlias(initialPath, "filesystem", "temp file path uses a Windows filesystem namespace alias");
|
|
@@ -192,7 +213,7 @@ export async function createOwnedTempFile(params) {
|
|
|
192
213
|
identity,
|
|
193
214
|
};
|
|
194
215
|
}
|
|
195
|
-
const dir = await
|
|
216
|
+
const dir = await makeTempDirectory(path.join(rootDir, prefix));
|
|
196
217
|
assertNoWindowsPathAlias(dir, "filesystem", "temp directory uses a Windows filesystem namespace alias");
|
|
197
218
|
// Windows file indexes can exceed Number.MAX_SAFE_INTEGER. Cleanup receipts
|
|
198
219
|
// must retain the exact identity or adjacent directories can compare equal.
|
|
@@ -10,5 +10,5 @@ export type TempWorkspaceRootAdmission = {
|
|
|
10
10
|
associateAncestry(descriptorFd: number): void;
|
|
11
11
|
};
|
|
12
12
|
export declare function admitExistingTempWorkspaceRoot(rootDir: string): TempWorkspaceRootAdmission;
|
|
13
|
-
export declare function admitTempWorkspaceRoot(rootDir: string): Promise<TempWorkspaceRootAdmission>;
|
|
14
|
-
export declare function admitTempWorkspaceRootSync(rootDir: string): TempWorkspaceRootAdmission;
|
|
13
|
+
export declare function admitTempWorkspaceRoot(rootDir: string, trustedWritableAncestors?: readonly string[]): Promise<TempWorkspaceRootAdmission>;
|
|
14
|
+
export declare function admitTempWorkspaceRootSync(rootDir: string, trustedWritableAncestors?: readonly string[]): TempWorkspaceRootAdmission;
|
|
@@ -42,15 +42,15 @@ function copyExactDirectoryObservation(stat) {
|
|
|
42
42
|
symbolicLink: stat.isSymbolicLink(),
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
|
-
function snapshotFromExactObservation(dir, uid, realPath, stat, privateDirectory = false) {
|
|
45
|
+
function snapshotFromExactObservation(dir, uid, realPath, stat, privateDirectory = false, allowWritableAncestor = false) {
|
|
46
46
|
if (stat.symbolicLink || !stat.directory) {
|
|
47
47
|
throw new FsSafeError("not-file", "temp workspace root component must be a real directory");
|
|
48
48
|
}
|
|
49
|
-
assertTrustedTempWorkspaceDirectory(stat, uid, privateDirectory, dir);
|
|
49
|
+
assertTrustedTempWorkspaceDirectory(stat, uid, privateDirectory, dir, allowWritableAncestor);
|
|
50
50
|
const identity = Object.freeze({ dev: stat.dev, ino: stat.ino });
|
|
51
51
|
// Retain copied immutable scalars, never a mutable Stats object supplied by
|
|
52
52
|
// an observation hook. Every later permission check uses a fresh snapshot.
|
|
53
|
-
return Object.freeze({ dir, identity, numericIdentity: safeNumericIdentity(stat), realPath, privateDirectory });
|
|
53
|
+
return Object.freeze({ dir, identity, numericIdentity: safeNumericIdentity(stat), realPath, privateDirectory, allowWritableAncestor });
|
|
54
54
|
}
|
|
55
55
|
function canonicalTempWorkspacePath(dir) {
|
|
56
56
|
assertNoWindowsPathAlias(dir, "filesystem");
|
|
@@ -58,10 +58,10 @@ function canonicalTempWorkspacePath(dir) {
|
|
|
58
58
|
assertNoWindowsPathAlias(canonical, "filesystem");
|
|
59
59
|
return canonical;
|
|
60
60
|
}
|
|
61
|
-
function snapshot(dir, uid, realPath = canonicalTempWorkspacePath(dir), privateDirectory = false) {
|
|
61
|
+
function snapshot(dir, uid, realPath = canonicalTempWorkspacePath(dir), privateDirectory = false, allowWritableAncestor = false) {
|
|
62
62
|
const stat = inspectDirectoryIdentitySync(dir);
|
|
63
63
|
const observation = copyExactDirectoryObservation(stat);
|
|
64
|
-
return { entry: snapshotFromExactObservation(dir, uid, realPath, observation, privateDirectory), stat };
|
|
64
|
+
return { entry: snapshotFromExactObservation(dir, uid, realPath, observation, privateDirectory, allowWritableAncestor), stat };
|
|
65
65
|
}
|
|
66
66
|
function hasCompleteExactIdentity(stat) {
|
|
67
67
|
return typeof stat.dev === "bigint" && typeof stat.ino === "bigint" &&
|
|
@@ -106,7 +106,7 @@ function inspectSnapshotIdentity(entry) {
|
|
|
106
106
|
}
|
|
107
107
|
function assertSnapshot(entry, uid) {
|
|
108
108
|
const stat = inspectSnapshotIdentity(entry);
|
|
109
|
-
assertTrustedTempWorkspaceDirectory(stat, uid, entry.privateDirectory, entry.dir);
|
|
109
|
+
assertTrustedTempWorkspaceDirectory(stat, uid, entry.privateDirectory, entry.dir, entry.allowWritableAncestor);
|
|
110
110
|
assertCanonicalRoot(entry);
|
|
111
111
|
}
|
|
112
112
|
function snapshotMissingRootComponent(parent, dir, ownerUid, privateDirectory) {
|
|
@@ -115,7 +115,7 @@ function snapshotMissingRootComponent(parent, dir, ownerUid, privateDirectory) {
|
|
|
115
115
|
return snapshot(dir, ownerUid, undefined, privateDirectory);
|
|
116
116
|
}
|
|
117
117
|
const current = inspectSnapshotIdentity(parent);
|
|
118
|
-
assertTrustedTempWorkspaceDirectory(current, ownerUid, parent.privateDirectory, parent.dir);
|
|
118
|
+
assertTrustedTempWorkspaceDirectory(current, ownerUid, parent.privateDirectory, parent.dir, parent.allowWritableAncestor);
|
|
119
119
|
let realPath;
|
|
120
120
|
try {
|
|
121
121
|
realPath = canonicalTempWorkspacePath(dir);
|
|
@@ -136,7 +136,7 @@ function snapshotMissingRootComponent(parent, dir, ownerUid, privateDirectory) {
|
|
|
136
136
|
function assertChain(chain, uid) {
|
|
137
137
|
for (const entry of chain) {
|
|
138
138
|
const current = inspectSnapshotIdentity(entry);
|
|
139
|
-
assertTrustedTempWorkspaceDirectory(current, uid, entry.privateDirectory, entry.dir);
|
|
139
|
+
assertTrustedTempWorkspaceDirectory(current, uid, entry.privateDirectory, entry.dir, entry.allowWritableAncestor);
|
|
140
140
|
}
|
|
141
141
|
const last = chain[chain.length - 1];
|
|
142
142
|
assertCanonicalRoot(last);
|
|
@@ -159,7 +159,7 @@ function associateTempWorkspaceRoot(entry, ownerUid, descriptorFd) {
|
|
|
159
159
|
if (stat.isSymbolicLink() || !stat.isDirectory()) {
|
|
160
160
|
throw new FsSafeError("not-file", "temp workspace cleanup parent must be a real directory");
|
|
161
161
|
}
|
|
162
|
-
assertTrustedTempWorkspaceDirectory(stat, ownerUid, entry.privateDirectory, entry.dir);
|
|
162
|
+
assertTrustedTempWorkspaceDirectory(stat, ownerUid, entry.privateDirectory, entry.dir, entry.allowWritableAncestor);
|
|
163
163
|
}
|
|
164
164
|
function discoverMissingTempWorkspaceAncestor(ancestor, missing, missingError) {
|
|
165
165
|
// The caller already classified the first ENOENT. Keep this handoff out of
|
|
@@ -197,8 +197,24 @@ function discoverMissingTempWorkspaceAncestor(ancestor, missing, missingError) {
|
|
|
197
197
|
}
|
|
198
198
|
}
|
|
199
199
|
}
|
|
200
|
-
function
|
|
200
|
+
function trustedAncestorPaths(values) {
|
|
201
|
+
if (values === undefined)
|
|
202
|
+
return new Set();
|
|
203
|
+
if (!Array.isArray(values))
|
|
204
|
+
throw new TypeError("trustedWritableAncestors must be an array of canonical absolute paths");
|
|
205
|
+
const paths = new Set();
|
|
206
|
+
for (const value of values) {
|
|
207
|
+
if (typeof value !== "string" || !path.isAbsolute(value) ||
|
|
208
|
+
path.normalize(value) !== value || canonicalTempWorkspacePath(value) !== value) {
|
|
209
|
+
throw new TypeError("trustedWritableAncestors must contain canonical absolute paths");
|
|
210
|
+
}
|
|
211
|
+
paths.add(value);
|
|
212
|
+
}
|
|
213
|
+
return paths;
|
|
214
|
+
}
|
|
215
|
+
function rootPlan(rootDir, trustedWritableAncestors) {
|
|
201
216
|
assertNoWindowsPathAlias(rootDir, "filesystem");
|
|
217
|
+
const trusted = trustedAncestorPaths(trustedWritableAncestors);
|
|
202
218
|
const ownerUid = effectiveOwner();
|
|
203
219
|
let ancestor = resolvePathPreservingWindowsRoot(rootDir);
|
|
204
220
|
assertNoWindowsPathAlias(ancestor, "filesystem");
|
|
@@ -222,12 +238,12 @@ function rootPlan(rootDir) {
|
|
|
222
238
|
if (existingCanonicalRoot) {
|
|
223
239
|
const discovery = snapshotFromExactObservation(ancestor, ownerUid, ancestor, initial, true);
|
|
224
240
|
return {
|
|
225
|
-
admission: canonicalRootAdmission(discovery, ownerUid),
|
|
241
|
+
admission: canonicalRootAdmission(discovery, ownerUid, trusted),
|
|
226
242
|
missing,
|
|
227
243
|
ownerUid,
|
|
228
244
|
};
|
|
229
245
|
}
|
|
230
|
-
const chain = canonicalAncestry(ancestor).map((dir) => snapshot(dir, ownerUid, dir, missing.length === 0 && dir === ancestor).entry);
|
|
246
|
+
const chain = canonicalAncestry(ancestor).map((dir) => snapshot(dir, ownerUid, dir, missing.length === 0 && dir === ancestor, trusted.has(dir)).entry);
|
|
231
247
|
// Missing-component creation needs a replay before its first mutation. When
|
|
232
248
|
// the complete root already exists through an alias, retain the historical
|
|
233
249
|
// guarded route because discovery and mutation use different path spellings.
|
|
@@ -261,7 +277,7 @@ function rootAdmission(chain, ownerUid) {
|
|
|
261
277
|
},
|
|
262
278
|
};
|
|
263
279
|
}
|
|
264
|
-
function canonicalRootAdmission(discovery, ownerUid) {
|
|
280
|
+
function canonicalRootAdmission(discovery, ownerUid, trusted) {
|
|
265
281
|
let admittedChain;
|
|
266
282
|
const chain = () => {
|
|
267
283
|
if (!admittedChain) {
|
|
@@ -287,7 +303,7 @@ function canonicalRootAdmission(discovery, ownerUid) {
|
|
|
287
303
|
const ancestry = canonicalAncestry(discovery.dir);
|
|
288
304
|
const candidate = ancestry.map((dir, index) => {
|
|
289
305
|
if (!TEMP_WORKSPACE_NUMERIC_IDENTITY_REPLAY || index !== ancestry.length - 1) {
|
|
290
|
-
return snapshot(dir, ownerUid, dir, index === ancestry.length - 1 && discovery.privateDirectory).entry;
|
|
306
|
+
return snapshot(dir, ownerUid, dir, index === ancestry.length - 1 && discovery.privateDirectory, trusted.has(dir)).entry;
|
|
291
307
|
}
|
|
292
308
|
const current = inspectSnapshotIdentity(discovery);
|
|
293
309
|
assertTrustedTempWorkspaceDirectory(current, ownerUid, discovery.privateDirectory, discovery.dir);
|
|
@@ -328,8 +344,8 @@ export function admitExistingTempWorkspaceRoot(rootDir) {
|
|
|
328
344
|
}
|
|
329
345
|
return admission ?? rootAdmission(chain, ownerUid);
|
|
330
346
|
}
|
|
331
|
-
export async function admitTempWorkspaceRoot(rootDir) {
|
|
332
|
-
const { admission, chain, missing, ownerUid } = rootPlan(rootDir);
|
|
347
|
+
export async function admitTempWorkspaceRoot(rootDir, trustedWritableAncestors) {
|
|
348
|
+
const { admission, chain, missing, ownerUid } = rootPlan(rootDir, trustedWritableAncestors);
|
|
333
349
|
if (admission)
|
|
334
350
|
return admission;
|
|
335
351
|
const guardedChain = chain;
|
|
@@ -358,8 +374,8 @@ export async function admitTempWorkspaceRoot(rootDir) {
|
|
|
358
374
|
}
|
|
359
375
|
return rootAdmission(guardedChain, ownerUid);
|
|
360
376
|
}
|
|
361
|
-
export function admitTempWorkspaceRootSync(rootDir) {
|
|
362
|
-
const { admission, chain, missing, ownerUid } = rootPlan(rootDir);
|
|
377
|
+
export function admitTempWorkspaceRootSync(rootDir, trustedWritableAncestors) {
|
|
378
|
+
const { admission, chain, missing, ownerUid } = rootPlan(rootDir, trustedWritableAncestors);
|
|
363
379
|
if (admission)
|
|
364
380
|
return admission;
|
|
365
381
|
const guardedChain = chain;
|
|
@@ -14,7 +14,7 @@ export declare function projectTempWorkspaceNumericIdentity(identity: TempWorksp
|
|
|
14
14
|
export declare function inspectTempWorkspaceDescriptorIdentitySync(fd: number, expected: TempWorkspaceIdentity, numeric: TempWorkspaceNumericIdentity | undefined): TempWorkspaceIdentityStat;
|
|
15
15
|
export declare function inspectTempWorkspaceDirectoryIdentitySync(dir: string, expected: TempWorkspaceIdentity, numeric: TempWorkspaceNumericIdentity | undefined): TempWorkspaceIdentityStat;
|
|
16
16
|
export declare function validateTempWorkspaceDirMode(mode: number): void;
|
|
17
|
-
export declare function assertTrustedTempWorkspaceDirectory(stat: Pick<BigIntStats, "uid" | "gid" | "mode"> | Pick<Stats, "uid" | "gid" | "mode">, uid: number | undefined, privateDirectory: boolean, dir: string): void;
|
|
17
|
+
export declare function assertTrustedTempWorkspaceDirectory(stat: Pick<BigIntStats, "uid" | "gid" | "mode"> | Pick<Stats, "uid" | "gid" | "mode">, uid: number | undefined, privateDirectory: boolean, dir: string, allowWritableAncestor?: boolean): void;
|
|
18
18
|
export declare function assertTempWorkspaceChildState(stat: BigIntStats | Stats, ownerUid: number | undefined, dir: string): void;
|
|
19
19
|
export declare function validateInitialTempWorkspaceChild(stat: BigIntStats, ownerUid: number | undefined, mode: number, dir: string): boolean;
|
|
20
20
|
export declare function childHasRequestedMode(stat: BigIntStats | Stats, mode: number): boolean;
|
|
@@ -44,7 +44,14 @@ export function validateTempWorkspaceDirMode(mode) {
|
|
|
44
44
|
throw new FsSafeError("insecure-permissions", "temp workspace must not be group/world writable");
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
-
|
|
47
|
+
let warnedTrustedWritableAncestor = false;
|
|
48
|
+
function warnTrustedWritableAncestor() {
|
|
49
|
+
if (warnedTrustedWritableAncestor)
|
|
50
|
+
return;
|
|
51
|
+
warnedTrustedWritableAncestor = true;
|
|
52
|
+
process.emitWarning("Temp workspace ancestor write protection is bypassed by trustedWritableAncestors; external isolation must prevent untrusted writers, including same-UID processes, from replacing the directory ancestry for the workspace lifetime.", { code: "FS_SAFE_TRUSTED_WRITABLE_ANCESTOR", type: "FsSafeWarning" });
|
|
53
|
+
}
|
|
54
|
+
export function assertTrustedTempWorkspaceDirectory(stat, uid, privateDirectory, dir, allowWritableAncestor = false) {
|
|
48
55
|
if (uid === undefined)
|
|
49
56
|
return;
|
|
50
57
|
const ownership = classifyTempWorkspaceOwner(stat, uid);
|
|
@@ -53,7 +60,8 @@ export function assertTrustedTempWorkspaceDirectory(stat, uid, privateDirectory,
|
|
|
53
60
|
}
|
|
54
61
|
// Unmapped host owners are unverifiable; trust the host directory hierarchy.
|
|
55
62
|
// Admit sticky ancestors so PrivateUsers keeps host /tmp and PrivateTmp usable.
|
|
56
|
-
// Leaf roots stay euid-owned/private
|
|
63
|
+
// Leaf roots stay euid-owned/private. Only explicitly trusted ancestors may
|
|
64
|
+
// rely on external isolation instead of sticky protection against other writers.
|
|
57
65
|
const writeMask = !privateDirectory && hasTrustedAndroidTempAncestorGroup(stat, uid, ownership)
|
|
58
66
|
? 0o002 : 0o022;
|
|
59
67
|
const writable = typeof stat.mode === "bigint"
|
|
@@ -65,11 +73,13 @@ export function assertTrustedTempWorkspaceDirectory(stat, uid, privateDirectory,
|
|
|
65
73
|
if (typeof stat.mode !== "bigint" && (!Number.isSafeInteger(stat.mode) || stat.mode < 0)) {
|
|
66
74
|
throw new FsSafeError("insecure-permissions", `temp workspace directory ${JSON.stringify(dir)} permissions are invalid; ${privateTempDirectoryFix(uid)}`);
|
|
67
75
|
}
|
|
68
|
-
if (writable && (privateDirectory || !sticky)) {
|
|
76
|
+
if (writable && (privateDirectory || (!sticky && !allowWritableAncestor))) {
|
|
69
77
|
throw new FsSafeError("insecure-permissions", privateDirectory
|
|
70
78
|
? `temp workspace directory ${JSON.stringify(dir)} has group/world-writable mode ${tempDirectoryMode(stat.mode)}; root and child must not be group/world writable, even with the sticky bit; ${privateTempDirectoryFix(uid)}`
|
|
71
79
|
: `temp workspace ancestor ${JSON.stringify(dir)} has group/world-writable mode ${tempDirectoryMode(stat.mode)} without sticky protection (sticky bit missing); have its administrator restore safe permissions (1777 for a shared system temp directory), or move the private root under trusted ancestors`);
|
|
72
80
|
}
|
|
81
|
+
if (!privateDirectory && writable && !sticky && allowWritableAncestor)
|
|
82
|
+
warnTrustedWritableAncestor();
|
|
73
83
|
if (ownership === "unmapped")
|
|
74
84
|
warnUnmappedTempWorkspaceAncestor();
|
|
75
85
|
}
|
|
@@ -70,5 +70,5 @@ export function warnUnmappedTempWorkspaceAncestor() {
|
|
|
70
70
|
if (warned)
|
|
71
71
|
return;
|
|
72
72
|
warned = true;
|
|
73
|
-
process.emitWarning("Temp workspace ancestor ownership is unverifiable in this user namespace; trusting the host directory hierarchy while enforcing ancestor
|
|
73
|
+
process.emitWarning("Temp workspace ancestor ownership is unverifiable in this user namespace; trusting the host directory hierarchy while enforcing the configured ancestor policy and a process-owned private root.", { code: "FS_SAFE_UNMAPPED_TEMP_ANCESTOR", type: "FsSafeWarning" });
|
|
74
74
|
}
|
|
@@ -9,6 +9,15 @@ export type TempWorkspaceOptions = {
|
|
|
9
9
|
dirMode?: DirectoryMode;
|
|
10
10
|
mode?: number;
|
|
11
11
|
cleanupSafety?: TempWorkspaceCleanupSafety;
|
|
12
|
+
/**
|
|
13
|
+
* Allow ancestor write bits at exact canonical paths, relying on external isolation.
|
|
14
|
+
* Untrusted writers, including same-UID model/terminal processes, must not be able
|
|
15
|
+
* to rename or recreate the ancestry for the workspace lifetime: later I/O may
|
|
16
|
+
* otherwise target replacement directories. Private modes and bounded cleanup
|
|
17
|
+
* do not prevent redirected I/O. Use only trusted deployment configuration.
|
|
18
|
+
* Emits FS_SAFE_TRUSTED_WRITABLE_ANCESTOR once per process when a check is bypassed.
|
|
19
|
+
*/
|
|
20
|
+
trustedWritableAncestors?: readonly string[];
|
|
12
21
|
};
|
|
13
22
|
export type TempWorkspace = {
|
|
14
23
|
dir: string;
|
|
@@ -23,6 +23,7 @@ export type FsSafeTestHooks = {
|
|
|
23
23
|
beforeRootListObservation?: (directoryPath: string, withFileTypes: boolean) => Promise<void> | void;
|
|
24
24
|
afterPinnedWriteFallbackRename?: (targetPath: string) => Promise<void> | void;
|
|
25
25
|
beforeSiblingTempWrite?: (tempPath: string) => Promise<void> | void;
|
|
26
|
+
beforeSidecarLockPublish?: (lockPath: string, temporaryPath: string) => void;
|
|
26
27
|
beforeSidecarLockSnapshotOpen?: (lockPath: string) => Promise<void> | void;
|
|
27
28
|
beforeRegularFileAppendOpen?: (filePath: string) => Promise<void> | void;
|
|
28
29
|
beforeRegularFileAppendOpenSync?: (filePath: string) => void;
|
|
@@ -31,6 +32,11 @@ export type FsSafeTestHooks = {
|
|
|
31
32
|
beforeTrashMove?: (targetPath: string, destPath: string) => void;
|
|
32
33
|
afterPublishTargetCreated?: (method: "hardlink" | "exclusive-copy" | "rename-noreplace", targetPath: string, identity: FileIdentityStat) => Promise<void> | void;
|
|
33
34
|
beforePublishDirectorySync?: NonNullable<FsSafeTestHooks["afterPublishTargetCreated"]>;
|
|
35
|
+
drvfsMountObservation?: (parentFd: number) => {
|
|
36
|
+
magic: bigint;
|
|
37
|
+
fdinfo: string;
|
|
38
|
+
mountinfo: string;
|
|
39
|
+
} | undefined;
|
|
34
40
|
};
|
|
35
41
|
export declare function getFsSafeTestHooks(): FsSafeTestHooks | undefined;
|
|
36
42
|
export declare function __setFsSafeTestHooksForTest(hooks?: FsSafeTestHooks): void;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type ReplaceFileAtomicOptions } from "./replace-file.js";
|
|
2
2
|
import type { DirectoryMode } from "./directory-mode-options.js";
|
|
3
|
-
export type WriteTextAtomicOptions = Pick<ReplaceFileAtomicOptions, "beforeRename" | "tempPrefix"> & {
|
|
3
|
+
export type WriteTextAtomicOptions = Pick<ReplaceFileAtomicOptions, "beforeRename" | "tempPrefix" | "allowDrvfsRenameRecovery"> & {
|
|
4
4
|
mode?: number;
|
|
5
5
|
dirMode?: DirectoryMode;
|
|
6
6
|
trailingNewline?: boolean;
|
|
@@ -18,6 +18,7 @@ export type WindowsOwnerAce = {
|
|
|
18
18
|
deny: boolean;
|
|
19
19
|
inheritOnly: boolean;
|
|
20
20
|
};
|
|
21
|
+
export declare const TRUSTED_INSTALLER_SID = "s-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464";
|
|
21
22
|
export declare function inspectWindowsOwner(params: {
|
|
22
23
|
targetPath: string;
|
|
23
24
|
env?: NodeJS.ProcessEnv;
|
|
@@ -6,7 +6,8 @@ import { windowsSecurityHelperPath } from "./windows-security-helper.js";
|
|
|
6
6
|
import { hasWindowsPathAlias } from "./windows-path-alias.js";
|
|
7
7
|
import { APP_CONTAINER_SID_RE } from "./windows-security-facts.js";
|
|
8
8
|
const SID_RE = /^\*?s-\d+-\d+(-\d+)+$/i;
|
|
9
|
-
const
|
|
9
|
+
export const TRUSTED_INSTALLER_SID = "s-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464";
|
|
10
|
+
const TRUSTED_OWNER_SIDS = new Set(["s-1-5-18", "s-1-5-32-544", TRUSTED_INSTALLER_SID]);
|
|
10
11
|
function normalizeSid(value) {
|
|
11
12
|
const normalized = value.trim().toLowerCase();
|
|
12
13
|
return normalized.startsWith("*") ? normalized.slice(1) : normalized;
|
|
Binary file
|
|
@@ -31,7 +31,7 @@ The exports group into a handful of themes. Documented helpers link to their con
|
|
|
31
31
|
| `resolveExistingPathsWithinRoot` | – | Validate existing regular files inside the root, while allowing missing paths. |
|
|
32
32
|
| `resolveStrictExistingPathsWithinRoot` | – | Require every target to exist as a regular non-symlink file inside the root. |
|
|
33
33
|
| `resolveWritablePathWithinRoot` | – | Resolve a write target inside a root. |
|
|
34
|
-
| `resolveRootPath`, `resolveRootPathSync`, `ResolvedRootPath`, `ROOT_PATH_ALIAS_POLICIES`, `RootPathAliasPolicy` | – | Resolve a root directory honoring alias policy. Awaited POSIX traversal uses asynchronous Node metadata and canonicalization; the explicit sync variant blocks. Windows retains synchronous traversal to preserve concurrent reads and atomic publication until canonicalization supports delete sharing.
|
|
34
|
+
| `resolveRootPath`, `resolveRootPathSync`, `ResolvedRootPath`, `ROOT_PATH_ALIAS_POLICIES`, `RootPathAliasPolicy` | – | Resolve a root directory honoring alias policy. Awaited POSIX traversal uses asynchronous Node metadata and canonicalization; the explicit sync variant blocks. Windows retains synchronous traversal to preserve concurrent reads and atomic publication until canonicalization supports delete sharing. Other identity/observation guards may still be synchronous. |
|
|
35
35
|
| `resolvePathViaExistingAncestorSync` | – | Walk to an existing ancestor for paths whose tail does not yet exist. |
|
|
36
36
|
| `resolvePathPrefix`, `resolvePathPrefixSync`, `ResolvedPathPrefix` | [path-prefix.md](path-prefix.md) | Follow physical symlink targets and return a canonical existing prefix with the raw missing suffix; propagate uncertain resolution failures. |
|
|
37
37
|
| `probePathCaseInsensitiveSync`, `ProbePathCaseOptions` | [path-case.md](path-case.md) | Observe local ASCII-case behavior with explicit read-only mode and owned temporary-probe cleanup. |
|
|
@@ -102,6 +102,11 @@ receipts remain numeric. Custom `ioFs` adapters must honor `{ bigint: true }` fo
|
|
|
102
102
|
Windows identities receive one re-inspection without reopening, then fail
|
|
103
103
|
validation if still unknown.
|
|
104
104
|
|
|
105
|
+
With the built-in adapter on POSIX, `openRootFile()` awaits the metadata, native
|
|
106
|
+
canonicalization, descriptor open, and final admission checks. Windows and custom
|
|
107
|
+
`ioFs` adapters retain their synchronous admission path; `openRootFileSync()`
|
|
108
|
+
remains synchronous on every platform.
|
|
109
|
+
|
|
105
110
|
On Windows, these existing-object readers retain their historical support for a
|
|
106
111
|
leading drive-relative spelling such as `C:existing.txt`: it is anchored to that
|
|
107
112
|
drive before confinement and namespace admission. Additional colons remain in
|
|
@@ -35,6 +35,10 @@ are independent of this directory policy.
|
|
|
35
35
|
|
|
36
36
|
Async replacements to the same destination are serialized inside the current process, so two overlapping `replaceFileAtomic()` calls do not interleave their temp-write/rename phases. Use a sidecar lock when multiple processes may write the same target.
|
|
37
37
|
|
|
38
|
+
On DrvFS mounts that hide renamed files while any descriptor is open, the
|
|
39
|
+
strict default rejects post-rename `ENOENT`. See the explicit
|
|
40
|
+
[`allowDrvfsRenameRecovery` exception](#drvfs-hidden-rename-recovery).
|
|
41
|
+
|
|
38
42
|
Relative paths retain their literal suffix so `.` and `..` keep Node's existing
|
|
39
43
|
filesystem semantics. On Windows, an ordinary drive-relative destination such
|
|
40
44
|
as `C:.\\state.json` is captured at entry by anchoring the drive's current
|
|
@@ -72,6 +76,7 @@ type ReplaceFileAtomicOptions = {
|
|
|
72
76
|
maxRestoreBytes?: number; // required with "restore-original"
|
|
73
77
|
destinationHardlinks?: "reject"; // default unset (no destination nlink policy)
|
|
74
78
|
renameIdentity?: "strict" | "verify-content-with-lock"; // default "strict"
|
|
79
|
+
allowDrvfsRenameRecovery?: boolean; // default false; verified DrvFS only
|
|
75
80
|
syncTempFile?: boolean; // fsync(temp) before rename, or the final file after copy fallback; default false
|
|
76
81
|
syncParentDir?: boolean; // fsync(parent) after rename, POSIX only; default false
|
|
77
82
|
throwOnCleanupError?: boolean; // report temp cleanup failure; default false
|
|
@@ -93,6 +98,65 @@ pinned destination. The snapshot does not make replacement a compare-and-swap
|
|
|
93
98
|
operation, so the destination parent must still be protected from untrusted
|
|
94
99
|
concurrent namespace mutation.
|
|
95
100
|
|
|
101
|
+
### DrvFS hidden-rename recovery
|
|
102
|
+
|
|
103
|
+
`allowDrvfsRenameRecovery: true` permits recovery only after a successful rename
|
|
104
|
+
is followed by `ENOENT`. It is off by default. Omitting it preserves strict
|
|
105
|
+
behavior and adds no filesystem calls. On Linux, enabling it first rules out
|
|
106
|
+
non-9p filesystems using the existing device identity or one native `fstatfs`
|
|
107
|
+
on an already-open parent descriptor. Only negative results are cached by
|
|
108
|
+
exact device number; device reuse can therefore only retain strict behavior,
|
|
109
|
+
never enable recovery. After a negative observation, subsequent writes on that
|
|
110
|
+
filesystem add no filesystem calls. Writers without an existing parent descriptor
|
|
111
|
+
reuse their ordinary recovery-parent acquisition for the first observation.
|
|
112
|
+
Unavailable native probes keep the existing preparation path (except known
|
|
113
|
+
block-device filesystems, which cannot be 9p). Healthy publications still use
|
|
114
|
+
their ordinary verification.
|
|
115
|
+
|
|
116
|
+
The retained parent's backing filesystem must have statfs magic `0x01021997`
|
|
117
|
+
(9p). Its `/proc/self/fdinfo/<fd>` mount ID must select exactly one entry in
|
|
118
|
+
`/proc/self/mountinfo` with filesystem type `9p` and `aname=drvfs` in the super
|
|
119
|
+
options. This includes the reporter's Docker Desktop Windows-drive bind mount.
|
|
120
|
+
Unavailable, malformed, ambiguous, or different mount evidence never enables
|
|
121
|
+
recovery. No create/unlink sampling, path-name heuristics, or positive mount
|
|
122
|
+
cache is used.
|
|
123
|
+
|
|
124
|
+
Recovery closes the operation's file descriptors, then reopens the published
|
|
125
|
+
name through guarded helpers. It requires the pre-rename exact bigint device
|
|
126
|
+
and inode, regular-file type, one link, size, mode, owner and group. Parent
|
|
127
|
+
identity/confinement checks remain in force. Byte-backed writes compare content
|
|
128
|
+
as well; callback staging compares a SHA-256 digest captured before rename.
|
|
129
|
+
There are at most five reopen attempts, retrying only `ENOENT` with 20 ms delays.
|
|
130
|
+
A swap, deletion or ownership change fails. A mode that prevents the required
|
|
131
|
+
read also fails; recovery never widens permissions to read the file. Descriptors
|
|
132
|
+
owned by other code can keep the name hidden and exhaust the retry budget.
|
|
133
|
+
|
|
134
|
+
**Residual risk:** closing the last descriptor gives up continuous inode
|
|
135
|
+
retention. If an inode is deleted and its complete identity is recycled before
|
|
136
|
+
verification, identity alone cannot distinguish it. NTFS file references include
|
|
137
|
+
a 16-bit sequence that wraps after 65,536 reuses of the same record; DrvFS
|
|
138
|
+
admission does not prove the underlying Windows filesystem is NTFS. Content
|
|
139
|
+
comparison narrows this risk but cannot distinguish an identical replacement.
|
|
140
|
+
Scheduling pauses are not bounded by retry counts or delays. Enable this option
|
|
141
|
+
only when accepting that tradeoff for the affected mount.
|
|
142
|
+
|
|
143
|
+
| API | Where to enable | Content check after recovery |
|
|
144
|
+
| --- | --- | --- |
|
|
145
|
+
| `replaceFileAtomic`, `replaceFileAtomicSync` | options | exact bytes |
|
|
146
|
+
| `writeTextAtomic`, `writeJson`, `writeJsonSync` | options | exact serialized bytes |
|
|
147
|
+
| `Root.write`, `writeJson`, `create`, `createJson` | Root defaults or per-call options | exact bytes for buffered input |
|
|
148
|
+
| `Root.copyIn`, streamed `Root.create` | Root defaults or per-call options | metadata; bytes are not retained |
|
|
149
|
+
| `fileStore`, `fileStoreSync` | store defaults or write options | exact bytes for buffered/JSON writes |
|
|
150
|
+
| `jsonStore`, `fileStore().json()` | JSON-store options or parent store defaults | exact serialized bytes |
|
|
151
|
+
| `writeSiblingTempFile`, sibling `writeExternalFileWithinRoot` | options | pre-rename digest |
|
|
152
|
+
| `stageFileInDirectory` | staging options | exact bytes |
|
|
153
|
+
| `writeSecretFileAtomic`, `createSecretFileAtomic` | options | exact bytes |
|
|
154
|
+
|
|
155
|
+
The same admission and metadata requirements apply to native and TypeScript
|
|
156
|
+
implementations and synchronous and asynchronous writes. This option is separate
|
|
157
|
+
from `renameIdentity: "verify-content-with-lock"`, which addresses changed inode
|
|
158
|
+
identities on other filesystems; DrvFS recovery never accepts a changed identity.
|
|
159
|
+
|
|
96
160
|
### `beforeRename`
|
|
97
161
|
|
|
98
162
|
Runs after the temp file is fully written and before the rename. Use it to take a backup snapshot, capture the about-to-be-replaced contents, or notify an observer. The helper retains the staged descriptor and exact bigint identity across the hook; replacing, deleting, hardlinking, or changing the temp entry to a non-regular file is rejected before publication:
|