Repository navigation
Expand file tree
/
Copy pathaction.yml
More file actions
119 lines (112 loc) · 5.49 KB
/
Copy pathaction.yml
File metadata and controls
119 lines (112 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
name: Python Security Auditing
description: Run bandit and pip-audit security scans on Python code and report findings
author: Development Seed
inputs:
tools:
description: Comma-separated tools to run (bandit, pip-audit)
default: bandit,pip-audit
bandit_scan_dirs:
description: Comma-separated directories for bandit to scan
default: .
bandit_severity_threshold:
description: Minimum bandit severity that blocks the job (high, medium, low)
default: high
pip_audit_block_on:
description: When to block on pip-audit findings — fixable, all, or none
default: fixable
package_manager:
description: How to resolve dependencies for pip-audit (uv, poetry, pipenv, requirements)
default: requirements
requirements_file:
description: Path to requirements file when package_manager=requirements
default: requirements.txt
comment_on:
description: When to post a PR comment — never, blocking (only when issues block the job), or always
default: 'never'
github_token:
description: GitHub token used for posting PR comments
default: ${{ github.token }}
working_directory:
description: Directory to run the audit from (useful when the project is in a subdirectory)
default: '.'
artifact_name:
description: Name of the artifact uploaded by the action
default: security-audit-reports
debug:
description: Enable debug logging for the Python audit module
default: 'false'
dependency_cooldown_days:
description: Skip releases of the action's own dependencies younger than this many days; 0 turns it off (needed for an index without upload times)
default: '7'
runs:
using: composite
steps:
# A report left by an earlier job (self-hosted runner) must not be uploaded as this one's.
- name: Remove an earlier bandit report
if: contains(inputs.tools, 'bandit')
shell: bash
run: rm -f -- "$GITHUB_WORKSPACE/results.sarif"
# No `python-version` here: it sets UV_PYTHON for the rest of the caller's job, overriding
# their project's Python. The audit step passes --python instead.
- name: Set up uv
uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2
# Dependencies resolve fresh on every run but skip releases younger than the cooldown, the
# window in which a hijacked PyPI release is usually caught. A flag, not UV_EXCLUDE_NEWER, so
# the caller's own `uv export` still audits their lock as committed.
- name: Run security audit
id: audit
shell: bash
continue-on-error: true
working-directory: ${{ inputs.working_directory }}
env:
TOOLS: ${{ inputs.tools }}
BANDIT_SCAN_DIRS: ${{ inputs.bandit_scan_dirs }}
BANDIT_SEVERITY_THRESHOLD: ${{ inputs.bandit_severity_threshold }}
BANDIT_SARIF_PATH: ${{ github.workspace }}/results.sarif
PIP_AUDIT_BLOCK_ON: ${{ inputs.pip_audit_block_on }}
PACKAGE_MANAGER: ${{ inputs.package_manager }}
REQUIREMENTS_FILE: ${{ inputs.requirements_file }}
COMMENT_ON: ${{ inputs.comment_on }}
GITHUB_TOKEN: ${{ inputs.github_token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
INPUT_DEBUG: ${{ inputs.debug }}
RUNNER_DEBUG: ${{ runner.debug }}
COOLDOWN_DAYS: ${{ inputs.dependency_cooldown_days }}
run: |
cooldown=()
if [ "$COOLDOWN_DAYS" != 0 ]; then
[[ "$COOLDOWN_DAYS" =~ ^[0-9]+$ ]] || { echo "::error::dependency_cooldown_days must be a whole number of days"; exit 1; }
# An absolute time: uv before 0.9.17 rejects "7 days", and setup-uv installs the uv a
# caller pins. GNU date (Linux, Windows) first, then BSD date (macOS).
cutoff=$(date -u -d "$COOLDOWN_DAYS days ago" +%Y-%m-%dT%H:%M:%SZ 2>/dev/null ||
date -u -v-"$COOLDOWN_DAYS"d +%Y-%m-%dT%H:%M:%SZ)
cooldown=(--exclude-newer "$cutoff")
fi
uv run --python 3.13 --no-project "${cooldown[@]}" --with "$GITHUB_ACTION_PATH" python -m python_security_auditing
# results.sarif exists only if bandit scanned every file in this job: the first step removes
# an earlier one, and the audit deletes it when bandit fails or skips files (an empty or
# partial report would close open alerts). Runs before the artifact upload, so a failed
# upload cannot skip it.
- name: Upload bandit SARIF to Code Scanning
if: contains(inputs.tools, 'bandit') && hashFiles('results.sarif') != ''
continue-on-error: true # needs `security-events: write`; the audit result stands without it
uses: github/codeql-action/upload-sarif@9b3254085494b55a0f7c4c2b3d2d52cc712ce370 # v4.39.1
with:
sarif_file: ${{ github.workspace }}/results.sarif
# A job may upload one SARIF per category, so each working_directory gets its own.
# For '.' it stays empty (= no category, the default analysis), as the bandit fork's
# upload did, so existing alerts keep matching.
category: ${{ inputs.working_directory != '.' && format('bandit/{0}', inputs.working_directory) || '' }}
- name: Upload ${{ inputs.artifact_name }}
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
with:
name: ${{ inputs.artifact_name }}
path: |
${{ inputs.working_directory }}/pip-audit-report.json
${{ github.workspace }}/results.sarif
if-no-files-found: ignore
- name: Fail if blocking issues found
if: steps.audit.outcome == 'failure'
shell: bash
run: exit 1