Skip to content

Commit 5ebf9f1

Browse files
authored
ci: avoid interpolating inputs into run: scripts (#740)
Github Actions makes it easy to inject arbitrary shell code into Github Actions scripts thanks to the way its templating language works. This change mediates that issue by passing action inputs to the `run:` scripts as env vars instead of using `${{ }}` expansions directly in the script bodies. The pr_labeler job is the only one that both runs on pull requests and has access to secrets, but we don't interpolate anything other than `github.event.number`, so that wouldn't allow any malicious person to steal credentials. reusable-pip-compile has access to secrets and accepts user input, but only from trusted sources (i.e., developers who already have write access to this repository) and can manually trigger workflows. Still, it's a good to tighten this up.
1 parent d646862 commit 5ebf9f1

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

.github/workflows/labeler.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ jobs:
5353
env:
5454
event_json: "${{ toJSON(github.event) }}"
5555
GITHUB_TOKEN: ${{ steps.create_token.outputs.token }}
56-
run:
57-
./venv/bin/python hacking/pr_labeler/label.py issue ${{ github.event.issue.number || inputs.number }}
56+
number: "${{ github.event.issue.number || inputs.number }}"
57+
run: |
58+
./venv/bin/python hacking/pr_labeler/label.py issue "${number}"
5859
- name: "Run the PR labeler"
5960
if: "github.event.pull_request || inputs.type == 'pr'"
6061
env:
6162
event_json: "${{ toJSON(github.event) }}"
6263
GITHUB_TOKEN: ${{ steps.create_token.outputs.token }}
63-
run:
64-
./venv/bin/python hacking/pr_labeler/label.py pr ${{ github.event.number || inputs.number }}
64+
number: "${{ github.event.number || inputs.number }}"
65+
run: |
66+
./venv/bin/python hacking/pr_labeler/label.py pr "${number}"

.github/workflows/reusable-pip-compile.yml

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,43 +82,52 @@ jobs:
8282
run: |
8383
hacking/get_bot_user.sh "ansible-documentation-bot" "Ansible Documentation Bot"
8484
- name: "Use a branch named ${{ inputs.pr-branch }}"
85+
env:
86+
base_branch: "${{ inputs.base-branch }}"
87+
pr_branch: "${{ inputs.pr-branch }}"
8588
id: branch
8689
run: |
8790
set -x
88-
if git branch -r | grep "origin/${{ inputs.pr-branch }}"; then
91+
if git branch -r | grep "origin/${pr_branch}"; then
8992
echo "branch-exists=true" >> "${GITHUB_OUTPUT}"
90-
git switch "${{ inputs.pr-branch }}"
93+
git switch "${pr_branch}"
9194
${{ inputs.reset-branch && 'git reset --hard' || 'git rebase' }} \
92-
"${{ inputs.base-branch }}"
95+
"${base_branch}"
9396
else
9497
echo "branch-exists=false" >> "${GITHUB_OUTPUT}"
95-
git switch -c "${{ inputs.pr-branch }}"
98+
git switch -c "${pr_branch}"
9699
fi
97100
- name: "Run nox ${{ inputs.nox-args }}"
98101
env:
99102
# Ensure the latest pip version is used
100103
VIRTUALENV_DOWNLOAD: '1'
104+
#
105+
nox_args: "${{ inputs.nox-args }}"
101106
run: |
102-
nox ${{ inputs.nox-args }}
107+
nox ${nox_args}
103108
- name: Push new dependency versions and create a PR
104109
env:
105110
GITHUB_TOKEN: ${{ steps.create_token.outputs.token }}
111+
base_branch: "${{ inputs.base-branch }}"
112+
pr_branch: "${{ inputs.pr-branch }}"
113+
message: "${{ inputs.message }}"
114+
changed_files: "${{ inputs.changed-files }}"
106115
run: |
107116
set -x
108117
git diff || :
109-
git add ${{ inputs.changed-files }}
110-
if git diff-index --quiet HEAD ${{ inputs.changed-files }}; then
118+
git add ${changed_files}
119+
if git diff-index --quiet HEAD ${changed_files}; then
111120
echo "Nothing to do!"
112121
exit
113122
fi
114123
115-
git commit -m "${{ inputs.message }}"
116-
git push --force origin "${{ inputs.pr-branch }}"
124+
git commit -m "${message}"
125+
git push --force origin "${pr_branch}"
117126
if [ "${{ steps.branch.outputs.branch-exists }}" = "false" ]
118127
then
119128
gh pr create \
120-
--base "${{ inputs.base-branch }}" \
121-
--title "${{ inputs.message }}" \
129+
--base "${base_branch}" \
130+
--title "${message}" \
122131
--body "" \
123132
--label dependency_update
124133
fi

0 commit comments

Comments
 (0)