Fix .env writer: quote values so shell metacharacters don't break source
CI / test (push) Successful in 3m14s

deploy.sh sources .env directly as bash (`set -a; source .env`), but
setup.sh wrote raw unquoted values. The default SMTP_FROM ("ANOUMA
<no-reply@...>") contains `<`/`>`, which bash parses as redirections,
failing with "syntax error near unexpected token `newline'" on every
deploy. Both scripts' env_set now write double-quoted values with
backslashes/quotes escaped — valid for both `source` and Docker
Compose's env_file parsing (which strips matching quotes).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-26 02:12:24 +02:00
co-authored by Claude Sonnet 5
parent 8d88363583
commit 8d60d48bd0
2 changed files with 21 additions and 5 deletions
+9 -3
View File
@@ -61,13 +61,19 @@ trap 'rm -f "$LOCK_FILE"' EXIT
# .env helpers — same idempotent read/write approach as setup.sh.
# ---------------------------------------------------------------------------
env_get() { grep -E "^$1=" .env 2>/dev/null | tail -n1 | cut -d'=' -f2- || true; }
# Quoted the same way as setup.sh's env_set — this file is `source`d as a
# bash script below, so an unquoted value with shell metacharacters would
# break parsing.
env_set() {
local key="$1" value="$2"
local key="$1" value="$2" escaped quoted
escaped="${value//\\/\\\\}"
escaped="${escaped//\"/\\\"}"
quoted="\"${escaped}\""
if grep -qE "^$key=" .env; then
local tmp; tmp="$(mktemp)"
awk -v k="$key" -v v="$value" 'BEGIN{FS=OFS="="} $1==k{$0=k "=" v} {print}' .env > "$tmp" && mv "$tmp" .env
awk -v k="$key" -v v="$quoted" 'BEGIN{FS=OFS="="} $1==k{$0=k "=" v} {print}' .env > "$tmp" && mv "$tmp" .env
else
printf '%s=%s\n' "$key" "$value" >> .env
printf '%s=%s\n' "$key" "$quoted" >> .env
fi
}
+12 -2
View File
@@ -263,13 +263,23 @@ env_get() { $SUDO grep -E "^$1=" "$ENV_FILE" 2>/dev/null | tail -n1 | cut -d'='
# passing, never re-interpolated into a nested shell string) so arbitrary
# characters in $value — quotes, backslashes, anything a typed SMTP/registry
# password might contain — can never break quoting or be misinterpreted.
#
# The value is written double-quoted, with backslashes and double quotes
# escaped: deploy.sh later `source`s this file as a bash script, so an
# unquoted value containing shell metacharacters (e.g. the default
# SMTP_FROM="ANOUMA <no-reply@...>", whose `<`/`>` are redirections) breaks
# with "syntax error near unexpected token `newline'". Quoting also works
# fine for Docker Compose's own env_file parsing, which strips matching
# surrounding quotes.
env_set() {
local key="$1" value="$2" tmp
local key="$1" value="$2" tmp escaped
escaped="${value//\\/\\\\}"
escaped="${escaped//\"/\\\"}"
tmp="$(mktemp)"
if $SUDO test -f "$ENV_FILE"; then
$SUDO grep -vE "^${key}=" "$ENV_FILE" > "$tmp" 2>/dev/null || true
fi
printf '%s=%s\n' "$key" "$value" >> "$tmp"
printf '%s="%s"\n' "$key" "$escaped" >> "$tmp"
$SUDO cp "$tmp" "$ENV_FILE"
rm -f "$tmp"
}