@openclaw/matrix 2026.11.10-beta.3 → 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.
Files changed (149) hide show
  1. package/node_modules/@openclaw/fs-safe/CHANGELOG.md +57 -0
  2. package/node_modules/@openclaw/fs-safe/README.md +6 -0
  3. package/node_modules/@openclaw/fs-safe/dist/archive-parser.wasm +0 -0
  4. package/node_modules/@openclaw/fs-safe/dist/atomic-io.d.ts +1 -0
  5. package/node_modules/@openclaw/fs-safe/dist/atomic-io.js +10 -0
  6. package/node_modules/@openclaw/fs-safe/dist/bounded-read.d.ts +1 -1
  7. package/node_modules/@openclaw/fs-safe/dist/bounded-read.js +15 -4
  8. package/node_modules/@openclaw/fs-safe/dist/directory-guard.js +11 -2
  9. package/node_modules/@openclaw/fs-safe/dist/drvfs-parent.d.ts +6 -0
  10. package/node_modules/@openclaw/fs-safe/dist/drvfs-parent.js +38 -0
  11. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-acquisition.d.ts +1 -1
  12. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-acquisition.js +2 -2
  13. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-acquire.js +13 -8
  14. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-io.js +13 -0
  15. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-mutation.d.ts +2 -2
  16. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-mutation.js +34 -24
  17. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-stale-admission.js +7 -2
  18. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync.js +20 -20
  19. package/node_modules/@openclaw/fs-safe/dist/file-observation.d.ts +13 -2
  20. package/node_modules/@openclaw/fs-safe/dist/file-observation.js +8 -4
  21. package/node_modules/@openclaw/fs-safe/dist/file-store-sync-write.d.ts +1 -0
  22. package/node_modules/@openclaw/fs-safe/dist/file-store-sync-write.js +11 -1
  23. package/node_modules/@openclaw/fs-safe/dist/file-store-write-options.d.ts +2 -0
  24. package/node_modules/@openclaw/fs-safe/dist/file-store-write-options.js +17 -0
  25. package/node_modules/@openclaw/fs-safe/dist/file-store.d.ts +2 -1
  26. package/node_modules/@openclaw/fs-safe/dist/file-store.js +12 -16
  27. package/node_modules/@openclaw/fs-safe/dist/guest-native-python.js +1 -1
  28. package/node_modules/@openclaw/fs-safe/dist/guest.js +3 -4
  29. package/node_modules/@openclaw/fs-safe/dist/json-document-store.d.ts +2 -0
  30. package/node_modules/@openclaw/fs-safe/dist/json-document-store.js +1 -0
  31. package/node_modules/@openclaw/fs-safe/dist/json-store.js +1 -0
  32. package/node_modules/@openclaw/fs-safe/dist/json.d.ts +5 -2
  33. package/node_modules/@openclaw/fs-safe/dist/json.js +6 -2
  34. package/node_modules/@openclaw/fs-safe/dist/local-file-descriptor.js +16 -10
  35. package/node_modules/@openclaw/fs-safe/dist/native-binding.d.ts +6 -0
  36. package/node_modules/@openclaw/fs-safe/dist/native-pinned-write.js +4 -0
  37. package/node_modules/@openclaw/fs-safe/dist/native-staged-file-metadata.d.ts +20 -0
  38. package/node_modules/@openclaw/fs-safe/dist/native-staged-file-metadata.js +18 -0
  39. package/node_modules/@openclaw/fs-safe/dist/native-staged-file.d.ts +3 -18
  40. package/node_modules/@openclaw/fs-safe/dist/native-staged-file.js +48 -24
  41. package/node_modules/@openclaw/fs-safe/dist/opened-file-failure.js +19 -7
  42. package/node_modules/@openclaw/fs-safe/dist/output.d.ts +1 -0
  43. package/node_modules/@openclaw/fs-safe/dist/output.js +3 -0
  44. package/node_modules/@openclaw/fs-safe/dist/path-policy.js +4 -1
  45. package/node_modules/@openclaw/fs-safe/dist/permissions-windows.js +35 -10
  46. package/node_modules/@openclaw/fs-safe/dist/pinned-open.d.ts +9 -2
  47. package/node_modules/@openclaw/fs-safe/dist/pinned-open.js +42 -23
  48. package/node_modules/@openclaw/fs-safe/dist/pinned-write-staged.js +85 -34
  49. package/node_modules/@openclaw/fs-safe/dist/pinned-write-types.d.ts +3 -0
  50. package/node_modules/@openclaw/fs-safe/dist/pinned-write.js +4 -0
  51. package/node_modules/@openclaw/fs-safe/dist/prepare-directory-mode.d.ts +1 -0
  52. package/node_modules/@openclaw/fs-safe/dist/prepare-directory-mode.js +1 -1
  53. package/node_modules/@openclaw/fs-safe/dist/private-temp-workspace.js +2 -2
  54. package/node_modules/@openclaw/fs-safe/dist/publication-recovery.d.ts +24 -0
  55. package/node_modules/@openclaw/fs-safe/dist/publication-recovery.js +151 -0
  56. package/node_modules/@openclaw/fs-safe/dist/publish-copy-stage.d.ts +1 -0
  57. package/node_modules/@openclaw/fs-safe/dist/publish-copy-stage.js +2 -1
  58. package/node_modules/@openclaw/fs-safe/dist/realpath.d.ts +1 -1
  59. package/node_modules/@openclaw/fs-safe/dist/realpath.js +22 -11
  60. package/node_modules/@openclaw/fs-safe/dist/replace-file-descriptor.d.ts +1 -0
  61. package/node_modules/@openclaw/fs-safe/dist/replace-file-descriptor.js +5 -0
  62. package/node_modules/@openclaw/fs-safe/dist/replace-file-temp-owner.d.ts +7 -0
  63. package/node_modules/@openclaw/fs-safe/dist/replace-file-temp-owner.js +75 -2
  64. package/node_modules/@openclaw/fs-safe/dist/replace-file-types.d.ts +2 -0
  65. package/node_modules/@openclaw/fs-safe/dist/replace-file.js +14 -10
  66. package/node_modules/@openclaw/fs-safe/dist/root-context.js +28 -12
  67. package/node_modules/@openclaw/fs-safe/dist/root-copy.js +1 -0
  68. package/node_modules/@openclaw/fs-safe/dist/root-create-input.js +2 -2
  69. package/node_modules/@openclaw/fs-safe/dist/root-file-final-admission.d.ts +4 -0
  70. package/node_modules/@openclaw/fs-safe/dist/root-file-final-admission.js +53 -36
  71. package/node_modules/@openclaw/fs-safe/dist/root-file-open-batch-sync.js +3 -1
  72. package/node_modules/@openclaw/fs-safe/dist/root-file.d.ts +2 -1
  73. package/node_modules/@openclaw/fs-safe/dist/root-file.js +24 -13
  74. package/node_modules/@openclaw/fs-safe/dist/root-impl.js +17 -1
  75. package/node_modules/@openclaw/fs-safe/dist/root-options.d.ts +4 -2
  76. package/node_modules/@openclaw/fs-safe/dist/root-path.d.ts +1 -1
  77. package/node_modules/@openclaw/fs-safe/dist/root-path.js +2 -1
  78. package/node_modules/@openclaw/fs-safe/dist/root-read-admission.js +31 -9
  79. package/node_modules/@openclaw/fs-safe/dist/root-write-admission.js +6 -2
  80. package/node_modules/@openclaw/fs-safe/dist/secret-file.d.ts +1 -0
  81. package/node_modules/@openclaw/fs-safe/dist/secret-file.js +2 -0
  82. package/node_modules/@openclaw/fs-safe/dist/sibling-staged-file.d.ts +1 -0
  83. package/node_modules/@openclaw/fs-safe/dist/sibling-staged-file.js +84 -4
  84. package/node_modules/@openclaw/fs-safe/dist/sibling-temp.d.ts +1 -0
  85. package/node_modules/@openclaw/fs-safe/dist/sibling-temp.js +2 -0
  86. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-acquire.js +22 -15
  87. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-publication.d.ts +22 -0
  88. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-publication.js +303 -0
  89. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-reclaim.d.ts +2 -0
  90. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-reclaim.js +9 -1
  91. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-root.d.ts +8 -0
  92. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-root.js +37 -3
  93. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-staging.d.ts +14 -0
  94. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-staging.js +155 -0
  95. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-stale-admission.js +7 -2
  96. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-write.d.ts +10 -0
  97. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-write.js +143 -0
  98. package/node_modules/@openclaw/fs-safe/dist/temp-target.js +28 -7
  99. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-admission.d.ts +2 -2
  100. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-admission.js +34 -18
  101. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-child-admission.d.ts +1 -1
  102. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-child-admission.js +13 -3
  103. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-ownership.js +1 -1
  104. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-types.d.ts +9 -0
  105. package/node_modules/@openclaw/fs-safe/dist/test-hooks.d.ts +6 -0
  106. package/node_modules/@openclaw/fs-safe/dist/text-atomic.d.ts +1 -1
  107. package/node_modules/@openclaw/fs-safe/dist/text-atomic.js +1 -0
  108. package/node_modules/@openclaw/fs-safe/dist/windows-owner.d.ts +1 -0
  109. package/node_modules/@openclaw/fs-safe/dist/windows-owner.js +2 -1
  110. package/node_modules/@openclaw/fs-safe/dist/windows-security-helper.exe +0 -0
  111. package/node_modules/@openclaw/fs-safe/docs/advanced.md +6 -1
  112. package/node_modules/@openclaw/fs-safe/docs/atomic.md +64 -0
  113. package/node_modules/@openclaw/fs-safe/docs/contributing.md +69 -0
  114. package/node_modules/@openclaw/fs-safe/docs/file-store.md +2 -0
  115. package/node_modules/@openclaw/fs-safe/docs/json-store.md +2 -0
  116. package/node_modules/@openclaw/fs-safe/docs/json.md +2 -0
  117. package/node_modules/@openclaw/fs-safe/docs/output.md +2 -0
  118. package/node_modules/@openclaw/fs-safe/docs/permissions.md +20 -7
  119. package/node_modules/@openclaw/fs-safe/docs/reading.md +24 -9
  120. package/node_modules/@openclaw/fs-safe/docs/root.md +2 -0
  121. package/node_modules/@openclaw/fs-safe/docs/security-model.md +2 -0
  122. package/node_modules/@openclaw/fs-safe/docs/sidecar-lock.md +12 -7
  123. package/node_modules/@openclaw/fs-safe/docs/staged-file.md +2 -0
  124. package/node_modules/@openclaw/fs-safe/docs/temp-isolation-reference.md +73 -0
  125. package/node_modules/@openclaw/fs-safe/docs/temp.md +65 -0
  126. package/node_modules/@openclaw/fs-safe/docs/testing.md +1 -0
  127. package/node_modules/@openclaw/fs-safe/docs/writing.md +2 -0
  128. package/node_modules/@openclaw/fs-safe/package.json +25 -20
  129. package/node_modules/@openclaw/fs-safe-darwin-arm64/fs-safe-native.node +0 -0
  130. package/node_modules/@openclaw/fs-safe-darwin-arm64/package.json +1 -1
  131. package/node_modules/@openclaw/fs-safe-darwin-x64/fs-safe-native.node +0 -0
  132. package/node_modules/@openclaw/fs-safe-darwin-x64/package.json +1 -1
  133. package/node_modules/@openclaw/fs-safe-freebsd-arm64/fs-safe-native.node +0 -0
  134. package/node_modules/@openclaw/fs-safe-freebsd-arm64/package.json +1 -1
  135. package/node_modules/@openclaw/fs-safe-freebsd-x64/fs-safe-native.node +0 -0
  136. package/node_modules/@openclaw/fs-safe-freebsd-x64/package.json +1 -1
  137. package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/fs-safe-native.node +0 -0
  138. package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/package.json +1 -1
  139. package/node_modules/@openclaw/fs-safe-linux-arm64-musl/fs-safe-native.node +0 -0
  140. package/node_modules/@openclaw/fs-safe-linux-arm64-musl/package.json +1 -1
  141. package/node_modules/@openclaw/fs-safe-linux-x64-gnu/fs-safe-native.node +0 -0
  142. package/node_modules/@openclaw/fs-safe-linux-x64-gnu/package.json +1 -1
  143. package/node_modules/@openclaw/fs-safe-linux-x64-musl/fs-safe-native.node +0 -0
  144. package/node_modules/@openclaw/fs-safe-linux-x64-musl/package.json +1 -1
  145. package/node_modules/@openclaw/fs-safe-win32-arm64-msvc/fs-safe-native.node +0 -0
  146. package/node_modules/@openclaw/fs-safe-win32-arm64-msvc/package.json +1 -1
  147. package/node_modules/@openclaw/fs-safe-win32-x64-msvc/fs-safe-native.node +0 -0
  148. package/node_modules/@openclaw/fs-safe-win32-x64-msvc/package.json +1 -1
  149. package/package.json +5 -5
