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 undefined req.routeConfig in frameworkErrors #4826

Merged
merged 9 commits into from
Jun 20, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Object.defineProperties(Request.prototype, {
},
routerPath: {
get () {
return this[kRouteContext].config.url
return this[kRouteContext].config?.url
}
},
routeOptions: {
Expand All @@ -182,8 +182,8 @@ Object.defineProperties(Request.prototype, {
const serverLimit = context.server.initialConfig.bodyLimit
const version = context.server.hasConstraintStrategy('version') ? this.raw.headers['accept-version'] : undefined
const options = {
method: context.config.method,
url: context.config.url,
method: context.config?.method,
url: context.config?.url,
bodyLimit: (routeLimit || serverLimit),
attachValidation: context.attachValidation,
logLevel: context.logLevel,
Expand All @@ -196,12 +196,12 @@ Object.defineProperties(Request.prototype, {
},
routerMethod: {
get () {
return this[kRouteContext].config.method
return this[kRouteContext].config?.method
}
},
routeConfig: {
get () {
return this[kRouteContext][kPublicRouteContext].config
return this[kRouteContext][kPublicRouteContext]?.config
}
},
routeSchema: {
Expand All @@ -211,7 +211,7 @@ Object.defineProperties(Request.prototype, {
},
is404: {
get () {
return this[kRouteContext].config.url === undefined
return this[kRouteContext].config?.url === undefined
}
},
connection: {
Expand Down
30 changes: 30 additions & 0 deletions test/inject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Stream = require('stream')
const util = require('util')
const Fastify = require('..')
const FormData = require('form-data')
const { Readable } = require('stream')

test('inject should exist', t => {
t.plan(2)
Expand Down Expand Up @@ -455,3 +456,32 @@ test('should handle errors in builder-style injection correctly', async (t) => {
t.equal(err.message, 'Kaboom')
}
})

test('Should not throw on access to routeConfig frameworkErrors handler - FST_ERR_BAD_URL', t => {
t.plan(6)

const fastify = Fastify({
frameworkErrors: function (err, req, res) {
t.ok(typeof req.id === 'string')
t.ok(req.raw instanceof Readable)
t.same(req.routerPath, undefined)
t.same(req.routeConfig, undefined)
res.send(`${err.message} - ${err.code}`)
}
})

fastify.get('/test/:id', (req, res) => {
res.send('{ hello: \'world\' }')
})

fastify.inject(
{
method: 'GET',
url: '/test/%world'
},
(err, res) => {
t.error(err)
t.equal(res.body, '\'/test/%world\' is not a valid url component - FST_ERR_BAD_URL')
}
)
})
4 changes: 2 additions & 2 deletions test/internals/reply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ test('within an instance', t => {
})
})

test('auto status code shoud be 200', t => {
test('auto status code should be 200', t => {
t.plan(3)
sget({
method: 'GET',
Expand All @@ -313,7 +313,7 @@ test('within an instance', t => {
})
})

test('auto type shoud be text/plain', t => {
test('auto type should be text/plain', t => {
t.plan(3)
sget({
method: 'GET',
Expand Down
59 changes: 59 additions & 0 deletions test/internals/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,65 @@ test('Regular request', t => {
t.end()
})

test('Request with undefined config', t => {
const headers = {
host: 'hostname'
}
const req = {
method: 'GET',
url: '/',
socket: { remoteAddress: 'ip' },
headers
}
const context = new Context({
schema: {
body: {
type: 'object',
required: ['hello'],
properties: {
hello: { type: 'string' }
}
}
},
server: {
[kReply]: {},
[kRequest]: Request
}
})
req.connection = req.socket
const request = new Request('id', 'params', req, 'query', 'log', context)
t.type(request, Request)
t.type(request.validateInput, Function)
t.type(request.getValidationFunction, Function)
t.type(request.compileValidationSchema, Function)
t.equal(request.id, 'id')
t.equal(request.params, 'params')
t.equal(request.raw, req)
t.equal(request.query, 'query')
t.equal(request.headers, headers)
t.equal(request.log, 'log')
t.equal(request.ip, 'ip')
t.equal(request.ips, undefined)
t.equal(request.hostname, 'hostname')
t.equal(request.body, undefined)
t.equal(request.method, 'GET')
t.equal(request.url, '/')
t.equal(request.originalUrl, '/')
t.equal(request.socket, req.socket)
t.equal(request.protocol, 'http')
t.equal(request.routeSchema, context[kPublicRouteContext].schema)
t.equal(request.routerPath, undefined)
t.equal(request.routerMethod, undefined)
t.equal(request.routeConfig, undefined)

// Aim to not bad property keys (including Symbols)
t.notOk('undefined' in request)

// This will be removed, it's deprecated
t.equal(request.connection, req.connection)
t.end()
})

test('Regular request - hostname from authority', t => {
t.plan(2)
const headers = {
Expand Down