Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/sh/secrets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# A caller that needs these functions in an elevated shell ships them there by name with `declare -f`.
# This is because sourcing this file in the calling shell does not reach one started by `sudo`.
# A function here that calls another is shipped with it, so `secret_verdict` needs `secret_shape_verdict`, `secret_state`, `unresolved_link_reason`, `secret_content_verdict`, `secret_content_state`, and `secret_content_refusal`.
# `read_env_value` needs `require_secret` and `secret_verdict` with all of those.

# `unresolved_link_reason`
# Echoes the clause that says why a symlink answering `-L` true and `-e` false does not resolve, worded for the account running it.
Expand Down Expand Up @@ -264,7 +265,13 @@ require_secret() {
# What the file itself prints goes to /dev/null rather than into the value.
# `BASH_ENV`, `POSIXLY_CORRECT` and `SHELLOPTS` are dropped so nothing but the file runs ahead of the read.
# It checks nothing about the file, so a caller runs `require_secret` on it first.
# The child defines `require_secret`, so a file the env file includes behind `declare -F require_secret` is checked, and an unguarded include is not.
read_env_value() {
local checks
checks=$(declare -f require_secret secret_verdict secret_shape_verdict secret_state unresolved_link_reason secret_content_verdict secret_content_state secret_content_refusal) || {
printf 'ERROR: read_env_value cannot read %s, since a function it ships to check the files that one includes is not defined\n' "$1" >&2
return 1
}
# shellcheck disable=SC2016 # Every expansion belongs to the child bash.
env -u BASH_ENV -u POSIXLY_CORRECT -u SHELLOPTS bash -Eeuo pipefail -c 'env_read_name=$2; unset -v "$env_read_name"; set -a; source "$1" >/dev/null; set +a; printf "%s" "${!env_read_name:-}"' _ "$1" "$2"
env -u BASH_ENV -u POSIXLY_CORRECT -u SHELLOPTS bash -Eeuo pipefail -c 'eval "$1"; shift; env_read_name=$2; unset -v "$env_read_name"; set -a; source "$1" >/dev/null; set +a; printf "%s" "${!env_read_name:-}"' _ "$checks" "$1" "$2"
}
50 changes: 50 additions & 0 deletions tests/test_secrets_sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,56 @@ def test_bash_env_does_not_run_ahead_of_the_file(self) -> None:
)
self.assertEqual((result.returncode, result.stdout), (0, ""))

def guarded_include(self, content: str, mode: int = 0o600) -> Path:
write(self.root / "extra.env", content, mode)
return write(
self.root / "app.env",
"APP_NAME=example\n"
"if declare -F require_secret >/dev/null; then\n"
' require_secret "${BASH_SOURCE%/*}/extra.env"\n'
"fi\n"
'. "${BASH_SOURCE%/*}/extra.env"\n',
)

def test_a_guarded_include_is_read_where_it_passes(self) -> None:
env = self.guarded_include("APP_HOST=two\n")
self.assertEqual(answer("read_env_value", str(env), "APP_HOST"), "two")

def test_a_guarded_include_with_a_loose_mode_is_refused(self) -> None:
env = self.guarded_include("APP_HOST=two\n", 0o666)
result = run('read_env_value "$1" APP_HOST', str(env))
self.assertEqual(
(result.returncode, result.stdout, result.stderr),
(
1,
"",
f"ERROR: {self.root}/extra.env is mode 666, and must give group and other no access\n",
),
)

def test_a_guarded_include_with_crlf_endings_is_refused(self) -> None:
env = self.guarded_include("APP_HOST=two\r\n")
result = run('read_env_value "$1" APP_HOST', str(env))
self.assertEqual(
(result.returncode, result.stdout, result.stderr),
(
1,
"",
(
f"ERROR: {self.root}/extra.env holds a carriage return, most often from CRLF "
"line endings, and must use LF line endings only\n"
),
),
)

def test_a_missing_check_function_fails_the_read_before_the_file_runs(self) -> None:
marker = self.root / "ran"
env = write(self.root / "app.env", f"touch {marker}\nAPP_HOST=two\n")
result = run('unset -f secret_state; read_env_value "$1" APP_HOST', str(env))
self.assertEqual((result.returncode, result.stdout), (1, ""))
self.assertIn("ERROR: ", result.stderr)
self.assertFalse(marker.exists())


class CarriedShfmtSettings(unittest.TestCase):
def test_the_tree_pins_every_shfmt_setting_a_carrier_cannot_reformat_for(self) -> None:
Expand Down
Loading