Summary
A Local File Inclusion vulnerability in esm.sh's esbuild plugin allows crafted npm packages to influence how the browser field in package.json is resolved, causing the server to read arbitrary files from the host filesystem.
When a malicious package is processed during the build pipeline, the CDN server may return contents of sensitive local files as part of the build response.
Affected context
The vulnerability arises in esm.sh deployments that use the esbuild-based pipeline to process third‑party npm packages and honor the browser field in package.json.
It is relevant when:
- The CDN or build service automatically fetches and builds npm packages supplied by users or upstream registries
- The build process runs with filesystem access to sensitive paths (e.g., configuration, credentials, or source code)
- The
browserfield is resolved in a way that can reference local filesystem paths instead of only controlled, in-package files
In these conditions, a malicious package can influence resolution logic and cause unintended file reads during build.
Exploit preconditions
- Attack surface: The esm.sh build API / CDN endpoint that accepts npm package names or versions to build.
- Required access: Ability to publish or control an npm package that esm.sh will fetch and build (for example, pointing the CDN at an attacker-controlled package name or version).
- Trigger point: The build process when esbuild resolves module entry points and dependency paths using the
browserfield inpackage.json. - Why exploitation is possible: Path resolution logic around the
browserfield permits references that escape the intended package scope, allowing the plugin to open and read arbitrary paths on the server filesystem (CWE-22: Path Traversal / Local File Inclusion).
Why this happens
The core issue is insufficient validation of filesystem paths derived from untrusted metadata, specifically the browser field in package.json processed by the esbuild plugin.
Instead of constraining resolution to safe, package-local paths, the plugin allows attacker-controlled values to influence which files are opened on the host system.
This maps to CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal'), where user-influenced paths are not properly normalized, validated, or sandboxed, enabling access to arbitrary files outside the intended directory boundary.
Potential impact
- An attacker may read sensitive configuration files present on the CDN/build host.
- An attacker may access source code or internal scripts deployed alongside the service.
- An attacker could obtain credentials, API keys, or secrets stored in readable files.
- An attacker can potentially map parts of the server's filesystem structure by probing for readable paths.
General mitigation guidance
Effective mitigation centers on strict path handling and sandboxing during build:
- Constrain resolution of the
browserfield and similar metadata-derived paths to the package's own directory, rejecting any absolute paths or path traversal segments (e.g.,..). - Normalize and validate all paths before file access, enforcing explicit allowlists of directories that builds are permitted to read from.
- Run build processes under a dedicated, low-privilege account with access only to an isolated working directory, avoiding shared secrets or sensitive configuration in that context.
- Consider using chroot-like jails, containers, or virtualized build sandboxes so that even if path validation fails, the accessible filesystem is limited.
- Reduce trust in package metadata by ignoring or tightly constraining fields that can redirect resolution (like
browser) when operating a multi-tenant CDN or public build service.
How teams detect issues like this
Detection usually combines code-level review with behavioral monitoring:
- Static analysis / code review: Look for file access operations in the esbuild plugin and related code that rely on package metadata (e.g.,
browser,main, or custom fields) without strict validation or normalization of paths. - Configuration / policy checks: Enforce internal policies requiring safe path-joining utilities, package-local path enforcement, and prohibition of absolute paths or traversal segments in resolution logic.
- Runtime monitoring: Instrument build workers to log file access patterns, especially reads outside expected working directories; alert on attempts to read known sensitive locations (
/etc, home directories, secret mounts). - Automated tooling: Integrate SAST or custom linters into CI for the CDN/build service codebase to detect usages of untrusted metadata in path construction and file reads.

