Skip to content

fix(realpath): reject non-directory path components and forcing them to ENOTDIR - #16153

Open
Martell0x1 wants to merge 9 commits into
uutils:mainfrom
Martell0x1:fix/realpath-parent-dir-not-directory
Open

Martell0x1 wants to merge 9 commits into
uutils:mainfrom
Martell0x1:fix/realpath-parent-dir-not-directory

Conversation

@Martell0x1

@Martell0x1 Martell0x1 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Problem

uutils realpath incorrectly accepts paths containing a regular file followed by ...

For example, /tmp/example/file/.. succeeds in uutils, whereas GNU realpath fails with ENOTDIR (Not a directory).

The implementation removes the preceding component when encountering ParentDir without verifying that the component is a directory.

Root Cause

In src/uucore/src/lib/features/fs.rs, the ParentDir branch performs:

OwningComponent::ParentDir => {
    result.pop();
}

This removes the preceding component without validating whether it can be traversed as a directory.

Fix

Validate path components before resolving ParentDir, rejecting non-directory and nonexistent intermediate components where required by the selected resolution mode.

Tests

  • Added regression tests covering invalid file/parent-directory traversal and valid directory/parent-directory traversal.
  • Tested against the realpath feature:
cargo test --no-default-features --features realpath
  • Manually verified behavior using regular files, directories, and symlinks.

Requires #16161
Closes #16117

Copilot AI balanced review requested due to automatic review settings November 3, 2026 01:46

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codspeed

codspeed Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 0.56%

2 warnings, 1 note

⚠️ 120 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

⚡ 1 improved benchmark
❌ 1 regressed benchmark
✅ 399 untouched benchmarks
⏩ 70 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ CPU Simulation mv_multiple_to_dir 22.4 ms 24.9 ms -10.09%
⚡ CPU Simulation ls_recursive_wide_tree[(10000, 1000)] 73.5 ms 66.8 ms +9.98%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Martell0x1:fix/realpath-parent-dir-not-directory (d115f09) with main (88707ee)2

Open in CodSpeed

Footnotes

  1. 70 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (4465bcc) during the generation of this report, so 88707ee was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@Martell0x1
Martell0x1 force-pushed the fix/realpath-parent-dir-not-directory branch from 2f1fa3d to 3cc5e18 Compare November 3, 2026 09:12
Copilot AI balanced review requested due to automatic review settings November 3, 2026 09:12

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Martell0x1

Copy link
Copy Markdown
Contributor Author

@codspeedbot explain why performance improved

