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: return 431 status code on HTTP header overflow error #4856

Merged
merged 1 commit into from
Jun 29, 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
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 = '431'
errorStatus = http.STATUS_CODES[errorCode]
body = `{"error":"${errorStatus}","message":"Exceeded maximum allowed HTTP header size","statusCode":431}`
errorLabel = 'header_overflow'
} else {
errorCode = '400'
errorStatus = http.STATUS_CODES[errorCode]
Expand Down
65 changes: 65 additions & 0 deletions test/header-overflow.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict'

const t = require('tap')
const test = t.test
const Fastify = require('..')
const sget = require('simple-get').concat

const maxHeaderSize = 1024

test('Should return 431 if request header fields are too large', t => {
t.plan(3)

const fastify = Fastify({ http: { maxHeaderSize } })
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,
headers: {
'Large-Header': 'a'.repeat(maxHeaderSize)
}
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 431)
})
})

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

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

const fastify = Fastify({ http: { maxHeaderSize } })
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.error(err)
t.equal(res.statusCode, 431)
})
})

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