Skip to content

Commit 8513598

Browse files
committed
Restore 'forbidden in restricted mode' errors
1 parent 3c894f5 commit 8513598

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/libexpr/eval.cc

+10
Original file line numberDiff line numberDiff line change
@@ -3069,6 +3069,11 @@ SourcePath EvalState::findFile(const LookupPath & lookupPath, const std::string_
30693069

30703070
auto res = (r / CanonPath(suffix)).resolveSymlinks();
30713071
if (res.pathExists()) return res;
3072+
3073+
// Backward compatibility hack: throw an exception if access
3074+
// to this path is not allowed.
3075+
if (auto accessor = res.accessor.dynamic_pointer_cast<FilteringSourceAccessor>())
3076+
accessor->checkAccess(res.path);
30723077
}
30733078

30743079
if (hasPrefix(path, "nix/"))
@@ -3139,6 +3144,11 @@ std::optional<SourcePath> EvalState::resolveLookupPathPath(const LookupPath::Pat
31393144
if (path.resolveSymlinks().pathExists())
31403145
return finish(std::move(path));
31413146
else {
3147+
// Backward compatibility hack: throw an exception if access
3148+
// to this path is not allowed.
3149+
if (auto accessor = path.accessor.dynamic_pointer_cast<FilteringSourceAccessor>())
3150+
accessor->checkAccess(path.path);
3151+
31423152
logWarning({
31433153
.msg = HintFmt("Nix search path entry '%1%' does not exist, ignoring", value)
31443154
});

tests/functional/restricted.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ nix-instantiate --restrict-eval ./simple.nix -I src1=./simple.nix -I src2=./conf
2323
(! nix-instantiate --restrict-eval --eval -E 'builtins.readFile ./simple.nix')
2424
nix-instantiate --restrict-eval --eval -E 'builtins.readFile ./simple.nix' -I src=../..
2525

26-
expectStderr 1 nix-instantiate --restrict-eval --eval -E 'let __nixPath = [ { prefix = "foo"; path = ./.; } ]; in builtins.readFile <foo/simple.nix>' #| grepQuiet "forbidden in restricted mode"
26+
expectStderr 1 nix-instantiate --restrict-eval --eval -E 'let __nixPath = [ { prefix = "foo"; path = ./.; } ]; in builtins.readFile <foo/simple.nix>' | grepQuiet "forbidden in restricted mode"
2727
nix-instantiate --restrict-eval --eval -E 'let __nixPath = [ { prefix = "foo"; path = ./.; } ]; in builtins.readFile <foo/simple.nix>' -I src=.
2828

2929
p=$(nix eval --raw --expr "builtins.fetchurl file://${_NIX_TEST_SOURCE_DIR}/restricted.sh" --impure --restrict-eval --allowed-uris "file://${_NIX_TEST_SOURCE_DIR}")
@@ -53,9 +53,9 @@ mkdir -p $TEST_ROOT/tunnel.d $TEST_ROOT/foo2
5353
ln -sfn .. $TEST_ROOT/tunnel.d/tunnel
5454
echo foo > $TEST_ROOT/bar
5555

56-
expectStderr 1 nix-instantiate --restrict-eval --eval -E "let __nixPath = [ { prefix = \"foo\"; path = $TEST_ROOT/tunnel.d; } ]; in builtins.readFile <foo/tunnel/bar>" -I $TEST_ROOT/tunnel.d #| grepQuiet "forbidden in restricted mode"
56+
expectStderr 1 nix-instantiate --restrict-eval --eval -E "let __nixPath = [ { prefix = \"foo\"; path = $TEST_ROOT/tunnel.d; } ]; in builtins.readFile <foo/tunnel/bar>" -I $TEST_ROOT/tunnel.d | grepQuiet "forbidden in restricted mode"
5757

58-
expectStderr 1 nix-instantiate --restrict-eval --eval -E "let __nixPath = [ { prefix = \"foo\"; path = $TEST_ROOT/tunnel.d; } ]; in builtins.readDir <foo/tunnel/foo2>" -I $TEST_ROOT/tunnel.d #| grepQuiet "forbidden in restricted mode"
58+
expectStderr 1 nix-instantiate --restrict-eval --eval -E "let __nixPath = [ { prefix = \"foo\"; path = $TEST_ROOT/tunnel.d; } ]; in builtins.readDir <foo/tunnel/foo2>" -I $TEST_ROOT/tunnel.d | grepQuiet "forbidden in restricted mode"
5959

6060
# Reading the parents of allowed paths should show only the ancestors of the allowed paths.
6161
[[ $(nix-instantiate --restrict-eval --eval -E "let __nixPath = [ { prefix = \"foo\"; path = $TEST_ROOT/tunnel.d; } ]; in builtins.readDir <foo/tunnel>" -I $TEST_ROOT/tunnel.d) == '{ "tunnel.d" = "directory"; }' ]]

0 commit comments

Comments
 (0)