@@ -60,7 +60,7 @@ import { FsSafeError } from "./errors.js";
60
60
  import { assertSynchronousCallbackResult, isMutationAuthorityError } from "./mutation-authority.js";
61
61
  import { captureNativeFdClose } from "./native-binding.js";
62
62
  import { replayPinnedInput, writePinnedInput } from "./pinned-write-input.js";
63
- import { nativeStagedFileMode } from "./native-staged-file-metadata.js";
63
+ import { assertNativeStaging, assertStagedFileBasename, nativeStagedFileMode } from "./native-staged-file-metadata.js";
64
64
  import { assertNativeCopyCompleted, createNativeCopyFile } from "./copy-file-input.js";
65
65
  import { assertNativeStagedDirectoryCurrent, openStagedDirectory } from "./staged-directory.js";
66
66
  import { assertFinalSymlinkRejected } from "./root-symlink-policy.js";
@@ -70,24 +70,9 @@ import { noReplaceUnavailable, rememberNoReplaceUnavailable } from "./native-nor
70
70
  import { isFsSafeNativeRequired } from "./native-config.js";
71
71
  import { restoreCopyMetadata } from "./copy-metadata.js";
72
72
  import { annotateReadOnlyOpenError } from "./file-open-error.js";
73
- export function assertNativeStaging(binding) {
74
- if ([
75
- binding.closeOwnedFd,
76
- binding.createStagedFile,
77
- binding.stagedFileMatches,
78
- binding.removeStagedFile,
79
- binding.renameReplaceAsync,
80
- binding.renameNoReplaceAsync,
81
- ].some((fn) => typeof fn !== "function")) {
82
- throw new FsSafeError("helper-unavailable", "native retained-directory staging is unavailable");
83
- }
84
- }
85
- function assertBasename(name, portable) {
86
- if (!name || name === "." || name === ".." || name.includes("/") || name.includes("\0") ||
87
- (portable && /[\\:\u0000-\u001f\u007f-\u009f\u2028\u2029]/u.test(name))) {
88
- throw new FsSafeError("invalid-path", "publication requires one direct-child basename");
89
- }
90
- }
73
+ import { hasErrorCode } from "./file-cleanup.js";
74
+ import { reopenRecoveredNativeFile, assertRecoveredPublication, recoverAfterHiddenRenameSync, shouldPrepareDrvfsRecovery, mayPrepareDrvfsRecoveryAtFd } from "./publication-recovery.js";
75
+ export { assertNativeStaging } from "./native-staged-file-metadata.js";
91
76
  const NOT_PUBLISHED = Object.freeze({ status: "not-published" });
