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

fix: handle invalid url #3128

Merged
merged 3 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,7 @@ function fastify (options) {
})

// 404 router, used for handling encapsulated 404 handlers
const fourOhFour = build404({
logger: options.logger,
genReqId: options.genReqId,
onBadUrl: onBadUrl
})
const fourOhFour = build404(options)

// HTTP server and its handler
const httpHandler = wrapRouting(router.routing, options)
Expand Down
20 changes: 19 additions & 1 deletion lib/fourOhFour.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const fourOhFourContext = {
* kFourOhFourContext: the context in the reply object where the handler will be executed
*/
function fourOhFour (options) {
const { logger, genReqId, onBadUrl } = options
const { logger, genReqId } = options

// 404 router, used for handling encapsulated 404 handlers
const router = FindMyWay({ onBadUrl: onBadUrl, defaultRoute: fourOhFourFallBack })
Expand All @@ -58,6 +58,24 @@ function fourOhFour (options) {
})
}

function onBadUrl (path, req, res) {
const { url, method } = req
const message = `Route ${method}:${url} not found`
const body = `{"error":"Not Found","message":"${message}","statusCode":404}`

// simulate normal route logging
const id = genReqId(req)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to have some feedback about the logging. Do we have a better approach for this one?

const childLogger = logger.child({ reqId: id })
childLogger.info({ req }, 'incoming request')
childLogger.info({ req }, message)
climba03003 marked this conversation as resolved.
Show resolved Hide resolved

res.writeHead(404, {
'Content-Type': 'application/json',
'Content-Length': body.length
})
res.end(body)
}

function setContext (instance, context) {
const _404Context = Object.assign({}, instance[kFourOhFourContext])
_404Context.onSend = context.onSend
Expand Down
18 changes: 9 additions & 9 deletions test/404s.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1746,29 +1746,29 @@ test('400 in case of bad url (pre find-my-way v2.2.0 was a 404)', t => {
method: 'GET'
}, (err, response) => {
t.error(err)
t.equal(response.statusCode, 400)
t.equal(response.statusCode, 404)
t.same(JSON.parse(response.payload), {
error: 'Bad Request',
message: "'%c0' is not a valid url component",
statusCode: 400
error: 'Not Found',
message: 'Route GET:/%c0 not found',
statusCode: 404
})
})
})

t.test('Only / is registered', t => {
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
t.plan(3)
const fastify = Fastify()
const fastify = Fastify({ logger: true })
fastify.get('/', () => t.fail('we should not be here'))
fastify.inject({
url: '/%c0',
method: 'GET'
}, (err, response) => {
t.error(err)
t.equal(response.statusCode, 400)
t.equal(response.statusCode, 404)
t.same(JSON.parse(response.payload), {
error: 'Bad Request',
message: "'%c0' is not a valid url component",
statusCode: 400
error: 'Not Found',
message: 'Route GET:/%c0 not found',
statusCode: 404
})
})
})
Expand Down