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

http: improve errors thrown in header validation #16715

Closed
wants to merge 1 commit into from

Conversation

joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Nov 3, 2017

Fixes: #16714

This PR:

  • Replace ERR_HTTP2_INVALID_HEADER_VALUE with ERR_HTTP_INVALID_HEADER_VALUE, don't really see a need to differentiate the two.
    • The error code documentation of ERR_HTTP2_INVALID_HEADER_VALUE is kept.
    • Include the header name and the value for debug-ability as [REQUEST] Better Errors in http validate headers #16714 suggests. Before it's Value must not be undefined or null(HTTP2) or The "value" argument must be specified(HTTP), after it's Invalid value "undefined" for header "test". If the user is setting the header in a loop this would be much more useful.
  • Use Error.captureStackTrace to exclude the validation functions from the stack trace
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

http, http2

@nodejs-github-bot nodejs-github-bot added the lib / src Issues and PRs related to general changes in the lib or src directory. label Nov 3, 2017
@joyeecheung
Copy link
Member Author

@joyeecheung joyeecheung added errors Issues and PRs related to JavaScript errors originated in Node.js core. http Issues or PRs related to the http subsystem. http2 Issues or PRs related to the http2 subsystem. semver-major PRs that contain breaking changes and should be released in the next major version. labels Nov 3, 2017
@joyeecheung
Copy link
Member Author

Would be nice to have common.expectsError checking excluded stack frames and printing the original message when the error classes are called without new. Will open a seperate PR for that.

Copy link
Member

@apapirovski apapirovski left a comment

Choose a reason for hiding this comment

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

LGTM with some nits 👍

### ERR_HTTP2_INVALID_HEADER_VALUE
### ERR_HTTP_INVALID_HEADER_VALUE
<a id="ERR_HTTP_INVALID_HEADER_VALUE"></a>
### ERR_HTTP_INVALID_HEADER_VALUE
Copy link
Member

Choose a reason for hiding this comment

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

Accidentally doubled here?

throw new errors.Error('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED');
if (value === undefined || value === null)
throw new errors.TypeError('ERR_HTTP2_INVALID_HEADER_VALUE');
var err;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: can we use let? (I don't think there's many/any vars in http2 except for for loops.)

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 am still under the impression that we favor var in cases like this...might be just outdated impressions, I'll fix that.

@@ -26,17 +26,17 @@ server.listen(0, common.mustCall(() => {
common.expectsError(
() => response.setTrailer('test', undefined),
{
code: 'ERR_HTTP2_INVALID_HEADER_VALUE',
Copy link
Member

Choose a reason for hiding this comment

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

I'm not 100% sure about this because the semantics of the http2 validation are slightly different, for example pseudo headers are an http2 only feature.

throw new errors.TypeError('ERR_INVALID_CHAR', 'header content', name);
err = new errors.TypeError('ERR_INVALID_CHAR', 'header content', name);
}
if (err) {
Copy link
Member

Choose a reason for hiding this comment

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

Since this is an object, can we do a strict comparison to undefined? (This function is kind of in a hot path.)

} else if (value === undefined || value === null) {
err = new errors.TypeError('ERR_HTTP_INVALID_HEADER_VALUE', value, name);
}
if (err) {
Copy link
Member

Choose a reason for hiding this comment

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

Same as for http, could we compare to undefined to avoid the whole document.all perf issue? (This is in a hot path.)

Copy link
Contributor

@maclover7 maclover7 left a comment

Choose a reason for hiding this comment

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

+1 to landing as semver-major

@@ -818,9 +818,11 @@ HTTP/1 connection specific headers are forbidden to be used in HTTP/2
requests and responses.

<a id="ERR_HTTP2_INVALID_HEADER_VALUE"></a>
### ERR_HTTP2_INVALID_HEADER_VALUE
### ERR_HTTP_INVALID_HEADER_VALUE
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is duplicated

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually it's accidentally changed..we need to keep the documentation for ERR_HTTP2_INVALID_HEADER_VALUE because it's out there already.

Copy link
Contributor

Choose a reason for hiding this comment

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

If the codebase doesn't raise an error with this code though, should it be removed? 😬


Used to indicate that an invalid HTTP/2 header value has been specified.
Used to indicate that an invalid HTTP header value has been specified.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we explicitly say HTTP/1.1 and HTTP/2?

@apapirovski
Copy link
Member

apapirovski commented Nov 3, 2017

Is this semver-major? It doesn't modify error codes except for http2 which is experimental. (And I'm not even 100% confident the error code for h2 should change, see above.)

@joyeecheung
Copy link
Member Author

@apapirovski Hmmm, actually I don't know what's our policy for "changing error code but it's an error in the experimental module"..

@joyeecheung
Copy link
Member Author

@apapirovski Ah, wait, it also modifies the HTTP error codes (ERR_MISSING_ARGS -> ERR_HTTP_INVALID_HEADER_VALUE)

@apapirovski
Copy link
Member

apapirovski commented Nov 3, 2017

@joyeecheung They already changed roughly a million times during v8.x... that ship might've sailed. 😆

Ah, wait, it also modifies the HTTP error codes (ERR_MISSING_ARGS -> ERR_HTTP_INVALID_HEADER_VALUE)

Ah true. Wonder if we could split into two separate PRs/commits so most of this could land right away?

@joyeecheung
Copy link
Member Author

@apapirovski

Ah true. Wonder if we could split into two separate PRs/commits so most of this could land right away?

Yeah that seems like a better idea. I will close this one and open another two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
errors Issues and PRs related to JavaScript errors originated in Node.js core. http Issues or PRs related to the http subsystem. http2 Issues or PRs related to the http2 subsystem. lib / src Issues and PRs related to general changes in the lib or src directory. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[REQUEST] Better Errors in http validate headers
4 participants