92
77
  class NativeStagedFile {
93
78
  #owner;
@@ -97,6 +82,10 @@ class NativeStagedFile {
97
82
  #verifyMode;
98
83
  #state = { status: "open", publication: NOT_PUBLISHED };
99
84
  #receipt;
85
+ #recoveryIdentity;
86
+ #recoveryContent;
87
+ #recovered = false;
88
+ #prepareRecovery;
100
89
  #publicationTask;
101
90
  #rejectFinalSymlink = false;
102
91
  #unsupportedNoReplace;
@@ -104,14 +93,15 @@ class NativeStagedFile {
104
93
  #fallbackCleanupError;
105
94
  constructor(owner) {
106
95
  const name = owner.name ?? `.fs-safe-${randomUUID()}.tmp`;
107
- assertBasename(name, owner.portableNames !== false);
96
+ assertStagedFileBasename(name, owner.portableNames !== false);
108
97
  this.#closeFd = captureNativeFdClose(owner.binding);
109
98
  this.#owner = { ...owner, name };
110
99
  this.#assertBeforeMutation = owner.assertBeforeMutation;
111
100
  this.#private = owner.permissionPolicy === "private-creation";
112
101
  this.#verifyMode = owner.permissionPolicy !== undefined;
102
+ this.#prepareRecovery = shouldPrepareDrvfsRecovery(owner.allowDrvfsRenameRecovery, owner.directory.identity.dev) &&
103
+ mayPrepareDrvfsRecoveryAtFd(owner.parentFd, owner.directory.identity.dev);
113
104
  }
114
- // Public staging supplies a Node parent; pinned writes supply a native parent.
115
105
  // The supplied closer owns parentFd even on construction or preparation failure.
116
106
  static async create(owner, input, maxBytes) {
117
107
  let staged;
@@ -127,6 +117,9 @@ class NativeStagedFile {
127
117
  }
128
118
  throw error;
129
119
  }
120
+ if (staged.#prepareRecovery && input.kind === "buffer") {
121
+ staged.#recoveryContent = typeof input.data === "string" ? Buffer.from(input.data, input.encoding) : Buffer.from(input.data);
122
+ }
130
123
  await staged.#prepare(input, maxBytes);
131
124
  return staged;
132
125
  }
@@ -137,6 +130,7 @@ class NativeStagedFile {
137
130
  // This owner never escapes; only the internal verifier borrows its fd.
138
131
  const staged = __addDisposableResource(env_1, await NativeStagedFile.create({
139
132
  binding, parentFd, closeParentFd, directory, mode: params.mode, sync: params.sync,
133
+ allowDrvfsRenameRecovery: params.allowDrvfsRenameRecovery,
140
134
  // Public staging uses portable names; existing POSIX writes accept literal names.
141
135
  portableNames: false, assertBeforeMutation: params.assertBeforeMutation,
142
136
  name: exclusive ? params.basename : undefined, strictFileSync: params.strictFileSync,
@@ -225,6 +219,8 @@ class NativeStagedFile {
225
219
  }
226
220
  #assertNamed(name, expectedMode) {
227
221
  const fd = this.#file();
222
+ if (this.#recovered)
223
+ assertRecoveredPublication(fs.fstatSync(fd, { bigint: true }), this.#recoveryIdentity);
228
224
  const mode = !this.#private ? nativeStagedFileMode(this.#owner.binding, this.#owner.parentFd, name, fd, this.#verifyMode ? expectedMode : undefined) : undefined;
229
225
  if (mode !== undefined)
230
226
  return mode;
@@ -348,7 +344,7 @@ class NativeStagedFile {
348
344
  const overwrite = options?.overwrite;
349
345
  try {
350
346
  const state = this.#open();
351
- assertBasename(basename, this.#owner.portableNames !== false);
347
+ assertStagedFileBasename(basename, this.#owner.portableNames !== false);
352
348
  if (basename === this.#owner.name || typeof overwrite !== "boolean") {
353
349
  throw new FsSafeError("invalid-path", "publication needs a distinct basename and explicit overwrite policy");
354
350
  }
@@ -359,6 +355,12 @@ class NativeStagedFile {
359
355
  this.#assertCurrent();
360
356
  assertFinalSymlinkRejected(path.join(this.#owner.directory.realPath, basename), this.#rejectFinalSymlink);
361
357
  }
358
+ if (this.#prepareRecovery) {
359
+ // Apply restrictive modes before exposing the name or releasing its last fd.
360
+ if ((this.#owner.mode & 0o600) !== 0o600)
361
+ fs.fchmodSync(this.#file(), this.#owner.mode);
362
+ this.#recoveryIdentity = fs.fstatSync(this.#file(), { bigint: true });
363
+ }
362
364
  try {
363
365
  if (overwrite) {
364
366
  await this.#owner.binding.renameReplaceAsync(this.#owner.parentFd, this.#owner.name, this.#owner.parentFd, basename);
@@ -403,13 +405,15 @@ class NativeStagedFile {
403
405
  const receipt = Object.freeze({ status: "published", staged: this.receipt, basename, overwrite });
404
406
  this.#open().publication = receipt;
405
407
  onPublished?.(receipt.staged.identity);
406
- const stagedMode = this.#assertNamed(basename, 0o600);
408
+ const stagedMode = this.#publishedMode(basename);
407
409
  this.#assertDirectory();
408
410
  // Keep contents private until the name passes its identity fence. Mode
409
411
  // changes use the owned fd, including for final mode 000.
410
412
  const fd = this.#file();
411
413
  if (this.#owner.mode !== 0o600 || stagedMode !== 0o600)
412
414
  fs.fchmodSync(fd, this.#owner.mode);
415
+ if (this.#recovered)
416
+ this.#recoveryIdentity.mode = (this.#recoveryIdentity.mode & ~4095n) | BigInt(this.#owner.mode);
413
417
  // Content was synced during preparation. A lost chmod leaves 0600, no wider
414
418
  // than modes retaining owner rw; restrictive modes still need a durable correction.
415
419
  const verify = () => {
@@ -427,6 +431,26 @@ class NativeStagedFile {
427
431
  return verify();
428
432
  });
429
433
  }
434
+ #publishedMode(basename) {
435
+ try {
436
+ return this.#assertNamed(basename, this.#recoveryIdentity ? Number(this.#recoveryIdentity.mode & 4095n) : 0o600);
437
+ }
438
+ catch (error) {
439
+ if (!hasErrorCode(error, "ENOENT") || !this.#recoveryIdentity)
440
+ throw error;
441
+ const state = this.#open();
442
+ const fd = this.#file();
443
+ return recoverAfterHiddenRenameSync({
444
+ parentFd: this.#owner.parentFd,
445
+ closeWriter: () => { state.fileFd = undefined; this.#closeFd(fd); this.#assertDirectory(); },
446
+ reopen: () => {
447
+ state.fileFd = reopenRecoveredNativeFile(this.#owner.binding, this.#owner.parentFd, basename, this.#recoveryIdentity, this.#recoveryContent);
448
+ this.#recovered = true;
449
+ return Number(this.#recoveryIdentity.mode & 4095n);
450
+ },
451
+ });
452
+ }
453
+ }
430
454
  #finalize() {
431
455
  if (this.#state.status === "closed") {
432
456
  return this.#state;
@@ -480,7 +504,6 @@ class NativeStagedFile {
480
504
  return this.#state;
481
505
  }
482
506
  async cleanup() {
483
- // Native descriptors stay owned through the rename and all durability fences.
484
507
  if (this.#publicationTask)
485
508
  await this.#publicationTask.catch(() => { });
486
509
  const closed = this.#finalize();
@@ -512,5 +535,6 @@ export async function stageFileInDirectory(options) {
512
535
  return await createNativeStage({
513
536
  binding, parentFd: parent.fd, closeParentFd: fs.closeSync,
514
537
  directory: parent.receipt, mode, permissionPolicy: "mode-only",
538
+ allowDrvfsRenameRecovery: options.allowDrvfsRenameRecovery,
515
539
  }, input);
516
540
  }
@@ -1,7 +1,7 @@
1
1
  import fsSync, {} from "node:fs";
2
2
  import { FsSafeError } from "./errors.js";
3
3
  import { inspectFileIdentity } from "./strict-file-identity.js";
4
- import { isFileObservationFailure, recordFileObservationFailure } from "./file-observation.js";
4
+ import { isFileObservationFailure, observedFilePublicationPending, recordFileObservationFailure } from "./file-observation.js";
5
5
  export function recordFileOpenFailure(error, filePath) {
6
6
  recordFileObservationFailure(error, `open:${filePath}`);
7
7
  throw error;
@@ -16,15 +16,26 @@ export function openedPathResolutionError(error = new FsSafeError("path-mismatch
16
16
  }
17
17
  export async function recordPreOpenFileChange(error, handle, filePath, before, opened) {
18
18
  if (!isFileObservationFailure(error, "identity") || !before?.isFile() || !opened?.isFile() ||
19
- ![0n, 1n].includes(before.nlink) || ![0n, 1n].includes(opened.nlink))
19
+ ![0n, 1n].includes(before.nlink) || ![0n, 1n, 2n].includes(opened.nlink))
20
20
  return;
21
21
  try {
22
22
  await inspectFileIdentity(async () => before);
23
- const current = await inspectFileIdentity(() => fsSync.fstatSync(handle.fd, { bigint: true }), opened);
24
- if (current.isFile() && [0n, 1n].includes(current.nlink) &&
25
- (before.dev !== current.dev || before.ino !== current.ino)) {
23
+ // A fresh exact descriptor distinguishes a completed handoff from a
24
+ // still-linked generation that needs separate publication proof.
25
+ const inspectCurrent = () => inspectFileIdentity(() => process.platform === "win32"
26
+ ? fsSync.fstatSync(handle.fd, { bigint: true }) : handle.stat({ bigint: true }), opened);
27
+ let current = await inspectCurrent();
28
+ let publication = false;
29
+ if (current.isFile() && current.nlink === 2n) {
30
+ publication = await observedFilePublicationPending(filePath, current);
31
+ current = await inspectCurrent();
32
+ }
33
+ if (current.isFile() && (before.dev !== current.dev || before.ino !== current.ino)) {
26
34
  // A stale pathname sample is not proof that its former inode was unlinked.
27
- recordFileObservationFailure(error, `changed:${filePath}`);
35
+ if ([0n, 1n].includes(current.nlink))
36
+ recordFileObservationFailure(error, `changed:${filePath}`);
37
+ else if (current.nlink === 2n && publication)
38
+ recordFileObservationFailure(error, `linked-change:${filePath}`);
28
39
  }
29
40
  }
30
41
  catch {
@@ -36,7 +47,8 @@ export async function recordOpenedFileFailure(error, handle, filePath, identity)
36
47
  !identity.isFile() || identity.nlink > 1n)
37
48
  return;
38
49
  try {
39
- const current = await inspectFileIdentity(() => fsSync.fstatSync(handle.fd, { bigint: true }), identity);
50
+ const current = await inspectFileIdentity(() => process.platform === "win32"
51
+ ? fsSync.fstatSync(handle.fd, { bigint: true }) : handle.stat({ bigint: true }), identity);
40
52
  if (current.isFile() && current.nlink === 0n)
41
53
  recordFileObservationFailure(error, `unlinked:${filePath}`);
42
54
  }
@@ -5,6 +5,7 @@ export type ExternalFileWriteOptions<T = void> = {
5
5
  maxBytes?: number;
6
6
  mode?: number;
7
7
  staging?: "workspace" | "sibling";
8
+ allowDrvfsRenameRecovery?: boolean;
8
9
  /** Isolate sibling producers; workspace staging is already private. */
9
10
  producerIsolation?: "private-directory";
10
11
  fallbackFileName?: string;
@@ -94,6 +94,7 @@ export async function writeExternalFileWithinRoot(options) {
94
94
  fallbackFileName,
95
95
  maxBytes,
96
96
  mode,
97
+ allowDrvfsRenameRecovery: options.allowDrvfsRenameRecovery,
97
98
  });
98
99
  return { path: siblingFinalPath, result };
99
100
  }
@@ -108,6 +109,7 @@ export async function writeExternalFileWithinRoot(options) {
108
109
  mode,
109
110
  mkdir: true,
110
111
  sourceHardlinks: "reject",
112
+ allowDrvfsRenameRecovery: options.allowDrvfsRenameRecovery,
111
113
  });
112
114
  return { path: finalPath, result };
113
115
  }
@@ -139,6 +141,7 @@ async function writeExternalFileViaSibling(params) {
139
141
  mode: params.mode,
140
142
  maxBytes: params.maxBytes,
141
143
  syncTempFile: true,
144
+ allowDrvfsRenameRecovery: params.allowDrvfsRenameRecovery,
142
145
  syncParentDir: true,
143
146
  });
144
147
  return result;
@@ -1,4 +1,5 @@
1
1
  import fs from "node:fs";
2
+ import { recordFileObservationFailure } from "./file-observation.js";
2
3
  import { ROOT_PATH_ALIAS_POLICIES, resolveRootPath, } from "./root-path.js";
3
4
  import { isNotFoundPathError } from "./path.js";
4
5
  import { shortPath } from "./error-detail.js";
@@ -44,6 +45,8 @@ export async function assertNoHardlinkedFinalPath(params) {
44
45
  return;
45
46
  }
46
47
  if (stat.nlink > 1) {
47
- throw new Error(`Hardlinked path is not allowed under ${boundaryLabel} (${shortPath(root)}): ${shortPath(filePath)}`);
48
+ const error = new Error(`Hardlinked path is not allowed under ${boundaryLabel} (${shortPath(root)}): ${shortPath(filePath)}`);
49
+ recordFileObservationFailure(error, "hardlink");
50
+ throw error;
48
51
  }
49
52
  }
@@ -5,7 +5,7 @@ import { executePermissionCommand } from "./permission-exec.js";
5
5
  import { safeStat } from "./permissions.js";
6
6
  import { normalizeLowercaseStringOrEmpty } from "./string-coerce.js";
7
7
  import { resolveWindowsSystemCommand } from "./windows-command.js";
8
- import { inspectWindowsOwner } from "./windows-owner.js";
8
+ import { inspectWindowsOwner, TRUSTED_INSTALLER_SID } from "./windows-owner.js";
9
9
  import { assertWindowsAclRemediationAllowed } from "./windows-appcontainer.js";
10
10
  import { hasWindowsPathAlias } from "./windows-path-alias.js";
11
11
  const INHERIT_FLAGS = new Set(["I", "OI", "CI", "IO", "NP"]);
@@ -25,7 +25,7 @@ const SID_RE = /^\*?s-\d+-\d+(-\d+)+$/i;
25
25
  const TRUSTED_SIDS = new Set([
26
26
  "s-1-5-18",
27
27
  "s-1-5-32-544",
28
- "s-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464",
28
+ TRUSTED_INSTALLER_SID,
29
29
  ]);
30
30
  const WORLD_SIDS = new Set(["s-1-1-0", "s-1-5-11", "s-1-5-32-545", "s-1-5-7", "s-1-5-32-546", "s-1-5-4", "s-1-2-0", "s-1-5-2"]);
31
31
  const STATUS_PREFIXES = [
@@ -46,24 +46,39 @@ function inspectWindowsPermissionsNative(targetPath, unverified) {
46
46
  if (!native)
47
47
  return undefined;
48
48
  const facts = native.readOwnerAndDacl(targetPath);
49
- if (facts.fallbackRequired) {
49
+ if (facts.fallbackRequired || !facts.isLocal || !facts.aceListComplete || facts.unsupportedAceTypes.length !== 0) {
50
50
  if (facts.currentAppContainerSid) {
51
51
  throw new FsSafeError("helper-unavailable", "AppContainer ACL facts require an unavailable external helper");
52
52
  }
53
53
  return undefined;
54
54
  }
55
+ // Native coarse flags also serve secure descriptors' stricter policy.
56
+ // Public reporting classifies the observations with the fallback's policy.
57
+ const acl = summarizeWindowsOwnerAcl({
58
+ sid: facts.ownerSid,
59
+ daclPresent: facts.daclPresent,
60
+ currentUserSid: facts.currentUserSid,
61
+ currentAppContainerSid: facts.currentAppContainerSid ?? undefined,
62
+ aces: facts.aces.map(ace => ({
63
+ sid: ace.sid, mask: ace.mask, deny: ace.aceType === "deny",
64
+ inheritOnly: ace.flags.inheritOnly,
65
+ })),
66
+ });
67
+ if (!acl.ok)
68
+ return { ...unverified, error: acl.error };
69
+ const worldWritable = acl.untrustedWorld.some(entry => entry.canWrite);
70
+ const groupWritable = acl.untrustedGroup.some(entry => entry.canWrite);
71
+ const worldReadable = acl.untrustedWorld.some(entry => entry.canRead);
72
+ const groupReadable = acl.untrustedGroup.some(entry => entry.canRead);
55
73
  return {
56
74
  ...unverified,
57
75
  source: "windows-acl",
58
- worldWritable: facts.worldWritable,
59
- groupWritable: facts.groupWritable,
60
- worldReadable: facts.worldReadable,
61
- groupReadable: facts.groupReadable,
76
+ worldWritable, groupWritable, worldReadable, groupReadable,
62
77
  ownerSid: facts.ownerSid,
63
- ownerTrusted: facts.ownerClass !== "foreign",
78
+ ownerTrusted: facts.ownerClass !== "foreign" || normalizeSid(facts.ownerSid) === TRUSTED_INSTALLER_SID,
64
79
  aclSummary: `native owner=${facts.ownerClass} world=` +
65
- `${facts.worldReadable ? "r" : "-"}${facts.worldWritable ? "w" : "-"} ` +
66
- `group=${facts.groupReadable ? "r" : "-"}${facts.groupWritable ? "w" : "-"}`,
80
+ `${worldReadable ? "r" : "-"}${worldWritable ? "w" : "-"} ` +
81
+ `group=${groupReadable ? "r" : "-"}${groupWritable ? "w" : "-"}`,
67
82
  };
68
83
  }
69
84
  export async function inspectWindowsPermissions(targetPath, unverified, opts) {
@@ -252,6 +267,16 @@ function summarizeWindowsOwnerAcl(owner) {
252
267
  };
253
268
  });
254
269
  const summary = summarizeWindowsAcl(entries, { USERSID: owner.currentUserSid });
270
+ // Admit serviced sources, but not user-owned or user-writable profiles.
271
+ const ownerSid = normalizeSid(owner.sid ?? "");
272
+ const serviceOwned = ownerSid === TRUSTED_INSTALLER_SID || ownerSid === "s-1-5-18" || ownerSid === "s-1-5-32-544";
273
+ const userWritable = entries.some(entry => entry.canWrite &&
274
+ (entry.sid === owner.currentUserSid || entry.sid === owner.currentAppContainerSid));
275
+ if (!serviceOwned || userWritable) {
276
+ const servicing = summary.trusted.filter(entry => entry.sid === TRUSTED_INSTALLER_SID);
277
+ summary.untrustedGroup.push(...servicing);
278
+ summary.trusted = summary.trusted.filter(entry => entry.sid !== TRUSTED_INSTALLER_SID);
279
+ }
255
280
  if (owner.currentAppContainerSid) {
256
281
  const own = summary.untrustedGroup.filter(entry => entry.sid === owner.currentAppContainerSid);
257
282
  summary.trusted.push(...own);
@@ -1,4 +1,5 @@
1
1
  import fs from "node:fs";
2
+ import type { OwnedFileDescriptorSync } from "./create-owned-file.js";
2
3
  export type PinnedOpenSyncFailureReason = "path" | "validation" | "io";
3
4
  export type PinnedOpenSyncResult = {
4
5
  ok: true;
@@ -6,6 +7,7 @@ export type PinnedOpenSyncResult = {
6
7
  fd: number;
7
8
  stat: fs.Stats;
8
9
  identity: fs.BigIntStats;
10
+ close?: () => void;
9
11
  } | {
10
12
  ok: false;
11
13
  reason: PinnedOpenSyncFailureReason;
@@ -18,7 +20,7 @@ export type PinnedOpenSyncFinalAdmission = (params: {
18
20
  }) => string;
19
21
  export type PinnedOpenSyncFs = Pick<typeof fs, "constants" | "lstatSync" | "realpathSync" | "openSync" | "fstatSync" | "closeSync">;
20
22
  export declare function isExpectedPathError(error: unknown): boolean;
21
- export declare function openPinnedFileSync(params: {
23
+ type PinnedOpenParams = {
22
24
  filePath: string;
23
25
  resolvedPath?: string;
24
26
  rejectPathSymlink?: boolean;
@@ -27,6 +29,11 @@ export declare function openPinnedFileSync(params: {
27
29
  maxBytes?: number;
28
30
  allowedType?: PinnedOpenSyncAllowedType;
29
31
  ioFs?: PinnedOpenSyncFs;
30
- admittedFd?: number;
32
+ admittedFile?: OwnedFileDescriptorSync;
33
+ finalAdmission?: (params: Parameters<PinnedOpenSyncFinalAdmission>[0]) => string | Promise<string>;
34
+ };
35
+ export declare function openPinnedFileSync(params: PinnedOpenParams & {
31
36
  finalAdmission?: PinnedOpenSyncFinalAdmission;
32
37
  }): PinnedOpenSyncResult;
38
+ export declare function openPinnedFile(params: PinnedOpenParams): Promise<PinnedOpenSyncResult>;
39
+ export {};
@@ -1,28 +1,42 @@
1
1
  import { numberStats } from "./exact-identity.js";
2
2
  import fs from "node:fs";
3
+ import asyncFs from "node:fs/promises";
4
+ import { promisify } from "node:util";
5
+ import { runAsync, runSync, wait } from "./atomic-io.js";
3
6
  import { isUnsafeDeviceReadPath } from "./device-path.js";
4
7
  import { FsSafeError } from "./errors.js";
5
- import { inspectFileIdentitySync } from "./strict-file-identity.js";
8
+ import { inspectFileIdentity, inspectFileIdentitySync } from "./strict-file-identity.js";
6
9
  import { resolveReadOpenFlags } from "./read-open-flags.js";
7
10
  import { assertNoWindowsPathAlias } from "./windows-path-alias.js";
8
11
  import { realpathSync } from "./realpath.js";
12
+ const openFd = promisify(fs.open);
13
+ const statFd = promisify(fs.fstat);
14
+ const closeFd = promisify(fs.close);
9
15
  export function isExpectedPathError(error) {
10
16
  const code = typeof error === "object" && error !== null && "code" in error ? String(error.code) : "";
11
17
  return code === "ENOENT" || code === "ENOTDIR" || code === "ELOOP";
12
18
  }
13
19
  export function openPinnedFileSync(params) {
20
+ return runSync(openPinnedFileProcedure(params, false));
21
+ }
22
+ export function openPinnedFile(params) {
23
+ return runAsync(openPinnedFileProcedure(params, true));
24
+ }
25
+ function* openPinnedFileProcedure(params, asynchronous) {
14
26
  const filePath = params.filePath;
15
27
  const resolvedPath = params.resolvedPath;
16
28
  const ioFs = params.ioFs ?? fs;
17
29
  const allowedType = params.allowedType ?? "file";
18
30
  const rejectPathSymlink = params.rejectPathSymlink === true;
31
+ const finalAdmission = params.finalAdmission;
19
32
  const statPolicy = {
20
33
  rejectHardlinks: params.rejectHardlinks,
21
34
  rejectLeafSymlinks: params.rejectLeafSymlinks,
22
35
  maxBytes: params.maxBytes,
23
36
  };
24
37
  const openReadFlags = resolveReadOpenFlags({ constants: ioFs.constants });
25
- let fd = params.admittedFd ?? null;
38
+ const admittedFile = params.admittedFile;
39
+ let fd = admittedFile?.fd ?? null;
26
40
  try {
27
41
  assertNoWindowsPathAlias(filePath, "filesystem", "file path uses a Windows filesystem namespace alias");
28
42
  if (resolvedPath !== undefined) {
@@ -32,39 +46,39 @@ export function openPinnedFileSync(params) {
32
46
  return { ok: false, reason: "validation" };
33
47
  }
34
48
  if (rejectPathSymlink) {
35
- const candidateStat = ioFs.lstatSync(filePath);
49
+ const candidateStat = asynchronous ? yield* wait(asyncFs.lstat(filePath)) : ioFs.lstatSync(filePath);
36
50
  if (candidateStat.isSymbolicLink()) {
37
51
  return { ok: false, reason: "validation" };
38
52
  }
39
53
  }
40
- const realPath = resolvedPath ??
41
- (ioFs === fs ? realpathSync(filePath) : ioFs.realpathSync(filePath));
54
+ const realPath = resolvedPath ?? (asynchronous ? yield* wait(asyncFs.realpath(filePath)) :
55
+ (ioFs === fs ? realpathSync(filePath) : ioFs.realpathSync(filePath)));
42
56
  assertNoWindowsPathAlias(realPath, "filesystem", "resolved path uses a Windows filesystem namespace alias");
43
57
  if (isUnsafeDeviceReadPath(realPath)) {
44
58
  return { ok: false, reason: "validation" };
45
59
  }
46
- const preOpenStat = inspectFileIdentitySync(() => {
47
- const stat = ioFs.lstatSync(realPath, { bigint: true });
48
- assertAllowedStat(stat, allowedType, statPolicy);
49
- return stat;
50
- });
51
- fd ??= ioFs.openSync(realPath, openReadFlags);
52
- const identity = inspectFileIdentitySync(() => {
53
- const stat = ioFs.fstatSync(fd, { bigint: true });
60
+ const admitStat = (stat) => {
54
61
  assertAllowedStat(stat, allowedType, statPolicy);
55
62
  return stat;
56
- }, preOpenStat);
57
- inspectFileIdentitySync(() => {
58
- const stat = ioFs.lstatSync(realPath, { bigint: true });
59
- assertAllowedStat(stat, allowedType, statPolicy);
60
- return stat;
61
- }, identity);
62
- const admittedPath = params.finalAdmission?.({
63
+ };
64
+ function* inspectPath(expected) {
65
+ return asynchronous
66
+ ? yield* wait(inspectFileIdentity(async () => admitStat(await asyncFs.lstat(realPath, { bigint: true })), expected))
67
+ : inspectFileIdentitySync(() => admitStat(ioFs.lstatSync(realPath, { bigint: true })), expected);
68
+ }
69
+ const preOpenStat = yield* inspectPath();
70
+ fd ??= asynchronous ? yield* wait(openFd(realPath, openReadFlags)) : ioFs.openSync(realPath, openReadFlags);
71
+ const identity = asynchronous
72
+ ? yield* wait(inspectFileIdentity(async () => admitStat(await statFd(fd, { bigint: true })), preOpenStat))
73
+ : inspectFileIdentitySync(() => admitStat(ioFs.fstatSync(fd, { bigint: true })), preOpenStat);
74
+ yield* inspectPath(identity);
75
+ const admittedPath = finalAdmission ? yield* wait(finalAdmission({
63
76
  path: realPath,
64
77
  descriptorIdentity: identity,
65
- }) ?? realPath;
78
+ })) : realPath;
66
79
  const openedStat = numberStats(identity);
67
- const opened = { ok: true, path: admittedPath, fd, stat: openedStat, identity };
80
+ const opened = { ok: true, path: admittedPath, fd, stat: openedStat, identity,
81
+ ...(admittedFile ? { close: admittedFile.close } : {}) };
68
82
  fd = null;
69
83
  return opened;
70
84
  }
@@ -79,7 +93,12 @@ export function openPinnedFileSync(params) {
79
93
  finally {
80
94
  if (fd !== null) {
81
95
  try {
82
- ioFs.closeSync(fd);
96
+ if (admittedFile)
97
+ admittedFile.close();
98
+ else if (asynchronous)
99
+ yield* wait(closeFd(fd));
100
+ else
101
+ ioFs.closeSync(fd);
83
102
  }
84
103
  catch {
85
104
  // Preserve the failed admission result; success transfers fd above.