RFR: 8306882: (fs) Path.toRealPath(LinkOption.NOFOLLOW_LINKS) fails when "../../" follows a link
Brian Burkhalter
bpb at openjdk.org
Tue Aug 29 19:57:12 UTC 2023
On Wed, 23 Aug 2023 01:09:16 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:
> Modify `Path.toRealPath` such that it does not collapse links such as "link/.." or "link/../.." when `LinkOption.NOFOLLOW_LINKS` is specified or, in some cases, not.
> I think that [the Unix implementation] needs a summary in the first place to understand why that change is needed.
I can add a comment in the source, but here's some illustrative behavior using `jshell`. It uses some files as:
./dir/subdir
./aaa@ -> dir/subdir
./out.txt
and attempts to convert `aaa/../../out.txt` to a real path.
**master**
jshell> Path p = Path.of("aaa/../../out.txt")
p ==> aaa/../../out.txt
jshell> p.toRealPath()
$2 ==> /Users/bpb/dev/bugs/jdk/Path-toRealPath-8306882/out.txt
jshell> p.toRealPath(LinkOption.NOFOLLOW_LINKS)
| Exception java.nio.file.NoSuchFileException: /Users/bpb/dev/bugs/jdk/Path-toRealPath-8306882/aaa/out.txt
| at UnixException.translateToIOException (UnixException.java:92)
| at UnixException.rethrowAsIOException (UnixException.java:106)
| at UnixException.rethrowAsIOException (UnixException.java:111)
| at UnixPath.toRealPath (UnixPath.java:927)
| at (#3:1)
As can be seen, when attempting to resolve the path without following links, the result is a non-existent file.
**patch**
jshell> Path p = Path.of("aaa/../../out.txt")
p ==> aaa/../../out.txt
jshell> p.toRealPath()
$2 ==> /Users/bpb/dev/bugs/jdk/Path-toRealPath-8306882/out.txt
jshell> p.toRealPath(LinkOption.NOFOLLOW_LINKS)
$3 ==> /Users/bpb/dev/bugs/jdk/Path-toRealPath-8306882/aaa/../../out.txt
Here the correct result is obtained.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/15397#issuecomment-1698040599
More information about the nio-dev
mailing list