# Native build-asset reader

The Vite build reads staged evidence and optional engineering-hero assets through `apps/web/read-regular-file.ts`. It invokes `scripts/read_regular_file.py` using `execFileSync` with an argument array and no shell. Python 3.12 or later is already required for dashboard development. `TWIN_PYTHON` selects the interpreter; otherwise the build uses `python` on Windows or `python3` on POSIX. An explicitly configured interpreter failure is an error, with no silent fallback.

## Contract and threat model

The reader rejects a final pathname component that is a symlink, directory or special file. It records the original regular file's identity, acquires a native handle without following that final component, validates the handle's type and identity, and checks the pathname again before reading. A different inode, a removed name, or an observed late symlink is rejected. Both handles close on success and on errors, including a read failure.

The attacker considered here can replace, rename or remove entries in the build's source directory, including creating symlinks and FIFOs. Source-directory ancestry and the interpreter/helper installation belong to the build environment. The reader does not promise an immutable snapshot against writes to the same inode, detect every transient rename history, or create a sandbox for hostile privileged filesystem drivers. Existing release manifests and content hashes retain their separate validation role.

## Platform acquisition

| Platform              | Acquisition and data access                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Linux                 | `O_PATH                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | O_NOFOLLOW`acquires a metadata-only descriptor, including the link itself when a symlink is substituted. After descriptor validation,`/proc/self/fd/<fd>` opens that held object for reading. A missing procfs fails closed; the source pathname is never reopened for data access. |
| Windows               | `CreateFileW` requests only `FILE_READ_ATTRIBUTES`, with `FILE_FLAG_OPEN_REPARSE_POINT` and `FILE_FLAG_BACKUP_SEMANTICS`. CPython's handle-based `fstat` needs that metadata right for its `FileBasicInfo` query; no file-data access is granted. It checks the real file type and identity; reparse points are explicitly rejected. `ReOpenFile` requests read access to the same verified object, not the pathname. CRT descriptors own and close both native handles. |
| macOS and other POSIX | `O_NOFOLLOW                                                                                                                                                                                                                                                                                                                                                                                                                                                              | O_NONBLOCK                                                                                                                                                                                                                                                                          | O_NOCTTY` prevents final-component symlink traversal, FIFO blocking and terminal acquisition. Type and identity checks precede every content read. The data descriptor duplicates the validated descriptor. This route does not claim that opening a device specially planted by a privileged actor has no driver-side effect. |

All routes preserve read-only files and directories. They require neither temporary hard links nor writes beside the source. The bridge preserves arbitrary binary bytes and the Node Buffer size limit; the child-process default 1 MiB output limit is not imposed on media assets. The helper is build tooling only and does not execute inside the browser or change the numerical engine.

## Regression proof

`tests/test_read_regular_file.py` mutates real filesystem entries at native acquisition. It retains the earlier replacement, late same-inode symlink, disappearance, no-content-read and descriptor-closure assertions. Additional cases cover a transient symlink restored before the pathname recheck, replacement during data-handle acquisition, read failures, binary data, read-only source directories and timeout-bounded FIFO substitution. File contents and stat results are not mocked.

The existing Node security tests invoke those native-boundary cases where filesystem acquisition moved into Python. Node integration cases also check raw binary transfer above 1 MiB, filenames containing shell metacharacters and strict interpreter selection. The existing Ubuntu/Windows Python matrix runs the native tests and bridge checks; a focused macOS job exercises its actual POSIX acquisition and byte bridge.

Unprivileged local Windows sessions may report three precise skips when symlink fixture creation raises WinError 1314. Directory, device, missing-file and other acquisition checks still run. GitHub Actions requires symlink capability: the same privilege error fails there, as do all other fixture errors everywhere. No reader validation or host security setting is changed.

Node cases delegated to unittest propagate those skips to the Node test report. Direct Node symlink fixtures still require symlink privileges; this local accommodation does not establish that every engine test can run without them.

## API references

- [Python 3.12 `os` descriptor APIs](https://docs.python.org/3.12/library/os.html)
- [Linux `open(2)`: `O_PATH` and `O_NOFOLLOW`](https://man7.org/linux/man-pages/man2/open.2.html)
- [Microsoft `CreateFileW`](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew)
- [Microsoft `ReOpenFile`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-reopenfile)
- [Microsoft metadata query access requirements](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-ntqueryinformationfile)
- [Apple `open(2)` source documentation](https://github.com/apple-oss-distributions/xnu/blob/main/bsd/man/man2/open.2)
