Repository navigation
security(env): stop copying secret values into shell state and the env cache #13950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b5eeaa7
security(env): stop copying secret values into shell state and the en…
jdx 33beb51
security(env): keep age and vars secrets out of the env cache, compar…
jdx 2f7bab3
security(env): reverse __MISE_DIFF blobs written by the previous mise
jdx 79e83f0
security(exec): keep env values out of fish argv
jdx a0495ec
security(env): version __MISE_DIFF and __MISE_SESSION, compare by ver…
jdx 7c7a7fc
fix(exec): apply the sandbox env filter before building the fish -C s…
jdx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # An env module that is not cacheable must keep the whole environment out of the env cache, | ||
| # whether it says so itself (cacheable = false) or the user marks its values redact = true. | ||
|
|
||
| export MISE_ENV_CACHE=1 | ||
| export __MISE_ENV_CACHE_KEY="dGVzdGtleXRlc3RrZXl0ZXN0a2V5dGVzdGtleXRlc3Q=" | ||
|
|
||
| COUNTER_FILE="$MISE_DATA_DIR/env_cache_test_counter" | ||
|
|
||
| make_plugin() { | ||
| local name="$1" cacheable="$2" | ||
| local dir="$MISE_DATA_DIR/plugins/$name" | ||
| mkdir -p "$dir/hooks" | ||
| cat >"$dir/metadata.lua" <<EOFMETA | ||
| PLUGIN = {} | ||
| PLUGIN.name = "$name" | ||
| PLUGIN.version = "1.0.0" | ||
| PLUGIN.homepage = "https://example.com" | ||
| PLUGIN.license = "MIT" | ||
| PLUGIN.description = "Test plugin" | ||
| PLUGIN.minRuntimeVersion = "0.3.0" | ||
| EOFMETA | ||
| cat >"$dir/hooks/mise_env.lua" <<EOFHOOK | ||
| function PLUGIN:MiseEnv(ctx) | ||
| local f = io.open("$COUNTER_FILE", "r") | ||
| local count = tonumber(f:read("*all")) or 0 | ||
| f:close() | ||
| f = io.open("$COUNTER_FILE", "w") | ||
| f:write(tostring(count + 1)) | ||
| f:close() | ||
| return { | ||
| env = {{key = "UNCACHEABLE_TEST_VAR", value = "value"}}, | ||
| cacheable = $cacheable, | ||
| watch_files = {} | ||
| } | ||
| end | ||
| EOFHOOK | ||
| } | ||
|
|
||
| cached_files() { | ||
| find "$MISE_STATE_DIR/env-cache" -type f 2>/dev/null | wc -l | tr -d ' ' | ||
| } | ||
|
|
||
| run_three_times() { | ||
| rm -rf "$MISE_STATE_DIR/env-cache" | ||
| echo 0 >"$COUNTER_FILE" | ||
| mise env -s bash >/dev/null 2>&1 | ||
| mise env -s bash >/dev/null 2>&1 | ||
| mise env -s bash >/dev/null 2>&1 | ||
| cat "$COUNTER_FILE" | ||
| } | ||
|
|
||
| make_plugin test-uncacheable-env false | ||
| cat >"$MISE_CONFIG_DIR/config.toml" <<EOF | ||
| [env] | ||
| _.test-uncacheable-env = {} | ||
| EOF | ||
| count="$(run_three_times)" | ||
| assert "echo $count" "3" | ||
| assert "echo 0" "$(cached_files)" | ||
|
|
||
| # the plugin says cacheable = true, but the user redacts its values: still not cached | ||
| make_plugin test-redacted-env true | ||
| cat >"$MISE_CONFIG_DIR/config.toml" <<EOF | ||
| [env] | ||
| _.test-redacted-env = { redact = true } | ||
| EOF | ||
| count="$(run_three_times)" | ||
| assert "echo $count" "3" | ||
| assert "echo 0" "$(cached_files)" | ||
|
|
||
| # control: without redact the same plugin is cached | ||
| cat >"$MISE_CONFIG_DIR/config.toml" <<EOF | ||
| [env] | ||
| _.test-redacted-env = {} | ||
| EOF | ||
| count="$(run_three_times)" | ||
| assert "echo $count" "1" | ||
| [ "$(cached_files)" -gt 0 ] || fail "expected the cacheable environment to be cached" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Secret values must not be copied into shell state, and must not reach the env cache. | ||
|
|
||
| export MISE_ENV_CACHE=1 | ||
| export __MISE_ENV_CACHE_KEY="dGVzdGtleXRlc3RrZXl0ZXN0a2V5dGVzdGtleXRlc3Q=" | ||
|
|
||
| cat >"$MISE_CONFIG_DIR/config.toml" <<TOML | ||
| [env] | ||
| PLAIN_VAR = "plain-value-marker" | ||
| API_TOKEN = { value = "s3cr3t-marker", redact = true } | ||
| TOML | ||
|
|
||
| # prints the decompressed msgpack of a __MISE_DIFF / __MISE_SESSION blob | ||
| decode_state() { | ||
| python3 -c ' | ||
| import base64, sys, zlib | ||
| raw = sys.argv[1] | ||
| sys.stdout.buffer.write(zlib.decompress(base64.b64decode(raw + "=" * (-len(raw) % 4)))) | ||
| ' "$1" | tr -c '[:print:]' ' ' | ||
| } | ||
|
|
||
| out="$(mise hook-env -s bash --force)" | ||
| diff_blob="$(sed -n "s/^export __MISE_DIFF=//p" <<<"$out")" | ||
| session_blob="$(sed -n "s/^export __MISE_SESSION=//p" <<<"$out")" | ||
| [ -n "$diff_blob" ] || fail "no __MISE_DIFF in hook-env output: $out" | ||
| [ -n "$session_blob" ] || fail "no __MISE_SESSION in hook-env output: $out" | ||
|
|
||
| # keys are tracked, values are not | ||
| export DECODED_DIFF DECODED_SESSION | ||
| DECODED_DIFF="$(decode_state "$diff_blob")" | ||
| DECODED_SESSION="$(decode_state "$session_blob")" | ||
| for var in DECODED_DIFF DECODED_SESSION; do | ||
| assert_contains "echo \"\$$var\"" "API_TOKEN" | ||
| assert_not_contains "echo \"\$$var\"" "s3cr3t-marker" | ||
| assert_not_contains "echo \"\$$var\"" "plain-value-marker" | ||
| done | ||
|
|
||
| # the shell still gets the real value | ||
| assert_contains "echo \"$out\"" "s3cr3t-marker" | ||
|
|
||
| # a redacted value is never written to the env cache | ||
| assert "find '$MISE_STATE_DIR/env-cache' -type f 2>/dev/null | wc -l | tr -d ' '" "0" | ||
|
|
||
| # without a secret, the same config is cached as before | ||
| cat >"$MISE_CONFIG_DIR/config.toml" <<TOML | ||
| [env] | ||
| PLAIN_VAR = "plain-value-marker" | ||
| TOML | ||
| mise env -s bash >/dev/null | ||
| assert_directory_exists "$MISE_STATE_DIR/env-cache" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.