Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added request to allowedPath option #321

Merged
merged 4 commits into from
Jul 30, 2022
Merged
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
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