Skip to content

Commit

Permalink
Merge pull request #8452 from edolstra/fix-relative-symlinks
Browse files Browse the repository at this point in the history
SourcePath::resolveSymlinks(): Fix handling of symlinks that start with '..'
  • Loading branch information
edolstra committed Jun 6, 2023
2 parents 9c6ede8 + f5c6b29 commit f5ab40c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/libfetchers/input-accessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,28 @@ SourcePath SourcePath::resolveSymlinks() const

int linksAllowed = 1024;

for (auto & component : path) {
res.path.push(component);
while (true) {
if (auto st = res.maybeLstat()) {
std::list<std::string> todo;
for (auto & c : path)
todo.push_back(std::string(c));

while (!todo.empty()) {
auto c = *todo.begin();
todo.pop_front();
if (c == "" || c == ".")
;
else if (c == "..")
res.path.pop();
else {
res.path.push(c);
if (auto st = res.maybeLstat(); st && st->type == InputAccessor::tSymlink) {
if (!linksAllowed--)
throw Error("infinite symlink recursion in path '%s'", path);
if (st->type != InputAccessor::tSymlink) break;
auto target = res.readLink();
res.path.pop();
if (hasPrefix(target, "/"))
res = CanonPath(target);
else {
res.path.pop();
res.path.extend(CanonPath(target));
}
} else
break;
res.path = CanonPath::root;
todo.splice(todo.begin(), tokenizeString<std::list<std::string>>(target, "/"));
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/eval.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ nix-instantiate --eval -E 'assert 1 + 2 == 3; true'
# Check that symlink cycles don't cause a hang.
ln -sfn cycle.nix $TEST_ROOT/cycle.nix
(! nix eval --file $TEST_ROOT/cycle.nix)

# Check that relative symlinks are resolved correctly.
mkdir -p $TEST_ROOT/xyzzy $TEST_ROOT/foo
ln -sfn ../xyzzy $TEST_ROOT/foo/bar
printf 123 > $TEST_ROOT/xyzzy/default.nix
[[ $(nix eval --impure --expr "import $TEST_ROOT/foo/bar") = 123 ]]

0 comments on commit f5ab40c

Please sign in to comment.