Skip to content

Commit 3ccb0c6

Browse files
Merge pull request #204 from aligent/fix/DO-2055_handle_trailing_env_newlines
DO-2055: improve extra env variable support
2 parents 0cf1823 + c4f0a41 commit 3ccb0c6

8 files changed

Lines changed: 324 additions & 102 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
name: Convert legacy environment variables
2+
description: |
3+
Converts the legacy KEY=value format, where any line that doesn't start a new KEY=
4+
continues the previous value (e.g. SSH keys), to $GITHUB_ENV format for export-env-vars.
5+
KEY<<DELIM blocks in the input are passed through, so anything $GITHUB_ENV accepts is
6+
also accepted. With mask set to 'true', every line of the output is masked.
7+
Warns if continuation lines are used.
8+
9+
inputs:
10+
value:
11+
description: Newline-separated KEY=value pairs in the legacy format, or $GITHUB_ENV format
12+
required: false
13+
default: ''
14+
mask:
15+
description: "'true' to mask every line of the output, 'false' if the input contains no secrets"
16+
required: true
17+
18+
outputs:
19+
value:
20+
description: The input in $GITHUB_ENV format, every variable written as a KEY<<DELIM block
21+
value: ${{ steps.convert.outputs.value }}
22+
23+
runs:
24+
using: composite
25+
steps:
26+
- name: Convert to GITHUB_ENV format
27+
id: convert
28+
shell: bash
29+
env:
30+
INPUT_VALUE: ${{ inputs.value }}
31+
INPUT_MASK: ${{ inputs.mask }}
32+
run: |
33+
# The runner doesn't enforce required inputs, and a missing mask would otherwise
34+
# mean no masking, so fail unless it is set explicitly.
35+
case "$INPUT_MASK" in
36+
true | false) ;;
37+
*)
38+
echo "::error title=convert-legacy-env-vars::The mask input must be 'true' or 'false', got '$INPUT_MASK'"
39+
exit 1
40+
;;
41+
esac
42+
43+
# Every variable is written as a KEY<<DELIM block. CRs are left in place for
44+
# export-env-vars, which strips them and warns about them.
45+
# awk prints its warning to stderr: only its stdout is captured, and the runner
46+
# processes workflow commands on stderr as well as stdout.
47+
converted=$(awk -v delim="EOF_$(cat /proc/sys/kernel/random/uuid)" '
48+
# Write the current variable as a block. Trailing blank lines are dropped, as
49+
# node-pr always did; blank lines inside a value are kept. The delimiter is
50+
# random so no value can end the block early.
51+
function flush() {
52+
if (key == "") return
53+
sub(/(\n[[:space:]]*)+$/, "", value)
54+
print key "<<" delim
55+
print value
56+
print delim
57+
key = ""
58+
value = ""
59+
}
60+
61+
# Inside a KEY<<DELIM block from the input: pass it through unchanged.
62+
block != "" {
63+
print
64+
if ($0 == block) block = ""
65+
next
66+
}
67+
68+
# KEY<<DELIM header (<< before any =, the same rule as the runner):
69+
# finish the current variable, then pass the block through.
70+
match($0, /^[^=]*<</) && index(substr($0, 1, RLENGTH), "=") == 0 {
71+
flush()
72+
print
73+
block = substr($0, RLENGTH + 1)
74+
next
75+
}
76+
77+
# KEY=value starts a new variable.
78+
/^[A-Za-z_][A-Za-z0-9_]*=/ {
79+
flush()
80+
i = index($0, "=")
81+
key = substr($0, 1, i - 1)
82+
value = substr($0, i + 1)
83+
next
84+
}
85+
86+
# Any other line continues the current variable. A non-blank continuation line
87+
# means the legacy multiline format is in use (blank lines are just trailing
88+
# newlines, which are common and harmless).
89+
key != "" {
90+
value = value "\n" $0
91+
if ($0 ~ /[^[:space:]]/) legacy = 1
92+
next
93+
}
94+
95+
# Before the first variable: pass through and let the runner validate it
96+
# (blank lines are skipped, anything else is a format error).
97+
{ print }
98+
99+
END {
100+
flush()
101+
if (legacy) {
102+
print "::warning title=convert-legacy-env-vars::Input uses continuation lines for multiline values,",
103+
"a legacy format that can misread base64 lines as new variables.",
104+
"Use a KEY<<DELIMITER block for multiline values instead." > "/dev/stderr"
105+
}
106+
}
107+
' <<< "$INPUT_VALUE")
108+
109+
# The converted text is passed on through `with:` (e.g. to export-env-vars), where it
110+
# is shown in the log and no longer matches the original secret, so GitHub would not
111+
# mask it. Mask the whole output with a single add-mask: the runner decodes %25, %0D
112+
# and %0A in the value, then masks the whole value and each of its lines (ignoring
113+
# empty ones). sed -z reads the whole output as one record so newlines can be escaped.
114+
# Skipped if there is nothing to mask, as the runner warns about empty add-mask values.
115+
if [ "$INPUT_MASK" = "true" ] && [[ "$converted" =~ [^[:space:]] ]]; then
116+
echo "::add-mask::$(sed -z 's/%/%25/g; s/\r/%0D/g; s/\n/%0A/g' <<< "$converted")"
117+
fi
118+
119+
out_delim="EOF_$(cat /proc/sys/kernel/random/uuid)"
120+
{
121+
echo "value<<$out_delim"
122+
printf '%s\n' "$converted"
123+
echo "$out_delim"
124+
} >> "$GITHUB_OUTPUT"
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: Export environment variables
2+
description: |
3+
Appends KEY=value lines (and KEY<<DELIM multiline blocks) to $GITHUB_ENV so later
4+
steps in the job can use them. With mask set to 'true', every value is masked before
5+
it is exported.
6+
7+
Warning: only pass trusted input, such as repository variables and secrets. The input
8+
can set any environment variable for later steps, including ones that run code
9+
(BASH_ENV, LD_PRELOAD, NODE_OPTIONS), so never include data an attacker can influence,
10+
such as pull request titles or branch names.
11+
12+
inputs:
13+
value:
14+
description: Newline-separated KEY=value pairs, or KEY<<DELIM blocks for multiline values
15+
required: false
16+
default: ''
17+
mask:
18+
description: "'true' to mask every value before it is exported, 'false' if the input contains no secrets"
19+
required: true
20+
21+
runs:
22+
using: composite
23+
steps:
24+
- name: Export environment variables
25+
shell: bash
26+
env:
27+
INPUT_VALUE: ${{ inputs.value }}
28+
INPUT_MASK: ${{ inputs.mask }}
29+
run: | # zizmor: ignore[github-env] see the comment above the $GITHUB_ENV write
30+
# The runner doesn't enforce required inputs, and a missing mask would otherwise
31+
# mean no masking, so fail unless it is set explicitly.
32+
case "$INPUT_MASK" in
33+
true | false) ;;
34+
*)
35+
echo "::error title=export-env-vars::The mask input must be 'true' or 'false', got '$INPUT_MASK'"
36+
exit 1
37+
;;
38+
esac
39+
40+
# CRs are stripped from the input (tr -d '\r'): on Linux the runner only splits on LF,
41+
# so CRLF input would leave a trailing CR on values and break KEY<<EOF delimiter matching.
42+
if [[ "$INPUT_VALUE" == *$'\r'* ]]; then
43+
echo "::warning title=export-env-vars::Input contains carriage returns (\\r), probably from Windows (CRLF)" \
44+
"line endings. They have been removed; re-save the variable or secret with LF line endings" \
45+
"to silence this warning."
46+
fi
47+
48+
# Mask values here, before they reach $GITHUB_ENV, so they are already masked
49+
# when later steps list the environment in their log headers.
50+
# The runner still does the parsing; this only works out which text is a value,
51+
# using the same rule as the runner: a line is KEY<<DELIM only if << comes before any =.
52+
if [ "$INPUT_MASK" = "true" ]; then
53+
awk '
54+
# Emit an add-mask command, skipping blank or whitespace-only values.
55+
# % is escaped because the runner decodes %25, %0D and %0A in add-mask values.
56+
function mask(s) {
57+
if (s ~ /[^[:space:]]/) {
58+
gsub(/%/, "%25", s)
59+
print "::add-mask::" s
60+
}
61+
}
62+
63+
# Remove CRs, as they are removed from what is written to $GITHUB_ENV.
64+
{ gsub(/\r/, "") }
65+
66+
# Inside a KEY<<DELIM block: mask each line whole (even if it contains =)
67+
# until the closing delimiter, which is not masked.
68+
delim != "" {
69+
if ($0 == delim) delim = ""
70+
else mask($0)
71+
next
72+
}
73+
74+
# KEY<<DELIM header: remember the delimiter; the header itself holds no value.
75+
match($0, /^[^=]*<</) && index(substr($0, 1, RLENGTH), "=") == 0 {
76+
delim = substr($0, RLENGTH + 1)
77+
next
78+
}
79+
80+
# KEY=value: mask everything after the first =.
81+
index($0, "=") {
82+
mask(substr($0, index($0, "=") + 1))
83+
}
84+
' <<< "$INPUT_VALUE"
85+
fi
86+
87+
# Writing to $GITHUB_ENV lets the input set any variable for later steps, including
88+
# ones that run code (BASH_ENV, LD_PRELOAD, NODE_OPTIONS), which is why zizmor flags
89+
# it. Exporting caller-supplied variables is this action's purpose, so callers must
90+
# only pass trusted input (repository variables and secrets), never data an attacker
91+
# can influence, such as pull request titles or branch names.
92+
tr -d '\r' <<< "$INPUT_VALUE" >> "$GITHUB_ENV"

