Skip to content

Commit 18fc901

Browse files
committed
Pin, scope and audit the GitHub Actions examples
Pin actions/checkout to a commit, drop default token permissions, explain each job permission and name every job while keeping the socket-security check name. The ignore-command example now runs only for ignore commands. A test keeps the examples' action pins in step with the commits the repository's CI uses.
1 parent 4eed245 commit 18fc901

9 files changed

Lines changed: 179 additions & 34 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# Changelog
22

3+
## 2.14.1
4+
5+
### Changed
6+
7+
- The GitHub Actions examples pin `actions/checkout` to a commit (v7.0.1), set
8+
`permissions: {}` and explain each job permission, and pass `zizmor` and
9+
`actionlint`. The `socket-security` required check name is unchanged.
10+
- The ignore-command example runs only for comments that contain a
11+
`@SocketSecurity ignore` command instead of for every pull request comment.
12+
13+
### Documentation
14+
15+
- The [CI guide](docs/ci-cd.md#required-github-checks) explains that an ignore
16+
command doesn't update the required check until the pull request workflow runs
17+
again, and how to pin the `socketdev/cli` image to a release. The guide's own
18+
examples pin their actions to commits.
19+
320
## 2.14.0
421

522
### Upgrade notes

‎docs/ci-cd.md‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ jobs:
207207
count: ${{ steps.select.outputs.count }}
208208
matrix: ${{ steps.select.outputs.matrix }}
209209
steps:
210-
- uses: actions/checkout@v5
210+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
211211
with:
212212
fetch-depth: 0
213213
persist-credentials: false
@@ -293,12 +293,12 @@ jobs:
293293
name: Socket scan (${{ matrix.name }})
294294
runs-on: ubuntu-latest
295295
steps:
296-
- uses: actions/checkout@v5
296+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
297297
with:
298298
fetch-depth: 0
299299
persist-credentials: false
300300
301-
- uses: actions/setup-python@v6
301+
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
302302
with:
303303
python-version: '3.12'
304304
@@ -559,6 +559,15 @@ default-branch baseline and scan branch pushes. Use the
559559
[ignore-command workflow](../workflows/github-actions-comments.yml) if your
560560
repository handles ignore commands in PR comments.
561561

562+
An ignore command updates the Socket comment but not the required check, which
563+
keeps the result of the last PR scan. Re-run the PR workflow, or push a commit,
564+
to apply the ignore to the check.
565+
566+
The examples pin actions by commit SHA and use the `socketdev/cli:latest`
567+
image so they stay current. For reproducible runs, pin the image to a release
568+
tag and digest, such as `socketdev/cli:2.14.0@sha256:<digest>`, and update it
569+
when you upgrade.
570+
562571
GitHub normally checks the PR's test merge commit when it has a status and
563572
otherwise checks the head commit. The default PR checkout uses the test merge
564573
commit, while a push run uses the pushed commit. Keep the required job name
@@ -651,7 +660,7 @@ provider caches instead, and key that cache on the CLI version, which determines
651660
the engine version. In GitHub Actions:
652661

653662
```yaml
654-
- uses: actions/cache@v4
663+
- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
655664
with:
656665
path: ${{ runner.temp }}/socket-cache
657666
key: socket-cli-<version>

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66

77
[project]
88
name = "socketsecurity"
9-
version = "2.14.0"
9+
version = "2.14.1"
1010
requires-python = ">= 3.11"
1111
license = {"file" = "LICENSE"}
1212
dependencies = [

‎socketsecurity/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
__author__ = 'socket.dev'
2-
__version__ = '2.14.0'
2+
__version__ = '2.14.1'
33
USER_AGENT_NAME = 'socket-python-cli'
44
USER_AGENT = f'{USER_AGENT_NAME}/{__version__}'
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""The GitHub Actions examples stay pinned, current, and safe to require.
2+
3+
Dependabot updates the actions in `.github/workflows`, but not the copy-paste
4+
templates in `workflows/` or the snippets in the docs, which is how the templates
5+
fell four majors behind. These tests fail when an example uses an unpinned action
6+
or a different commit than the repository's own CI, so a Dependabot bump surfaces
7+
the examples that need the same update.
8+
"""
9+
import re
10+
from pathlib import Path
11+
12+
import pytest
13+
14+
ROOT = Path(__file__).resolve().parents[2]
15+
TEMPLATES = sorted((ROOT / "workflows").glob("github-actions*.yml"))
16+
EXAMPLES = TEMPLATES + [ROOT / "docs" / "ci-cd.md"]
17+
USES = re.compile(r"uses:\s*(?P<action>[\w.-]+/[\w./-]+)@(?P<ref>\S+)(?P<comment>[^\n]*)")
18+
PINNED = re.compile(r"^[0-9a-f]{40}$")
19+
20+
21+
def _uses(path):
22+
return [m.groupdict() | {"path": path.relative_to(ROOT)} for m in USES.finditer(path.read_text())]
23+
24+
25+
def _ci_pins():
26+
pins = {}
27+
files = list((ROOT / ".github" / "workflows").glob("*.yml"))
28+
files += list((ROOT / ".github" / "actions").glob("*/action.yml"))
29+
for path in files:
30+
for use in _uses(path):
31+
if PINNED.match(use["ref"]):
32+
pins.setdefault(use["action"], set()).add(use["ref"])
33+
return pins
34+
35+
36+
def test_the_templates_exist():
37+
assert {path.name for path in TEMPLATES} == {
38+
"github-actions.yml",
39+
"github-actions-push.yml",
40+
"github-actions-comments.yml",
41+
}
42+
43+
44+
@pytest.mark.parametrize("path", EXAMPLES, ids=lambda path: path.name)
45+
def test_examples_pin_actions_to_a_commit_with_its_version(path):
46+
unpinned = [
47+
f"{use['path']}: {use['action']}@{use['ref']}"
48+
for use in _uses(path)
49+
if not PINNED.match(use["ref"]) or not re.search(r"#\s*v\d", use["comment"])
50+
]
51+
assert not unpinned, "pin these to a full commit SHA with a `# vX.Y.Z` comment: " + ", ".join(unpinned)
52+
53+
54+
@pytest.mark.parametrize("path", EXAMPLES, ids=lambda path: path.name)
55+
def test_examples_use_the_same_commit_as_ci(path):
56+
pins = _ci_pins()
57+
stale = [
58+
f"{use['path']}: {use['action']}@{use['ref'][:12]} (CI uses "
59+
f"{', '.join(sorted(ref[:12] for ref in pins[use['action']]))})"
60+
for use in _uses(path)
61+
if use["action"] in pins and use["ref"] not in pins[use["action"]]
62+
]
63+
assert not stale, "update these to the commit .github/workflows uses: " + "; ".join(stale)
64+
65+
66+
def test_only_the_pull_request_workflow_reports_the_required_check():
67+
"""GitHub counts a skipped job as passing, so the required check needs its own workflow."""
68+
pr_workflow = (ROOT / "workflows" / "github-actions.yml").read_text()
69+
on_block = re.search(r"^on:\n((?:[ \t]+.*\n|\n)*)", pr_workflow, re.M).group(1)
70+
triggers = re.findall(r"^ (\w+):", on_block, re.M)
71+
assert triggers == ["pull_request"]
72+
for path in TEMPLATES:
73+
names = re.findall(r"^ ([\w-]+):\n|^ name: ([\w-]+)$", path.read_text(), re.M)
74+
reported = {job or name for job, name in names}
75+
if path.name == "github-actions.yml":
76+
assert "socket-security" in reported
77+
else:
78+
assert "socket-security" not in reported, f"{path.name} reports the required check name"

‎uv.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎workflows/github-actions-comments.yml‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
1-
# Copy this workflow to .github/workflows/ to handle PR ignore commands.
1+
# Socket Security ignore commands (optional).
2+
#
3+
# Copy to .github/workflows/ if reviewers reply `@SocketSecurity ignore <package>`
4+
# or `@SocketSecurity ignore-all` to accept alerts. This run updates the Socket
5+
# comment only. The required `socket-security` check keeps its result until the
6+
# pull request workflow runs again, so re-run that check (or push a commit) after
7+
# an ignore command.
28
name: Socket Security Ignore Commands
39

410
on:
5-
issue_comment:
11+
# issue_comment runs on the default branch with this repository's secrets.
12+
# This workflow never checks out pull request code, runs only for ignore
13+
# commands, and the CLI honors a command only from a commenter with write access.
14+
issue_comment: # zizmor: ignore[dangerous-triggers]
615
types: [created]
716

17+
permissions: {}
18+
819
concurrency:
9-
group: socket-ignore-${{ github.event.issue.number }}
20+
group: socket-security-ignore-${{ github.event.issue.number }}
1021
cancel-in-progress: false
1122

1223
jobs:
1324
socket-security-ignore:
14-
if: github.event.issue.pull_request
15-
permissions:
16-
contents: read
17-
issues: write
18-
pull-requests: write
25+
name: socket-security-ignore
26+
# Skip ordinary comments. contains() is case-insensitive and also matches
27+
# `ignore-all`; the CLI reads the command from the comment's first line.
28+
if: github.event.issue.pull_request && contains(github.event.comment.body, 'SocketSecurity ignore')
1929
runs-on: ubuntu-latest
20-
container: socketdev/cli:latest
30+
permissions:
31+
contents: read # check out the default branch
32+
issues: write # update the Socket comment and react to the command
33+
pull-requests: write # read the pull request's comments
34+
# Pinning: see the image comment in github-actions.yml.
35+
container: socketdev/cli:latest # zizmor: ignore[unpinned-images]
2136
steps:
22-
- uses: actions/checkout@v4
37+
- name: Check out the default branch
38+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
2339
with:
2440
fetch-depth: 0
2541
persist-credentials: false
2642

27-
- name: Handle Socket Security Ignore Commands
43+
- name: Apply Socket ignore commands
2844
env:
2945
SOCKET_SECURITY_API_KEY: ${{ secrets.SOCKET_SECURITY_API_KEY }}
3046
GH_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}

