Skip to content

Whitespace is allowed within Header names, should be an Invalid header #1362

Closed

Description

@paultiplady

Raising here after reporting in requests: psf/requests#4592 (comment)

When receiving a malformed Set-Cookie header containing a CRLF, the \r\n character is incorrectly included in the header value, causing requests to include it in the generated Cookie header.

Expected Result

Per RFC https://tools.ietf.org/html/rfc7230#section-3.2.4, "A user agent that receives an obs-fold in a response message that is not within a message/http container MUST replace each received obs-fold with one or more SP octets prior to interpreting the field value."

Or, just raise an error on the invalid HTTP header.

Actual Result

An invalid Set-Cookie value was returned containing a CRLF character.

Reproduction Steps

Requests maintainers came up with a quick repro using that library in psf/requests#4592 (comment).

Here's the bogus cookie (extracted from the raw HTTP text):

Set-Cookie: ___utmvbtouVBFmB=gZg
    XbNOjalT: Lte; path=/; Max-Age=900

Which gives:

Cookie: ___utmvbtouVBFmB=gZg\r\n    XbNOjalT: Lte; <other-cookies>

Activity

  1. njsmith commented on Apr 18, 2018

    @njsmith
    Contributor

    I'm surprised this wasn't fixed by #1318. Or was it, but requests is using an older version?

  2. nateprewitt commented on Apr 18, 2018

    @nateprewitt
    Member

    That's a good callout @njsmith, it looks like this may be fixed, but urllib3 hasn't cut a release in almost a year. I don't have time to move the original repro to urllib3 tonight but can take a look tomorrow.

  3. sethmlarson commented on Apr 18, 2018

    @sethmlarson
    Member

    Thanks all for the investigation! If someone can confirm this is already fixed I'll close this issue as duplicate. 🎉

  4. nateprewitt commented on Apr 18, 2018

    @nateprewitt
    Member

    Went ahead and tested. I can't get this to work in Requests with the current urllib3 master, but that wasn't making sense. I made a urllib3-only repro and it appears to be fixed for the newline case. I'll dig into it on the Requests end and see if we're botching something.

    urllib3 doesn't however handle the other case I posed in the first test of whitespace between a header name and the colon.

    Same section of the RFC:

    No whitespace is allowed between the header field-name and colon. In
    the past, differences in the handling of such whitespace have led to
    security vulnerabilities in request routing and response handling.

    So that should probably still be handled, but I'm not sure if you want to track that here or not.

  5. sethmlarson commented on Apr 18, 2018

    @sethmlarson
    Member

    We can track that here, thanks for the repro!

  6. changed the title [-]Malformed `Set-Cookie` header containing CRLF is ignored[/-] [+]Whitespace is allowed within Header names, should be an Invalid header[/+] on Apr 18, 2018
  7. njsmith commented on Apr 18, 2018

    @njsmith
    Contributor

    For the record since it came up in gitter: h11 does remove the \r\n when folding the lines in the original case, and does reject headers with stray whitespace before the colon. (It also refuses to send outgoing headers with \r or \n in them, but unfortunately does not currently validate the location field to prevent sending paths containing \r or \n. That has a bug here: python-hyper/h11#50)

  8. nateprewitt commented on Apr 18, 2018

    @nateprewitt
    Member

    @njsmith, just to clarify, does h11 only reject sending headers with white space between the name and colon or receiving as well? I’m not sure if we want to hard-fail on receipt but rather handle it by stripping it as per the proxy specification.

    Making content unreachable from the library is likely going to create a negative user experience given how many bad servers are out there.

  9. njsmith commented on Apr 18, 2018

    @njsmith
    Contributor

    h11 errors out if it gets whitespace before the colon. If this ends up causing interop problems we can revisit it (and we should expect that we will have to handle some interop problems during the h11 rollout and budget for it, because no one actually knows how http works). But I would strongly prefer to wait until we see this in the wild, because as the spec notes, being too accepting here has led to security bugs. (For example: some kind of firewall is checking for EvilHeader:, but it doesn't check for EvilHeader :, so it allows the latter through. Then urllib3 silently converts that back to EvilHeader:. This is more of an issue for servers than clients, but I'd rather be conservative if we can.)

  10. added this to the milestone on Jun 9, 2018
  11. 10 remaining items

  12. utadashang commented on May 17, 2026

    @utadashang

    Hi maintainers, I reproduced this issue on the current main branch and confirmed that obs-folded response header values can still be exposed through HTTPHeaderDict.

    I noticed there are already linked PRs working on this, including #5017 and #5020, so I do not want to duplicate ongoing work. I’m happy to help test/review the current approach or prepare a smaller alternative patch if maintainers prefer a different direction.

    Thanks!

  13. JoeyXi commented on May 18, 2026

    @JoeyXi

    I have withdrawn #5013 to avoid duplicating or confusing the current work on this issue. #5017 appears to be the active PR linked to close the obs-fold response-header value scope, while #5013 covered a different whitespace-before-colon response-header-name path. Thanks, and I will defer to the ongoing maintainer review there.

  14. aigrouphobinh commented on May 19, 2026

    @aigrouphobinh

    Hi maintainers — I opened a PR that fixes this issue by rejecting invalid folded headers / CRLF in header values and adds regression tests.

    PR: #5034
    Fixes #1362
    (Bounty $100 / OpenCollective)

  15. liangtovi-debug commented on May 19, 2026

    @liangtovi-debug

    Submitted fix in PR #5035 to sanitize folded response headers and add a regression test for folded Set-Cookie values.

  16. liangtovi-debug commented on May 19, 2026

    @liangtovi-debug

    /claim #1362
    Implemented in PR #5035: #5035

    Includes response-header obs-fold sanitization and regression coverage for folded Set-Cookie values.

    Please associate this claim with the OpenCollective bounty if accepted.

  17. minnhnh commented on May 27, 2026

    @minnhnh

    Hi! I opened PR #5063 to address this (reject obs-fold / CRLF in response header values and add a regression test). Happy to adjust if you'd like different behavior/exception type. If this resolves the issue, please let me know how you'd like to handle the OpenCollective bounty.

  18. zhaog100 commented on May 28, 2026

    @zhaog100

    Claiming this bounty. Will implement the fix for whitespace in header names.

    GitHub: zhaog100
    Identity: 小米辣 🌶️

  19. sethmlarson commented on May 29, 2026

    @sethmlarson
    Member

    The pull request from @adityawrk seems the highest quality: #5020 I've closed all others.

  20. darlina-bounty-codex commented on May 29, 2026

    @darlina-bounty-codex

    I opened a PR for this issue here:

    #5068

    It adds regression coverage for invalid outbound header names with whitespace and folded Set-Cookie response headers, plus the corresponding validation/normalization changes.

  21. adityawrk commented on Jun 18, 2026

    @adityawrk
    Contributor

    The pull request from @adityawrk seems the highest quality: #5020 I've closed all others.

    Thanks @sethmlarson. Please me know if you need anything else on the PR.

  22. linked a pull request that will close this issueUnfold obsolete folded response header values #5020on Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions