Skip to content

Commit 1220349

Browse files
committed
docs(security): add path traversal warning to normalizePath()
Add security warning to normalizePath() JSDoc explaining that the function resolves '..' patterns as part of normalization. Developers processing untrusted user input (HTTP requests, file uploads, URL parameters) must validate for path traversal attacks BEFORE calling this function. Include examples showing how normalizePath() resolves traversal patterns: - '/../etc/passwd' → '/etc/passwd' - '/safe/../../unsafe' → '/unsafe' This prevents misuse of normalizePath() on untrusted input where path traversal validation should happen first.
1 parent cc47920 commit 1220349

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/path.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,13 @@ export function isRelative(pathLike: string | Buffer | URL): boolean {
381381
* @param {string | Buffer | URL} pathLike - The path to normalize
382382
* @returns {string} The normalized path with forward slashes and collapsed segments
383383
*
384+
* @security
385+
* **WARNING**: This function resolves `..` patterns as part of normalization, which means
386+
* paths like `/../etc/passwd` become `/etc/passwd`. When processing untrusted user input
387+
* (HTTP requests, file uploads, URL parameters), you MUST validate for path traversal
388+
* attacks BEFORE calling this function. Check for patterns like `..`, `%2e%2e`, `\..`,
389+
* and other traversal encodings first.
390+
*
384391
* @example
385392
* ```typescript
386393
* // Basic normalization
@@ -401,6 +408,10 @@ export function isRelative(pathLike: string | Buffer | URL): boolean {
401408
* normalizePath('..') // '..'
402409
* normalizePath('///foo///bar///') // '/foo/bar'
403410
* normalizePath('foo/../..') // '..'
411+
*
412+
* // Security: Path traversal is resolved (intended behavior for trusted paths)
413+
* normalizePath('/../etc/passwd') // '/etc/passwd' ⚠️
414+
* normalizePath('/safe/../../unsafe') // '/unsafe' ⚠️
404415
* ```
405416
*/
406417
/*@__NO_SIDE_EFFECTS__*/

0 commit comments

Comments
 (0)