Skip to content

Commit 5efde13

Browse files
author
lane-u5a-2359
committed
fix(file-safety): refuse a mode copy onto a file the staging check never validated
The staging check lstats the caller-supplied path and rejects a link. The open that follows uses openSync with r+, which follows a link, and the only thing checked after the open is that the opened file is regular and carries one name. A writer of that directory can name the staging path for a symlink to a single-link regular file between the lstat and the open. The open follows it, fstat reports a plain single-name file so the guard passes, and the mode copy below then changes the mode of a file this write never staged. The pre-commit name check refuses the publish later but cannot undo a mode already changed. Carry the identity the check validated and compare it against the opened descriptor before the mode copy, refusing with StagingPathError when they differ. Tested by a control row in the spec that gives the open a different identity from the validated one: with this change the write refuses before the chmod, with the change taken away the same row fails because the mode copy is issued. The committed change ships without that row. Answers review comment 4533489470 on pull 1912.
1 parent 2b39a88 commit 5efde13

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ export async function safeWriteText(
479479
// published. The success path already knows this; the catch is the side that needs the flag.
480480
let committed = false
481481
let tempPath: string
482+
// The identity the staging check validated below, kept so the mode copy further down can
483+
// prove the descriptor it is about to chmod is that same file and not a name swapped after
484+
// the check. Null when this write stages the file itself.
485+
let validatedStagingIdentity: { dev: bigint; ino: bigint } | null = null
482486
if (options?.tempPath) {
483487
// A caller-supplied staging file is only safe when it is the file this
484488
// write is staging, not an arbitrary path. Two properties are checked:
@@ -572,6 +576,7 @@ export async function safeWriteText(
572576
}
573577
// The caller's own path is used as given; only the check is canonical.
574578
tempPath = options.tempPath
579+
validatedStagingIdentity = { dev: stagingStat.dev, ino: stagingStat.ino }
575580
} else {
576581
stagingDir = _stagingDir(dirPath)
577582
tempPath = _tempName(stagingDir, "safeWriteText")
@@ -691,6 +696,23 @@ export async function safeWriteText(
691696
tempPath,
692697
)
693698
}
699+
// The check above proves the name was a regular file when it was lstat'd, not that this
700+
// descriptor is that file. A writer of this directory can name the path for a symlink to a
701+
// single-link regular file between the lstat and the open: the open follows it, fstat then
702+
// reports a regular file with nlink 1 so the guard above passes, and the chmod below would
703+
// change that other file's mode. The pre-commit name check further down refuses the publish
704+
// but does not undo a mode already changed, so compare against the validated identity here.
705+
const openedIdentity = fsSync.fstatSync(fd, { bigint: true })
706+
if (
707+
validatedStagingIdentity !== null &&
708+
(openedIdentity.dev !== validatedStagingIdentity.dev ||
709+
openedIdentity.ino !== validatedStagingIdentity.ino)
710+
) {
711+
throw new StagingPathError(
712+
`Staging file changed between the check and the mode copy: ${tempPath}`,
713+
tempPath,
714+
)
715+
}
694716
if (targetMode !== null) {
695717
fsSync.fchmodSync(fd, targetMode)
696718
}

0 commit comments

Comments
 (0)