|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +repo_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) |
| 6 | +work_dir=$(mktemp -d) |
| 7 | +trap 'rm -rf "$work_dir"' EXIT |
| 8 | + |
| 9 | +fail() { |
| 10 | + echo "FAIL: $*" >&2 |
| 11 | + exit 1 |
| 12 | +} |
| 13 | + |
| 14 | +curl() { |
| 15 | + printf '%s\n' "$@" >"$CURL_ARGS_FILE" |
| 16 | + cat >"$CURL_STDIN_FILE" |
| 17 | +} |
| 18 | + |
| 19 | +export CURL_ARGS_FILE CURL_STDIN_FILE |
| 20 | + |
| 21 | +test_curl_helper() { |
| 22 | + local script=$1 token_mode=$2 |
| 23 | + local helper |
| 24 | + |
| 25 | + helper=$(awk '/^curl_command\(\)/,/^}/' "$repo_root/$script") |
| 26 | + [ -n "$helper" ] || fail "curl_command not found in $script" |
| 27 | + eval "$helper" |
| 28 | + |
| 29 | + for old_curl in 0 1; do |
| 30 | + CURL_ARGS_FILE="$work_dir/args" |
| 31 | + CURL_STDIN_FILE="$work_dir/stdin" |
| 32 | + proxy="" |
| 33 | + export proxy |
| 34 | + cs_falcon_oauth_token="REGRESSION_SECRET_TOKEN" |
| 35 | + |
| 36 | + if [ "$token_mode" = "argument" ]; then |
| 37 | + curl_command "$cs_falcon_oauth_token" "https://api.example.invalid/resource" |
| 38 | + else |
| 39 | + curl_command "https://api.example.invalid/resource" |
| 40 | + fi |
| 41 | + |
| 42 | + if grep -qF "$cs_falcon_oauth_token" "$CURL_ARGS_FILE"; then |
| 43 | + fail "$script exposed the bearer token in curl arguments (old_curl=$old_curl)" |
| 44 | + fi |
| 45 | + grep -qF 'https://api.example.invalid/resource' "$CURL_ARGS_FILE" || |
| 46 | + fail "$script did not pass the expected URL (old_curl=$old_curl)" |
| 47 | + grep -qF "$cs_falcon_oauth_token" "$CURL_STDIN_FILE" || |
| 48 | + fail "$script did not provide the bearer token through stdin (old_curl=$old_curl)" |
| 49 | + grep -qF -- '--proto' "$CURL_ARGS_FILE" || |
| 50 | + fail "$script did not restrict the request protocol (old_curl=$old_curl)" |
| 51 | + grep -qF -- '--proto-redir' "$CURL_ARGS_FILE" || |
| 52 | + fail "$script did not restrict the redirect protocol (old_curl=$old_curl)" |
| 53 | + done |
| 54 | +} |
| 55 | + |
| 56 | +test_curl_helper \ |
| 57 | + bash/containers/falcon-container-sensor-pull/falcon-container-sensor-pull.sh argument |
| 58 | +test_curl_helper bash/install/falcon-linux-install.sh global |
| 59 | +test_curl_helper bash/install/falcon-linux-uninstall.sh global |
| 60 | +test_curl_helper bash/migrate/falcon-linux-migrate.sh global |
| 61 | + |
| 62 | +test_xtrace_guard() { |
| 63 | + local script=$1 guard trace_file |
| 64 | + |
| 65 | + guard=$(awk '/^case \$- in$/,/^esac$/' "$repo_root/$script") |
| 66 | + [ -n "$guard" ] || fail "xtrace guard not found in $script" |
| 67 | + trace_file="$work_dir/xtrace" |
| 68 | + |
| 69 | + FALCON_CLIENT_SECRET="XTRACE_SECRET_SENTINEL" \ |
| 70 | + bash -xc "$guard; : \"\$FALCON_CLIENT_SECRET\"" \ |
| 71 | + >/dev/null 2>"$trace_file" |
| 72 | + |
| 73 | + if grep -qF 'XTRACE_SECRET_SENTINEL' "$trace_file"; then |
| 74 | + fail "$script allowed a credential into bash xtrace output" |
| 75 | + fi |
| 76 | +} |
| 77 | + |
| 78 | +test_xtrace_guard bash/containers/falcon-container-sensor-pull/falcon-container-sensor-pull.sh |
| 79 | +test_xtrace_guard bash/install/falcon-linux-install.sh |
| 80 | +test_xtrace_guard bash/install/falcon-linux-uninstall.sh |
| 81 | +test_xtrace_guard bash/migrate/falcon-linux-migrate.sh |
| 82 | + |
| 83 | +test_hash_verification() { |
| 84 | + local script=$1 helper test_file expected_sha |
| 85 | + |
| 86 | + helper=$(awk '/^verify_sha256\(\)/,/^}/' "$repo_root/$script") |
| 87 | + [ -n "$helper" ] || fail "verify_sha256 not found in $script" |
| 88 | + eval "$helper" |
| 89 | + die() { exit 1; } |
| 90 | + |
| 91 | + test_file="$work_dir/installer" |
| 92 | + printf '%s' 'verified installer content' >"$test_file" |
| 93 | + expected_sha=$(openssl dgst -sha256 "$test_file" | awk '{ print $NF }') |
| 94 | + verify_sha256 "$test_file" "$expected_sha" || |
| 95 | + fail "$script rejected a valid installer hash" |
| 96 | + |
| 97 | + if (verify_sha256 "$test_file" '0000000000000000000000000000000000000000000000000000000000000000'); then |
| 98 | + fail "$script accepted an invalid installer hash" |
| 99 | + fi |
| 100 | + [ ! -e "$test_file" ] || fail "$script retained an installer with an invalid hash" |
| 101 | +} |
| 102 | + |
| 103 | +test_hash_verification bash/install/falcon-linux-install.sh |
| 104 | +test_hash_verification bash/migrate/falcon-linux-migrate.sh |
| 105 | + |
| 106 | +if rg -n 'Invalid Access Token:.*\$cs_falcon_oauth_token|Failed to retrieve maintenance token\. Response:' \ |
| 107 | + "$repo_root/bash" --glob '*.sh'; then |
| 108 | + fail 'a Bash error path exposes a credential or raw maintenance-token response' |
| 109 | +fi |
| 110 | + |
| 111 | +if rg -n 'curl .*X-aws-ec2-metadata-token:.*\$token' \ |
| 112 | + "$repo_root/bash" --glob '*.sh'; then |
| 113 | + fail 'an EC2 metadata token is exposed in curl arguments' |
| 114 | +fi |
| 115 | + |
| 116 | +if rg -n '(Invoke-FalconAuth|GetToken) - \$content:|Retrieved maintenance token:|Starting .*parameters.*\$(Install|Uninstall)Params' \ |
| 117 | + "$repo_root/powershell" --glob '*.ps1'; then |
| 118 | + fail 'a PowerShell log statement exposes an authentication or installer token' |
| 119 | +fi |
| 120 | + |
| 121 | +echo 'PASS: credential handling regression checks' |
0 commit comments