Skip to content

Commit a6c1184

Browse files
authored
Improve security of our GitHub Actions (#18413)
Recently CPython introduced this new tool: https://github.com/python/cpython/blob/8eebe4e6d02bb4ad3f1ca6c52624186903dce893/.pre-commit-config.yaml#L64-L67 Which finds different security related problems with GitHub Actions. I added this tool to our `.pre-commit-config.yaml` and followed all its recommendations. Changes: - I added `persist-credentials: false` to all `checkout` actions, see `# Whether to configure the token or SSH key with the local git config` in https://github.com/actions/checkout - I moved all permissions from workflow level to job level - I changed `.github/workflows/mypy_primer_comment.yml` to be a reusable workflow, see https://woodruffw.github.io/zizmor/audits/#dangerous-triggers
1 parent d7ebe2e commit a6c1184

9 files changed

+41
-15
lines changed

.github/workflows/build_wheels.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ on:
66
tags: ['*']
77

88
permissions:
9-
contents: write
9+
contents: read
1010

1111
jobs:
1212
build-wheels:
1313
if: github.repository == 'python/mypy'
1414
runs-on: ubuntu-latest
1515
steps:
1616
- uses: actions/checkout@v4
17+
with:
18+
persist-credentials: false
1719
- uses: actions/setup-python@v5
1820
with:
1921
python-version: '3.11'

.github/workflows/docs.yml

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ jobs:
3535
VERIFY_MYPY_ERROR_CODES: 1
3636
steps:
3737
- uses: actions/checkout@v4
38+
with:
39+
persist-credentials: false
3840
- uses: actions/setup-python@v5
3941
with:
4042
python-version: '3.12'

.github/workflows/mypy_primer.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ jobs:
2626
mypy_primer:
2727
name: Run mypy_primer
2828
runs-on: ubuntu-latest
29-
permissions:
30-
contents: read
3129
strategy:
3230
matrix:
3331
shard-index: [0, 1, 2, 3, 4]
@@ -38,6 +36,7 @@ jobs:
3836
with:
3937
path: mypy_to_test
4038
fetch-depth: 0
39+
persist-credentials: false
4140
- uses: actions/setup-python@v5
4241
with:
4342
python-version: "3.12"
@@ -93,8 +92,6 @@ jobs:
9392
name: Join artifacts
9493
runs-on: ubuntu-latest
9594
needs: [mypy_primer]
96-
permissions:
97-
contents: read
9895
steps:
9996
- name: Merge artifacts
10097
uses: actions/upload-artifact/merge@v4

.github/workflows/mypy_primer_comment.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
name: Comment with mypy_primer diff
22

3-
on:
3+
on: # zizmor: ignore[dangerous-triggers]
44
workflow_run:
55
workflows:
66
- Run mypy_primer
77
types:
88
- completed
99

10-
permissions:
11-
contents: read
12-
pull-requests: write
10+
permissions: {}
1311

1412
jobs:
1513
comment:
1614
name: Comment PR from mypy_primer
1715
runs-on: ubuntu-latest
16+
permissions:
17+
contents: read
18+
pull-requests: write
1819
if: ${{ github.event.workflow_run.conclusion == 'success' }}
1920
steps:
2021
- name: Download diffs

.github/workflows/sync_typeshed.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ on:
55
schedule:
66
- cron: "0 0 1,15 * *"
77

8-
permissions:
9-
contents: write
10-
pull-requests: write
8+
permissions: {}
119

1210
jobs:
1311
sync_typeshed:
1412
name: Sync typeshed
1513
if: github.repository == 'python/mypy'
1614
runs-on: ubuntu-latest
15+
permissions:
16+
contents: write
17+
pull-requests: write
1718
timeout-minutes: 10
1819
steps:
1920
- uses: actions/checkout@v4
2021
with:
2122
fetch-depth: 0
23+
persist-credentials: true # needed to `git push` the PR branch
2224
# TODO: use whatever solution ends up working for
2325
# https://github.com/python/typeshed/issues/8434
2426
- uses: actions/setup-python@v5

.github/workflows/test.yml

+4
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ jobs:
136136

137137
steps:
138138
- uses: actions/checkout@v4
139+
with:
140+
persist-credentials: false
139141

140142
- name: Debug build
141143
if: ${{ matrix.debug_build }}
@@ -217,6 +219,8 @@ jobs:
217219
CC: i686-linux-gnu-gcc
218220
steps:
219221
- uses: actions/checkout@v4
222+
with:
223+
persist-credentials: false
220224
- name: Install 32-bit build dependencies
221225
run: |
222226
sudo dpkg --add-architecture i386 && \

.github/workflows/test_stubgenc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ jobs:
2929
steps:
3030

3131
- uses: actions/checkout@v4
32+
with:
33+
persist-credentials: false
3234

3335
- name: Setup 🐍 3.9
3436
uses: actions/setup-python@v5

.pre-commit-config.yaml

+18-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ repos:
1616
- id: ruff
1717
args: [--exit-non-zero-on-fix]
1818
- repo: https://github.com/python-jsonschema/check-jsonschema
19-
rev: 0.30.0
19+
rev: 0.31.0
2020
hooks:
21-
- id: check-dependabot
2221
- id: check-github-workflows
22+
- id: check-github-actions
23+
- id: check-readthedocs
2324
- repo: https://github.com/rhysd/actionlint
2425
rev: v1.7.6
2526
hooks:
@@ -29,5 +30,20 @@ repos:
2930
-ignore=property "allow_failure" is not defined,
3031
-ignore=SC2(046|086),
3132
]
33+
additional_dependencies:
34+
# actionlint has a shellcheck integration which extracts shell scripts in `run:` steps from GitHub Actions
35+
# and checks these with shellcheck. This is arguably its most useful feature,
36+
# but the integration only works if shellcheck is installed
37+
- "github.com/wasilibs/go-shellcheck/cmd/[email protected]"
38+
- repo: https://github.com/woodruffw/zizmor-pre-commit
39+
rev: v1.0.1
40+
hooks:
41+
- id: zizmor
42+
# Should be the last one:
43+
- repo: meta
44+
hooks:
45+
- id: check-hooks-apply
46+
- id: check-useless-excludes
47+
3248
ci:
3349
autoupdate_schedule: quarterly

action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ branding:
3232
runs:
3333
using: composite
3434
steps:
35-
- name: mypy setup
35+
- name: mypy setup # zizmor: ignore[template-injection]
3636
shell: bash
3737
run: |
3838
echo ::group::Installing mypy...

0 commit comments

Comments
 (0)