Skip to content

fix(glob): prevent directory symlink cycles during traversal - #19622

Merged
fdncred merged 8 commits into
nushell:mainfrom
manavkdubey:fix/glob-ancestor-cycles-8736
Nov 21, 2026
Merged

fdncred merged 8 commits into
nushell:mainfrom
manavkdubey:fix/glob-ancestor-cycles-8736

Conversation

@manavkdubey

@manavkdubey manavkdubey commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #8736. Related to #15218.

Recursive globbing could follow a directory symlink back to an ancestor and walk the same subtree repeatedly. For example, ls **/sources.list could return the same file through progressively longer paths.

Both glob backends now skip descent when a directory link leads back to an ancestor and continue searching sibling directories. The link itself can still match, and separate aliases to the same target still produce their own paths.

The review follow-up limits cycle checks to patterns containing **, preserving fixed-depth matches such as ls */up/*.txt. Both walkers defer directory identity lookups until they follow a directory link. Sibling links share the parent lookup and extend the ancestor chain independently. The ls cycle tests for both backends and the glob alias test now run on Windows as well as Unix.

User-facing changes (Release notes)

Recursive globbing now skips directory symlink cycles and continues searching other directories.

Additional notes

Release benchmarks

I compared Cargo release builds on macOS arm64. The timings are median wall times and include Nushell startup.

  • One symlink loop. ls -a **/sources.list | length went from 33 matches to one. Legacy glob went from 15.0 to 10.1 ms, and dc-glob went from 12.5 to 9.3 ms.
  • Two symlink loops. Both patched backends returned all seven expected entries in about 10 ms. The upstream runs hit the wall-time or CPU limit before returning a count.
  • 256 links sharing a parent. Legacy glob took 21.6 ms on upstream, 25.9 ms on the earlier local revision, and 22.1 ms with the shared lookup. The siblings now reuse one parent chain. Each link still checks its target against that chain so cycles are pruned and valid aliases can be traversed independently.
  • 256 links under separate parents. Legacy glob took 20.0 ms on upstream, 20.1 ms on the previously published PR, and 27.7 ms with this follow-up. For dc-glob, the times were 15.9, 18.1, and 24.0 ms. All builds returned 257 matches. Deferring identity lookups avoids opening an extra identity handle for each directory on link-free Windows walks. In this fixture, each link has a separate parent, so the walkers repeatedly resolve the common ancestors. The slowdown was measured on macOS. The Windows timing impact still needs measurement.
  • Ordinary tree with 4,680 files. The two-thread ls medians were within about 1.4 ms of upstream. With one Rayon thread, dc-glob took about 1–2 ms longer across the two patterns tested. The result sets matched. The source of that smaller timing difference is still uncertain.

Compatibility and validation

One mixed-pattern case remains open. With target/up pointing back to target, ls -a */up/**/marker.txt can lose target/up/marker.txt. This reproduces on both the previously published PR revision and the follow-up. I'd appreciate your take on whether to handle that case here or in a separate matcher change.

On macOS, 13,319 workspace tests and 242 doctests passed, along with formatting, both Clippy checks, Typos, and the WASM check and build. Native Windows and Linux verification of the follow-up is pending.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review overview

🟡 Changes recommended

The ** cycle fix works on both backends. With two links back to the walk root, ls -a **/*.txt returns the 2 real files where main loops, and aliases to the same directory still list. Two things are left. The check also drops matches from patterns without **, and the per-directory id lookup is expensive on Windows.

Findings: 1 High severity · 1 Medium severity · 1 Low severity

Open (3)

Notes from my clanker, checked on 26ec3d5. Built the branch in debug and release and ran each case in a probe script on the branch binary and on main at ca70639, legacy and dc-glob, on macOS. I trial-patched the legacy fill_todo change from the high finding and ran cargo test -p nu-glob --lib with it. The Windows numbers come from a standalone timing loop, not from a nu build. CI ran clippy and the tests on macOS and Windows, Linux was still queued, and I didn't rerun them.

Comment thread crates/nu-glob/src/lib.rs Resolved
Comment thread crates/nu-glob/src/ancestors.rs Resolved
Comment thread crates/nu-command/tests/commands/ls.rs Resolved
@manavkdubey

manavkdubey commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review @fdncred . I pushed a follow-up for all three threads. Both backends now return the expected paths in your fixed-depth examples, directory identity lookups are deferred until a link is followed, and the public cycle tests are enabled on Windows.

I also rechecked the release timings. Sharing the parent lookup brought the sibling-link fixture close to upstream. The fixture with links under separate parents still shows a slowdown, so I included those numbers in the description along with the remaining */up/**/marker.txt case.

I'd appreciate another look when you have a chance, particularly your take on whether the mixed-pattern case should be handled in this PR.

