Support all whitespace characters in header values - #225
Open
TimvdLippe wants to merge 1 commit into
Open
Conversation
This PR is part of a series of PRs across several crates, to eventually fix web-platform-test failures for Servo. Servo currently fails [`/content-security-policy/generic/only-valid-whitespaces-are-allowed.html`](https://wpt.fyi/results/content-security-policy/generic/only-valid-whitespaces-are-allowed.html?product=servo) which is a test that checks whether various whitespace characters are properly parsed in the `Content-Security-Policy` header. Currently, Servo times out on this test, since it attempts to send a HTTP request with Hyper. Hyper calls `httparse` to parse the response returned by the test server with, which includes the `Content-Security-Policy` with the special whitespace character. When `httparse` attempts to parse this, it fails since it contains characters it currently does not accept. While RFC7230 (HTTP) does not allow such characters to be present, for web browsers [section 3.5](https://datatracker.ietf.org/doc/html/rfc7230#section-3.5) is relevant. That section states that for historical reasons (which is the case for web browsers), both `%x0B` and `%x0C` MAY be treated as regular space characters. Since currently `httparse` rejects parsing, Servo is unable to implement that requirement. To fix this issue, the following changes are made: 1. In `httparse`, support parsing `%x0B` and `%x0C` in header values 2. In `http`, consider `%x0B` and `%x0C` as opaque bytes for byte constructors 3. In `hyper`, use `HeaderValue::from_bytes` rather than the generic `HeaderValue::from_maybe_shared_unchecked` to operate on the raw bytes 4. In `content-security-policy` update policy parsing to check if directive value is an ASCII string. It already handles `%x0B` and `%x0C` in `is_char_ascii_whitespace`, but with the HTTP changes it exposed this other missing check 5. In Servo, replace `header.to_str()` with `str::from_utf8(header.as_bytes())`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR is part of a series of PRs across several crates, to eventually fix web-platform-test failures for Servo.
Servo currently fails
/content-security-policy/generic/only-valid-whitespaces-are-allowed.htmlwhich is a test that checks whether various whitespace characters are properly parsed in theContent-Security-Policyheader.Currently, Servo times out on this test, since it attempts to send a HTTP request with Hyper. Hyper calls
httparseto parse the response returned by the test server with, which includes theContent-Security-Policywith the special whitespace character. Whenhttparseattempts to parse this, it fails since it contains characters it currently does not accept.While RFC7230 (HTTP) does not allow such characters to be present, for web browsers section 3.5 is relevant. That section states that for historical reasons (which is the case for web browsers), both
%x0Band%x0CMAY be treated as regular space characters.Since currently
httparserejects parsing, Servo is unable to implement that requirement. To fix this issue, the following changes are made:httparse, support parsing%x0Band%x0Cin header values (Support all whitespace characters in header values #225)http, consider%x0Band%x0Cas opaque bytes for byte constructors (Support all whitespace characters in header values hyperium/http#862)hyper, useHeaderValue::from_bytesrather than the genericHeaderValue::from_maybe_shared_uncheckedto operate on the raw bytes (Support all whitespace characters in header values hyperium/hyper#4155)content-security-policyupdate policy parsing to check if directive value is an ASCII string. It already handles%x0Band%x0Cinis_char_ascii_whitespace, but with the HTTP changes it exposed this other missing check (Support all whitespace characters in header values rust-ammonia/rust-content-security-policy#74)header.to_str()withstr::from_utf8(header.as_bytes())(net, script: Support all whitespace characters in header values servo/servo#47194)