Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(ci): scan only changed files in semgrep pull request runs (#116604)

Georges-Antoine Assic00c471652c1
diff --git a/.agents/security.md b/.agents/security.md
index 0fb8d66bbe75..20e3e71516e9 100644
--- a/.agents/security.md
+++ b/.agents/security.md
@@ -134,7 +134,7 @@ queryset.filter(name__icontains=value) # SAFE
**Do not add new `pickle` usage.**
Unpickling bytes that any other process could influence is arbitrary code execution — the pickle stream can name any importable callable and pass it arguments.
This applies to the equivalents too: `cPickle`, `cloudpickle`, `dill`, `marshal`, `shelve`, `joblib`, `pandas.read_pickle`, and `numpy.load(..., allow_pickle=True)`.
-CI already fails on new pickle: the `avoid-pickle` rule from the `p/security-audit` Semgrep pack runs as a blocking rule in the diff-aware `semgrep ci` jobs (see `.github/workflows/ci-security.yaml`), so introducing one is a hard stop, not a warning.
+CI already fails on new pickle: the `avoid-pickle` rule from the `p/security-audit` Semgrep pack runs as a blocking rule in the security semgrep jobs (see `.github/workflows/ci-security.yaml`), so introducing one is a hard stop, not a warning.
Pickle can also appear without the word `pickle`: Django's default cache backend (`cache.set` / `cache.get`) pickles values transparently, so treat the cache as a pickle boundary and only store JSON-serializable values in it.
**Use JSON instead.**
diff --git a/.github/actions/semgrep-ci/action.yml b/.github/actions/semgrep-ci/action.yml
index a1620777d461..a0300ed27b94 100644
--- a/.github/actions/semgrep-ci/action.yml
+++ b/.github/actions/semgrep-ci/action.yml
@@ -1,10 +1,10 @@
-name: Semgrep diff-aware scan
+name: Semgrep scan
description: >
- Run `semgrep ci` in the digest-pinned Semgrep image. On pull requests semgrep
- resolves the merge-base with the base branch itself (GH_TOKEN fast path, then
- deepening fetches) and reports only findings the PR introduces; on pushes there
- is no baseline, so it runs a full scan. Runs logged out: nothing is uploaded to
- semgrep.dev, and any finding fails the step via the exit code.
+ Run `semgrep scan --error` in the digest-pinned Semgrep image. On pull requests
+ it scans only the files the PR adds or modifies, with no baseline; on pushes it
+ scans the whole tree. Runs logged out: nothing is uploaded to semgrep.dev, and
+ any finding fails the step via the exit code. Callers check out with
+ fetch-depth 2, so the merge commit's first parent (the base tip) is present.
inputs:
image:
@@ -12,7 +12,7 @@ inputs:
required: true
args:
description: >
- Arguments for `semgrep ci` (--config, --include/--exclude, --exclude-rule,
+ Arguments for `semgrep scan` (--config, --include/--exclude, --exclude-rule,
--jobs, ...). Split on whitespace and handed to semgrep as positional
parameters, so a single argument must not contain spaces or quotes.
Everything else reaches semgrep verbatim -- glob characters included,
@@ -22,29 +22,23 @@ inputs:
required: true
full-scan:
description: >
- 'true' runs `semgrep scan --error` over the whole tree instead of the
- diff-aware `semgrep ci`, so a pull request reports every finding, the way a
- push does. Set it when the pull request changes a rule or the scan setup:
- the diff-aware scan cannot see what an edited rule matches in unchanged code.
+ 'true' scans the whole tree on a pull request too, the way a push does.
+ Set it when the pull request changes a rule or the scan setup: a scan of
+ the changed files cannot see what an edited rule matches in unchanged code.
required: false
default: 'false'
runs:
using: composite
steps:
- - name: Run semgrep ci
+ - name: Run semgrep scan
shell: bash
env:
SEMGREP_ARGS: ${{ inputs.args }}
SEMGREP_IMAGE: ${{ inputs.image }}
FULL_SCAN: ${{ inputs.full-scan }}
- # Lets semgrep resolve the PR merge-base with one API call instead of
- # deepening fetches (cli/src/semgrep/meta.py GithubMeta._find_branchoff_point)
- GH_TOKEN: ${{ github.token }}
- # GithubMeta reads the GITHUB_* vars and the event payload to detect the
- # PR context; the baseline git operations need the checkout's persisted
- # auth from .git/config, and safe.directory because the workspace is
- # owned by the runner user while the container runs as root.
+ # safe.directory because the workspace is owned by the runner user while
+ # the container runs as root, and semgrep lists targets through git.
run: |
# Split `args` HERE and pass the words as positional parameters.
# Never splice the string into the command the container shell
@@ -64,21 +58,63 @@ runs:
semgrep_args=($SEMGREP_ARGS)
set +f
- if [[ "$FULL_SCAN" == 'true' ]]; then
- semgrep_command=(scan --error)
- else
- semgrep_command=(ci)
+ semgrep_in_container() {
+ docker run --rm \
+ -v "$GITHUB_WORKSPACE:/src" -w /src \
+ -e SEMGREP_ENABLE_VERSION_CHECK=false \
+ "$SEMGREP_IMAGE" \
+ sh -c 'git config --global --add safe.directory /src && exec semgrep "$@"' sh \
+ "$@"
+ }
+
+ full_scan() {
+ semgrep_in_container scan --error --metrics=off --verbose --timeout 300 "${semgrep_args[@]}"
+ }
+
+ if [[ "$FULL_SCAN" == 'true' || "$GITHUB_EVENT_NAME" != 'pull_request' ]]; then
+ full_scan
+ exit 0
fi
- docker run --rm \
- -v "$GITHUB_WORKSPACE:/src" -w /src \
- -v "$GITHUB_EVENT_PATH:/github-event.json:ro" \
- -e SEMGREP_ENABLE_VERSION_CHECK=false \
- -e GITHUB_ACTIONS -e GITHUB_EVENT_NAME -e GITHUB_EVENT_PATH=/github-event.json \
- -e GITHUB_REPOSITORY -e GITHUB_SERVER_URL -e GITHUB_API_URL \
- -e GITHUB_SHA -e GITHUB_REF -e GITHUB_HEAD_REF -e GITHUB_RUN_ID \
- -e GITHUB_REPOSITORY_ID -e GITHUB_REPOSITORY_OWNER_ID \
- -e GH_TOKEN \
- "$SEMGREP_IMAGE" \
- sh -c 'git config --global --add safe.directory /src && exec semgrep "$@"' sh \
- "${semgrep_command[@]}" --metrics=off --verbose --timeout 300 "${semgrep_args[@]}"
+ # A diff-aware scan drops a match that the base also has, even where a
+ # nosemgrep comment suppressed it there, so moving a call away from its
+ # comment passes it. Master passes the full scan and the rules are
+ # intrafile, so any finding in a changed file is one this PR introduces.
+ changed_list="$RUNNER_TEMP/semgrep-changed"
+ git diff -z --name-only --diff-filter=d HEAD^1 HEAD > "$changed_list"
+
+ # A file named on the command line bypasses --include, --exclude and
+ # semgrep's default ignores (tests/, node_modules/, ...), so only the
+ # changed files in semgrep's own target list are scanned.
+ # --x-ls is experimental; the pinned image keeps it stable.
+ target_list="$RUNNER_TEMP/semgrep-targets"
+ semgrep_in_container scan --x-ls --metrics=off "${semgrep_args[@]}" > "$target_list"
+
+ declare -A is_target
+ while IFS= read -r path; do
+ is_target["$path"]=1
+ done < "$target_list"
+
+ targets=()
+ while IFS= read -r -d '' path; do
+ # --x-ls prints one path per line, so a name with a newline never matches it.
+ if [[ "$path" == *$'\n'* ]]; then
+ echo "A changed file name contains a newline, so running the full scan"
+ full_scan
+ exit 0
+ fi
+ if [[ -n "${is_target[$path]:-}" ]]; then
+ targets+=("$path")
+ fi
+ done < "$changed_list"
+
+ echo "Scanning ${#targets[@]} changed file(s)"
+ if (( ${#targets[@]} == 0 )); then
+ exit 0
+ fi
+ # Keeps the command line under the kernel's argument size limit.
+ if (( ${#targets[@]} > 3000 )); then
+ full_scan
+ exit 0
+ fi
+ semgrep_in_container scan --error --metrics=off --verbose --timeout 300 "${semgrep_args[@]}" -- "${targets[@]}"
diff --git a/.github/workflows/ci-security.yaml b/.github/workflows/ci-security.yaml
index 5e49996cfa2a..42e35fe577e7 100644
--- a/.github/workflows/ci-security.yaml
+++ b/.github/workflows/ci-security.yaml
@@ -35,7 +35,7 @@ jobs:
devex: ${{ steps.oversized.outputs.force_all || steps.filter.outputs.devex || 'true' }}
test-rules: ${{ steps.oversized.outputs.force_all || steps.filter.outputs.test-rules || 'true' }}
desktop: ${{ steps.oversized.outputs.force_all || steps.filter.outputs.desktop || 'true' }}
- # Pushes default to 'false' because `semgrep ci` already scans the whole tree on a push.
+ # Pushes default to 'false' because the semgrep-ci action already scans the whole tree on a push.
scan-setup: ${{ steps.oversized.outputs.force_all || steps.filter.outputs.scan-setup || 'false' }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
@@ -169,6 +169,8 @@ jobs:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+ with:
+ fetch-depth: 2
- name: Run Semgrep
uses: ./.github/actions/semgrep-ci
@@ -216,6 +218,8 @@ jobs:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+ with:
+ fetch-depth: 2
- name: Run Semgrep
uses: ./.github/actions/semgrep-ci
@@ -252,6 +256,8 @@ jobs:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+ with:
+ fetch-depth: 2
- name: Run Semgrep
uses: ./.github/actions/semgrep-ci
@@ -290,6 +296,8 @@ jobs:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+ with:
+ fetch-depth: 2
- name: Run Semgrep
uses: ./.github/actions/semgrep-ci
@@ -332,6 +340,8 @@ jobs:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+ with:
+ fetch-depth: 2
# Single-rule scan — keeps the failure surface here aligned with the
# job name. Other custom TS rules (no-direct-fuse-instantiation,
@@ -370,6 +380,8 @@ jobs:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+ with:
+ fetch-depth: 2
# exclude all directories already scanned by other jobs
#
@@ -522,9 +534,9 @@ jobs:
${{ env.SEMGREP_IMAGE }} \
semgrep --test .semgrep/
- # semgrep-general runs `semgrep ci`, which drops every match that the merge-base
- # copy of a file also has. A rule-only change leaves every workflow unchanged, so
- # that scan cannot report what the edited rule now matches. This full scan can.
+ # On a pull request semgrep-general scans only the changed files. A rule-only change
+ # leaves every workflow unchanged, so that scan cannot report what the edited rule
+ # now matches. This full scan can.
- name: Scan workflows with the workflow rules
run: |
docker run --rm -v "${{ github.workspace }}:/src" -w /src \