Summary
A path traversal vulnerability in webpack-dev-middleware can cause requests to resolve outside the configured output directory when a publicPath without a trailing slash is used.
If the middleware can read from a physical filesystem, an attacker may retrieve files located one directory above the intended output path.
A path traversal vulnerability in webpack-dev-middleware can cause requests to resolve outside the configured output directory when a publicPath without a trailing slash is used.
If the middleware can read from a physical filesystem, an attacker may retrieve files located one directory above the intended output path.
Affected context
The issue occurs in webpack-dev-middleware deployments where:
- The configured publicPath does not end with a trailing slash
- Request paths are converted into filesystem paths by getFilenameFromUrl
- The middleware uses a physical filesystem through writeToDisk or a custom outputFileSystem
Deployments using the default publicPath value of "auto", which resolves to "/", are not affected by this specific path construction condition. The default in-memory filesystem may also limit exposure because it generally contains only generated build output.
The issue occurs in webpack-dev-middleware deployments where:
- The configured publicPath does not end with a trailing slash
- Request paths are converted into filesystem paths by getFilenameFromUrl
- The middleware uses a physical filesystem through writeToDisk or a custom outputFileSystem
Deployments using the default publicPath value of "auto", which resolves to "/", are not affected by this specific path construction condition. The default in-memory filesystem may also limit exposure because it generally contains only generated build output.
Exploit preconditions
- Attack surface: HTTP request handling by webpack-dev-middleware
- Required access: Ability to send requests to the development or asset-serving endpoint
- Configuration requirement: A publicPath without a trailing slash must be configured
- Filesystem requirement: The middleware must be backed by a physical filesystem, such as through writeToDisk or a custom outputFileSystem
- Trigger point: Filename resolution in getFilenameFromUrl
- Why exploitation is possible: A prefix check and fixed-length slice can disagree about path boundaries, allowing a crafted pathname to produce a parent-directory component after slicing
- Attack surface: HTTP request handling by webpack-dev-middleware
- Required access: Ability to send requests to the development or asset-serving endpoint
- Configuration requirement: A publicPath without a trailing slash must be configured
- Filesystem requirement: The middleware must be backed by a physical filesystem, such as through writeToDisk or a custom outputFileSystem
- Trigger point: Filename resolution in getFilenameFromUrl
- Why exploitation is possible: A prefix check and fixed-length slice can disagree about path boundaries, allowing a crafted pathname to produce a parent-directory component after slicing
Why this happens
The underlying bug is an inconsistent path-boundary model between validation and path construction. The traversal guard evaluates normalized path segments, while containment relies on a string prefix comparison and the later slice assumes that the publicPath ends at the same logical boundary.
When the publicPath has no trailing slash, a dot-dot sequence embedded in the adjacent pathname segment can evade the segment-based guard. The fixed offset then exposes a parent-directory component to path.join, which can resolve outside the intended output directory. This maps to CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal').
The underlying bug is an inconsistent path-boundary model between validation and path construction. The traversal guard evaluates normalized path segments, while containment relies on a string prefix comparison and the later slice assumes that the publicPath ends at the same logical boundary.
When the publicPath has no trailing slash, a dot-dot sequence embedded in the adjacent pathname segment can evade the segment-based guard. The fixed offset then exposes a parent-directory component to path.join, which can resolve outside the intended output directory. This maps to CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal').
Potential impact
- An attacker may read sensitive files located one directory above the configured output path
- Source files, environment-related files, or deployment metadata could be exposed
- Development credentials or service configuration may be disclosed
- The affected development or asset-serving process could lose confidentiality
- Downstream compromise could become possible if exposed files contain secrets
- An attacker may read sensitive files located one directory above the configured output path
- Source files, environment-related files, or deployment metadata could be exposed
- Development credentials or service configuration may be disclosed
- The affected development or asset-serving process could lose confidentiality
- Downstream compromise could become possible if exposed files contain secrets
General mitigation guidance
Mitigation generally focuses on making URL-to-filesystem mapping boundary-aware and reducing filesystem exposure.
Common defensive patterns include:
- Resolve candidate paths and verify they remain within the intended directory using canonical path containment checks
- Treat publicPath and pathname boundaries as structured path components rather than raw string prefixes
- Reject traversal indicators after consistent URL decoding and normalization
- Prefer an isolated, read-only, in-memory filesystem for development output when disk access is unnecessary
- Restrict writeToDisk and custom filesystem access to narrowly scoped output directories
- Run development middleware with least-privilege filesystem permissions and without access to sensitive parent directories
- Avoid exposing development middleware directly to untrusted networks
Mitigation generally focuses on making URL-to-filesystem mapping boundary-aware and reducing filesystem exposure.
Common defensive patterns include:
- Resolve candidate paths and verify they remain within the intended directory using canonical path containment checks
- Treat publicPath and pathname boundaries as structured path components rather than raw string prefixes
- Reject traversal indicators after consistent URL decoding and normalization
- Prefer an isolated, read-only, in-memory filesystem for development output when disk access is unnecessary
- Restrict writeToDisk and custom filesystem access to narrowly scoped output directories
- Run development middleware with least-privilege filesystem permissions and without access to sensitive parent directories
- Avoid exposing development middleware directly to untrusted networks
How teams detect issues like this
Detection can combine code inspection, configuration review, and runtime monitoring:
- Use static analysis rules to identify path.join or equivalent calls fed by request-derived strings
- Review code for startsWith-based containment checks, fixed-offset pathname slicing, and validation performed before a separate path transformation
- Flag publicPath configurations that lack explicit trailing-slash or boundary handling
- Inventory deployments using writeToDisk or custom outputFileSystem implementations
- Test URL-to-file mapping with boundary-adjacent names, encoded separators, and parent-directory representations in isolated environments
- Enforce policies that prevent development asset servers from reading outside approved output roots
Automated code review and security testing tools can help identify inconsistent normalization and containment logic.
Detection can combine code inspection, configuration review, and runtime monitoring:
- Use static analysis rules to identify path.join or equivalent calls fed by request-derived strings
- Review code for startsWith-based containment checks, fixed-offset pathname slicing, and validation performed before a separate path transformation
- Flag publicPath configurations that lack explicit trailing-slash or boundary handling
- Inventory deployments using writeToDisk or custom outputFileSystem implementations
- Test URL-to-file mapping with boundary-adjacent names, encoded separators, and parent-directory representations in isolated environments
- Enforce policies that prevent development asset servers from reading outside approved output roots
Automated code review and security testing tools can help identify inconsistent normalization and containment logic.