Comment thread src/uucore/src/lib/features/fs.rs Outdated
OwningComponent::CurDir => {}
OwningComponent::ParentDir => {
// checking for current component
if !result.as_os_str().is_empty() {

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.

this also runs with MissingHandling::Missing, so realpath -m missing/.. and -m file/.. fail now, GNU accepts both
please skip the check in that mode and add a -m test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ignored the check for MissingHandler::Missing

Comment thread src/uucore/src/lib/features/fs.rs Outdated
Ok(meta) if meta.is_dir() => {}
Ok(_) => {
return Err(Error::new(ErrorKind::NotADirectory, "Not a directory"));
}

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.

why rebuild a NotFound error? Err(e) => return Err(e) is enough

Comment thread src/uucore/src/lib/features/fs.rs Outdated
OwningComponent::CurDir => {}
OwningComponent::ParentDir => {
// checking for current component
if !result.as_os_str().is_empty() {

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.

could you please reuse ensure_is_directory(&result)? here? it already gives ENOTDIR, without the hardcoded strings

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread tests/by-util/test_realpath.rs Outdated

// spell-checker:ignore nusr

use uucore::util_name;

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.

util_name is already imported from uutests, does this compile?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

actually it did , i guess as rust allows importing the same macro's name but from different paths

.ucmd()
.arg("regular_file/..")
.fails()
.code_is(1)

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.

the issue also mentions -L, -e and readlink -f, please add tests for them
-L still goes through normalize_path, so it isn't fixed yet

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes i totaly forgot to add Logical path support .

Comment thread tests/by-util/test_realpath.rs Outdated

at.mkdir("directory");

scene.ucmd().arg("directory/..").succeeds();

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.

please check the stdout too, not only the exit code

Copilot AI balanced review requested due to automatic review settings November 3, 2026 14:26

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Martell0x1
Martell0x1 requested a review from sylvestre November 3, 2026 14:31
@Martell0x1
Martell0x1 force-pushed the fix/realpath-parent-dir-not-directory branch from e7c8a6b to 8fbaeb1 Compare November 3, 2026 14:39
Copilot AI balanced review requested due to automatic review settings November 3, 2026 14:39

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for looking into this!
The new tests hardcode "Not a directory", so they will fail on Windows. Please use a platform const like the one in test_readlink.rs.
The failing tests should also check .no_stdout().

Comment thread src/uucore/src/lib/features/fs.rs Outdated
}
}

fn validate_parent_compoenent(path: &Path) -> IOResult<()> {

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.

typo: compoenent
and could you please add a one-line doc? the name doesn't say it checks for ..

Comment thread tests/by-util/test_readlink.rs Outdated
.arg("regular_file/..")
.fails()
.code_is(1)
.stderr_contains("Not a directory");

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.

please use NOT_A_DIRECTORY, it is already defined at the top of the file for windows

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

okay

.ucmd()
.arg("directory/..")
.succeeds()
.stdout_is(format!("{}\n", at.root_dir_resolved()));

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.

the other tests using root_dir_resolved() have #[cfg_attr(wasi_runner, ignore = ...)], these ones probably need it too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread tests/by-util/test_realpath.rs Outdated
.arg("regular_file/..")
.fails()
.code_is(1)
.stderr_contains("Not a directory");

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.

this fails on windows, the message there is "The directory name is invalid."
please use a NOT_A_DIRECTORY const like in test_readlink.rs (same for the -L and -e tests)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Copilot AI balanced review requested due to automatic review settings November 3, 2026 15:52

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Martell0x1
Martell0x1 force-pushed the fix/realpath-parent-dir-not-directory branch from 97f51ff to 1a8c9ac Compare November 3, 2026 15:52
Copilot AI balanced review requested due to automatic review settings November 3, 2026 15:52

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Martell0x1

Copy link
Copy Markdown
Contributor Author

@sylvestre
Thanks for the feedback! I've addressed both suggestions:

  • Replaced the hardcoded "Not a directory" message with the existing platform-aware NOT_A_DIRECTORY constant in the default, -L, and -e regression tests. This accounts for the different error messages returned on Windows and Unix.
  • Added the wasi_runner ignore attributes to the tests that use root_dir_resolved(), following the existing convention in the test file because host paths may not be visible in the WASI sandbox.

Thanks for pointing these out!

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Copilot AI balanced review requested due to automatic review settings November 3, 2026 21:02

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Comment thread src/uucore/src/lib/features/fs.rs Outdated
#[cfg(windows)]
use std::os::windows::io::AsRawHandle;
use std::path::{Component, MAIN_SEPARATOR, Path, PathBuf};
use std::path::{Component, Path, PathBuf, MAIN_SEPARATOR};

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.

the last commit reformatted unrelated code with an old rustfmt style (imports, the 's'/'S' lines below)
please revert those hunks, the workspace is edition 2024, cargo fmt --all will put them back

Comment thread src/uucore/src/lib/features/fs.rs Outdated
}
OwningComponent::CurDir => {}
OwningComponent::ParentDir => {
// checking for current component

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.

this comment doesn't add anything, please remove it

OwningComponent::CurDir => {}
OwningComponent::ParentDir => {
// checking for current component
if !result.as_os_str().is_empty() && miss_mode != MissingHandling::Missing {

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.

this also runs with -s (ResolveMode::None), so realpath -s file/.. and -s missing/.. change too
could you please check what GNU does and add a -s test?

#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")]
fn test_realpath_directory_followed_by_parent_dir_missing_mode() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

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.

please also test -m regular_file/.., it should succeed too

Copilot AI balanced review requested due to automatic review settings November 3, 2026 23:12

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Martell0x1
Martell0x1 requested a review from sylvestre November 3, 2026 23:12
@Martell0x1

Copy link
Copy Markdown
Contributor Author

@sylvestre
I've addressed the requested changes. Sorry for the back-and-forth, and thank you for your patience!

I'm still relatively new to open source, so I'm learning a lot through reviews like yours. I really appreciate you taking the time to point out these details and help me improve.

@Martell0x1
Martell0x1 force-pushed the fix/realpath-parent-dir-not-directory branch from bc37014 to ebdec2c Compare November 3, 2026 23:22
Copilot AI balanced review requested due to automatic review settings November 3, 2026 23:22

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Copilot AI balanced review requested due to automatic review settings November 4, 2026 10:54

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Copilot AI balanced review requested due to automatic review settings November 4, 2026 10:56

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Martell0x1

Copy link
Copy Markdown
Contributor Author

Hi @sylvestre
I guess the CI status is green , kindly review my latest edits

@Martell0x1

Copy link
Copy Markdown
Contributor Author

Hi @sylvestre , Any Updates on this PR ?

This branch has not been deployed

No deployments
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.

realpath: "file/.." resolves lexically instead of failing with ENOTDIR

3 participants