Summary
The esm.sh static file server can resolve URL-decoded paths outside its configured application directory because it does not verify that the joined path remains within that directory. An unauthenticated remote attacker may be able to retrieve files readable by the server process, including when Dev is disabled.
Affected context
The issue occurs in the documented web.NewHandler static file server in web/handler.go. It is relevant when URL-decoded request paths are joined with Config.AppDir without checking that the resolved path stays within that directory. URL-encoded dot segments can bypass the plain traversal redirect performed by Go's ServeMux; the issue is reachable with Dev disabled.
Exploit preconditions
- Attack surface: The esm.sh web server's static file handler, reachable over HTTP.
- Required access: No authentication is required.
- Trigger point: A request containing URL-encoded path traversal segments that are decoded and joined with
Config.AppDir. - Why exploitation is possible: The handler does not verify that the resulting file path remains within the configured application directory. Disclosure is limited to files readable by the server process.
Why this happens
The handler joins a URL-decoded path with the configured application directory without enforcing path confinement. This is a path traversal flaw, mapped to CWE-22: Improper Limitation of a Pathname to a Restricted Directory. A pathname that includes traversal components can resolve outside the intended directory when the application does not canonicalize and validate the final path before opening it.
Potential impact
- An attacker could retrieve files outside the application directory that are readable by the server process.
- Exposed files may disclose sensitive information, depending on the server's filesystem permissions and contents.
- The confirmed impact is file disclosure; code execution is not established.
General mitigation guidance
Defensive patterns for static file serving include:
- Resolve and canonicalize requested paths, then verify the final path remains beneath the configured root before opening it.
- Prefer file-serving APIs or designs that enforce directory confinement by default.
- Treat URL paths as untrusted input and validate them after decoding and normalization.
- Run the server with least-privilege filesystem permissions and limit its access to sensitive files.
How teams detect issues like this
- Use static analysis to identify joins between request-derived paths and filesystem roots that lack a containment check.
- Review file-serving code for validation performed before decoding or normalization, or for checks that do not apply to the final resolved path.
- Test path-confinement policies with encoded and normalized traversal variants in controlled security tests.
- Monitor access logs for unusual requests targeting paths outside the expected static-file namespace.

