Skip to content

fix(freeze): canonicalize non-existent subdirectories, intermediate symlinks, and tilde paths in check-freeze.sh - #3624

Closed
Quinnod345 wants to merge 1 commit into
garrytan:mainfrom
Quinnod345:fix/check-freeze-nonexistent-subdir-and-tilde
Closed

Quinnod345 wants to merge 1 commit into
garrytan:mainfrom
Quinnod345:fix/check-freeze-nonexistent-subdir-and-tilde

Conversation

@Quinnod345

@Quinnod345 Quinnod345 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3623

Why (in your own words)

/freeze is supposed to keep edits inside one directory, but _resolve_path in freeze/bin/check-freeze.sh only canonicalizes the parent directory when it already exists. When a Write targets a directory that does not exist yet, the raw string goes to the prefix check, so <boundary>/../outside/new-dir/x, <boundary>/out-link/new-dir/x (where out-link points outside) and <boundary>/.. are all allowed. The same gap falsely denies a new subdirectory when the boundary sits under a symlinked parent like macOS /tmp. Separately, file_path never gets the ~ expansion that the freeze dir gets, so ~/.zshrc is joined onto cwd and allowed whenever cwd is inside the boundary.

This PR makes _resolve_path walk the path one component at a time (collapsing ./.. and following symlinks at any depth, same 40-hop cap), then run pwd -P on the deepest directory that exists and reattach the rest. It also expands a leading ~ on file_path and swaps sed 's|/\+|/|g' for sed 's|//*|/|g', because BSD sed reads \+ as a literal +.

Live evidence

Script: a boundary at $T/home/proj/src ($T is the physical temp dir, since /tmp itself is a symlink on macOS), an out-link symlink inside it pointing at $T/outside, and HOME=$T/home. Each line is the hook's decision for a Write to that path.

# main (b61744517)
allow  $T/home/proj/src/../../../outside/new-dir/secret.ts  (cwd $T)
allow  $T/home/proj/src/out-link/new-dir/secret.ts  (cwd $T)
allow  $T/home/proj/src/..  (cwd $T)
allow  ~/.zshrc  (cwd $T/home/proj/src)
deny   ~/proj/src/index.ts  (cwd $T)
deny   /tmp/fz/home/proj/src/new-dir/Component.tsx  (cwd $T)

# this PR
deny   $T/home/proj/src/../../../outside/new-dir/secret.ts  (cwd $T)
deny   $T/home/proj/src/out-link/new-dir/secret.ts  (cwd $T)
deny   $T/home/proj/src/..  (cwd $T)
deny   ~/.zshrc  (cwd $T/home/proj/src)
allow  ~/proj/src/index.ts  (cwd $T)
allow  /tmp/fz/home/proj/src/new-dir/Component.tsx  (cwd $T)

The last line is the boundary saved as /tmp/fz/home/proj/src, so it only reproduces on macOS. BSD sed on macOS 27.2:

$ printf 'a//b/+c/\n' | /usr/bin/sed 's|/\+|/|g;s|/$||'
a//b/c
$ printf 'a//b/+c/\n' | /usr/bin/sed 's|//*|/|g;s|/$||'
a/b/+c

Tests:

$ bun test test/hook-scripts.test.ts test/investigate-freeze-path.test.ts
 315 pass
 2 skip
 0 fail
 888 expect() calls

The 3 new tests in test/hook-scripts.test.ts fail on main (3 of 3 new tests fail; running both suites against main's freeze/bin/check-freeze.sh on macOS gives 310 pass, 2 skip, 5 fail) and pass here. The 2 skips are the existing Windows-only tests.

Scope

  • Changed: _resolve_path and the ~ expansion / sed line in freeze/bin/check-freeze.sh, plus 3 tests in test/hook-scripts.test.ts. The resolved paths now go into _RESOLVED_FILE / _RESOLVED_FREEZE, so the deny message shows the path as the tool sent it (after ~ expansion) instead of the resolved one.
  • Verified live by: the script above and the bun run above, on macOS 27.2 (arm64), bash 3.2.
  • Did NOT test: Windows or Linux. The new symlink test is skipped on win32 like the existing one; the .. and ~ tests are not, so windows-free-tests will be their first Windows run.

Liveness proof (required for external contributors)

Checklist

@trunk-io

trunk-io Bot commented Oct 29, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@capy-ai

capy-ai Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Thank you for this fix. A crew lane is picking it up to land on current main with Co-authored-by credit to you.

@capy-ai

capy-ai Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Thanks @Quinnod345. Your idea of walking every path component is what shipped. We rewrote it on current main as #3758 so it also fails closed on symlink loops, long link chains and Windows path forms. That merged as 510dd07 in v1.92.9.0 and credits you with Co-authored-by. Closing this one as superseded.

@capy-ai capy-ai Bot closed this Nov 1, 2026
Quinnod345 added a commit to Quinnod345/ponder that referenced this pull request Nov 1, 2026
@Quinnod345

Copy link
Copy Markdown
Contributor Author

Thanks @capy-ai! Glad the path traversal approach helped secure the freeze boundary, and appreciate the Co-authored-by credit in v1.92.9.0!

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.

bug(freeze): check-freeze.sh allows boundary escape when target parent directory does not exist and mis-resolves tilde paths

1 participant