Merge pull request 'fix: SECURITY: SOPS decryption without integrity verification (#61)' (#70) from fix/issue-61 into main
All checks were successful
ci/woodpecker/push/ci Pipeline was successful

This commit is contained in:
dev-qwen 2026-03-31 19:27:55 +00:00
commit 842e529004

View file

@ -30,23 +30,27 @@ if [ -f "$FACTORY_ROOT/.env.enc" ] && command -v sops &>/dev/null; then
_saved_forge_url="${FORGE_URL:-}" _saved_forge_url="${FORGE_URL:-}"
_saved_forge_token="${FORGE_TOKEN:-}" _saved_forge_token="${FORGE_TOKEN:-}"
# Use temp file + validate dotenv format before sourcing (avoids eval injection) # Use temp file + validate dotenv format before sourcing (avoids eval injection)
_tmpenv=$(mktemp) || { echo "Warning: failed to create temp file for .env.enc" >&2; exit 1; } # SOPS -d automatically verifies MAC/GCM authentication tag during decryption
if sops -d --output-type dotenv "$FACTORY_ROOT/.env.enc" > "$_tmpenv" 2>/dev/null; then _tmpenv=$(mktemp) || { echo "Error: failed to create temp file for .env.enc" >&2; exit 1; }
# Validate: non-empty, non-comment lines must match KEY=value pattern if ! sops -d --output-type dotenv "$FACTORY_ROOT/.env.enc" > "$_tmpenv" 2>/dev/null; then
# Filter out blank lines and comments before validation echo "Error: failed to decrypt .env.enc — decryption failed, possible corruption" >&2
_validated=$(grep -E '^[A-Za-z_][A-Za-z0-9_]*=' "$_tmpenv" 2>/dev/null || true) rm -f "$_tmpenv"
if [ -n "$_validated" ]; then exit 1
# Write validated content to a second temp file and source it fi
_validated_env=$(mktemp) # Validate: non-empty, non-comment lines must match KEY=value pattern
printf '%s\n' "$_validated" > "$_validated_env" # Filter out blank lines and comments before validation
# shellcheck source=/dev/null _validated=$(grep -E '^[A-Za-z_][A-Za-z0-9_]*=' "$_tmpenv" 2>/dev/null || true)
source "$_validated_env" if [ -n "$_validated" ]; then
rm -f "$_validated_env" # Write validated content to a second temp file and source it
else _validated_env=$(mktemp)
echo "Warning: .env.enc decryption output failed format validation" >&2 printf '%s\n' "$_validated" > "$_validated_env"
fi # shellcheck source=/dev/null
source "$_validated_env"
rm -f "$_validated_env"
else else
echo "Warning: failed to decrypt .env.enc — secrets not loaded" >&2 echo "Error: .env.enc decryption output failed format validation" >&2
rm -f "$_tmpenv"
exit 1
fi fi
rm -f "$_tmpenv" rm -f "$_tmpenv"
set +a set +a