-
Notifications
You must be signed in to change notification settings - Fork 8k
[Security hardening] Add automated security audit workflow #2442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PascalThuet
wants to merge
9
commits into
github:main
Choose a base branch
from
PascalThuet:codex/add-security-audit-workflow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e4b38e7
Add automated security audit workflow
PascalThuet 65b0dab
Address security audit review feedback
PascalThuet 397e0d7
Add security workflow regression tests
PascalThuet e1e8051
Address follow-up security workflow review
PascalThuet 6f1da27
Use compile for security audit requirements
PascalThuet 129d19e
Address latest security workflow review
PascalThuet aa02062
Address latest security audit review
PascalThuet 1f85b92
Harden security-sensitive repository surfaces
PascalThuet 4599155
Address remaining security review feedback
PascalThuet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| { | ||
| "results": [ | ||
| { | ||
| "code": "34 run_cmd,\n35 shell=True,\n36 capture_output=True,\n37 text=True,\n38 cwd=cwd,\n39 timeout=300,\n40 )\n41 output = {\n42 \"exit_code\": proc.returncode,\n43 \"stdout\": proc.stdout,\n", | ||
| "col_offset": 19, | ||
| "end_col_offset": 13, | ||
| "filename": "src/specify_cli/workflows/steps/shell/__init__.py", | ||
| "issue_confidence": "HIGH", | ||
| "issue_cwe": { | ||
| "id": 78, | ||
| "link": "https://cwe.mitre.org/data/definitions/78.html" | ||
| }, | ||
| "issue_severity": "HIGH", | ||
| "issue_text": "subprocess call with shell=True identified, security issue.", | ||
| "line_number": 35, | ||
| "line_range": [ | ||
| 33, | ||
| 34, | ||
| 35, | ||
| 36, | ||
| 37, | ||
| 38, | ||
| 39, | ||
| 40 | ||
| ], | ||
| "more_info": "https://bandit.readthedocs.io/en/1.9.4/plugins/b602_subprocess_popen_with_shell_equals_true.html", | ||
| "test_id": "B602", | ||
| "test_name": "subprocess_popen_with_shell_equals_true" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """Check that committed security audit requirements are up to date.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parents[2] | ||
| COMMITTED_REQUIREMENTS = REPO_ROOT / ".github" / "security-audit-requirements.txt" | ||
| DEPENDENCY_INPUTS = ("pyproject.toml", ".github/security-audit-requirements.txt") | ||
|
|
||
|
|
||
| def _dependency_diff_refs() -> tuple[str, str]: | ||
| base_ref = os.environ.get("DEPENDENCY_DIFF_BASE", "").strip() | ||
| head_ref = os.environ.get("DEPENDENCY_DIFF_HEAD", "").strip() or "HEAD" | ||
| if base_ref and not set(base_ref) <= {"0"}: | ||
| return base_ref, head_ref | ||
| return "HEAD^", "HEAD" | ||
|
|
||
|
|
||
| def _dependency_inputs_changed() -> bool: | ||
| base_ref, head_ref = _dependency_diff_refs() | ||
| try: | ||
| result = subprocess.run( | ||
| [ | ||
| "git", | ||
| "diff", | ||
| "--name-only", | ||
| base_ref, | ||
| head_ref, | ||
| "--", | ||
| *DEPENDENCY_INPUTS, | ||
| ], | ||
| check=True, | ||
| cwd=REPO_ROOT, | ||
| stderr=subprocess.PIPE, | ||
| stdout=subprocess.PIPE, | ||
| text=True, | ||
| ) | ||
| except subprocess.CalledProcessError as exc: | ||
| print( | ||
| "Could not determine changed dependency inputs; checking requirements.", | ||
| file=sys.stderr, | ||
| ) | ||
| if exc.stderr: | ||
| print(exc.stderr.strip(), file=sys.stderr) | ||
| return True | ||
|
|
||
| changed_inputs = [line for line in result.stdout.splitlines() if line] | ||
| if not changed_inputs: | ||
| print("Dependency audit inputs unchanged; sync check skipped.") | ||
| return False | ||
|
|
||
| print(f"Dependency audit inputs changed: {', '.join(changed_inputs)}") | ||
| return True | ||
|
|
||
|
|
||
| def main() -> int: | ||
| if not _dependency_inputs_changed(): | ||
| return 0 | ||
|
|
||
| generated_requirements = Path(os.environ["GENERATED_REQUIREMENTS"]) | ||
| generated_requirements.parent.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| subprocess.run( | ||
| [ | ||
| "uv", | ||
| "pip", | ||
| "compile", | ||
| "pyproject.toml", | ||
| "--extra", | ||
| "test", | ||
| "--universal", | ||
| "--generate-hashes", | ||
| "--quiet", | ||
| "--no-header", | ||
| "--output-file", | ||
| str(generated_requirements), | ||
| ], | ||
| check=True, | ||
| cwd=REPO_ROOT, | ||
| ) | ||
|
|
||
| committed = COMMITTED_REQUIREMENTS.read_text(encoding="utf-8") | ||
| generated = generated_requirements.read_text(encoding="utf-8") | ||
| if committed == generated: | ||
| return 0 | ||
|
|
||
| print( | ||
| "Regenerate .github/security-audit-requirements.txt with the documented " | ||
| "uv pip compile command.", | ||
| file=sys.stderr, | ||
| ) | ||
| return 1 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| name: Security Audit | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| on: | ||
| push: | ||
| branches: ["main"] | ||
| pull_request: | ||
| schedule: | ||
| - cron: "17 4 * * 1" | ||
| workflow_dispatch: | ||
|
|
||
| jobs: | ||
| dependency-audit: | ||
| name: Dependency audit (${{ matrix.os }}, Python ${{ matrix.python-version }}) | ||
| runs-on: ${{ matrix.os }} | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| os: [ubuntu-latest, windows-latest] | ||
| python-version: ["3.11", "3.12", "3.13"] | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 | ||
| with: | ||
| fetch-depth: 2 | ||
|
|
||
| - name: Install uv | ||
| uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0 | ||
|
|
||
| - name: Set up Python ${{ matrix.python-version }} | ||
| uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6 | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
|
|
||
| - name: Compile scheduled audit requirements | ||
| if: ${{ github.event_name == 'schedule' }} | ||
| run: | | ||
| uv pip compile pyproject.toml --extra test --python-version "${{ matrix.python-version }}" --generate-hashes --quiet --output-file "${{ runner.temp }}/spec-kit-audit-requirements.txt" | ||
|
|
||
| - name: Run pip-audit (scheduled live resolution) | ||
| if: ${{ github.event_name == 'schedule' }} | ||
| run: uvx --from pip-audit==2.10.0 pip-audit --disable-pip --require-hashes -r "${{ runner.temp }}/spec-kit-audit-requirements.txt" --progress-spinner off | ||
|
|
||
| - name: Check committed audit requirements are current | ||
| if: ${{ github.event_name != 'schedule' }} | ||
| env: | ||
| DEPENDENCY_DIFF_BASE: ${{ github.event.pull_request.base.sha || github.event.before || '' }} | ||
| DEPENDENCY_DIFF_HEAD: ${{ github.sha }} | ||
| GENERATED_REQUIREMENTS: ${{ runner.temp }}/security-audit-requirements.txt | ||
| run: python .github/scripts/check_security_requirements.py | ||
|
|
||
| - name: Run pip-audit (committed requirements) | ||
| if: ${{ github.event_name != 'schedule' }} | ||
| run: uvx --from pip-audit==2.10.0 pip-audit --disable-pip --require-hashes -r .github/security-audit-requirements.txt --progress-spinner off | ||
|
|
||
|
mnriem marked this conversation as resolved.
|
||
| static-analysis: | ||
| name: Static analysis | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 | ||
|
|
||
| - name: Install uv | ||
| uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0 | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6 | ||
| with: | ||
| python-version: "3.13" | ||
|
|
||
| - name: Run Bandit | ||
| run: uvx --from bandit==1.9.4 bandit -r src -lll --baseline .github/bandit-baseline.json | ||
|
PascalThuet marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.