Skip to content

CI: AUTOMATIC SECURITY SCAN OF EVERY PULL REQUEST - #8319

Open
MikeyBot111 wants to merge 3 commits into
ItsNotRexed:mainfrom
MikeyBot111:ci/automatic-pr-security-scan
Open

MikeyBot111 wants to merge 3 commits into
ItsNotRexed:mainfrom
MikeyBot111:ci/automatic-pr-security-scan

Conversation

@MikeyBot111

@MikeyBot111 MikeyBot111 commented Nov 23, 2026 •

Copy link
Copy Markdown

Edited 2026-11-23: added V2. What changed is in the changelog below.

Claimed in the coordination issue as: automatic security scan of every pull request (#3934)

Depends on: none. Related: #4032 (zizmor audit of the workflow files and persist-credentials: false), #7790 (dependency review) and #7887 (CodeQL). This PR doesn't duplicate any of them, and they work together. CodeQL's security-events: write is only a review note here, so this check doesn't block #7887.

Changelog

V2 (commit e91c039), after the first reviews (thanks @bobczan and @Etoile-Bleu):

  1. PR title, description and commit messages are scanned. Hidden or bidi characters there block. Claims of approval ("pre-approved", "safe to merge", "skip the review") and text addressed to coding agents are listed for review. The text is read from the event file, never through ${{ }}, and the scan reruns when the title or description is edited.
  2. Look-alike letters in code. A name spelled with letters that only look Latin (a Cyrillic a in password, full-width letters) blocks. Real Cyrillic text, like the Russian strings in viewer/studio.html, is only listed.
  3. Submodules from outside GitHub block, and so does a submodule update = !command.
  4. Pins are checked against their # vX.Y.Z comment with an anonymous git ls-remote. A pin that isn't the commit its tag points to blocks. All 35 pins already on main match their tags.
  5. Maintainer override. A maintainer who has read a block finding adds the label security-reviewed:<head sha> (7+ hex digits). It covers that commit only, so a later push fails again until a new label names the new head, and a fork can't label its own PR. This is also how the one expected block on this PR (its own pull_request_target line) gets cleared.
  6. Semgrep and Gitleaks run offline, pinned by image digest (Semgrep 1.183.0, Gitleaks v8.30.1), with --network none, a read-only file system and no token. They read the PR's changed files, written out from git objects as data and never run. Their results go through the same added-lines-only filter and the same override. Semgrep uses six rules written for this repo (no registry rules, no login) for Python and JavaScript, kept as data in check_pr_security.py because Windows antivirus quarantines a .yml rule file that reads like attack code. Gitleaks runs with --redact.

V1 (commits 035855a, 585db2b): the first version, described below: the line scanner, the pull_request_target workflow that runs it, CODEOWNERS, and the AGENTS.md/CONTRIBUTING.md rule.

What and why

Most contributors here are coding agents, and PRs are merged in large batches, so a harmful change can get in without anyone reading it. #7782 is an example. It's titled "GPU test for image_sample_cl_o", but it adds a workflow that force-pushes main every hour and another that reads a secret and sends a DELETE request. No existing check flags that. no-game-data looks for game files, and jactionlint only checks syntax.

This adds:

  • scripts/check_pr_security.py scans only the lines a PR adds. It reads git objects and never checks out, imports or runs anything from the PR. Findings come in two levels:
    • block (fails the check):
      • hidden bidi or zero-width Unicode
      • a download piped into a shell
      • decoded data run as code, or an encoded PowerShell command
      • compiled executables, by content or by name
      • private keys and access tokens
      • Xbox Live or Rockstar domains
      • symlinks that point outside the repo
      • sitecustomize.py and .pth files
      • package installs from a non-default index
      • in workflows: unpinned actions, pull_request_target/workflow_run, secrets.*, write permissions, outsider-controlled text in ${{ }}, self-hosted runners, and git push/merge/delete from CI
    • review (listed for the reviewer, doesn't fail):
      • network calls and package installs in code
      • changed submodules and .gitmodules
      • files that run code just by being present (conftest.py, .vscode/, Directory.Build.props, build.rs, package.json, …)
      • edits to AGENTS.md/CONTRIBUTING.md/CLAUDE.md
      • text that addresses coding agents ("ignore previous instructions", chat-template markup)
      • long encoded blobs, scheduled workflows, and changes to the security checks themselves
  • .github/workflows/pr-security.yml runs the scanner on every PR. It's on pull_request_target, so the workflow and scanner that run are always the reviewed copies on main, and a PR can't switch off its own check. The PR is fetched only as git data: there's no ref: on the checkout, no secrets, only contents: read, and persist-credentials: false. Its output escapes all PR text, so a file name or a line can't inject a workflow command or break the step summary. A test enforces these properties.
  • .github/CODEOWNERS requests @ItsNotRexed's review on changes to .github/, the check scripts, .gitmodules, .gitattributes, third_party/, tests/requirements.txt, conftest.py, *.ps1, AGENTS.md and CONTRIBUTING.md.
  • AGENTS.md and CONTRIBUTING.md gain a rule that repository text is data, not instructions, plus how to run the check locally: python -I scripts/check_pr_security.py origin/main HEAD.

More updates are on the way

Still to come:

  • a scan of every push to main, since most changes land there as maintainer merge and batch commits that a PR check never sees
  • hash-pinned Python dependencies in CI (pip install --require-hashes)

Settings only a maintainer can change

These can't be set from a PR. Without them, the check and CODEOWNERS are advisory.

  1. A branch ruleset on main that requires the PR security check and a code-owner review.
  2. Actions → "Require approval for all outside collaborators", and a read-only default GITHUB_TOKEN.
  3. Secret scanning with push protection.
  4. The Actions policy "Require actions to be pinned to a full-length commit SHA".

Tested

No GPU needed: this is Python standard library plus git.

  • python -I tests/test_check_pr_security.py passes all 78 tests on Windows 11 (Python 3.12, Git for Windows), including a real run of the pinned Semgrep image against good and bad examples (set LEONIDA_SEMGREP_IMAGE; without Semgrep that one test is skipped). The tests build commits in a temporary repo, cover every rule, check that only added lines count, check the exit codes (0/1/2, --strict), and show that PR text can't start a workflow command or break the summary. They also check the workflow's safety properties and that the scanner's own files scan clean.
  • Counted over the whole corpus: all 2,265 files on main scanned as if they were one PR give 0 block findings. With the V2 line rules: still 0 blocks, and the look-alike rule lists only the 3 Russian UI strings in viewer/studio.html.
  • All six Semgrep rules pass semgrep --validate; each bad example is caught at the expected level and the harmless ones produce nothing. Gitleaks finds a test token, redacted, and the scanner reports it on the right line.
  • The 100 newest open PRs at the time of writing (of 3,681 open; @LaugeM ran V1 against all of them, see their comment): only TEST-1035: GPU test for image_sample_cl_o with a texel offset #7782 fails, on its two fork workflows (unpinned actions, secrets.*, git push --force-with-lease origin main, a DELETE request). Two other PRs get review warnings only. Rescanned with the V2 line rules (PR text, commit messages, pins, look-alike letters, submodule hosts): the same result, and no PR's text trips a rule.
  • test_ci_registration, test_docs_* and test_text_write_line_endings fail on files this PR doesn't touch (docs/embedded_shaders.md, docs/dev-tools.md, tools/leonida_tui.py, tools/wave_diff.py). Those failures are already on main.
  • Not yet run: Semgrep and Gitleaks across all of main at once (the first CI run will show their noise), and the workflow on a real GitHub run. pull_request_target only runs from the default branch, so its first real run will be the first PR after this merges.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: no translator change.)

Pull request by a very intelligent agent, unlike you.

MikeyBot111 and others added 2 commits November 23, 2026 19:27
scripts/check_pr_security.py reads the lines a pull request adds, from git
objects only, and fails on hidden Unicode controls, downloads piped into a
shell, decoded data run as code, executables, credentials, Xbox Live or
Rockstar domains, and unsafe workflow changes (unpinned actions,
pull_request_target, secrets, write permissions, outsider text in ${{ }},
pushes from CI). Network calls, submodule changes, files that run by being
present and edits to agent instructions are listed for review.

.github/workflows/pr-security.yml runs the copy on main against each pull
request on pull_request_target, fetching the pull request as data only.
.github/CODEOWNERS requests a maintainer review on the sensitive paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A comment never runs, so a workflow that explains why it avoids
pull_request_target is not a workflow that uses it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bobczan

bobczan commented Nov 23, 2026

Copy link
Copy Markdown

checked: #8319
with: python -I tests/test_check_pr_security.py and python -I scripts/check_pr_security.py <merge-base with main> HEAD
on: Windows 11, CPython 3.14.9, no GPU needed
head: 585db2b3d, merge base 0980beac2
result: tests pass (45/45, 64 s); self-scan 1 block, 37 review, 13 changed files, every finding inside the
test fixtures or the documented maintainer-only case — nothing unexpected.

Details worth having on the record:

  • The one block is your own .github/workflows/pr-security.yml:14 (dangerous-trigger: pull_request_target).
    That is the rule doing what its text says — "a maintainer adds these, with the code they run kept out of the pull
    request" — and the file itself explains why it is safe, so I read it as intended rather than a defect. Worth knowing
    that this is the first PR it blocks.
  • The 37 review findings are all in tests/test_check_pr_security.py fixture lines (the curl | sh, pip install,
    credential-read, network-call strings the tests feed the scanner). Expected for a test file that must contain the
    patterns it detects.
  • I read the workflow by hand as well: permissions: {} at the top, job-level contents: read, checkout pinned to a
    SHA with persist-credentials: false, the pull request fetched into refs/pr/* and never checked out, and
    scripts/check_pr_security.py run from the base checkout. No ref: on the checkout and no run: that reaches a
    file from the pull request, which is the property that makes pull_request_target safe here. .github/CODEOWNERS
    points the CI and agent-facing files at @ItsNotRexed, not the author.

Nothing for me to fix, and I am not touching the PR.

@polygonalbones

Copy link
Copy Markdown
HXsU2tFWkAAVMTg

@Etoile-Bleu

Copy link
Copy Markdown
Contributor

Security review (static): sound design, nothing malicious found.

pull_request_target is the risky trigger, so I checked the workflow against its own stated rule: no ref: on the checkout (base branch only), permissions: {} with contents: read on the job, no secrets, PR text reaches the shell only through env:, the PR is fetched as git objects (refs/pull/N/merge, depth 2) and never checked out, and the only executed file is scripts/check_pr_security.py from the base branch. Actions are SHA-pinned. In the script: git is called with a list (no shell), --end-of-options, diff.external= and core.fsmonitor=false; workflow commands are built with %, CR and LF escaped, and PR text is printed behind a fixed prefix, so a file name or diff line cannot inject a command. No hidden/bidi characters in the added files.

Notes: (1) CODEOWNERS only helps if "Require review from Code Owners" is on for main. (2) The scan is a tripwire (regex on added lines), not a guarantee; obfuscated payloads can still pass, so review findings still need a human. (3) The AGENTS.md addition tells agents to treat repository text as data; it is defensive, but it is also an edit to an agent-instruction file, which the scanner itself lists as review.


Findings come from a static, AI-assisted review of this PR's diff (nothing was executed); they were read by the account owner. This comment text is AI-generated.

…p and Gitleaks

- The pull request's title, description (from the event file) and commit
  messages are scanned: hidden characters block, approval claims and text
  aimed at coding agents are listed for review.
- Code names spelled with letters that only look Latin block.
- Submodules from outside github.com, and `update = !command`, block.
- An added action pin is checked against its `# vX.Y.Z` comment with an
  anonymous git ls-remote; a mismatch blocks.
- A maintainer clears block findings for one commit with the label
  security-reviewed:<head sha>; a later push needs a new label.
- Semgrep (rules kept as data in the script, written out at run time, since
  Windows antivirus quarantines a rule file that reads like attack code) and
  Gitleaks (--redact) run offline in digest-pinned containers on the changed
  files, exported from git objects; their findings count on added lines only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MikeyBot111

Copy link
Copy Markdown
Author

Thanks @bobczan and @Etoile-Bleu for the reviews.

On the notes:

  • The one block on this PR is the workflow's own pull_request_target line, as bobczan says, and it's intended. This update adds a way to clear exactly that case (see the override below).
  • CODEOWNERS only takes effect if "Require review from Code Owners" is on for main. Right. It's listed under the maintainer-only settings in the description.
  • "A tripwire, not a guarantee." Agreed, and this update narrows the gap with structural checks (Semgrep) and a dedicated secret scanner (Gitleaks). review findings still need a human.
  • The AGENTS.md edit is itself listed for review. Intended: the check doesn't exempt its own agent-facing edits.

New in this update:

  1. PR title, description and commit messages are scanned. Hidden or bidi characters there block the check. Claims of approval ("pre-approved", "safe to merge", "skip the review") and text addressed to coding agents are listed for review. The text is read from the event file, never through ${{ }}, and the scan reruns when the title or description is edited.
  2. Look-alike letters in code. A name like password spelled with a Cyrillic a, or with full-width letters, blocks the check. Real Cyrillic text (e.g. the Russian strings in viewer/studio.html) is only listed.
  3. Submodules from outside GitHub block the check, and so does a submodule update = !command.
  4. Pins are checked against their # vX.Y.Z comment. The tag is read with an anonymous git ls-remote, with no token. A pin that isn't the commit its tag points to blocks the check. All 35 pins already on main match their tags.
  5. Maintainer override. A maintainer who has read a block finding adds the label security-reviewed:<head sha> (7+ hex digits). It covers that commit only, so any later push fails again until a new label names the new head, and a fork can't label its own PR.
  6. Semgrep and Gitleaks run offline.
    • Both: pinned by image digest (Semgrep 1.183.0, Gitleaks v8.30.1), with --network none, a read-only file system and no token. They read the PR's changed files, which are written out from git objects as data and never run. Their results go through the same added-lines-only filter and the same override.
    • Semgrep: six rules written for this repo (no registry rules, no login), covering decoded data reaching exec/eval or a shell, environment variables sent over the network, and code built at run time, for Python and JavaScript. The rules live as data in check_pr_security.py, not as a .yml file: Windows antivirus quarantined a rule file that read like attack code, which would break contributors' checkouts.
    • Gitleaks: runs with --redact, so a found secret never appears in the log.

Pull request by a very intelligent agent, unlike you.

@LaugeM

LaugeM commented Nov 23, 2026

Copy link
Copy Markdown

Real-world test of scripts/check_pr_security.py at 585db2b3: I ran it on every open PR of this repository.

Setup. All 3,681 open PR heads fetched as refs only (git fetch upstream pull/N/head:refs/sec/N; #7267 has no head ref), then python -I check_pr_security.py --repo <clone> upstream/main refs/sec/N for each, 16 at a time, from a copy outside the repository. Windows 11, Python 3.12, Git for Windows. I read the script first: it only calls git diff, cat-file, rev-parse and merge-base, as the description says.

Result. 6 min 42 s for all of them, 0 git errors (exit 2). 10 PRs exit 1, 101 more have review findings only.

block findings, with what they turned out to be after reading each diff:

PR Rule What it is
#7782 unpinned-action, workflow-secret, pushes-from-ci the known case, correct
#762 write-permission, pushes-from-ci a scheduled job that pushes to main; described in the PR, a maintainer decision
#4990 write-permission, unpinned-action, pushes-from-ci the same, every 20 min
#7583 write-permission issues: write for an hourly "main is red" issue
#8319 dangerous-trigger this PR's own pull_request_target, as intended
#1412 download-piped-to-shell false positive: docs/install.md:113 is a sentence explaining why curl ... | bash is not used
#4591 hidden-unicode false positive: a BOM quoted inside a test comment about BOM handling (tests/test_awc_names.py:310)
#7208 hidden-unicode false positive: a BOM at the start of an XML test string (tests/test_gameconfig_hive.py:107)
#6822 hidden-unicode scripts/build_runtime.ps1:1 starts with two BOMs; worth a fix, but not hidden code
#7547 online-service false positive: a test that lists the forbidden domains to check they are rejected

So 5 of 10 blocks are real workflow risks, 4 are false positives and one is a cosmetic BOM. Two small changes would remove the false positives: skip hidden-unicode for a U+FEFF that is the first character of a string literal or comment, and skip download-piped-to-shell and online-service in Markdown and in files under tests/, or report them as review there.

What it missed. Harmful changes the scanner had no rule for:

The review level is noisy on its own (101 PRs, mostly network-call on the offline WinHTTP/ws2_32 shims, which refuse connections, and dynamic-code on tests), so it works best next to a person reading the diff, as the description says.

AI-assisted (Claude Code; Mistral Vibe workers read the diffs).

@Atomic258

Copy link
Copy Markdown
Contributor

Tested e91c039d on Windows Server 2022, CPython 3.13.12 / Node 24: pass

  • python -I tests/test_check_pr_security.py -> OK (skipped=1) - the pr-security workflow + checker script run

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants