Skip to content

Commit

Permalink
feat: add request object to allowedPath option (#321)
Browse files Browse the repository at this point in the history
* feat: added request to allowedPath option

* added allowedPath tests

* fix: test type

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
  • Loading branch information
gurumaxi and Eomm committed Jul 30, 2022
1 parent 5cb943e commit 663b24d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ with `ignoreTrailingSlash` set to `true`.

#### `allowedPath`

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

This function allows filtering the served files.
This function allows filtering the served files. Also, with the help of the request object a more complex path authentication is possible.
If the function returns `true`, the file will be served.
If the function returns `false`, Fastify's 404 handler will be called.

Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Leo <https://github.com/leomelzer>
/// <reference types="node" />

import { FastifyPluginCallback, FastifyReply } from 'fastify';
import { FastifyPluginCallback, FastifyReply, FastifyRequest } from 'fastify';
import { Stats } from 'fs';

declare module "fastify" {
Expand Down Expand Up @@ -86,7 +86,7 @@ declare namespace fastifyStatic {
redirect?: boolean;
wildcard?: boolean;
list?: boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat;
allowedPath?: (pathName: string, root?: string) => boolean;
allowedPath?: (pathName: string, root: string, request: FastifyRequest) => boolean;
/**
* @description
* Opt-in to looking for pre-compressed files
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async function fastifyStatic (fastify, opts) {
}
}

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

Expand Down
43 changes: 42 additions & 1 deletion test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ t.test('sendFile disabled', (t) => {
})
})

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

const pluginOptions = {
Expand Down Expand Up @@ -1014,6 +1014,47 @@ t.test('allowedPath option', (t) => {
})
})

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

const pluginOptions = {
root: path.join(__dirname, '/static'),
allowedPath: (pathName, root, request) => request.query.key === 'temporaryKey'
}
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)
fastify.listen({ port: 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.equal(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?key=temporaryKey',
followRedirect: false
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
})
})
})
})

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

Expand Down
7 changes: 5 additions & 2 deletions test/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fastify, { FastifyInstance, FastifyPluginCallback } from 'fastify'
import fastify, { FastifyInstance, FastifyPluginCallback, FastifyRequest } from 'fastify'
import { Server } from 'http';
import { expectAssignable, expectError, expectType } from 'tsd'
import * as fastifyStaticStar from '../..';
Expand Down Expand Up @@ -52,7 +52,10 @@ const options: FastifyStaticOptions = {
setHeaders: (res: any, pathName: any) => {
res.setHeader('test', pathName)
},
preCompressed: false
preCompressed: false,
allowedPath: (pathName: string, root: string, request: FastifyRequest) => {
return true;
}
}

expectError<FastifyStaticOptions>({
Expand Down

0 comments on commit 663b24d

Please sign in to comment.