Skip to content

chore: refuse settings read at build through import.meta.env - #230

Merged
TylerVigario merged 1 commit into
mainfrom
chore/no-build-time-settings
Dec 1, 2026
Merged

TylerVigario merged 1 commit into
mainfrom
chore/no-build-time-settings

Conversation

@TylerVigario

Copy link
Copy Markdown
Owner

Vite replaces import.meta.env.X in server code with X's value at build, and the release publishes the server bundle. So a secret read that way would ship in a public release, and any setting read that way would make the tarball depend on the machine that built it. Every setting already comes from process.env, read when the server runs, but nothing held that. Reading through import.meta.env is how Astro's guide reads a setting, so it's the easy way to break it.

What changes

  • ESLint refuses any import.meta.env read other than Astro's own flags: PROD, DEV, MODE, SSR, BASE_URL, SITE and ASSETS_PREFIX. Those come from the config and the command, the same on every machine. Reading the object whole (const env = import.meta.env, destructuring, spreading) is refused too.
  • CLAUDE.md states the rule beside the one on Astro's key.

The code doesn't change: its one read is import.meta.env.PROD.

Measured

  • The premise: a scratch route read import.meta.env.PROBE_SECRET_A and process.env.PROBE_SECRET_B, built with both set. The first came out as its literal value in dist/server, and the second stayed process.env.PROBE_SECRET_B.
  • The rule refuses a named read (.SMTP_PASS, ?.SECRET, ["SECRET"], .PUBLIC_X), the bare object, destructuring and spreading. It does so in a .ts module, in .astro frontmatter and in an .astro script.
  • It allows PROD, DEV and SITE, plain and with ?., and import.meta.dirname.

Checks. The pre-push gate passed: lint, typecheck, format, 629 tests, build, page budgets and the forms check.

🤖 Generated with Claude Code

https://claude.ai/code/session_01J3VdAGPsVYY55a4BDjPKRT

Vite replaces import.meta.env.X in server code with X's value at build,
and the release publishes the server bundle. A secret read that way
would ship in a public release, and any setting read that way would
make the tarball depend on the machine that built it. Measured: a
variable set only for the build and read as import.meta.env.X in a
route came out as its value in dist/server, while the same read through
process.env stayed a read.

Every setting already comes from process.env, so nothing changes in the
code. ESLint now refuses any import.meta.env read but Astro's own flags
(PROD, DEV, MODE, SSR, BASE_URL, SITE, ASSETS_PREFIX), which are the
same on every machine, and refuses reading the object whole. CLAUDE.md
states the rule beside the one on Astro's key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J3VdAGPsVYY55a4BDjPKRT
@TylerVigario
TylerVigario merged commit 19c8613 into main Dec 1, 2026
19 checks passed
@TylerVigario
TylerVigario deleted the chore/no-build-time-settings branch December 1, 2026 17:22
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