Skip to content

Commit c31f17d

Browse files
authored
Merge pull request from GHSA-pgh6-m65r-2rhq
* fix redirect injection * remove console.log * fix extra case
1 parent bbdf96f commit c31f17d

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

index.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ async function fastifyStatic (fastify, opts) {
152152
}
153153

154154
if (opts.redirect === true) {
155-
reply.redirect(301, getRedirectUrl(request.raw.url))
155+
try {
156+
reply.redirect(301, getRedirectUrl(request.raw.url))
157+
} catch (error) {
158+
reply.send(error)
159+
}
156160
} else {
157161
reply.callNotFound()
158162
}
@@ -443,8 +447,18 @@ function getEncodingExtension (encoding) {
443447
}
444448

445449
function getRedirectUrl (url) {
446-
const parsed = new URL(url, 'http://localhost.com/')
447-
return parsed.pathname + (parsed.pathname[parsed.pathname.length - 1] !== '/' ? '/' : '') + (parsed.search || '')
450+
if (url.startsWith('//') || url.startsWith('/\\')) {
451+
// malicous redirect
452+
return '/'
453+
}
454+
try {
455+
const parsed = new URL(url, 'http://localhost.com/')
456+
return parsed.pathname + (parsed.pathname[parsed.pathname.length - 1] !== '/' ? '/' : '') + (parsed.search || '')
457+
} catch (error) {
458+
const err = new Error(`Invalid redirect URL: ${url}`)
459+
err.statusCode = 400
460+
throw err
461+
}
448462
}
449463

450464
module.exports = fp(fastifyStatic, {

test/static.test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -3283,8 +3283,14 @@ t.test(
32833283
}
32843284
)
32853285

3286-
t.test('should not redirect to protocol-relative locations', { only: 1 }, (t) => {
3286+
t.test('should not redirect to protocol-relative locations', (t) => {
32873287
const urls = [
3288+
['//^/..', '/', 301],
3289+
['//^/.', null, 404], // it is NOT recognized as a directory by pillarjs/send
3290+
['//:/..', '/', 301],
3291+
['/\\\\a//google.com/%2e%2e%2f%2e%2e', '/', 301],
3292+
['//a//youtube.com/%2e%2e%2f%2e%2e', '/', 301],
3293+
['/^', null, 404], // it is NOT recognized as a directory by pillarjs/send
32883294
['//google.com/%2e%2e', '/', 301],
32893295
['//users/%2e%2e', '/', 301],
32903296
['//users', null, 404]

0 commit comments

Comments
 (0)