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

When http header size limit is reached, instead of a generic 400 response, return 431 Request Header Fields Too Large #25528

Closed
albertstill opened this issue Jan 16, 2019 · 8 comments
Labels
feature request Issues that request new features to be added to Node.js. http Issues or PRs related to the http subsystem.

Comments

@albertstill
Copy link
Contributor

We overlooked a Node security release in November 2018 that downsized the max http header size limit to 8192 bytes.

After a Node bump we occasionally saw 400s in production due to large cookies but the response contained no body or interesting headers, all we got is a generic 400 http error code. Is there a good reason for this?

We solved the problem eventually but it would of been nice if the HTTP response body gave text explanation of the error. For example Max HTTP header size of ${maxHttpHeaderSize} reached.

Thanks.

@bnoordhuis bnoordhuis added http Issues or PRs related to the http subsystem. feature request Issues that request new features to be added to Node.js. labels Jan 17, 2019
@bnoordhuis
Copy link
Member

For example Max HTTP header size of ${maxHttpHeaderSize} reached.

Including the actual size limit might leak mildly sensitive information. I say "mildly" because you can binary-search it with multiple requests but why make life easier for an attacker?

@albertstill
Copy link
Contributor Author

Since I posted this issue, I discovered there is a HTTP error code for this exact case, 431 Request Header Fields Too Large. This would be the perfect solution and mean we can leave out the response body suggestion.

This would also be really valuable in CDN/logging reporting tools as we can pinpoint exactly how many clients are getting bloated cookies and triggering 431's.

@albertstill albertstill changed the title When http header size limit is reached, add human text explanation in 400 response body When http header size limit is reached, instead of a generic 400 response, return 431 Request Header Fields Too Large Jan 18, 2019
@albertstill
Copy link
Contributor Author

Furthermore 431 exists in Node's http.STATUS_CODES already, i.e node -p "require('http').STATUS_CODES[431]" prints Request Header Fields Too Large

@albertstill
Copy link
Contributor Author

Also seems the default 400 behaviour is documented in the Event: 'clientError' documentation.

Default behavior is to close the socket with an HTTP '400 Bad Request' response if possible, otherwise the socket is immediately destroyed.

The following code would cause a server to behave how I wanted, using the HPE_HEADER_OVERFLOW error code.

server.on('clientError', (err, socket) => {
  if (err.code === 'HPE_HEADER_OVERFLOW') {
    socket.end('HTTP/1.1 431 Request Header Fields Too Large\r\n\r\n');
    return;
  }

  socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});

Is it intentional the consumer has to handle this mapping themselves? Seems a sensible default but I understand if this is a design principle of Node.

@sam-github
Copy link
Contributor

431 sounds pretty reasonable to me, @nodejs/http @mcollina, would you agree?

@mcollina
Copy link
Member

I’m OK in using a 431, it seems a good idea! I think we might not want to change the default, and change this in a semver-major PR. Would you like to open one?

@albertstill
Copy link
Contributor Author

I'd love to give the PR a crack, I'm reading the contributing guide now and should have a PR within 24 hours, I've located the code responsible for the 400.

@albertstill
Copy link
Contributor Author

Here is the PR @sam-github @mcollina #25605

albertstill added a commit to albertstill/node that referenced this issue Feb 1, 2019
Instead of returning a generic 400 response when the
max header size is reached, return a 431 Request Header
Fields Too Large.

This is a semver-major because it changes the HTTP
status code for requests that trigger the header
overflow error.

PR-URL: nodejs#25605
Fixes: nodejs#25528
Refs: https://tools.ietf.org/html/rfc6585#section-5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. http Issues or PRs related to the http subsystem.
Projects
None yet
4 participants