Summary
A path traversal vulnerability in esm.sh's tar archive handling allows malicious archives to write files to unintended locations on the server filesystem.
Because path.Clean only normalizes paths and does not block absolute or otherwise dangerous paths in tar entries, an attacker supplying a crafted tar file could cause file overwrite or placement outside the intended extraction directory.
Affected context
This issue affects components in esm.sh that extract or process tar archives supplied directly or indirectly by external input.
The vulnerability is relevant when:
- Tar file contents are not fully trusted (e.g., uploaded by users or fetched from external sources), and
- The extraction logic relies on
path.Cleanfor safety without additional checks against absolute or parent-directory paths.
In such contexts, archive entries may be written outside the intended root extraction directory.
Exploit preconditions
- Attack surface: Tar archive processing endpoints, jobs, or background tasks in esm.sh that handle externally controllable tar files.
- Required access: Ability to influence or provide tar archives processed by the system (e.g., via an upload, remote fetch, or build pipeline input).
- Trigger point: Extraction or processing of tar entries where paths are sanitized only with
path.Cleanbut not validated against absolute paths or directory traversal patterns. - Why exploitation is possible:
path.Cleanremoves redundant separators and resolves./.., but it does not inherently reject absolute paths or enforce confinement to a specific base directory. Attackers can use absolute paths (and in some cases cleverly structured relative paths) to escape the intended extraction root.
Why this happens
The root cause is an overreliance on path normalization (path.Clean) as a security boundary for file extraction from tar archives.
This maps directly to CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal').
The code assumes that cleaned paths are safe, but does not implement additional checks to:
- Reject absolute paths (e.g., beginning with
/), or - Enforce that target paths remain within a designated base directory.
As a result, the extraction logic may write files outside the intended sandbox when processing malicious tar entries.
Potential impact
- Overwrite of existing files on the server within the effective filesystem permissions of the esm.sh process
- Placement of unexpected files or content in configuration or data directories
- Potential alteration of cached packages, bundles, or artifacts served by the CDN
- In certain deployments, escalation to broader system compromise if sensitive files or executable scripts are overwritten
General mitigation guidance
Effective mitigation focuses on strict path validation and directory confinement when handling archive contents:
- Treat all archive entry paths as untrusted, regardless of prior normalization.
- Reject tar entries whose paths are absolute or that resolve outside a designated extraction root after joining and cleaning.
- Implement a robust "zip-slip" style defense: join the cleaned entry path to a known base directory and verify that the resulting path still has the base directory as its prefix.
- Avoid writing files directly to operational or configuration directories; instead, extract to a controlled temporary directory and only promote vetted content.
- Run archive-processing components with least-privilege file system permissions to limit blast radius if traversal occurs.
These patterns reduce dependence on any single normalization function and confine effects even under unexpected input.
How teams detect issues like this
Detection typically combines code-level analysis and operational monitoring:
- Static analysis / code review:
- Search for tar (or other archive) extraction routines that use
path.Clean(or similar) without subsequently validating that the final path resides within a fixed base directory. - Flag logic that accepts archive entry names directly from untrusted sources and uses them in filesystem operations (
os.Create,os.OpenFile, renames, etc.) without explicit traversal checks. - Review any custom extraction utilities for missing prefix checks or failure to reject absolute paths.
- Policy and testing:
- Add unit and integration tests with crafted tar archives containing absolute paths and
../segments to ensure they are rejected or safely handled. - Integrate security-focused linters or SAST tools configured to recognize archive extraction anti-patterns and path traversal risks.
- Runtime monitoring:
- Log extraction destinations and watch for unexpected file writes outside the intended directories, where feasible, to surface anomalous behavior in production.