‎.github/workflows/aio-app-deployment.yml‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,17 @@ jobs:
125125
IMSORGID: ${{ secrets.AIO_IMS_ORG_ID }}
126126
SCOPES: ${{ secrets.AIO_SCOPES }}
127127

128-
- name: Export extra environment variables
129-
env:
130-
EXTRA_VARS: ${{ vars.AIO_DEPLOY_EXTRA_VARS }}
131-
EXTRA_SECRETS: ${{ secrets.AIO_DEPLOY_EXTRA_SECRETS }}
132-
run: |
133-
if [ -n "$EXTRA_VARS" ]; then
134-
while IFS= read -r line; do
135-
[ -n "$line" ] && echo "$line" | tr -d '\r' >> "$GITHUB_ENV"
136-
done <<< "$EXTRA_VARS"
137-
fi
138-
if [ -n "$EXTRA_SECRETS" ]; then
139-
while IFS= read -r line; do
140-
[ -n "$line" ] && echo "$line" | tr -d '\r' >> "$GITHUB_ENV"
141-
done <<< "$EXTRA_SECRETS"
142-
fi
128+
- name: Export extra regular environment variables
129+
uses: $/.github/actions/export-env-vars
130+
with:
131+
value: ${{ vars.AIO_DEPLOY_EXTRA_VARS }}
132+
mask: false
133+
134+
- name: Export extra secret environment variables
135+
uses: $/.github/actions/export-env-vars
136+
with:
137+
value: ${{ secrets.AIO_DEPLOY_EXTRA_SECRETS }}
138+
mask: true
143139

