/

CVE-2026-49982

Severity:

HIGH RISK

(8.2)

Path Traversal (CWE-22) in Node.js tmp file creator

CVE-2026-49982

Analyzed

Vulnerability type

Category:

Path Traversal / Directory Traversal

CWE:

CWE-20, CWE-22

Impact:

Arbitrary File System Write Outside Intended Directory

Severity:

HIGH RISK

(8.2)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

A type-check bypass in the Node.js tmp library allows crafted non-string values to bypass simple ".." checks and generate file paths outside the intended temporary directory.

When applications forward untrusted data directly into tmp helpers, an attacker can cause files or directories to be created in attacker-chosen locations with the host process's privileges.

Affected context

This vulnerability appears in Node.js applications that rely on the tmp package (version 0.2.6) for creating temporary files or directories.

It is most relevant when applications accept untrusted input (for example, JSON bodies, query parameters, or form fields) and pass it directly as prefix, postfix, or template options to tmp.file, tmp.fileSync, tmp.dir, tmp.dirSync, tmp.tmpName, or tmp.tmpNameSync without enforcing strict string typing.

In such cases, non-string values like arrays or buffers can be coerced into paths that escape the configured tmpdir.

Exploit preconditions

  • Attack surface: Any application endpoint or code path that accepts user-controlled data and forwards it to tmp option fields (prefix, postfix, template, or similar) without normalization or type enforcement.
  • Required access: Ability to supply crafted request data (e.g., JSON bodies or query strings) that the application maps to tmp options.
  • Trigger point: Invocation of a tmp helper (tmp.file*, tmp.dir*, tmp.tmpName*) using untrusted, non-string option values that are later coerced to strings.
  • Why exploitation is possible: The _assertPath guard only checks for the substring ".." on values it treats as strings, but can be bypassed by non-string inputs whose includes('..') is falsy while their eventual stringification still contains "../". These coerced values flow into path.join, enabling directory traversal outside the intended temporary directory.

Why this happens

The core issue is an improper input validation and type handling pattern mapped to:

  • CWE-20: Improper Input Validation
  • CWE-22: Improper Limitation of a Pathname to a Restricted Directory (Path Traversal)

The _assertPath function attempts to defend against directory traversal by rejecting strings containing "..". However, it relies on a simplistic check that assumes the option values are plain strings. When non-string objects (arrays, buffers, or objects with custom toString or includes behavior) are passed:

  • The guard calls includes('..'), which may return falsy or behave unexpectedly.
  • Later, the same value is coerced into a string—via Array.prototype.join or generic string coercion—inside _generateTmpName and combined using path.join(tmpDir, opts.dir, name).

Because the guard and the eventual stringification do not share the same assumptions, path components like "../" can be introduced after validation, leading to directory traversal and file creation outside the intended temporary directory.

Potential impact

  • Creation of files or directories at attacker-controlled paths outside the configured temporary directory.
  • Overwriting or clobbering of existing files if the host process has write permissions, which could affect configuration files, logs, or other application assets.
  • Planting of files that may later be consumed by other components, potentially enabling secondary attacks (e.g., configuration manipulation, log injection, or influencing other processes).
  • Escalation of impact when combined with weak file permissions or downstream components that execute or trust files found in specific locations.

General mitigation guidance

Effective mitigation focuses on strengthening input validation and decoupling untrusted data from filesystem paths:

  • Enforce strict typing for path-related options: Normalize and coerce all tmp options (prefix, postfix, template, etc.) to validated strings before passing them to the library. Reject non-string types instead of relying on implicit stringification.
  • Apply canonicalization and path policies: Ensure that generated paths remain within an expected base directory by canonicalizing and verifying resulting paths before use. Apply a "deny traversal" policy that rejects any value that would resolve outside the intended directory.
  • Separate user input from path structure: Avoid directly embedding untrusted input into path prefixes or templates. Use internal identifiers (UUIDs, counters) and, if necessary, whitelist-safe subsets of user input, such as alphanumeric characters.
  • Least-privilege file operations: Run the application with minimal filesystem privileges and avoid executing with permissions that can modify sensitive system or application directories. Consider isolating temporary directories and tightening their permissions.
  • Defensive defaults in API design: For libraries or shared utilities, design wrappers around tmp that expose only safe configurations and sanitize any caller-provided options, so application code does not directly interact with low-level path construction.

How teams detect issues like this

Detection can combine static and review-based approaches:

  • Static analysis and linting:
  • Flag calls to tmp.file*, tmp.dir*, or tmp.tmpName* where any option (prefix, postfix, template, or analogous) originates from untrusted sources (e.g., HTTP request bodies, query parameters, headers) without explicit validation or type checks.
  • Identify patterns where non-string inputs are passed into functions that eventually interact with the filesystem (e.g., calls that rely on implicit string coercion).
  • Code review focus areas:
  • Review middleware and request-handling layers to ensure they do not directly feed request fields into tmp options.
  • Look for joins of tmpdir with user-influenced segments using path.join, and confirm that resulting paths are constrained to a known base directory.
  • Runtime monitoring and policy enforcement:
  • Instrument file creation APIs to log or alert when processes write outside expected temporary directories, especially under application service accounts.
  • Enforce policies that restrict write access from application processes to a small set of directories, making unexpected writes more visible.
  • Automated tooling:
  • Integrate automated code scanning tools that support taint analysis from request input to filesystem operations, focusing on Node.js ecosystems and libraries like tmp.

Sources

SourceLink
github.comgithub.com/raszi/node-tmp/security/advisories/GHSA-7c78-jf6q-g5cm

Frequently asked

questions

Exploitability depends on whether an application forwards untrusted data into tmp options without sanitization. If no user-controlled fields influence prefix, postfix, template, or related options, the practical risk is lower, but generic utility layers that map request fields into these options are particularly exposed.

An attacker typically needs only the ability to send crafted requests (such as HTTP requests with JSON bodies or query strings) that the application maps into tmp option fields. No direct filesystem or shell access is required; exploitation occurs through normal application request handling.

The most robust fix is to strictly treat all path-related options as validated strings, rejecting any non-string types and applying conservative character whitelists. Additionally, ensure that any generated path is verified to reside within the intended temporary directory before file or directory creation.

Adopt a consistent strategy for handling user input that feeds into filesystem operations: enforce strong typing, avoid implicit stringification, perform canonicalization and boundary checks, and centralize path construction in well-reviewed utility functions. Embedding these patterns into coding standards and CI-enforced checks helps prevent similar type- and validation-bypass bugs.

Find out what's already
exploitable in your codebase.