From 4f9842568007b4b4d415e951c50bc768d3db4fd0 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sun, 25 Oct 2026 15:19:55 +0000 Subject: [PATCH] feat!: default dotfiles to 'ignore' Change the default for the `dotfiles` option from `'allow'` to `'ignore'`, matching the `@fastify/send` and `serve-static` defaults. Dotfiles and files inside hidden directories (for example `/.well-known/...`) are no longer served unless `dotfiles: 'allow'` is set explicitly. This protects sensitive dotfiles such as `.env`, `.htpasswd` and `.git/config` that may be left in the static root. Breaking change; bump the major version to 11.0.0. --- README.md | 18 ++++++++++++++++++ index.js | 2 +- package.json | 2 +- test/dir-list.test.js | 6 ++++++ test/static.test.js | 23 ++++++++++++++++++++++- 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a4d5c8e..88683f4 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,24 @@ The following options are also supported and will be passed directly to the - [`maxAge`](https://www.npmjs.com/package/@fastify/send#maxage) - [`onFile`](https://www.npmjs.com/package/@fastify/send#onfile) +#### `dotfiles` + +Default: `'ignore'` + +How files and directories whose name begins with a dot (`.env`, `.git`, `.npmrc`, `.well-known`, ...) are treated. This option is passed through to [`@fastify/send`](https://www.npmjs.com/package/@fastify/send#dotfiles): + +- `'allow'` - No special treatment; dotfiles are served like any other file. +- `'deny'` - Return a `403` for any request for a dotfile. +- `'ignore'` - Treat the dotfile as if it does not exist and return a `404`. **This is the default.** + +> [!NOTE] +> The default (`'ignore'`) matches the `@fastify/send` and `serve-static` +> defaults. Dotfiles — and files inside hidden directories such as +> `/.well-known/...` — are served only if you opt in with `dotfiles: 'allow'`. +> This protects sensitive files such as `.env`, `.htpasswd` or `.git/config` +> that may be left in the static root. If you serve `/.well-known/...` (for +> example for ACME or PGP discovery), set `dotfiles: 'allow'` explicitly. + These options can be altered when calling `reply.sendFile('filename.html', options)` or `reply.sendFile('filename.html', 'otherfilename.html', options)` on each response. `onFile(path, stat)` receives the selected file's absolute path and file system statistics. It may return a promise, which is awaited before response processing and streaming begin. A thrown error or rejected promise aborts the operation and is handled by Fastify's error pipeline. When `allowedPath` is configured, final-path authorization runs first; denied files do not invoke `onFile`. diff --git a/index.js b/index.js index 08bc031..20dbf5b 100644 --- a/index.js +++ b/index.js @@ -56,7 +56,7 @@ async function fastifyStatic (fastify, opts) { throw invalidDirListOpts } - opts.dotfiles ??= 'allow' + opts.dotfiles ??= 'ignore' const sendOptions = { root: opts.root, diff --git a/package.json b/package.json index ee367f8..530c89d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fastify/static", - "version": "10.1.7", + "version": "11.0.0", "description": "Plugin for serving static files as fast as possible.", "main": "index.js", "type": "commonjs", diff --git a/test/dir-list.test.js b/test/dir-list.test.js index 4d125e8..d52cc37 100644 --- a/test/dir-list.test.js +++ b/test/dir-list.test.js @@ -181,6 +181,7 @@ test('dir list, custom options', async t => { root: path.join(__dirname, '/static'), prefix: '/public', index: false, + dotfiles: 'allow', list: true } @@ -206,6 +207,7 @@ test('dir list, custom options with empty array index', async t => { root: path.join(__dirname, '/static'), prefix: '/public', index: [], + dotfiles: 'allow', list: true } @@ -277,6 +279,7 @@ test('dir list html format', async t => { root: path.join(__dirname, '/static'), prefix: '/public', index: false, + dotfiles: 'allow', list: { format: 'html', names: ['index', 'index.htm'], @@ -568,6 +571,7 @@ test('json format with url parameter format', async t => { root: path.join(__dirname, '/static'), prefix: '/public', index: false, + dotfiles: 'allow', list: { format: 'json', render () { @@ -606,6 +610,7 @@ test('json format with url parameter format and without render option', async t root: path.join(__dirname, '/static'), prefix: '/public', index: false, + dotfiles: 'allow', list: { format: 'json' } @@ -640,6 +645,7 @@ test('html format with url parameter format', async t => { root: path.join(__dirname, '/static'), prefix: '/public', index: false, + dotfiles: 'allow', list: { format: 'html', render () { diff --git a/test/static.test.js b/test/static.test.js index 4c14169..40753e9 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -3269,6 +3269,25 @@ test('if dotfiles are properly served according to plugin options', async (t) => }) }) +test('dotfiles are ignored by default', async (t) => { + t.plan(2) + const fastify = Fastify() + + fastify.register(fastifyStatic, { + root: path.join(__dirname, 'static'), + prefix: '/static/' + }) + + t.after(() => fastify.close()) + + await fastify.listen({ port: 0 }) + fastify.server.unref() + + const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/.example') + t.assert.ok(!response.ok) + t.assert.deepStrictEqual(response.status, 404) +}) + test('register with failing glob handler', async (t) => { const fastifyStatic = proxyquire.noCallThru()('../', { glob: function globStub (_pattern, _options, cb) { @@ -4393,6 +4412,7 @@ test('should serve files into hidden dir with wildcard `false`', async (t) => { const pluginOptions = { root: path.join(__dirname, '/static-hidden'), wildcard: false, + dotfiles: 'allow', serveDotFiles: true } const fastify = Fastify() @@ -4437,7 +4457,8 @@ test('should serve files into hidden dir without wildcard option', async (t) => t.plan(8) const pluginOptions = { - root: path.join(__dirname, '/static-hidden') + root: path.join(__dirname, '/static-hidden'), + dotfiles: 'allow' } const fastify = Fastify() fastify.register(fastifyStatic, pluginOptions)