Skip to content

Commit ce5053c

Browse files
committed
fix: prevent script injection in workflows (#318)
Fixes https://t.corp.amazon.com/V1559008677 Move github.event references to env vars to prevent script injection vulnerabilities in workflow run steps. This follows the same pattern as aws-observability/aws-otel-js-instrumentation@3d9ac9d By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent bbc70c3 commit ce5053c

File tree

7 files changed

+683
-71
lines changed

7 files changed

+683
-71
lines changed

.github/workflows/post_release_version_bump.yml

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ on:
66
version:
77
description: 'Version number (e.g., 1.0.1)'
88
required: true
9+
is_patch:
10+
description: 'Is this a patch? (true or false)'
11+
required: true
12+
default: 'false'
913

1014
env:
1115
AWS_DEFAULT_REGION: us-east-1
16+
VERSION_INPUT: ${{ github.event.inputs.version }}
17+
IS_PATCH_INPUT: ${{ github.event.inputs.is_patch }}
1218

1319
permissions:
1420
id-token: write
@@ -20,15 +26,15 @@ jobs:
2026
runs-on: ubuntu-latest
2127
steps:
2228
- name: Checkout main
23-
uses: actions/checkout@v2
29+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 #v5.0.0
2430
with:
2531
ref: main
2632
fetch-depth: 0
2733

2834
- name: Extract Major.Minor Version and setup Env variable
2935
run: |
30-
echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
31-
echo "MAJOR_MINOR=$(echo ${{ github.event.inputs.version }} | sed -E 's/([0-9]+\.[0-9]+)\.[0-9]+/\1/')" >> $GITHUB_ENV
36+
echo "VERSION=${{ env.VERSION_INPUT }}" >> $GITHUB_ENV
37+
echo "MAJOR_MINOR=$(echo ${{ env.VERSION_INPUT }} | sed -E 's/([0-9]+\.[0-9]+)\.[0-9]+/\1/')" >> $GITHUB_ENV
3238
3339
- name: Get current major.minor version from main branch
3440
id: get_version
@@ -58,21 +64,21 @@ jobs:
5864
needs: check-version
5965
steps:
6066
- name: Configure AWS credentials for BOT secrets
61-
uses: aws-actions/configure-aws-credentials@v4
67+
uses: aws-actions/configure-aws-credentials@a03048d87541d1d9fcf2ecf528a4a65ba9bd7838 #v5.0.0
6268
with:
6369
role-to-assume: ${{ secrets.AWS_ROLE_ARN_SECRETS_MANAGER }}
6470
aws-region: ${{ env.AWS_DEFAULT_REGION }}
6571

6672
- name: Get Bot secrets
67-
uses: aws-actions/aws-secretsmanager-get-secrets@v1
73+
uses: aws-actions/aws-secretsmanager-get-secrets@a9a7eb4e2f2871d30dc5b892576fde60a2ecc802 #v2.0.10
6874
id: bot_secrets
6975
with:
7076
secret-ids: |
7177
BOT_TOKEN ,${{ secrets.BOT_TOKEN_SECRET_ARN }}
7278
parse-json-secrets: true
7379

7480
- name: Setup Git
75-
uses: actions/checkout@v2
81+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 #v5.0.0
7682
with:
7783
fetch-depth: 0
7884
token: ${{ env.BOT_TOKEN_GITHUB_RW_PATOKEN }}
@@ -84,8 +90,8 @@ jobs:
8490
8591
- name: Extract Major.Minor Version and setup Env variable
8692
run: |
87-
echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
88-
echo "MAJOR_MINOR=$(echo ${{ github.event.inputs.version }} | sed -E 's/([0-9]+\.[0-9]+)\.[0-9]+/\1/')" >> $GITHUB_ENV
93+
echo "VERSION=${{ env.VERSION_INPUT }}" >> $GITHUB_ENV
94+
echo "MAJOR_MINOR=$(echo ${{ env.VERSION_INPUT }} | sed -E 's/([0-9]+\.[0-9]+)\.[0-9]+/\1/')" >> $GITHUB_ENV
8995
9096
- name: Determine release branch and checkout
9197
run: |
@@ -95,27 +101,49 @@ jobs:
95101
96102
- name: Update version to next development version in main
97103
run: |
98-
DEV_VERSION="${{ github.event.inputs.version }}.dev0"
104+
DEV_VERSION="${{ env.VERSION_INPUT }}.dev0"
99105
sed -i "s/public static string version = \".*\";/public static string version = \"${DEV_VERSION}\";/" src/AWS.Distro.OpenTelemetry.AutoInstrumentation/Version.cs
100106
sed -i "s/private readonly string version = \".*\";/private readonly string version = \"${DEV_VERSION}\";/" build/Build.InstallationScripts.cs
101-
VERSION="${{ github.event.inputs.version }}"
102-
sed -i -e 's/dotnet:v.*"/dotnet:v'$VERSION'"/' .github/workflows/daily_scan.yml
107+
VERSION="${{ env.VERSION_INPUT }}"
108+
sed -i -e 's/dotnet:v.*"/dotnet:v'$VERSION'"/' .github/workflows/daily-scan.yml
109+
110+
# for patch releases, avoid merge conflict by manually resolving CHANGELOG with main
111+
if [[ "${{ env.IS_PATCH_INPUT }}" == "true" ]]; then
112+
# Copy the patch release entries
113+
sed -n "/^## v${VERSION}/,/^## v[0-9]/p" CHANGELOG.md | sed '$d' > /tmp/patch_release_section.txt
114+
git fetch origin main
115+
git show origin/main:CHANGELOG.md > CHANGELOG.md
116+
# Insert the patch release entries after Unreleased
117+
awk -i inplace '/^## v[0-9]/ && !inserted { system("cat /tmp/patch_release_section.txt"); inserted=1 } {print}' CHANGELOG.md
118+
fi
119+
103120
git add src/AWS.Distro.OpenTelemetry.AutoInstrumentation/Version.cs
104121
git add build/Build.InstallationScripts.cs
105-
git add .github/workflows/daily_scan.yml
122+
git add .github/workflows/daily-scan.yml
123+
git add CHANGELOG.md
106124
git commit -m "Prepare main for next development cycle: Update version to $DEV_VERSION"
107125
git push --set-upstream origin "prepare-main-for-next-dev-cycle-${VERSION}"
108126
109127
- name: Create Pull Request to main
110128
env:
111129
GITHUB_TOKEN: ${{ env.BOT_TOKEN_GITHUB_RW_PATOKEN }}
112130
run: |
113-
DEV_VERSION="${{ github.event.inputs.version }}.dev0"
131+
DEV_VERSION="${{ env.VERSION_INPUT }}.dev0"
114132
gh pr create --title "Post release $VERSION: Update version to $DEV_VERSION" \
115133
--body "This PR prepares the main branch for the next development cycle by updating the version to $DEV_VERSION and updating the image version to be scanned to the latest released.
116134
117135
This PR should only be merge when release for version v$VERSION is success.
118136
119137
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice." \
120138
--head prepare-main-for-next-dev-cycle-${VERSION} \
121-
--base main
139+
--base main
140+
141+
- name: Force our CHANGELOG to override merge conflicts
142+
run: |
143+
git merge origin/main || true
144+
git checkout --ours CHANGELOG.md
145+
git add CHANGELOG.md
146+
if ! git diff --quiet --cached; then
147+
git commit -m "Force our CHANGELOG to override merge conflicts"
148+
git push origin "prepare-main-for-next-dev-cycle-${VERSION}"
149+
fi

.github/workflows/pre_release_prepare.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ on:
1414

1515
env:
1616
AWS_DEFAULT_REGION: us-east-1
17+
VERSION_INPUT: ${{ github.event.inputs.version }}
18+
IS_PATCH_INPUT: ${{ github.event.inputs.is_patch }}
1719

1820
permissions:
1921
contents: write
@@ -52,12 +54,12 @@ jobs:
5254
5355
- name: Extract Major.Minor Version and setup Env variable
5456
run: |
55-
echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
56-
echo "MAJOR_MINOR=$(echo ${{ github.event.inputs.version }} | sed -E 's/([0-9]+\.[0-9]+)\.[0-9]+/\1/')" >> $GITHUB_ENV
57+
echo "VERSION=${{ env.VERSION_INPUT }}" >> $GITHUB_ENV
58+
echo "MAJOR_MINOR=$(echo ${{ env.VERSION_INPUT }} | sed -E 's/([0-9]+\.[0-9]+)\.[0-9]+/\1/')" >> $GITHUB_ENV
5759
5860
- name: Create branches
5961
run: |
60-
IS_PATCH=${{ github.event.inputs.is_patch }}
62+
IS_PATCH=${{ env.IS_PATCH_INPUT }}
6163
if [[ "$IS_PATCH" != "true" && "$IS_PATCH" != "false" ]]; then
6264
echo "Invalid input for IS_PATCH. Must be 'true' or 'false'."
6365
exit 1
@@ -103,5 +105,5 @@ jobs:
103105
--body "This PR updates the version to ${VERSION}.
104106
105107
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice." \
106-
--head v${{ github.event.inputs.version }}_release \
108+
--head v${{ env.VERSION_INPUT }}_release \
107109
--base release/v${MAJOR_MINOR}.x

0 commit comments

Comments
 (0)