Skip to content

Commit

Permalink
Add option to filter files (#172)
Browse files Browse the repository at this point in the history
* Add option to filter files

* Update static.test.js

Co-authored-by: Matteo Collina <[email protected]>
Co-authored-by: Matteo Collina <[email protected]>
  • Loading branch information
3 people authored Feb 18, 2021
1 parent ea6422c commit 9a2e352
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ for getting the file list.
This option cannot be set to `false` with `redirect` set to `true` on a server
with `ignoreTrailingSlash` set to `true`.

#### `allowedPath`

Default: `(pathname, root) => true`

This function allows filtering the served files.
If the function returns `true`, the file will be served.
If the function returns `false`, Fastify's 404 handler will be called.

#### `list`

Default: `undefined`
Expand Down
12 changes: 12 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ export interface FastifyStaticOptions extends SendOptions {
redirect?: boolean;
wildcard?: boolean | string;
list?: boolean | ListOptions;
allowedPath?: (pathName: string, root?: string) => boolean;

// Passed on to `send`
acceptRanges?: boolean;
cacheControl?: boolean;
dotfiles?: 'allow' | 'deny' | 'ignore';
etag?: boolean;
extensions?: string[];
immutable?: boolean;
index?: string[];
lastModified?: boolean;
maxAge?: string | number;
}

declare const fastifyStatic: FastifyPlugin<FastifyStaticOptions>
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ async function fastifyStatic (fastify, opts) {
maxAge: opts.maxAge
}

const allowedPath = opts.allowedPath

function pumpSendToReply (request, reply, pathname, rootPath, rootPathOffset = 0, pumpOptions = {}) {
const options = Object.assign({}, sendOptions, pumpOptions)

Expand All @@ -51,6 +53,10 @@ async function fastifyStatic (fastify, opts) {
}
}

if (allowedPath && !allowedPath(pathname, options.root)) {
return reply.callNotFound()
}

const stream = send(request.raw, pathname, options)
let resolvedFilename
stream.on('file', function (file) {
Expand Down
41 changes: 41 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,47 @@ t.test('sendFile disabled', t => {
})
})

t.test('allowedPath option', t => {
t.plan(3)

const pluginOptions = {
root: path.join(__dirname, '/static'),
allowedPath: (pathName) => pathName !== '/foobar.html'
}
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)
fastify.listen(0, err => {
t.error(err)

fastify.server.unref()

t.test('/foobar.html not found', t => {
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/foobar.html',
followRedirect: false
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 404)
genericErrorResponseChecks(t, response)
})
})

t.test('/index.css found', t => {
t.plan(2)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/index.css',
followRedirect: false
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
})
})
})
})

t.test('download', t => {
t.plan(7)

Expand Down

0 comments on commit 9a2e352

Please sign in to comment.