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 a bug for header field getting malformed when it extends over multiple lines #111

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

okomestudio
Copy link

The message headers section of RFC 2616 (Hypertext Transfer Protocol -- HTTP/1.1) states that

Header fields can be extended over multiple lines by preceding each extra line with at least one SP or HT.

The current implementation of s3lib.http.headers_raw_to_dict function unfortunately tends to read only the first line of such multi-line header field (and discard the rest in the following loop making tuples).

This PR refactors the function to be able to properly read multi-line header field. To do this reliably, it also more strictly interprets CRLF (i.e., \r\n) as the end-of-line marker, as per RFC 2616.

@okomestudio
Copy link
Author

I think the CI failed because the version of tox used no longer support Python 3.3. Making the tests pass requires downgrade to tox<=2.9.1, but I do not have control over it.

@Gallaecio Gallaecio closed this Aug 8, 2019
@Gallaecio Gallaecio reopened this Aug 8, 2019
@codecov
Copy link

codecov bot commented Aug 8, 2019

Codecov Report

Merging #111 into master will decrease coverage by 0.32%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master     #111      +/-   ##
==========================================
- Coverage   95.43%   95.11%   -0.33%     
==========================================
  Files           7        7              
  Lines         482      491       +9     
  Branches       98      101       +3     
==========================================
+ Hits          460      467       +7     
- Misses         15       17       +2     
  Partials        7        7              
Impacted Files Coverage Δ
w3lib/util.py 69.69% <0.00%> (ø) ⬆️
w3lib/form.py 96.42% <0.00%> (ø) ⬆️
w3lib/url.py 98.04% <0.00%> (ø) ⬆️
w3lib/http.py 95.12% <0.00%> (-4.88%) ⬇️
w3lib/html.py 93.80% <0.00%> (ø) ⬆️
w3lib/encoding.py 100.00% <0.00%> (ø) ⬆️

w3lib/http.py Outdated
headers = headers_raw.splitlines()

headers = []
for line in headers_raw.split(b'\r\n'):
Copy link
Member

Choose a reason for hiding this comment

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

Why limit line boundaries to \r\n?

Copy link
Author

Choose a reason for hiding this comment

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

The reason is that the specification (RFC 2616 cited above or RFC7230 which appears more recent) states that in HTTP messages, lines are folded specifically by CRLF (\r\n) and not by LF (\n). Not following this had lead to an issue where a header field value containing a LF (\n) was accidentally interpreted as a multiline field value. Without leading at least one space or tab char in the following line, however, the header ended up being interpreted as malformed and the lines were simply discarded when parsed out.

This is rather pathological and use of LF within a field value feels like a bad idea, but it doesn't appear to violate the specification so some HTTP messages with that structure exist.

Copy link
Member

@Gallaecio Gallaecio Aug 12, 2019

Choose a reason for hiding this comment

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

I would leave .splitlines() nonetheless, though, given the current information we have, to be lenient with servers that may not follow the standard.

If later it turns out by allowing certain forms of line break we are misinterpreting header values, then we can change this accordingly.

Copy link
Author

Choose a reason for hiding this comment

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

I propose that the function headers_raw_to_dict take in an optional parameter strict with the default value that does not change the existing behavior. This way, the code works as is, but would be ready for those who want a stricter interpretation of the HTTP standard via a simple switch.

I think exposing this switch to the users requires a similar change at the user-facing API, but that should be done in a separate PR, if desired.

@Gallaecio
Copy link
Member

Will you consider keeping splitlines?

@okomestudio
Copy link
Author

Before fixing, let me do a couple tests to see if reverting to splitlines do not introduce any other issues.

One thing though is that by not sticking to the standard, the code would be lenient on using \n as the line delimiter but it will not parse standard-compliant messages correctly.

It's okay by me if that's the design compromise for the library (and I myself am not sure how often servers do break the rule, using \n instead of \r\n). But I remember forking the repo to generate this patch due to an issue I had with real response headers being truncated. So there definitely is a trade-off, albeit it might be rare.

(One possible workaround might be to inspect each header to see which of \n and \r\n is used as the line delimiter. I'm not sure that's desired or reliable.)

@okomestudio
Copy link
Author

Sorry for taking a while but I finally managed to update the PR as requested. Please note my comment about the optional switch. I hope that the PR makes sense now.

w3lib/http.py Outdated Show resolved Hide resolved
Co-Authored-By: Adrián Chaves <adrian@chaves.io>
@yozachar
Copy link

Bumping to close outdated PR.

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.

None yet

3 participants