Repository navigation
fix(store): bound the reader's memory by checking each file's size first - #252
Conversation
walkFiles read every package file in full and compared byteLength with maxFileBytes afterwards, so a package holding an arbitrarily large file was loaded before it was refused. The reader now stats each file and refuses an oversized one from its size, which is what the limit is for; the post-read comparison stays as the guard for a file that grows between the two calls. Observed on main b5abe0b: an import of a 17-byte file with mode 000 and maxFileBytes 8 reported io_error (EACCES), reachable only after the read was attempted. The new case asserts invalid_package naming the limit instead, and skips on Windows and as root, where the mode is not enforced.
Review —
|
|
Owner approval. Approved at Reviewed in this comment. No blocking findings: the size decision now precedes the read, a mutation that removes the At this head: Merging as a merge commit at the exact reviewed head. Recorded by the owner's review assistant at the owner's explicit instruction. |
Summary
Fixes #251. The local store read every package file in full before comparing its size with
maxFileBytes, so the per-file limit bounded the answer but not the memory the reader allocated. The reader nowstats each file and refuses an oversized one from its size; the post-read comparison stays as the guard for a file that grows between the two calls.Problem and expected behavior
walkFilesinsrc/application/TemplateStore.tsdid:On
mainatb5abe0b, a package whosefiles/src/main.ccis 17 bytes, mode000, imported withmaxFileBytes: 8, reported:EACCESis only reachable after the read was attempted, so the size check was not what refused the file. The limit is documented insrc/domain/templatePackage.ts:71as the bound on one file, and the P4 scan (TemplateScan.ts, in #250, not yet onmainatb5abe0b) already stats before reading for the same reason. Expected: the size decides, the buffer is never allocated, and the error names the limit.Change and scope
walkFilesstats each regular file and refusessize > maxFileByteswithinvalid_packagebeforereadFile; the post-read comparison remains and is commented as the growth-between-calls guard.TemplateStore.test.ts, skipped on Windows and as root (where the file mode is not enforced).Not included: changing any store layout, metadata, limits, error codes, or contracts; a bounded
readimplementation for the growth race (the same narrow TOCTOU the scan accepts and documents).Contract and boundary impact
application(src/application/TemplateStore.ts).docs/public/tool-catalog.jsonwas regenerated: none.invalid_packageinstead of anio_errorcaused by reading it; no new code.Expected effects:
Evidence and regression coverage
src/application/TemplateStore.test.ts(16 → 17 in that file).b5abe0b— the case fails withexpected 'io_error' to be 'invalid_package'. The same case passes at934c74d. The defect proof is the error path itself:EACCEScan only be produced by thereadFilecall, so it shows the read happened before the size decision.npm run test:local -- src/application/TemplateStore.test.tsstat), not measured as peak RSS; no test allocates an arbitrarily large buffer.stat-and-readrace is not exercised; the post-read check covers it but the race itself is the same accepted TOCTOU the scan documents.000is not enforced there.For evidence-bearing changes:
unknown, notcomplete.undetermined, not a fabricated yes or no.Validation performed
All commands ran in
/tmp/wt-readboundat934c74d, on Linux.npm run test:local -- src/application/TemplateStore.test.ts— 17 passed (red before the change: 1 failed, 16 passed).npx tsc --noEmit— exit 0.npx oxfmt --checkon the two changed files — "All matched files use the correct format."npx oxlinton the two changed files — exit 0; only the two pre-existingTemplateStore.tswarnings (FileWalkunused, file over 500 lines), unchanged by this PR.npm run check:prwithLIBUVCPP_ROOT=/home/zwy/下载/libuvcpp-1.7.0-linux-x64,PKG_CONFIG_PATH=$LIBUVCPP_ROOT/lib/pkgconfig,LD_LIBRARY_PATH=$LIBUVCPP_ROOT/lib— exit 0; vitest 78 files passed, 2 skipped; 1449 passed, 14 skipped (1463). The 14 skips are the same named lanes (native-probe build cache absent, opt-in published-package lane, Windows-only locators); none is claimed as covered.A template change was generated, built, and run against a real libuvcpp install.
Not applicable (no template or libuvcpp-facing change).
Compatibility, safety, and release
Author readiness
Human owner: @Antruly
Before merge
type(scope): outcome.Fixes #251)