Commit 6f99488
authored
chore(policy): harden loadPresetFromFile against oversize and symlinked inputs (#3020)
## Summary
Three follow-up hardening items from PR #2077 review against
`loadPresetFromFile` in `src/lib/policies.ts`: a 10 MB file-size guard,
symlink rejection via `O_NOFOLLOW`, and tmp-dir cleanup in the
corresponding test block. The size-and-symlink checks now use an atomic
`openSync(O_RDONLY | O_NOFOLLOW) → fstatSync → readFileSync(fd)` pattern
(per CodeRabbit review), so there is no TOCTOU window between the stat
and the read and the kernel rejects symbolic links at open time.
## Related Issue
Closes #2521
## Changes
- Add `MAX_PRESET_FILE_BYTES = 10_000_000` and reject files larger than
the limit before reading their contents.
- Replace the prior `existsSync` + `statSync` pair with an atomic
`openSync(O_RDONLY | O_NOFOLLOW)` followed by `fstatSync` on the
descriptor and `readFileSync(fd, "utf-8")`, with a `try/finally`
`closeSync` on every early-return path. Symbolic links are rejected via
`ELOOP` from `openSync` and surfaced with a clear error pointing the
user at `realpath`.
- Track and clean up `mkdtempSync` directories created by the `writeTmp`
helper inside the `loadPresetFromFile` describe block via `afterEach`.
- Add tests covering the new oversize-file (>10 MB) and
symlink-rejection paths.
## Type of Change
- [x] Code change (feature, bug fix, or refactor)
- [ ] Code change with doc updates
- [ ] Doc only (prose changes, no code sample modifications)
- [ ] Doc only (includes code sample changes)
## Verification
- [x] `npx prek run --all-files` passes
- [x] `npm test` passes
- [x] Tests added or updated for new or changed behavior
- [x] No secrets, API keys, or credentials committed
- [ ] Docs updated for user-facing behavior changes
- [ ] `make docs` builds without warnings (doc changes only)
- [ ] Doc pages follow the [style
guide](https://github.com/NVIDIA/NemoClaw/blob/main/docs/CONTRIBUTING.md)
(doc changes only)
- [ ] New doc pages include SPDX header and frontmatter (new pages only)
---
Signed-off-by: Tinson Lai <tinsonl@nvidia.com>
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Enhanced preset file validation with strict extension checking and a
10MB file size limit.
* Improved security by rejecting symbolic links and enforcing stricter
file access and permission checks, with clearer error messages for
invalid files.
* **Tests**
* Added tests covering preset file size rejection and symbolic link
rejection scenarios.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Signed-off-by: Tinson Lai <tinsonl@nvidia.com>1 parent 60a282d commit 6f99488
2 files changed
Lines changed: 93 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
| |||
617 | 619 | | |
618 | 620 | | |
619 | 621 | | |
620 | | - | |
621 | | - | |
622 | | - | |
623 | | - | |
624 | 622 | | |
625 | 623 | | |
626 | 624 | | |
627 | 625 | | |
628 | | - | |
629 | | - | |
| 626 | + | |
| 627 | + | |
630 | 628 | | |
631 | | - | |
632 | | - | |
| 629 | + | |
633 | 630 | | |
634 | 631 | | |
635 | | - | |
636 | | - | |
637 | | - | |
638 | | - | |
639 | | - | |
640 | | - | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
641 | 645 | | |
642 | 646 | | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
643 | 679 | | |
644 | 680 | | |
645 | 681 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
1278 | 1278 | | |
1279 | 1279 | | |
1280 | 1280 | | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
1281 | 1289 | | |
1282 | 1290 | | |
| 1291 | + | |
1283 | 1292 | | |
1284 | 1293 | | |
1285 | 1294 | | |
| |||
1399 | 1408 | | |
1400 | 1409 | | |
1401 | 1410 | | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
| 1443 | + | |
1402 | 1444 | | |
1403 | 1445 | | |
1404 | 1446 | | |
| |||
0 commit comments