Skip to content

Commit 2359a38

Browse files
authored
Merge pull request #30 from kitconcept/issue-28
Strip CRLF line endings from env_file values
2 parents 4f67eb2 + 9f2b4be commit 2359a38

3 files changed

Lines changed: 128 additions & 4 deletions

File tree

‎news/28.bugfix‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Strip CRLF line endings from environment variable values, which GitHub's web form stores when editing multi-line secrets. Values are reported as processed.

‎scripts/docker-entrypoint.sh‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ configure_ssh_key() {
5959

6060
# Parse a NAME=VALUE file and export every entry. $1 is the file to read.
6161
load_env_file() {
62-
local source_file="$1" env_file_len line
62+
local source_file="$1" env_file_len line cr_lines=0
6363
env_file_len=$(grep -cv -e '^#' -e '^$' "${source_file}" || true)
6464
if [[ ${env_file_len} -gt 0 ]]; then
6565
echo "Environment Variables: Additional values"
@@ -73,10 +73,17 @@ load_env_file() {
7373
# Values are taken verbatim, matching `docker --env-file`: quotes in the
7474
# file are part of the value, not delimiters around it.
7575
#
76-
# The ENV_FILE input has no trailing newline, so the `-n` test is what
77-
# keeps the final line from being dropped by read's non-zero exit. A file
78-
# read through env_file_path usually does end in a newline; both work.
76+
# Strip trailing carriage returns from CRLF line endings (GitHub's web form
77+
# stores multi-line secrets with CRLF). The ENV_FILE input has no trailing
78+
# newline, so the `-n` test is what keeps the final line from being dropped
79+
# by read's non-zero exit. A file read through env_file_path usually does
80+
# end in a newline; both work.
7981
while IFS= read -r line || [ -n "${line}" ]; do
82+
# Remove trailing \r if present (from CRLF line endings)
83+
if [[ "${line}" == *$'\r' ]]; then
84+
line="${line%$'\r'}"
85+
((++cr_lines))
86+
fi
8087
case "${line}" in
8188
''|\#*) continue ;;
8289
esac
@@ -86,6 +93,15 @@ load_env_file() {
8693
fi
8794
export "${line?}"
8895
done < "${source_file}"
96+
if [ ${cr_lines} -gt 0 ]; then
97+
# Reported unconditionally, not only under DEBUG: the whole problem with
98+
# CRLF is that it is invisible everywhere else.
99+
if [ ${cr_lines} -eq 1 ]; then
100+
echo "Environment Variables: removed carriage returns from 1 line"
101+
else
102+
echo "Environment Variables: removed carriage returns from ${cr_lines} lines"
103+
fi
104+
fi
89105
if [ "${DEBUG}" != "0" ]; then
90106
echo "Environment vars after: $(env|wc -l)"
91107
fi

‎tests/entrypoint.bats‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,70 @@ export_from_env_file() {
314314
[[ "$output" == *"is not in NAME=VALUE format"* ]]
315315
}
316316

317+
# --- env_file CRLF handling (issue #28) -----------------------------------------------
318+
319+
@test "env_file strips CRLF line endings from values (issue #28)" {
320+
# GitHub's web form saves multi-line secrets as CRLF. Every line except the
321+
# last ends in \r, which becomes part of the value unless stripped.
322+
# Use $'...' syntax to embed literal CRLF in the string.
323+
export_from_env_file $'DB_USER=plone\r\nDB_PORT=5432\r\n' DB_USER
324+
[ "$status" -eq 0 ]
325+
[ "$output" = "plone" ]
326+
}
327+
328+
@test "env_file strips CRLF from multiple values without corruption (issue #28)" {
329+
export_from_env_file $'DB_USER=admin\r\nDB_PASS=secret\r\nDB_HOST=localhost\r\n' DB_PASS
330+
[ "$status" -eq 0 ]
331+
[ "$output" = "secret" ]
332+
}
333+
334+
@test "env_file reports stripped carriage returns (issue #28)" {
335+
# Like trim_inputs, the fix must report on stdout so the user sees the issue.
336+
run env -i PATH="${PATH}" HOME="${BATS_TEST_TMPDIR}" \
337+
ENV_FILE=$'DB_USER=plone\r\n' \
338+
bash -c "
339+
source '${ENTRYPOINT}'
340+
ENV_FILE_DEST=\"\${HOME}/.env\"
341+
configure_env_file
342+
"
343+
[ "$status" -eq 0 ]
344+
[[ "$output" == *"Environment Variables: removed carriage returns from 1 line"* ]]
345+
}
346+
347+
@test "env_file counts multiple CRLF strips in the report (issue #28)" {
348+
run env -i PATH="${PATH}" HOME="${BATS_TEST_TMPDIR}" \
349+
ENV_FILE=$'DB_USER=admin\r\nDB_PASS=secret\r\nDB_HOST=localhost\r\n' \
350+
bash -c "
351+
source '${ENTRYPOINT}'
352+
ENV_FILE_DEST=\"\${HOME}/.env\"
353+
configure_env_file
354+
"
355+
[ "$status" -eq 0 ]
356+
[[ "$output" == *"Environment Variables: removed carriage returns from 3 lines"* ]]
357+
}
358+
359+
@test "env_file with LF-only lines does not report carriage returns (issue #28)" {
360+
# Verify the fix doesn't report when there are no CR chars to strip.
361+
export_from_env_file $'DB_USER=plone\nDB_PORT=5432' DB_USER
362+
[ "$status" -eq 0 ]
363+
[[ ! "$output" =~ "carriage returns" ]]
364+
}
365+
366+
@test "env_file with blank lines after CRLF stripping does not abort (issue #28)" {
367+
# If a line only contained \r, after stripping it becomes empty and should
368+
# be skipped gracefully without aborting.
369+
export_from_env_file $'DB_USER=admin\r\n\r\nDB_PASS=secret\r\n' DB_USER
370+
[ "$status" -eq 0 ]
371+
[ "$output" = "admin" ]
372+
}
373+
374+
@test "env_file with CRLF comment lines skips them correctly (issue #28)" {
375+
# Comments ending in CR should still be recognized and skipped.
376+
export_from_env_file $'# config\r\nDB_USER=plone\r\n' DB_USER
377+
[ "$status" -eq 0 ]
378+
[ "$output" = "plone" ]
379+
}
380+
317381
# --- env_file_path (issue #3) -------------------------------------------------
318382

319383
# Write a file with the given content, run configure_env_file_path against it,
@@ -425,6 +489,49 @@ export_from_env_file_path() {
425489
[[ "$output" == *"Input remote_host is required!"* ]]
426490
}
427491

492+
# --- env_file_path CRLF handling (issue #28) -------------------------------------------------
493+
494+
@test "env_file_path strips CRLF line endings from values (issue #28)" {
495+
# Files edited in Windows or via GitHub's web form may have CRLF line endings.
496+
local file="${BATS_TEST_TMPDIR}/crlf.env"
497+
printf '%s' "$(printf 'DB_USER=admin\r\nDB_HOST=localhost\r\n')" > "${file}"
498+
run env -i PATH="${PATH}" HOME="${BATS_TEST_TMPDIR}" \
499+
ENV_FILE_PATH="${file}" WANT=DB_USER \
500+
bash -c "
501+
source '${ENTRYPOINT}'
502+
configure_env_file_path >/dev/null 2>&1
503+
printf '[%s]' \"\${!WANT}\"
504+
"
505+
[ "$status" -eq 0 ]
506+
[ "$output" = "[admin]" ]
507+
}
508+
509+
@test "env_file_path reports stripped carriage returns (issue #28)" {
510+
local file="${BATS_TEST_TMPDIR}/crlf.env"
511+
printf '%s' "$(printf 'DB_USER=admin\r\nDB_PASS=secret\r\n')" > "${file}"
512+
run env -i PATH="${PATH}" HOME="${BATS_TEST_TMPDIR}" \
513+
ENV_FILE_PATH="${file}" \
514+
bash -c "
515+
source '${ENTRYPOINT}'
516+
configure_env_file_path
517+
"
518+
[ "$status" -eq 0 ]
519+
[[ "$output" == *"Environment Variables: removed carriage returns from 2 lines"* ]]
520+
}
521+
522+
@test "env_file_path with LF-only lines does not report carriage returns (issue #28)" {
523+
local file="${BATS_TEST_TMPDIR}/lf.env"
524+
printf '%s\n' "DB_USER=admin" > "${file}"
525+
run env -i PATH="${PATH}" HOME="${BATS_TEST_TMPDIR}" \
526+
ENV_FILE_PATH="${file}" \
527+
bash -c "
528+
source '${ENTRYPOINT}'
529+
configure_env_file_path
530+
"
531+
[ "$status" -eq 0 ]
532+
[[ ! "$output" =~ "carriage returns" ]]
533+
}
534+
428535
# --- scale_after --------------------------------------------------------------
429536

430537
@test "scale_after does nothing when unset" {

0 commit comments

Comments
 (0)