Skip to content

Commit 3624e4f

Browse files
fix(file-safety): refuse a captured-DACL publish over a symbolic link before the commit
The link-backup guard left a hole: when the DACL was captured (daclDumpPath set) and the publish target is the link path itself, the step 4 rename would replace the LINK with a regular file, and the DACL restore would then apply the saved ACEs to the wrong object. The refusal now sits exactly where the link is detected, before the rename: a new DaclPublishRefusedError (exported with the DaclCaptureError/DaclRestoreError family, same shape) states that the target is unchanged. DaclCaptureError does not describe this (the capture succeeded) and DaclRestoreError does not (nothing was committed, so nothing was rolled back). Regression test in the win32 DACL suite: capture succeeds (icacls /save), lstat reports a symbolic link, publishOverLink set -> the write rejects with DaclPublishRefusedError, no commit rename, and neither /restore nor the restrictive fallback command runs. Negative controls: removing the refusal reddens exactly the new test; refusing unconditionally reddens exactly the no-DACL linux symlink test (which still publishes with no backup). Linux symlink replacement and regular-file backup behavior are unchanged. 83 spec tests green, eslint clean, prettier clean, tsc adds nothing beyond the pre-existing local donor-package drift.
1 parent 9980ac7 commit 3624e4f

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎src/services/file-safety/__tests__/safeWriteText.spec.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as path from "path"
77

88
import {
99
DaclCaptureError,
10+
DaclPublishRefusedError,
1011
DaclRestoreError,
1112
PostCommitDurabilityError,
1213
resolveLockKey,
@@ -743,6 +744,30 @@ describe("safeWriteText", () => {
743744
expect(execFile).toHaveBeenCalledTimes(1)
744745
})
745746

747+
it("win32: a captured-DACL publish over a symbolic link is refused before the commit rename", async () => {
748+
const targetPath = "/tmp/test-dir/link.json"
749+
vi.mocked(fs.realpath).mockResolvedValue(targetPath)
750+
vi.mocked(fsSync.openSync).mockReturnValue(1)
751+
// The publish target is the link path itself, and it is a symbolic link.
752+
vi.mocked(fs.lstat).mockResolvedValue(_fileStats(true))
753+
754+
// The capture itself succeeded (default execFile mock), so this is not a
755+
// DaclCaptureError; nothing was committed, so no rollback and no
756+
// DaclRestoreError either. The refusal is its own contract.
757+
await expect(
758+
safeWriteText(targetPath, "data", { backup: true, publishOverLink: true, platform: "win32" }),
759+
).rejects.toThrow(DaclPublishRefusedError)
760+
761+
// No commit rename, and the refusal prevents the post-commit ACL steps:
762+
// neither /restore nor the restrictive fallback (drop inheritance + grant) ran.
763+
expect(fs.rename).not.toHaveBeenCalled()
764+
const aclArgs = vi.mocked(execFile).mock.calls.map((call) => String(call[1]))
765+
expect(aclArgs.some((a) => a.includes("/restore"))).toBe(false)
766+
expect(aclArgs.some((a) => a.includes("/inheritance"))).toBe(false)
767+
// Only the /save capture ran.
768+
expect(execFile).toHaveBeenCalledTimes(1)
769+
})
770+
746771
it("win32: refuses the write without an onWarning notice when the DACL cannot be saved", async () => {
747772
const targetPath = "/tmp/test-dir/target.txt"
748773
vi.mocked(fs.realpath).mockResolvedValue(targetPath)

‎src/services/file-safety/safeWriteText.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,27 @@ export class DaclRestoreError extends Error {
133133
}
134134
}
135135

136+
/**
137+
* Thrown when a captured-DACL publish targets a symbolic link: the saved ACEs describe the
138+
* link path, but the commit rename replaces the LINK itself with a regular file, so restoring
139+
* the DACL would apply one object's access rights to another. The write is refused before the
140+
* commit rename - the target link is unchanged, and neither a restore nor the restrictive
141+
* fallback is attempted. DaclCaptureError does not fit (the capture succeeded) and
142+
* DaclRestoreError does not fit (nothing was committed, so nothing was rolled back).
143+
*/
144+
export class DaclPublishRefusedError extends Error {
145+
readonly targetPath: string
146+
147+
constructor(targetPath: string, cause?: unknown) {
148+
super(
149+
"Refusing to publish over a symbolic link with a captured DACL: the commit rename would replace the link itself, and the saved access rights would be applied to the wrong object. The target is unchanged; the path is reported here.",
150+
{ cause },
151+
)
152+
this.name = "DaclPublishRefusedError"
153+
this.targetPath = targetPath
154+
}
155+
}
156+
136157
// -- helpers ---------------------------------------------------------------
137158

138159
/** Generate a unique temp file name in the given directory. */
@@ -771,6 +792,12 @@ export async function safeWriteText(
771792
throw backupError
772793
}
773794
releaseBackupOnSuccess = true
795+
} else if (daclDumpPath !== null) {
796+
// The DACL was captured (so DaclCaptureError does not describe this) and
797+
// nothing has been committed (so DaclRestoreError, which reports a failed
798+
// rollback, would be wrong too). Refuse here - before the step 4 rename -
799+
// and the link keeps both its content and its access rights.
800+
throw new DaclPublishRefusedError(targetPath)
774801
}
775802
} catch (err: unknown) {
776803
if (errorCode(err) !== "ENOENT") throw err

0 commit comments

Comments
 (0)