diff --git a/.github/workflows/security-scan-codex.yml b/.github/workflows/security-scan-codex.yml index a5851912b2..925d241bcb 100644 --- a/.github/workflows/security-scan-codex.yml +++ b/.github/workflows/security-scan-codex.yml @@ -214,6 +214,34 @@ jobs: ENDOR_API_CREDENTIALS_KEY: ${{ secrets.ENDOR_API_CREDENTIALS_KEY }} ENDOR_API_CREDENTIALS_SECRET: ${{ secrets.ENDOR_API_CREDENTIALS_SECRET }} run: | + # Sample memory, disk and OOM kills inside the worker step, because a + # shard killed for memory never runs later steps. This cannot help a + # lost runner, which keeps no log; it covers killed (137/143) and + # surviving shards. `free` counts tmpfs as shared/cache, so report + # available and shared memory rather than used. + oom_seen=0 + sample() { + mem=$(free -m | awk '/^Mem:/ {printf "mem_available_mb=%s mem_shared_mb=%s mem_total_mb=%s", $7, $5, $2} /^Swap:/ {printf " swap_used_mb=%s", $3}') + disk=$(df --output=pcent / | tail -1 | tr -d ' ') + echo "runner-resources $(date -u +%T) $mem root_disk_used=$disk" + # Print only OOM kills this shell hasn't printed yet. + kills=$(sudo -n dmesg -T 2>/dev/null | grep -i 'killed process' || true) + if [ -n "$kills" ]; then + printf '%s\n' "$kills" | tail -n "+$((oom_seen + 1))" | sed 's/^/runner-resources oom: /' + oom_seen=$(printf '%s\n' "$kills" | wc -l) + fi + } + ( + while sleep 60; do + sample + timeout 20 docker stats --no-stream --format 'runner-resources container={{.Name}} mem={{.MemUsage}}' 2>/dev/null || true + done + ) 2>/dev/null & + sampler=$! + # Take a last sample (listing every OOM kill so far), then stop the + # sampler and its pending sleep. Under -e, a failing trap command + # replaces the worker's exit status, so the whole cleanup is best effort. + trap '{ sample; pkill -P "$sampler"; kill "$sampler"; } 2>/dev/null || true' EXIT bun scripts/security/run-codex-scan-worker.ts \ --batch-limit "$CODEX_SECURITY_SCAN_LIMIT" \ --lane "$CODEX_SECURITY_SCAN_LANE" \ diff --git a/scripts/security/endor/fixtures/run.sh b/scripts/security/endor/fixtures/run.sh index 688b04efe8..65384f43a0 100644 --- a/scripts/security/endor/fixtures/run.sh +++ b/scripts/security/endor/fixtures/run.sh @@ -16,6 +16,7 @@ set -eu # docker run --rm --network bridge --cap-drop ALL \ # --cap-add NET_ADMIN --cap-add SETUID --cap-add SETGID --cap-add SETPCAP \ # --cap-add CHOWN --security-opt no-new-privileges:true --read-only \ +# --memory 3g --memory-swap 3g \ # --tmpfs /tmp:rw,nosuid,nodev,exec --tmpfs /output:rw,mode=1777 \ # --mount type=bind,source="$PWD/scripts/security/endor/fixtures",target=/fixture,readonly \ # --mount type=bind,source="$PWD/scripts/security/endor/scan.sh",target=/scan.sh,readonly \ diff --git a/scripts/security/run-endor-plugin-scan.test.ts b/scripts/security/run-endor-plugin-scan.test.ts index 681bac272f..0640eb32fb 100644 --- a/scripts/security/run-endor-plugin-scan.test.ts +++ b/scripts/security/run-endor-plugin-scan.test.ts @@ -171,6 +171,12 @@ describe("runEndorPluginScan", () => { ); const runArgs = await dockerRunArgs(workspace); expect(runArgs).toContain("--rm"); + // Without a cap, concurrent large scans exhaust the hosted runner's memory; + // a swap limit equal to the memory limit keeps the container off swap. + expect(runArgs).toEqual(expect.arrayContaining(["--memory", "--memory-swap"])); + const memory = runArgs[runArgs.indexOf("--memory") + 1]; + expect(memory).toMatch(/^\d+g$/); + expect(runArgs[runArgs.indexOf("--memory-swap") + 1]).toBe(memory); expect(runArgs).toContain( `type=bind,source=${join(workspace, "endor-artifact")},target=/workspace,readonly`, ); diff --git a/scripts/security/run-endor-plugin-scan.ts b/scripts/security/run-endor-plugin-scan.ts index 9b30d4e110..44c6e681e4 100644 --- a/scripts/security/run-endor-plugin-scan.ts +++ b/scripts/security/run-endor-plugin-scan.ts @@ -25,6 +25,8 @@ const CHILD_RUNTIME_ENV_KEYS = [ ] as const; const DEFAULT_ENDOR_TIMEOUT_MS = 20 * 60 * 1000; const DOCKER_REMOVE_TIMEOUT_MS = 10_000; +// Hosted ubuntu-22.04 runners have 16 GB, and a shard runs 4 scans at once by default. +const ENDOR_CONTAINER_MEMORY = "3g"; // Community packages with large dependency trees produce reports over 8 MiB. // Only the bounded summary is kept; diagnostics are capped separately. const MAX_ENDOR_REPORT_BYTES = 64 * 1024 * 1024; @@ -281,6 +283,14 @@ export async function runEndorPluginScan(input: { "--security-opt", "no-new-privileges:true", "--read-only", + // A --memory-swap equal to --memory allows no swap. The /tmp tmpfs (npm + // tree, npm cache, call graph) counts against the cap, so an oversized scan + // is killed inside this container and fails only its own report, instead + // of exhausting the runner. + "--memory", + ENDOR_CONTAINER_MEMORY, + "--memory-swap", + ENDOR_CONTAINER_MEMORY, "--entrypoint", "/usr/local/bin/clawhub-endor-entrypoint", "--tmpfs", diff --git a/specs/endor-plugin-scans.md b/specs/endor-plugin-scans.md index ba82302b2a..337b11427d 100644 --- a/specs/endor-plugin-scans.md +++ b/specs/endor-plugin-scans.md @@ -20,8 +20,9 @@ A claimed job runs Endor when: - the job is a package release whose family is `code-plugin`. That covers official and community code plugins. Skills and bundle plugins never -run Endor. There is no backfill: existing releases gain a result the next time -they are scanned for any reason (new publication, owner rescan, admin rescan). +run Endor. The `package-scan-backfill` cron enqueues Endor report-only jobs for +releases whose Endor result is missing or `failed` (see +`specs/plugin-single-scan-routing.md`); any other scan also refreshes it. ## Result contract @@ -34,7 +35,8 @@ they are scanned for any reason (new publication, owner rescan, admin rescan). package with a root `pnpm-lock.yaml`: `endorctl` needs pnpm for it, and the scanner image only runs npm and Yarn through credential-stripping shims. - A scanner failure or timeout is saved as `failed` with a generic reason. It - does not fail or retry the job; owners or admins can request a rescan. + does not fail or immediately retry the job; the backfill cron re-enqueues it + later, and owners or admins can request a rescan. - A later scan replaces the stored result; this is not a report archive. The previous result stays visible while replacement work is queued or running. @@ -64,6 +66,14 @@ Endor runs with `docker run --rm` from a digest-pinned image built by the manual - installs firewall rules blocking private, link-local and special-use IPv4/IPv6 destinations, then drops all capabilities and runs as the unprivileged `node` user (it uses public DNS resolvers because the firewall blocks private ones); +- is capped at 3 GiB of memory with no swap, so a shard's default 4 concurrent + scans leave headroom on the 16 GB hosted runner. The `/tmp` tmpfs, which + holds the npm tree, npm cache and call graph, counts against the cap. An + oversized scan is killed inside its own container and only its report fails. + Because the backfill cron re-enqueues failed reports, a package that never + fits keeps failing on later passes until the cap changes. Without + the cap, from 2026-11-07 concurrent scans of large packages exhausted the + runner: the kernel killed the worker, or GitHub lost contact with the runner; - runs npm and Yarn through shims that strip Endor credentials and disable dependency scripts. The image pins an npm version that also suppresses Git dependency `prepare` scripts; the isolation proof below checks this.