‎workflows/github-actions-push.yml‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1-
# Copy this workflow to .github/workflows/ to scan pushes and update the baseline.
1+
# Socket Security push scans.
2+
#
3+
# Copy to .github/workflows/ alongside github-actions.yml. Default-branch scans
4+
# are the baseline pull request comparisons run against, so without this
5+
# workflow those comparisons use an increasingly old scan. Push scans never
6+
# block; they report to the Socket Dashboard.
27
name: Socket Security Pushes
38

49
on:
510
push:
611
branches: ['**']
712

13+
permissions: {}
14+
815
concurrency:
9-
group: socket-push-${{ github.ref }}-${{ github.sha }}
16+
group: socket-security-push-${{ github.ref }}-${{ github.sha }}
1017
cancel-in-progress: true
1118

1219
jobs:
1320
socket-security-push:
14-
permissions:
15-
contents: read
21+
name: socket-security-push
1622
runs-on: ubuntu-latest
17-
container: socketdev/cli:latest
23+
permissions:
24+
contents: read # check out the repository
25+
# Pinning: see the image comment in github-actions.yml.
26+
container: socketdev/cli:latest # zizmor: ignore[unpinned-images]
1827
steps:
19-
- uses: actions/checkout@v4
28+
- name: Check out the repository
29+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
2030
with:
2131
fetch-depth: 0
2232
persist-credentials: false
2333

