Skip to content

Read validator input on Windows without O_NOFOLLOW - #55

Closed
julian27sg wants to merge 1 commit into
cloudflare:mainfrom
julian27sg:fix/win32-validator-input
Closed

julian27sg wants to merge 1 commit into
cloudflare:mainfrom
julian27sg:fix/win32-validator-input

Conversation

@julian27sg

Copy link
Copy Markdown

Implements the direction proposed in #53. The approach and the analysis there are that issue's; this is the implementation and the measurements.

Problem

Node leaves O_NOFOLLOW and O_NONBLOCK undefined on win32, so readFileWithinLimit refuses every input before reading a byte:

Failed to read findings JSON: OS no-follow and nonblocking input protection is unavailable

Phase 4 validation cannot run natively on Windows at all. #31, #38, #48 and #49 make the failure quieter or better explained; none makes the validators run.

Change

POSIX is untouched: where both constants exist, the original O_RDONLY | O_NOFOLLOW | O_NONBLOCK open runs exactly as before and the new branch is never reached.

On win32:

  1. lstatSync(file, { bigint: true }), reject a symlink, a non-regular file, or ino === 0n.
  2. openSync(file, O_RDONLY).
  3. fstatSync(fd, { bigint: true }), reject unless ino and dev still match step 1.
  4. The existing isFile(), size and UTF-8 checks are unchanged.

Every other platform without the two constants keeps the current hard rejection.

Why bigint

NTFS file IDs exceed 2^53, where adjacent integers share one double:

Number(12666373953348436n) === Number(12666373953348437n)  // true

Adjacent MFT records are the normal case, so a Number-based compare could pass on a swapped file. Bigint stats remove that.

Measured

Node 24.13.1 on Windows 11:

validate-findings.test.cjs         34 tests, 0 fail, 5 skipped
validate-coverage-ledger.test.cjs  31 tests, 0 fail, 7 skipped

The skips are the existing HAS_SAFE_INPUT_OPEN and win32 guards, which this change does not touch.

Four inputs per validator (empty array, top-level object, malformed JSON, partial record) produce byte-identical stdout and exit codes against unmodified upstream running on Linux.

The patched files still pass 34/34 and 31/31 on Linux with no skips, so the POSIX path shows no regression.

What this does not do

  • openSync follows the reparse point before the compare rejects, so the target is opened and never read. This is weaker than O_NOFOLLOW, which refuses the open itself.
  • O_NONBLOCK has no Windows analogue, so a path that blocks on open is not guarded.
  • The win32 branch has no tests of its own. The three tests still skipped on Windows gate on HAS_SAFE_INPUT_OPEN, deliberately left unchanged so this does not collide with Fix CLI test suite hanging failures on platforms without O_NOFOLLOW/O_NONBLOCK #31 and test: guard the remaining CLI tests behind HAS_SAFE_INPUT_OPEN #38. Happy to add swap-detection, ino === 0n and symlink tests if you want them here rather than in a follow-up.
  • FAT32, exFAT and network shares are untested. The ino === 0n check is what keeps those failing closed.

Node leaves O_NOFOLLOW and O_NONBLOCK undefined on win32, so
readFileWithinLimit rejected every input before reading a byte and
neither validator CLI could run there at all.

POSIX is unchanged and still takes the race-free open. Windows now
lstats the path with bigint stats, rejects a symlink, a non-regular file
and an ino of 0n, opens read-only, and rejects the descriptor when ino
or dev no longer match. Bigint matters here: NTFS file IDs exceed 2^53,
where two adjacent IDs share one double and the compare would pass on a
swapped file.

The branch is weaker than O_NOFOLLOW. openSync follows the reparse point
before the compare rejects, so the target is opened and never read, and
O_NONBLOCK has no Windows analogue. Where the file index is unavailable
it fails closed exactly as the code does today.

Refs #53.
@julian27sg julian27sg closed this by deleting the head repository Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant