Skip to content

Commit e46bab7

Browse files
authored
fix(e2e): harden static server root containment check
1 parent a91ad6d commit e46bab7

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

tests/e2e/_support/serve-static.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
import { createReadStream, statSync } from 'node:fs'
33
import { createServer } from 'node:http'
4-
import { extname, join, normalize, resolve } from 'node:path'
4+
import { extname, isAbsolute, join, normalize, relative, resolve } from 'node:path'
55
import process from 'node:process'
66

77
const [, , dirArg, portArg] = process.argv
@@ -31,9 +31,10 @@ function resolveAsset(urlPath) {
3131
const clean = urlPath.split('?')[0].split('#')[0]
3232
const decoded = decodeURIComponent(clean)
3333
// Treat trailing-slash URLs as index.html.
34-
const relative = decoded.endsWith('/') ? `${decoded}index.html` : decoded
35-
const target = normalize(join(root, relative))
36-
if (!target.startsWith(root))
34+
const requestPath = decoded.endsWith('/') ? `${decoded}index.html` : decoded
35+
const target = normalize(join(root, requestPath))
36+
const fromRoot = relative(root, target)
37+
if (fromRoot.startsWith('..') || isAbsolute(fromRoot))
3738
return null
3839
try {
3940
const st = statSync(target)

0 commit comments

Comments
 (0)