24-
- name: Run Socket Security Push Scan
34+
- name: Run the Socket push scan
2535
env:
2636
SOCKET_SECURITY_API_KEY: ${{ secrets.SOCKET_SECURITY_API_KEY }}
2737
GH_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}

‎workflows/github-actions.yml‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
1-
# Copy this workflow to .github/workflows/ to enable the PR scan.
1+
# Socket Security pull request scan.
2+
#
3+
# Copy to .github/workflows/ and make the `socket-security` job a required status
4+
# check. Keep that job name exclusive to this workflow: GitHub counts a skipped
5+
# job as passing, so another workflow reporting the same name could satisfy the
6+
# check without scanning the pull request.
7+
#
8+
# Companion workflows: github-actions-push.yml keeps the default-branch baseline
9+
# current, and github-actions-comments.yml (optional) handles ignore commands.
210
name: Socket Security Pull Requests
311

412
on:
513
pull_request:
614
types: [opened, synchronize, reopened, ready_for_review]
715

16+
permissions: {}
17+
818
concurrency:
9-
group: socket-pr-${{ github.event.pull_request.number }}
19+
group: socket-security-pr-${{ github.event.pull_request.number }}
1020
cancel-in-progress: true
1121

1222
jobs:
1323
socket-security:
14-
permissions:
15-
contents: read
16-
issues: write
17-
pull-requests: write
24+
name: socket-security
1825
runs-on: ubuntu-latest
19-
container: socketdev/cli:latest
26+
permissions:
27+
contents: read # check out the repository
28+
issues: write # post the Socket comments and react to ignore commands
29+
pull-requests: write # read the pull request and update its comments
30+
# `latest` keeps this template current with every release. For reproducible
31+
# runs, pin a release tag and digest instead, for example
32+
# socketdev/cli:2.14.0@sha256:<digest>, and update it when you upgrade.
33+
container: socketdev/cli:latest # zizmor: ignore[unpinned-images]
2034
steps:
21-
- uses: actions/checkout@v4
35+
- name: Check out the repository
36+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
2237
with:
2338
fetch-depth: 0
2439
persist-credentials: false
2540

26-
- name: Run Socket Security PR Scan
41+
- name: Run the Socket pull request scan
2742
env:
2843
SOCKET_SECURITY_API_KEY: ${{ secrets.SOCKET_SECURITY_API_KEY }}
2944
GH_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)