@manavkdubey
manavkdubey force-pushed the fix/glob-ancestor-cycles-8736 branch from dc9ccf6 to 2a84496 Compare November 20, 2026 21:13
@manavkdubey
manavkdubey requested a review from fdncred November 20, 2026 21:16
@fdncred

fdncred commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Seems like the unresolved one above still needs some work. The mixed */up/** case should be fixed in this PR if possible.

@BTF-Kabir-2020

Copy link
Copy Markdown

I took a closer look at the symlink handling here, because the cycle guard looked like it could affect more than just recursive traversal.

Both backends behave the same. On base f6e23c4, */up/**/marker.txt with target/up -> . returns target/up/marker.txt followed by repeated paths like up/up/up/.../marker.txt (63 on Windows, 40 on Linux). On head 2a84496, it returns nothing. The control pattern */child/**/marker.txt, with no symlink, still returns the expected file on both revisions and both backends.

The repeated base results are all the same file. The important difference is that base finds the valid target/up/marker.txt path, while head drops it.

I reproduced this on Windows using junctions, since creating real symlinks required elevated privileges in my environment. I then verified it in Docker using real Unix symlinks on overlayfs, where directory identity is based on (dev, ino). I also ran the Linux cases through the APIs Nushell actually uses: glob_with_parent for legacy and dc_glob::glob_with with GlobWalkOptions::nu_glob for dc-glob. The results were the same.

I instrumented Ancestors to see where the match gets dropped. For target/up, from_parents(link=target\up, count=2) seeds the chain with target and . before ** is reached. Then enter(target/up) resolves to the same directory identity as target, so it returns None and the walker never opens the link. This uses same_file::Handle on Windows and (dev, ino) on Linux. dc-glob follows the same path with absolute paths: it seeds [<root>, <root>/target], then enter(<root>/target/up) resolves to the same directory identity as <root>/target.

The cycle guard still catches cycles encountered inside **. I tested this separately with target/child/back pointing to the absolute path of target, using */child/**/back/**/marker.txt. Head exits cleanly with zero results on both backends, and the trace shows a collision on back. Base repeats paths like child/back/child/back/.../marker.txt on Linux and times out or overflows the stack on Windows.

I wouldn't count the combined up + back case as proof of deeper cycle detection, since up is already pruned before the walk gets anywhere near back.

I'm not proposing a fix here. Simply moving from_parents until recursive matching starts may not be enough — the chain still needs the identity of the directory recursion starts from.

Is the collision on a fixed-depth symlink intentional too, or did the guard end up covering more than the recursive case?

@manavkdubey

manavkdubey commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @fdncred and @BTF-Kabir-2020. I pushed a follow-up that fixes the mixed-pattern case in this PR. The cycle check now applies only inside a ** walk, and each ** starts tracking from the directory it begins in. Explicit components never prune, so */up/*.txt still returns the same paths as main, and a link matched by an explicit component can start a new recursive walk.

With target/up -> ., ls -a */up/**/marker.txt now returns target/up/marker.txt once on both backends. */child/**/back/**/marker.txt returns target/child/back/marker.txt, and with back/back -> back, **/back/**/marker.txt returns back/marker.txt and back/back/marker.txt.

@BTF-Kabir-2020 Thanks for tracing this. The collision on the fixed-depth link wasn't intended. The guard now covers only the recursive part, and each ** starts its chain at the directory it begins in, so target/up/up is still pruned while target/up/marker.txt is returned. Your */child/**/back/**/marker.txt case now returns target/child/back/marker.txt on both backends.

On macOS, fmt, typos, clippy and the nu-glob, ls, glob and open tests pass.

I'd appreciate another look when you have a chance.

@manavkdubey

Copy link
Copy Markdown
Contributor Author

Seems like the unresolved one above still needs some work. The mixed /up/* case should be fixed in this PR if possible.

The cycle check now only runs inside a ** walk, so fixed-depth patterns like /up/.txt and ////*.txt return the same paths as main on both backends.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review overview

🟡 Changes recommended

All three earlier threads are fixed and resolved. One timing nit is left: links under separate parents now cost a lookup per directory between the link and the walk root.

Findings: 0 High severity · 0 Medium severity · 1 Low severity

Open (1)

Notes from my clanker, checked on 019a349. Release builds of main (ca70639) and the branch, timed with hyperfine, waiting for an idle CPU before each run. Some dc-glob runs saw load during the run, but three tries agreed. Not timed on Windows.

Comment thread crates/nu-glob/src/lib.rs Resolved
@manavkdubey
manavkdubey requested a review from fdncred November 21, 2026 18:57
@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Nov 21, 2026
@fdncred
fdncred merged commit d5b8ab0 into nushell:main Nov 21, 2026
15 checks passed
@fdncred

fdncred commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Circular directories result in infinite loop when recursive pattern is used

3 participants