Skip to content

Commit cad312a

Browse files
RafaelGSSaduh95
authored andcommitted
doc: clarify fileURLToPath security considerations
Add clarification that fileURLToPath() decodes encoded dot-segments (%2e%2e) which are normalized as path traversal. Applications must perform their own path validation to prevent directory traversal attacks. Also applies to fileURLToPathBuffer(). PR-URL: #60887 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 249683f commit cad312a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

doc/api/url.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,19 @@ changes:
12001200
This function ensures the correct decodings of percent-encoded characters as
12011201
well as ensuring a cross-platform valid absolute path string.
12021202

1203+
**Security Considerations:**
1204+
1205+
This function decodes percent-encoded characters, including encoded dot-segments
1206+
(`%2e` as `.` and `%2e%2e` as `..`), and then normalizes the resulting path.
1207+
This means that encoded directory traversal sequences (such as `%2e%2e`) are
1208+
decoded and processed as actual path traversal, even though encoded slashes
1209+
(`%2F`, `%5C`) are correctly rejected.
1210+
1211+
**Applications must not rely on `fileURLToPath()` alone to prevent directory
1212+
traversal attacks.** Always perform explicit path validation and security checks
1213+
on the returned path value to ensure it remains within expected boundaries
1214+
before using it for file system operations.
1215+
12031216
```mjs
12041217
import { fileURLToPath } from 'node:url';
12051218

@@ -1253,6 +1266,15 @@ representation of the path, a `Buffer` is returned. This conversion is
12531266
helpful when the input URL contains percent-encoded segments that are
12541267
not valid UTF-8 / Unicode sequences.
12551268
1269+
**Security Considerations:**
1270+
1271+
This function has the same security considerations as [`url.fileURLToPath()`][].
1272+
It decodes percent-encoded characters, including encoded dot-segments
1273+
(`%2e` as `.` and `%2e%2e` as `..`), and normalizes the path. **Applications
1274+
must not rely on this function alone to prevent directory traversal attacks.**
1275+
Always perform explicit path validation on the returned buffer value before
1276+
using it for file system operations.
1277+
12561278
### `url.format(URL[, options])`
12571279
12581280
<!-- YAML
@@ -1869,6 +1891,7 @@ console.log(myURL.origin);
18691891
[`querystring`]: querystring.md
18701892
[`url.domainToASCII()`]: #urldomaintoasciidomain
18711893
[`url.domainToUnicode()`]: #urldomaintounicodedomain
1894+
[`url.fileURLToPath()`]: #urlfileurltopathurl-options
18721895
[`url.format()`]: #urlformaturlobject
18731896
[`url.href`]: #urlhref
18741897
[`url.parse()`]: #urlparseurlstring-parsequerystring-slashesdenotehost

0 commit comments

Comments
 (0)