diff --git a/README.md b/README.md index 563153b..f04d155 100644 --- a/README.md +++ b/README.md @@ -219,21 +219,21 @@ The following options are also supported and will be passed directly to the #### `dotfiles` -Default: `'allow'` +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. **This is the default.** +- `'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`. +- `'ignore'` - Treat the dotfile as if it does not exist and return a `404`. **This is the default.** > [!NOTE] -> The default here (`'allow'`) deliberately differs from the `@fastify/send` -> and `serve-static` defaults (`'ignore'`). Dotfiles are served unless you -> opt out. If your static root may contain sensitive files such as `.env`, -> `.htpasswd` or `.git/config`, set `dotfiles: 'ignore'` (or `'deny'`) -> explicitly. This default also means `/.well-known/...` is served without -> extra configuration. +> 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. diff --git a/index.js b/index.js index 62a90ec..45a91ac 100644 --- a/index.js +++ b/index.js @@ -57,7 +57,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 6cf5b6f..917d9de 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fastify/static", - "version": "10.1.12", + "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 49f26a3..efd8267 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 ef241e8..3fd700a 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -3310,6 +3310,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) { @@ -4434,6 +4453,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() @@ -4478,7 +4498,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)