Skip to content

Update parser of header value to trim linear white space - #739

Merged
FlorentinDUBOIS merged 1 commit into
0.14from
fix/0.14/header-value-and-linear-white-space
Nov 26, 2021
Merged

FlorentinDUBOIS merged 1 commit into
0.14from
fix/0.14/header-value-and-linear-white-space

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator
  • According to the rfc 2616, section 2.2 and 4.2, the linear white space
    (lws) which are the horizontal tabulation ('\t') and the space (' ') MAY
    be trimmed.

    See rfc 2616, section 4.2:

    The field-content does not include any leading or trailing LWS:
    linear white space occurring before the first non-whitespace
    character of the field-value or after the last non-whitespace
    character of the field-value. Such leading or trailing LWS MAY be
    removed without changing the semantics of the field value. Any LWS
    that occurs between field-content MAY be replaced with a single SP
    before interpreting the field value or forwarding the message
    downstream.

Signed-off-by: Florentin Dubois [email protected]

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator Author

Those modifications fix the issue #726

* According to the rfc 2616, section 2.2 and 4.2, the linear white space
  (lws) which are the horizontal tabulation ('\t') and the space (' ') MAY
  be trimmed.

  See rfc 2616, section 4.2:

  > The field-content does not include any leading or trailing LWS:
  > linear white space occurring before the first non-whitespace
  > character of the field-value or after the last non-whitespace
  > character of the field-value. Such leading or trailing LWS MAY be
  > removed without changing the semantics of the field value. Any LWS
  > that occurs between field-content MAY be replaced with a single SP
  > before interpreting the field value or forwarding the message
  > downstream.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/0.14/header-value-and-linear-white-space branch from 4b015a7 to bb940d1 Compare November 26, 2021 09:20
@Keksoj

Keksoj commented Nov 26, 2021

Copy link
Copy Markdown

Looks fair to me. The RFC being quoted in the comments is a very good point.

@FlorentinDUBOIS
FlorentinDUBOIS merged commit 8c0b667 into 0.14 Nov 26, 2021
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/0.14/header-value-and-linear-white-space branch November 26, 2021 14:18
@Geal

Geal commented Dec 1, 2021

Copy link
Copy Markdown
Member

I'm happy to see this fixed 😃

There's a few follow up changes to do though. I'm sorry I couldn't review that sooner 😣

First detail: be careful when citing RFC 2616, it's 20 years old and has since ben replaced by the 7230 series: https://www.mnot.net/blog/2014/06/07/rfc2616_is_dead
A lot has changed in 20 years 😁

pub name: &'a [u8],
pub value: &'a [u8],
pub struct Header {
pub name: Vec<u8>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this will result in a massive performance regression. The parser is designed to avoid allocations as much as possible, while this will introduce two allocations per header, even for headers that are not used by the HTTP implementation.

pub value: Vec<u8>,
}

#[cfg(feature = "tolerant-http1-parser")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the fix for this should probably be available even under the tolerant-http1-parser feature. That feature is only there to accept IS0-8859-1 characters in header values, something that should not exist anymore (but for prod reasons we had to add it)

// > that occurs between field-content MAY be replaced with a single SP
// > before interpreting the field value or forwarding the message
// > downstream.
let value = String::from_utf8_lossy(value)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

there is probably a way to trim without allocating, by iterating from the last character of the slice an only checking for space characters (you will even avoid the utf8 parsing).

It is also the wrong place to trim: Transfer-Encoding can get a comma separated list of values, so if we get the header Transfer-Encoding: chunked\t, gzip\r\n, the bug will still be present because this only trims at the end of the header value

} else if compare_no_case(self.name, b"transfer-encoding") {
if compare_no_case(self.value, b"chunked") {
} else if compare_no_case(&self.name, b"transfer-encoding") {
if compare_no_case(&self.value, b"chunked") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is where the value should be trimmed:
First, by making an iterator over the comma separated values, as is done for the Connection header (I don't know why I did not do that long ago, and why we did not receive any bug report for that over the years...), and second by trimming left and right the values produced before comparing them to chunked and others

BufferMove::Advance(7),
BufferMove::Delete(1),
BufferMove::Delete(18),
BufferMove::Delete(16),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is very likely a bug. With this serie of moves for the headers Cookie:BAR=FOO;SOZUBALANCEID=0; \r\nHost: hello.com, we would send the following slices:

  • Cookie:
  • BAR=FOO
  • SOZUBALANCEID=0; is not transmitted. The two following spaces were trimmed when parsing but are still in the buffer
  • <- the two spaces
  • then the parser thinks that the next header starts at \r\nHost: hello.com

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator Author

Thanks for the review, it seems that I merged this fix a bit fast. I will make changes that you suggest in another pull requests and link this one and the dedicated issue as well.

Thanks again 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants