Skip to content

Skill: keep .env.harnessrouter out of a Next.js server build - #2

Open
eric-epsilla wants to merge 1 commit into
HarnessRouter:mainfrom
eric-epsilla:nextjs-key-trace
Open

eric-epsilla wants to merge 1 commit into
HarnessRouter:mainfrom
eric-epsilla:nextjs-key-trace

Conversation

@eric-epsilla

Copy link
Copy Markdown

Problem

Next.js traces the files a server route reads and copies them into the build output. I followed the Skill's Next.js key loader (references/key-setup.md) in a fresh Next.js 16.4.6 app with output: 'standalone'. After the build, .env.harnessrouter, key included, was in .next/standalone/ and listed in .next/server/app/api/ping/route.js.nft.json. Anything that deploys traced files ships the key: a standalone image, or Vercel.

Measured

Next.js 16.4.6, one route handler calling the loader, a fake key in .env.harnessrouter:

Loader Bundler Fix Key in *.nft.json Key copied to .next/standalone/
Skill snippet Turbopack none yes yes
Skill snippet webpack none yes yes
Skill snippet Turbopack /* turbopackIgnore: true */ on the path no no
Skill snippet webpack /* turbopackIgnore: true */ on the path yes yes
Skill snippet both outputFileTracingExcludes no no
Template loadApiKey() Turbopack none yes yes
Template loadApiKey() webpack none no no
Template loadApiKey() both outputFileTracingExcludes no no

With the exclusion in place, the built server still reads the key:

  • standalone with HR_API_KEY set answers {"ok":true};
  • next start in the project, with only the file, answers {"ok":true}.

The ignore comment alone is not enough because webpack's tracer does not read it.

Change

  • references/key-setup.md:
    • exclude the file in next.config with outputFileTracingExcludes: { '*': ['./.env.harnessrouter'] };
    • after building, check that grep -rl env.harnessrouter .next --include='*.nft.json' prints nothing and that .next/standalone/.env.harnessrouter does not exist;
    • don't put the file on a production build machine; set HR_API_KEY in the host's secret settings.
  • references/security-and-testing.md: the server build's traced output is on the acceptance list.
  • tests/test_cloud_scope.py: checks that both stay. It fails on main.

Verification

  • python3 -m unittest discover -s tests -p 'test_*.py': 45 tests, OK (1 skipped).
  • With the reference changes reverted, the new test fails.

🤖 Generated with Claude Code

Next.js traces the files a server route reads and copies them into its output. Measured on
Next.js 16.4.6 with output: 'standalone': the key-setup loader and the Node template's loadApiKey
both put .env.harnessrouter, key included, into .next/standalone/, with Turbopack and with webpack.
A standalone image or a platform that deploys traced files would ship the key.

A /* turbopackIgnore: true */ comment on the path stopped Turbopack's trace but not webpack's.
outputFileTracingExcludes in next.config stopped both, for both loaders, and the built server
still read the key from HR_API_KEY (standalone) and from the file (next start in the project).

key-setup.md: the exclusion, the check (no match in .next/**/*.nft.json, no file in
.next/standalone), and not to put the file on a production build machine.
security-and-testing.md: the traced output is on the acceptance list.
tests/test_cloud_scope.py checks that both stay; it fails on main.

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

Copy link
Copy Markdown
Author

@richard-epsilla, this one is a key leak, so it should go before #1.

Problem. In a Next.js 16 app built with output: 'standalone', following the Skill's key loader copies .env.harnessrouter with the key into .next/standalone/. This happens with both Turbopack and webpack. The table in the description has the eight builds.

Approach. Exclude the file with outputFileTracingExcludes in next.config. It was the only fix that held for both bundlers and for both the snippet and the template's loadApiKey. I also added the trace check to key setup and to the acceptance list.

What I'd like from you:

  1. Merge this first. A team that already deployed a standalone build from the current snippet has the key in that image. Whether to tell anyone is your call.
  2. Decide whether the templates should also carry a comment pointing at the exclusion. My recommendation: no. The exclusion lives in the app's config, not in the template, and key-setup.md is where a coding agent reads it.

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.

1 participant