2024-05-28 19:43:04 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2016-04-14 16:24:06 +03:00
|
|
|
source common.sh
|
|
|
|
|
2024-06-16 18:56:50 +03:00
|
|
|
clearStoreIfPossible
|
2016-04-14 16:24:06 +03:00
|
|
|
|
2017-11-20 18:50:49 +02:00
|
|
|
nix-instantiate --restrict-eval --eval -E '1 + 2'
|
2022-12-07 13:58:58 +02:00
|
|
|
(! nix-instantiate --eval --restrict-eval ./restricted.nix)
|
2018-01-16 19:50:38 +02:00
|
|
|
(! nix-instantiate --eval --restrict-eval <(echo '1 + 2'))
|
2017-11-20 18:50:49 +02:00
|
|
|
nix-instantiate --restrict-eval ./simple.nix -I src=.
|
|
|
|
nix-instantiate --restrict-eval ./simple.nix -I src1=simple.nix -I src2=config.nix -I src3=./simple.builder.sh
|
2016-04-14 16:24:06 +03:00
|
|
|
|
2017-11-20 18:50:49 +02:00
|
|
|
(! nix-instantiate --restrict-eval --eval -E 'builtins.readFile ./simple.nix')
|
2023-10-05 19:12:18 +03:00
|
|
|
nix-instantiate --restrict-eval --eval -E 'builtins.readFile ./simple.nix' -I src=../..
|
2016-04-14 16:24:06 +03:00
|
|
|
|
2023-10-05 19:12:18 +03:00
|
|
|
(! nix-instantiate --restrict-eval --eval -E 'builtins.readDir ../../src/nix-channel')
|
|
|
|
nix-instantiate --restrict-eval --eval -E 'builtins.readDir ../../src/nix-channel' -I src=../../src
|
2016-04-14 16:24:06 +03:00
|
|
|
|
2023-12-06 15:05:32 +02:00
|
|
|
expectStderr 1 nix-instantiate --restrict-eval --eval -E 'let __nixPath = [ { prefix = "foo"; path = ./.; } ]; in builtins.readFile <foo/simple.nix>' | grepQuiet "forbidden in restricted mode"
|
2023-11-30 17:16:17 +02:00
|
|
|
nix-instantiate --restrict-eval --eval -E 'let __nixPath = [ { prefix = "foo"; path = ./.; } ]; in builtins.readFile <foo/simple.nix>' -I src=.
|
2016-04-14 16:24:06 +03:00
|
|
|
|
2019-11-27 01:05:30 +02:00
|
|
|
p=$(nix eval --raw --expr "builtins.fetchurl file://$(pwd)/restricted.sh" --impure --restrict-eval --allowed-uris "file://$(pwd)")
|
2017-10-30 13:39:59 +02:00
|
|
|
cmp $p restricted.sh
|
|
|
|
|
2019-11-27 01:05:30 +02:00
|
|
|
(! nix eval --raw --expr "builtins.fetchurl file://$(pwd)/restricted.sh" --impure --restrict-eval)
|
2017-10-30 13:39:59 +02:00
|
|
|
|
2019-11-27 01:05:30 +02:00
|
|
|
(! nix eval --raw --expr "builtins.fetchurl file://$(pwd)/restricted.sh" --impure --restrict-eval --allowed-uris "file://$(pwd)/restricted.sh/")
|
2017-10-30 13:39:59 +02:00
|
|
|
|
2019-11-27 01:05:30 +02:00
|
|
|
nix eval --raw --expr "builtins.fetchurl file://$(pwd)/restricted.sh" --impure --restrict-eval --allowed-uris "file://$(pwd)/restricted.sh"
|
2017-10-30 13:39:59 +02:00
|
|
|
|
2019-11-27 01:05:30 +02:00
|
|
|
(! nix eval --raw --expr "builtins.fetchurl https://github.com/NixOS/patchelf/archive/master.tar.gz" --impure --restrict-eval)
|
|
|
|
(! nix eval --raw --expr "builtins.fetchTarball https://github.com/NixOS/patchelf/archive/master.tar.gz" --impure --restrict-eval)
|
|
|
|
(! nix eval --raw --expr "fetchGit git://github.com/NixOS/patchelf.git" --impure --restrict-eval)
|
2018-01-16 19:50:38 +02:00
|
|
|
|
|
|
|
ln -sfn $(pwd)/restricted.nix $TEST_ROOT/restricted.nix
|
|
|
|
[[ $(nix-instantiate --eval $TEST_ROOT/restricted.nix) == 3 ]]
|
|
|
|
(! nix-instantiate --eval --restrict-eval $TEST_ROOT/restricted.nix)
|
|
|
|
(! nix-instantiate --eval --restrict-eval $TEST_ROOT/restricted.nix -I $TEST_ROOT)
|
|
|
|
(! nix-instantiate --eval --restrict-eval $TEST_ROOT/restricted.nix -I .)
|
|
|
|
nix-instantiate --eval --restrict-eval $TEST_ROOT/restricted.nix -I $TEST_ROOT -I .
|
2018-02-06 16:38:45 +02:00
|
|
|
|
2019-11-27 01:05:30 +02:00
|
|
|
[[ $(nix eval --raw --impure --restrict-eval -I . --expr 'builtins.readFile "${import ./simple.nix}/hello"') == 'Hello World!' ]]
|
2018-08-03 07:24:57 +03:00
|
|
|
|
2023-12-05 16:14:28 +02:00
|
|
|
# Check that we can't follow a symlink outside of the allowed paths.
|
2023-12-06 00:02:59 +02:00
|
|
|
mkdir -p $TEST_ROOT/tunnel.d $TEST_ROOT/foo2
|
2023-12-05 16:14:28 +02:00
|
|
|
ln -sfn .. $TEST_ROOT/tunnel.d/tunnel
|
|
|
|
echo foo > $TEST_ROOT/bar
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2023-12-06 00:02:59 +02:00
|
|
|
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"
|
|
|
|
|
|
|
|
# Reading the parents of allowed paths should show only the ancestors of the allowed paths.
|
|
|
|
[[ $(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"; }' ]]
|
2023-12-05 16:14:28 +02:00
|
|
|
|
2018-08-03 07:24:57 +03:00
|
|
|
# Check whether we can leak symlink information through directory traversal.
|
|
|
|
traverseDir="$(pwd)/restricted-traverse-me"
|
|
|
|
ln -sfn "$(pwd)/restricted-secret" "$(pwd)/restricted-innocent"
|
|
|
|
mkdir -p "$traverseDir"
|
|
|
|
goUp="..$(echo "$traverseDir" | sed -e 's,[^/]\+,..,g')"
|
|
|
|
output="$(nix eval --raw --restrict-eval -I "$traverseDir" \
|
2019-11-27 01:05:30 +02:00
|
|
|
--expr "builtins.readFile \"$traverseDir/$goUp$(pwd)/restricted-innocent\"" \
|
2018-08-03 07:24:57 +03:00
|
|
|
2>&1 || :)"
|
|
|
|
echo "$output" | grep "is forbidden"
|
2021-12-09 17:26:46 +02:00
|
|
|
echo "$output" | grepInverse -F restricted-secret
|
2015-09-04 23:23:08 +03:00
|
|
|
|
|
|
|
expectStderr 1 nix-instantiate --restrict-eval true ./dependencies.nix | grepQuiet "forbidden in restricted mode"
|