Skip to content

Commit addec3c

Browse files
committed
Restore 'forbidden in restricted mode' errors
1 parent f63dc71 commit addec3c

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/libexpr/eval.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,6 +3087,11 @@ SourcePath EvalState::findFile(const LookupPath & lookupPath, const std::string_
30873087

30883088
auto res = (r / CanonPath(suffix)).resolveSymlinks();
30893089
if (res.pathExists()) return res;
3090+
3091+
// Backward compatibility hack: throw an exception if access
3092+
// to this path is not allowed.
3093+
if (auto accessor = res.accessor.dynamic_pointer_cast<FilteringSourceAccessor>())
3094+
accessor->checkAccess(res.path);
30903095
}
30913096

30923097
if (hasPrefix(path, "nix/"))
@@ -3157,6 +3162,11 @@ std::optional<SourcePath> EvalState::resolveLookupPathPath(const LookupPath::Pat
31573162
if (path.resolveSymlinks().pathExists())
31583163
return finish(std::move(path));
31593164
else {
3165+
// Backward compatibility hack: throw an exception if access
3166+
// to this path is not allowed.
3167+
if (auto accessor = path.accessor.dynamic_pointer_cast<FilteringSourceAccessor>())
3168+
accessor->checkAccess(path.path);
3169+
31603170
logWarning({
31613171
.msg = HintFmt("Nix search path entry '%1%' does not exist, ignoring", value)
31623172
});

tests/functional/restricted.sh

Lines changed: 3 additions & 3 deletions
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)