| 1 | # SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | load "../helpers/load" |
| 5 | |
| 6 | INSTANCE=bats |
| 7 | |
| 8 | local_setup_file() { |
| 9 | unset LIMA_SHELLENV_ALLOW |
| 10 | unset LIMA_SHELLENV_BLOCK |
| 11 | } |
| 12 | |
| 13 | local_setup() { |
| 14 | # make sure changes from previous tests are removed |
| 15 | limactl shell "$INSTANCE" sh -c '[ ! -f ~/.bash_profile ] || sed -i -E "/^export (FOO|BAR|SSH_)/d" ~/.bash_profile' |
| 16 | } |
| 17 | |
| 18 | @test 'there are no FOO*, BAR*, or SSH_FOO* variables defined in the VM' { |
| 19 | # just to confirm because the other tests depend on these being unused |
| 20 | run -0 limactl shell "$INSTANCE" printenv |
| 21 | refute_line --regexp '^FOO' |
| 22 | refute_line --regexp '^BAR' |
| 23 | refute_line --regexp '^SSH_FOO' |
| 24 | } |
| 25 | |
| 26 | @test 'environment is not preserved by default' { |
| 27 | export FOO=foo |
| 28 | run -0 limactl shell "$INSTANCE" printenv |
| 29 | refute_line --regexp '^FOO=' |
| 30 | } |
| 31 | |
| 32 | @test 'environment is preserved with --preserve-env' { |
| 33 | export FOO=foo |
| 34 | run -0 limactl shell --preserve-env "$INSTANCE" printenv |
| 35 | assert_line FOO=foo |
| 36 | } |
| 37 | |
| 38 | @test 'profile settings inside the VM take precedence over preserved variables' { |
| 39 | limactl shell "$INSTANCE" sh -c 'echo "export FOO=bar" >>~/.bash_profile' |
| 40 | export FOO=foo |
| 41 | run -0 limactl shell --preserve-env "$INSTANCE" printenv |
| 42 | assert_line FOO=bar |
| 43 | } |
| 44 | |
| 45 | @test 'builtin block list is used when LIMA_SHELLENV_BLOCK is not set' { |
| 46 | # default block list includes SSH_* |
| 47 | export SSH_FOO=ssh_foo |
| 48 | run -0 limactl shell --preserve-env "$INSTANCE" printenv |
| 49 | refute_line --regexp '^SSH_FOO=' |
| 50 | } |
| 51 | |
| 52 | @test 'custom block list replaces builtin block list' { |
| 53 | export LIMA_SHELLENV_BLOCK=FOO |
| 54 | export FOO=foo |
| 55 | export SSH_FOO=foo |
| 56 | run -0 limactl shell --preserve-env "$INSTANCE" printenv |
| 57 | refute_line --regexp '^FOO=' |
| 58 | assert_line SSH_FOO=foo |
| 59 | } |
| 60 | |
| 61 | @test 'custom block list starting with + appends to builtin block list' { |
| 62 | export LIMA_SHELLENV_BLOCK=+FOO |
| 63 | export FOO=foo |
| 64 | export SSH_FOO=foo |
| 65 | run -0 limactl shell --preserve-env "$INSTANCE" printenv |
| 66 | refute_line --regexp '^FOO=' |
| 67 | refute_line --regexp '^SSH_FOO=' |
| 68 | } |
| 69 | |
| 70 | @test 'block list entries can use * wildcard at the end' { |
| 71 | export LIMA_SHELLENV_BLOCK="FOO*" |
| 72 | export FOO=foo |
| 73 | export FOOBAR=foobar |
| 74 | export BAR=bar |
| 75 | run -0 limactl shell --preserve-env "$INSTANCE" printenv |
| 76 | refute_line --regexp '^FOO' |
| 77 | assert_line BAR=bar |
| 78 | } |
| 79 | |
| 80 | @test 'wildcard works at the start of the pattern' { |
| 81 | export LIMA_SHELLENV_BLOCK="*FOO" |
| 82 | export FOO=foo |
| 83 | export BARFOO=barfoo |
| 84 | run -0 limactl shell --preserve-env "$INSTANCE" printenv |
| 85 | refute_line --regexp '^BARFOO=' |
| 86 | refute_line --regexp '^FOO=' |
| 87 | } |
| 88 | |
| 89 | @test 'block list can use a , separated list with whitespace ignored' { |
| 90 | export LIMA_SHELLENV_BLOCK="FOO*, , BAR" |
| 91 | export FOO=foo |
| 92 | export FOOBAR=foobar |
| 93 | export BAR=bar |
| 94 | export BARBAZ=barbaz |
| 95 | run -0 limactl shell --preserve-env "$INSTANCE" printenv |
| 96 | refute_line --regexp '^FOO' |
| 97 | refute_line --regexp '^BAR=' |
| 98 | assert_line BARBAZ=barbaz |
| 99 | } |
| 100 | |
| 101 | @test 'allow list can use a , separated list with whitespace ignored' { |
| 102 | export LIMA_SHELLENV_ALLOW="SSH_FOO, , BAR*, LD_UID" |
| 103 | export SSH_FOO=ssh_foo |
| 104 | export SSH_BAR=ssh_bar |
| 105 | export SSH_BLOCK=ssh_block |
| 106 | export BAR=bar |
| 107 | export BARBAZ=barbaz |
| 108 | export LD_UID=randomuid |
| 109 | run -0 limactl shell --preserve-env "$INSTANCE" printenv |
| 110 | |
| 111 | assert_line SSH_FOO=ssh_foo |
| 112 | assert_line BAR=bar |
| 113 | assert_line BARBAZ=barbaz |
| 114 | assert_line LD_UID=randomuid |
| 115 | |
| 116 | refute_line --regexp '^SSH_BAR=' |
| 117 | refute_line --regexp '^SSH_BLOCK=' |
| 118 | } |
| 119 | |
| 120 | @test 'wildcard patterns work in all positions' { |
| 121 | export LIMA_SHELLENV_BLOCK="*FOO*BAR*" |
| 122 | export FOO=foo |
| 123 | export FOOBAR=foobar |
| 124 | export FOOXYZBAR=fooxyzbar |
| 125 | export FOOBAZ=foobaz |
| 126 | export BAZBAR=bazbar |
| 127 | export BAR=bar |
| 128 | export XFOOYBARZDOTCOM=xfooybarzdotcom |
| 129 | export NORMAL_VAR=normal_var |
| 130 | export UNRELATED=unrelated |
| 131 | run -0 limactl shell --preserve-env "$INSTANCE" printenv |
| 132 | |
| 133 | refute_line --regexp '^FOOBAR=' |
| 134 | refute_line --regexp '^FOOXYZBAR=' |
| 135 | refute_line --regexp '^XFOOYBARZDOTCOM=' |
| 136 | |
| 137 | assert_line FOOBAZ=foobaz |
| 138 | assert_line NORMAL_VAR=normal_var |
| 139 | assert_line UNRELATED=unrelated |
| 140 | assert_line BAZBAR=bazbar |
| 141 | assert_line BAR=bar |
| 142 | assert_line FOO=foo |
| 143 | } |
| 144 | |
| 145 | @test 'allowlist overrides default blocklist with wildcards' { |
| 146 | export LIMA_SHELLENV_ALLOW="SSH_*,CUSTOM*" |
| 147 | export LIMA_SHELLENV_BLOCK="+*TOKEN" |
| 148 | export SSH_AUTH_SOCK=ssh_auth_sock |
| 149 | export SSH_CONNECTION=ssh_connection |
| 150 | export CUSTOM_VAR=custom_var |
| 151 | export MY_TOKEN=my_token |
| 152 | export UNRELATED=unrelated |
| 153 | run -0 limactl shell --preserve-env "$INSTANCE" printenv |
| 154 | |
| 155 | assert_line SSH_AUTH_SOCK=ssh_auth_sock |
| 156 | assert_line SSH_CONNECTION=ssh_connection |
| 157 | assert_line CUSTOM_VAR=custom_var |
| 158 | refute_line --regexp '^MY_TOKEN=' |
| 159 | assert_line UNRELATED=unrelated |
| 160 | } |
| 161 | |
| 162 | @test 'invalid characters in patterns cause fatal errors' { |
| 163 | export LIMA_SHELLENV_BLOCK="FOO-BAR" |
| 164 | run ! limactl shell --preserve-env "$INSTANCE" printenv |
| 165 | assert_output --partial "Invalid LIMA_SHELLENV_BLOCK pattern" |
| 166 | assert_output --partial "contains invalid character" |
| 167 | } |
| 168 | |
| 169 | @test 'limactl info includes the default block list' { |
| 170 | run -0 limactl info |
| 171 | run -0 limactl yq '.shellEnvBlock[]' <<<"$output" |
| 172 | assert_line PATH |
| 173 | assert_line "SSH_*" |
| 174 | assert_line USER |
| 175 | } |