144140
- name: Deploy
145141
working-directory: ${{ inputs.app-directory }}

‎.github/workflows/aws-cdk.yml‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,21 +216,17 @@ jobs:
216216
;;
217217
esac
218218
219-
- name: Export extra environment variables
220-
env:
221-
EXTRA_SECRETS: ${{ secrets.EXTRA_SECRETS }}
222-
EXTRA_VARS: ${{ vars.EXTRA_VARS }}
223-
run: |
224-
if [ -n "$EXTRA_VARS" ]; then
225-
while IFS= read -r line; do
226-
[ -n "$line" ] && echo "$line" | tr -d '\r' >> "$GITHUB_ENV"
227-
done <<< "$EXTRA_VARS"
228-
fi
229-
if [ -n "$EXTRA_SECRETS" ]; then
230-
while IFS= read -r line; do
231-
[ -n "$line" ] && echo "$line" | tr -d '\r' >> "$GITHUB_ENV"
232-
done <<< "$EXTRA_SECRETS"
233-
fi
219+
- name: Export extra regular environment variables
220+
uses: $/.github/actions/export-env-vars
221+
with:
222+
value: ${{ vars.EXTRA_VARS }}
223+
mask: false
224+
225+
- name: Export extra secret environment variables
226+
uses: $/.github/actions/export-env-vars
227+
with:
228+
value: ${{ secrets.EXTRA_SECRETS }}
229+
mask: true
234230

235231
- name: Expand shell variables in inputs
236232
id: expand-inputs

0 commit comments

Comments
 (0)