Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix symlink handling #9363

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libexpr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ std::string showType(const Value & v);
/**
* If `path` refers to a directory, then append "/default.nix".
*/
SourcePath resolveExprPath(const SourcePath & path);
SourcePath resolveExprPath(SourcePath path);

struct InvalidPathError : EvalError
{
Expand Down
18 changes: 13 additions & 5 deletions src/libexpr/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -686,17 +686,25 @@ Expr * EvalState::parse(
}


SourcePath resolveExprPath(const SourcePath & path)
SourcePath resolveExprPath(SourcePath path)
{
unsigned int followCount = 0, maxFollow = 1024;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
unsigned int followCount = 0, maxFollow = 1024;
unsigned int followCount = 0, maxFollow = 1000;

1024 would suggest some base-2 exponential behavior.


/* If `path' is a symlink, follow it. This is so that relative
path references work. */
auto path2 = path.resolveSymlinks();
while (true) {
// Basic cycle/depth limit to avoid infinite loops.
if (++followCount >= maxFollow)
throw Error("too many symbolic links encountered while traversing the path '%s'", path);
Copy link
Contributor

Choose a reason for hiding this comment

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

We may want to say what limit it ran into?

Suggested change
throw Error("too many symbolic links encountered while traversing the path '%s'", path);
throw Error("too many symbolic links encountered while traversing the path '%s' (limit is %d)", path, maxFollow);

Copy link
Member Author

Choose a reason for hiding this comment

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

Nah, if you have more than 1024 symlinks, it's almost certainly a cycle.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then let's be explicit about intent, then users will know what to do with the error:

Suggested change
throw Error("too many symbolic links encountered while traversing the path '%s'", path);
throw Error("more than %d symbolic links encountered while traversing the path '%s' (there is probably a cycle)", path, maxFollow);

Copy link
Member

Choose a reason for hiding this comment

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

coreutils:

$ ln -s bar foo
$ ln -s foo bar
$ cat foo
cat: foo: Too many levels of symbolic links

Of course coreutils is kinda bad with error messages, but we can align with it for a consistent user experience and improve on it.

Suggested change
throw Error("too many symbolic links encountered while traversing the path '%s'", path);
throw Error("too many levels of symbolic links while traversing the path '%s'; assuming it's part of a cycle after %d indirections", originalPath, maxFollows);

It would also be slightly more helpful to show the original path instead of an arbitrary path within the cycle, in case it's not the cycle that's the mistake, but rather the use of the cycle. Also note that the original symlink does not even have to be part of the cycle; it only has to lead to it.

if (path.lstat().type != InputAccessor::tSymlink) break;
path = {path.accessor, CanonPath(path.readLink(), path.path.parent().value_or(CanonPath::root))};
}
Copy link
Member

Choose a reason for hiding this comment

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

So if I understand correctly, this is a "shallow" version of resolveSymlinks that only resolves the final path component?

In any case, this code should have a name and not be in the parser. Could you put this in a method like SourcePath::followSymlink perhaps?

I haven't had enough coffee yet to really understand the subtlety between the two methods yet, so if you could describe that in the doc comments, that will probably even help those who aren't caffeine deprived.


/* If `path' refers to a directory, append `/default.nix'. */
if (path2.lstat().type == InputAccessor::tDirectory)
return path2 + "default.nix";
if (path.lstat().type == InputAccessor::tDirectory)
return path + "default.nix";

return path2;
return path;
}


Expand Down
1 change: 1 addition & 0 deletions tests/functional/lang/eval-okay-symlink-resolution.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"test"
1 change: 1 addition & 0 deletions tests/functional/lang/eval-okay-symlink-resolution.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import symlink-resolution/foo/overlays/overlay.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"test"
1 change: 1 addition & 0 deletions tests/functional/lang/symlink-resolution/foo/overlays
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import ../lib