Skip to content

Build-machine env vars written into the worker bundle #618

Backlog

Description

@neuromechanist

Problem

A bare reference to import.meta.env in server code makes Vite write the whole object into the production worker bundle (dist/_worker.js/index.js) as Object.assign(__vite_import_meta_env__, { ... }). That object includes every build-machine environment variable whose name appears anywhere in the same file. The typeof import.meta.env !== "undefined" guard is such a bare reference.

In a local bunx astro build on staging plus PR #617, these still leak:

File Function Keys written into the bundle
src/lib/docs-base.ts resolveDocsBase TERM, PATH, _ (the full build-machine PATH)
src/lib/api-base.ts apiBase _
src/lib/data-base.ts resolveDataBase _
src/lib/zarr-base.ts zarrBase _
Astro's own action serializers (not our code) serializeActionResult, deserializeActionResult _

PR #617 fixed the same leak in src/middleware.ts (which it had introduced), src/lib/osa-widget.ts and src/lib/osa-overview.ts by reading each key by name.

To check: build, then search the worker bundle for Object.assign(__vite_import_meta_env__.

Proposed fix

  • Read each key by name (import.meta.env.PUBLIC_API_BASE_URL) without the typeof guard, as Fix quality-view number labels, deep link, lede spacing and dev CSP #617 does.
  • First confirm that nothing imports these modules outside Vite, where import.meta.env is undefined (the guard's original reason).
  • Add a post-build check that fails when Object.assign(__vite_import_meta_env__ in the worker bundle contains a non-PUBLIC_ key, for example inside scripts/check-quality-views-size.mjs's build step or a new script.

Agentic AI tools were used. Workflows follow the Research Skills (doi:10.5281/zenodo.20696515). The author assumes full responsibility for this content and associated changes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions