Skip to content

Commit

Permalink
fix: return 414 status code if URI is too long
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Jun 26, 2023
1 parent 6e06229 commit d5b1bec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,11 @@ function fastify (options) {
errorStatus = http.STATUS_CODES[errorCode]
body = `{"error":"${errorStatus}","message":"Client Timeout","statusCode":408}`
errorLabel = 'timeout'
} else if (err.code === 'HPE_HEADER_OVERFLOW') {
errorCode = '414'
errorStatus = http.STATUS_CODES[errorCode]
body = `{"error":"${errorStatus}","message":"Client URI too long","statusCode":414}`
errorLabel = 'uri_too_long'
} else {
errorCode = '400'
errorStatus = http.STATUS_CODES[errorCode]
Expand Down
32 changes: 32 additions & 0 deletions test/uri-too-long.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const t = require('tap')
const test = t.test
const maxHeaderSize = 1024
const fastify = require('..')({ http: { maxHeaderSize } })
const sget = require('simple-get').concat

test('Should return 414 if URI is too long', t => {
t.plan(2)

fastify.route({
method: 'GET',
url: '/',
handler: (_req, reply) => {
reply.send({ hello: 'world' })
}
})

fastify.listen({ port: 0 }, function (err) {
t.error(err)

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + `/${'a'.repeat(maxHeaderSize)}`
}, (_err, res) => {
t.equal(res.statusCode, 414)
})
})

t.teardown(() => fastify.close())
})

0 comments on commit d5b1bec

Please sign in to comment.