Skip to content

Commit e647753

Browse files
committed
ci: fix command injection and harden workflow security
- Use env variables instead of direct expression interpolation in shell scripts - Add persist-credentials: false to checkout steps - Remove unnecessary pull-requests: write permission
1 parent df8aa75 commit e647753

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ jobs:
5757
with:
5858
token: ${{ secrets.GH_TOKEN }}
5959
submodules: true
60+
persist-credentials: false
6061

6162
- uses: ./.github/actions/setup-docker
6263
id: setup-docker
@@ -69,16 +70,24 @@ jobs:
6970
echo "NPM_REGISTRY=https://registry.npmmirror.com" >> $GITHUB_ENV
7071
7172
- name: Build and Push Docker Image
73+
env:
74+
EVENT_NAME: ${{ github.event_name }}
75+
UNIVER_DOCS_SITE: ${{ inputs.univer-docs-site }}
76+
BUILDER_NAME: ${{ steps.setup-docker.outputs.builder-name }}
77+
PUSH_TAG_AS_LATEST: ${{ vars.PUSH_TAG_AS_LATEST }}
78+
CR: ${{ env.CR }}
79+
NPM_REGISTRY: ${{ env.NPM_REGISTRY }}
80+
NEXT_POSTHOG_APIKEY: ${{ secrets.NEXT_POSTHOG_APIKEY }}
7281
run: |
73-
IMAGE_TAG=$(echo "${{ github.sha }}" | cut -c 1-7)
74-
if [ "${{ github.event_name }}" == "schedule" ] || [ "${{ inputs.univer-docs-site }}" == "true" ]; then
82+
IMAGE_TAG=$(echo "$GITHUB_SHA" | cut -c 1-7)
83+
if [ "$EVENT_NAME" == "schedule" ] || [ "$UNIVER_DOCS_SITE" == "true" ]; then
7584
make push_image REPOSITORY=univer-docs-site \
76-
PUSH_TAG=$IMAGE_TAG \
77-
BUILDER=${{ steps.setup-docker.outputs.builder-name }} \
85+
PUSH_TAG="$IMAGE_TAG" \
86+
BUILDER="$BUILDER_NAME" \
7887
OSARCH=linux/amd64 \
79-
PUSH_TAG_AS_LATEST=${{ vars.PUSH_TAG_AS_LATEST }} \
80-
CR=${{ env.CR }} NPM_REGISTRY=${{ env.NPM_REGISTRY }} \
81-
NEXT_POSTHOG_APIKEY=${{ secrets.NEXT_POSTHOG_APIKEY }}
88+
PUSH_TAG_AS_LATEST="$PUSH_TAG_AS_LATEST" \
89+
CR="$CR" NPM_REGISTRY="$NPM_REGISTRY" \
90+
NEXT_POSTHOG_APIKEY="$NEXT_POSTHOG_APIKEY"
8291
fi
8392
8493
deploy-site:
@@ -90,16 +99,18 @@ jobs:
9099

91100
steps:
92101
- name: Set IMAGE_TAG environment variable
102+
env:
103+
IMAGE_TAG_INPUT: ${{ inputs.image-tag }}
93104
run: |
94-
if [ -z "${{ inputs.image-tag }}" ]; then
95-
echo "IMAGE_TAG=$(echo ${{ github.sha }} | cut -c 1-7)" >> $GITHUB_ENV
105+
if [ -z "$IMAGE_TAG_INPUT" ]; then
106+
echo "IMAGE_TAG=$(echo "$GITHUB_SHA" | cut -c 1-7)" >> "$GITHUB_ENV"
96107
else
97-
echo "IMAGE_TAG=${{ inputs.image-tag }}" >> $GITHUB_ENV
108+
echo "IMAGE_TAG=$IMAGE_TAG_INPUT" >> "$GITHUB_ENV"
98109
fi
99110
100111
- name: Set FEATURE environment variable
101112
if: ${{ startsWith(github.event_name == 'schedule' && 'staging' || inputs.environment, 'feature') }}
102-
run: echo "FEATURE=$(echo ${GITHUB_REF#refs/heads/feat/})" >> $GITHUB_ENV
113+
run: echo "FEATURE=$(echo "${GITHUB_REF#refs/heads/feat/}")" >> "$GITHUB_ENV"
103114

104115
- uses: peter-evans/repository-dispatch@v2
105116
name: Deploy Docs Site
@@ -124,21 +135,23 @@ jobs:
124135

125136
steps:
126137
- name: Set IMAGE_TAG environment variable
138+
env:
139+
IMAGE_TAG_INPUT: ${{ inputs.image-tag }}
127140
run: |
128-
if [ -z "${{ inputs.image-tag }}" ]; then
129-
echo "IMAGE_TAG=$(echo ${{ github.sha }} | cut -c 1-7)" >> $GITHUB_ENV
141+
if [ -z "$IMAGE_TAG_INPUT" ]; then
142+
echo "IMAGE_TAG=$(echo "$GITHUB_SHA" | cut -c 1-7)" >> "$GITHUB_ENV"
130143
else
131-
echo "IMAGE_TAG=${{ inputs.image-tag }}" >> $GITHUB_ENV
144+
echo "IMAGE_TAG=$IMAGE_TAG_INPUT" >> "$GITHUB_ENV"
132145
fi
133146
134147
- name: Set FEATURE environment variable
135148
if: ${{ startsWith(github.event_name == 'schedule' && 'staging' || inputs.environment, 'feature') }}
136-
run: echo "FEATURE=$(echo ${GITHUB_REF#refs/heads/feat/})" >> $GITHUB_ENV
149+
run: echo "FEATURE=$(echo "${GITHUB_REF#refs/heads/feat/}")" >> "$GITHUB_ENV"
137150

138151
- name: Set US ACR region
139152
if: ${{ inputs.acr-region == 'us-east-1' || inputs.environment == 'prod' || inputs.environment == 'feature prod' }}
140153
run: |
141-
echo "CR=univer-acr-eastus-registry.us-east-1.cr.aliyuncs.com" >> $GITHUB_ENV
154+
echo "CR=univer-acr-eastus-registry.us-east-1.cr.aliyuncs.com" >> "$GITHUB_ENV"
142155
143156
- uses: peter-evans/repository-dispatch@v2
144157
name: Deploy Docs Site

.github/workflows/test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ concurrency:
1212

1313
permissions:
1414
contents: read
15-
pull-requests: write
1615

1716
jobs:
1817
build:
@@ -23,6 +22,7 @@ jobs:
2322
with:
2423
submodules: recursive
2524
token: ${{ secrets.GH_TOKEN }}
25+
persist-credentials: false
2626

2727
- name: Setup Node.js
2828
uses: ./.github/actions/setup-node
@@ -38,6 +38,7 @@ jobs:
3838
with:
3939
submodules: recursive
4040
token: ${{ secrets.GH_TOKEN }}
41+
persist-credentials: false
4142

4243
- name: Setup Node.js
4344
uses: ./.github/actions/setup-node

0 commit comments

Comments
 (0)