Notes

NxCI/CDSecurity

Running our own Nx remote cache, safely

A small self-hosted cache server cut lint from minutes to under a second. Most of the work went into making sure nobody could poison it.

Applies to
An Nx monorepo (Nx 23.1 or later) that wants a shared remote cache without Nx Cloud.
Verified with
A self-hosted cache server I designed and built in November 2026, used by laptops, pull requests and every deploy.

Our Nx monorepo had no shared cache. Every laptop kept its own, and CI passed one between runs through the GitHub Actions cache, which is scoped to a branch and a job. So a pull request, a laptop and a deploy each rebuilt what main had already built.

Before building anything I measured it. A container mirroring our post-merge job filled a throwaway cache, then a laptop with no local cache ran the usual checks against it:

CallerWithout the cacheWith it
Lint (44 projects)6 min 22 s0.4 s
Typecheck3 min 40 s13.7 s
Generate the database client4.2 s0.03 s
One commit, a laptop with an empty local cache, reading from a cache CI had just filled.

Nx makes the server part easy

Nx 23.1 can talk to any server that implements its remote cache spec: GET and PUT on /v1/cache/{hash}, pointed at with two environment variables. No Nx Cloud licence needed. Our server is about a thousand lines of TypeScript, running as a small container in front of one storage bucket, with a 30-day retention rule.

The hard part is trust

A shared build cache has one nasty failure mode, and it has a CVE (CVE-2025-36852, nicknamed CREEP): an untrusted run, like a pull request or a laptop, writes an entry under a hash, and a trusted run later restores it into a production build. That's why the simple bucket-backed cache plugins were deprecated. A careless custom server walks straight back into it.

So the design starts from who may write:

CallerReadsWrites
Laptopseverythingnothing
Pull request checkseverythingnothing
Main: the build jobbuild entriesbuild entries
Main: the check jobseverythingcheck entries
A test environment's releaseits own area, then shared buildsonly its own area
Production releasebuild entriesnothing
Who can do what. Only code that has reached main can write anything production reads.

A few rules make that hold:

Giving test environments their own area

Our test environments run on their own branches, which drift from main, so they got few cache hits and rebuilt the same packages on every deploy. They needed somewhere to write, without ever being able to write somewhere main or production reads.

GitHub Actions can hand a job a short-lived OIDC token that says which repository, workflow and environment it is. But those tokens expire within minutes, and Nx reads its access token once at startup, so a long build would start failing halfway through. So the job trades the GitHub token for a session first:

  1. Job startsGitHub issues an OIDC token for it
  2. Exchangethe server checks repository, workflow, branch and environment
  3. Sessiona signed token, valid for 90 minutes
  4. Buildreads and writes only that environment's area
The session exchange. A token for a production environment, another branch or another repository is refused.

Removing an environment from the server's allow-list ends its sessions immediately.

The cache must never fail a build

The cache only saves time, so it's never allowed to break anything:

Developers run one setup command that adds the server and their read-only token to their shell profile, and a --check flag to confirm it works.