Skip to content

Commit 28b2848

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 78ad2f4 commit 28b2848

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
@@ -1325,6 +1325,19 @@ changes:
13251325
This function ensures the correct decodings of percent-encoded characters as
13261326
well as ensuring a cross-platform valid absolute path string.
13271327

1328+
**Security Considerations:**
1329+
1330+
This function decodes percent-encoded characters, including encoded dot-segments
1331+
(`%2e` as `.` and `%2e%2e` as `..`), and then normalizes the resulting path.
1332+
This means that encoded directory traversal sequences (such as `%2e%2e`) are
1333+
decoded and processed as actual path traversal, even though encoded slashes
1334+
(`%2F`, `%5C`) are correctly rejected.
1335+
1336+
**Applications must not rely on `fileURLToPath()` alone to prevent directory
1337+
traversal attacks.** Always perform explicit path validation and security checks
1338+
on the returned path value to ensure it remains within expected boundaries
1339+
before using it for file system operations.
1340+
13281341
```mjs
13291342
import { fileURLToPath } from 'node:url';
13301343

@@ -1378,6 +1391,15 @@ representation of the path, a `Buffer` is returned. This conversion is
13781391
helpful when the input URL contains percent-encoded segments that are
13791392
not valid UTF-8 / Unicode sequences.
13801393
1394+
**Security Considerations:**
1395+
1396+
This function has the same security considerations as [`url.fileURLToPath()`][].
1397+
It decodes percent-encoded characters, including encoded dot-segments
1398+
(`%2e` as `.` and `%2e%2e` as `..`), and normalizes the path. **Applications
1399+
must not rely on this function alone to prevent directory traversal attacks.**
1400+
Always perform explicit path validation on the returned buffer value before
1401+
using it for file system operations.
1402+
13811403
### `url.format(URL[, options])`
13821404
13831405
<!-- YAML
@@ -2008,6 +2030,7 @@ console.log(myURL.origin);
20082030
[`querystring`]: querystring.md
20092031
[`url.domainToASCII()`]: #urldomaintoasciidomain
20102032
[`url.domainToUnicode()`]: #urldomaintounicodedomain
2033+
[`url.fileURLToPath()`]: #urlfileurltopathurl-options
20112034
[`url.format()`]: #urlformaturlobject
20122035
[`url.href`]: #urlhref
20132036
[`url.parse()`]: #urlparseurlstring-parsequerystring-slashesdenotehost

0 commit comments

Comments
 (0)