Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async function fastifyStatic (fastify, opts) {
throw invalidDirListOpts
}

opts.dotfiles ??= 'allow'
opts.dotfiles ??= 'ignore'

const sendOptions = {
root: opts.root,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 6 additions & 0 deletions test/dir-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ test('dir list, custom options', async t => {
root: path.join(__dirname, '/static'),
prefix: '/public',
index: false,
dotfiles: 'allow',
list: true
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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'
}
Expand Down Expand Up @@ -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 () {
Expand Down
23 changes: 22 additions & 1 deletion test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
Loading