Skip to content

Commit 275c359

Browse files
committed
fix: honor per-response dotfile policy in directory listings
1 parent b861a47 commit 275c359

2 files changed

Lines changed: 45 additions & 12 deletions

File tree

‎index.js‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,12 @@ async function fastifyStatic (fastify, opts) {
232232
/**
233233
* @param {import("fastify").FastifyReply} reply
234234
* @param {string} pathname
235+
* @param {string} dotfiles
235236
* @return {Promise<boolean>}
236237
*/
237-
async function sendDirectoryList (reply, pathname) {
238-
// never list a directory whose path contains a dotfile segment when
239-
// dotfiles are denied or ignored: the 403/404 from send must stand
240-
if (opts.dotfiles !== 'allow' && containsDotFile(pathname)) {
238+
async function sendDirectoryList (reply, pathname, dotfiles) {
239+
// Keep send's dotfile denial intact; its 403 is not a missing index.
240+
if (dotfiles !== 'allow' && containsDotFile(pathname)) {
241241
return false
242242
}
243243

@@ -255,7 +255,7 @@ async function fastifyStatic (fastify, opts) {
255255
options: opts.list,
256256
route: pathname,
257257
prefix,
258-
dotfiles: opts.dotfiles
258+
dotfiles
259259
}).catch((err) => reply.send(err))
260260

261261
return true
@@ -426,7 +426,7 @@ async function fastifyStatic (fastify, opts) {
426426
options: opts.list,
427427
route: pathname,
428428
prefix,
429-
dotfiles: opts.dotfiles
429+
dotfiles: options.dotfiles
430430
}).catch((err) => reply.send(err))
431431
}
432432

@@ -462,7 +462,7 @@ async function fastifyStatic (fastify, opts) {
462462
(!options.index || !options.index.length) &&
463463
pathnameForSend[pathnameForSend.length - 1] === '/'
464464
) {
465-
if (opts.list && await sendDirectoryList(reply, pathname)) {
465+
if (opts.list && await sendDirectoryList(reply, pathname, options.dotfiles)) {
466466
return
467467
}
468468
}
@@ -488,7 +488,7 @@ async function fastifyStatic (fastify, opts) {
488488
}
489489

490490
// if file exists, send real file, otherwise send dir list if name match
491-
if (opts.list && dirList.handle(pathname, opts.list) && await sendDirectoryList(reply, pathname)) {
491+
if (opts.list && dirList.handle(pathname, opts.list) && await sendDirectoryList(reply, pathname, options.dotfiles)) {
492492
return
493493
}
494494

@@ -1067,10 +1067,6 @@ function decodePathname (pathname) {
10671067
return decodeURI(pathname.replace(encodedHashOrQuestionMarkRegex, decodeURIComponent))
10681068
}
10691069

1070-
/**
1071-
* @param {string} url
1072-
* @return {string}
1073-
*/
10741070
/**
10751071
* Whether any segment of a URL pathname is a dotfile (`.foo`), mirroring
10761072
* `@fastify/send`'s check (`.` alone is not a dotfile).
@@ -1081,6 +1077,10 @@ function containsDotFile (pathname) {
10811077
return pathname.split('/').some((s) => s.length > 1 && s[0] === '.')
10821078
}
10831079

1080+
/**
1081+
* @param {string} url
1082+
* @return {string}
1083+
*/
10841084
function getRedirectUrl (url) {
10851085
let i = 0
10861086
// we detect how many slash before a valid path

‎test/dir-list.test.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,39 @@ test('dir list does not list dot-directories when dotfiles are denied or ignored
886886
t.assert.deepStrictEqual(response.json(), { dirs: ['nested'], files: ['key.pem'] })
887887
})
888888

889+
test('dir list uses per-response dotfiles option for dot-directories', async t => {
890+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'fastify-static-dotdir-override-'))
891+
t.after(() => fs.rmSync(root, { recursive: true, force: true }))
892+
fs.mkdirSync(path.join(root, '.secret', 'nested'), { recursive: true })
893+
fs.writeFileSync(path.join(root, '.secret', 'key.pem'), 'key')
894+
fs.writeFileSync(path.join(root, '.secret', 'nested', 'dump.sql'), 'dump')
895+
896+
for (const { configured, override, statusCode } of [
897+
{ configured: 'allow', override: 'deny', statusCode: 403 },
898+
{ configured: 'allow', override: 'ignore', statusCode: 404 },
899+
{ configured: 'deny', override: 'allow', statusCode: 200 }
900+
]) {
901+
const fastify = Fastify()
902+
fastify.register(fastifyStatic, { root, serve: false, dotfiles: configured, index: false, list: true })
903+
fastify.get('/file', (_request, reply) => reply.sendFile('/.secret/', { dotfiles: override }))
904+
fastify.get('/nested', (_request, reply) => reply.sendFile('/.secret/nested/', { dotfiles: override }))
905+
t.after(() => fastify.close())
906+
907+
for (const route of ['/file', '/nested']) {
908+
const response = await fastify.inject(route)
909+
t.assert.deepStrictEqual(response.statusCode, statusCode, `${configured} overridden with ${override}: ${route}`)
910+
if (statusCode === 200) {
911+
t.assert.deepStrictEqual(response.json(), route === '/file'
912+
? { dirs: ['nested'], files: ['key.pem'] }
913+
: { dirs: [], files: ['dump.sql'] })
914+
} else {
915+
t.assert.ok(!response.body.includes('key.pem'))
916+
t.assert.ok(!response.body.includes('dump.sql'))
917+
}
918+
}
919+
}
920+
})
921+
889922
test('dir list error', async t => {
890923
t.plan(6)
891924

0 commit comments

Comments
 (0)