From 5bd37e24347bfafddd655bcec6232a14fb466f40 Mon Sep 17 00:00:00 2001 From: Jesse Rosalia Date: Fri, 6 Nov 2026 21:14:18 -0800 Subject: [PATCH] Reject CR/LF/NUL in header values before passing them to pycurl PycurlClient built the HTTPHEADER list via raw "%s: %s" string interpolation with no validation, so a header value containing CR or LF could inject an additional header line (e.g. via Stripe-Request- Trigger, built from an unverified webhook event id). Other HTTP client backends in this file are already protected by their own underlying library (stdlib http.client, h11, aiohttp); pycurl has no such check built in, so it needs one here. Co-Authored-By: Claude Sonnet 5 Committed-By-Agent: claude --- ...ar_reject-pycurl-header-injection.change.md | 6 ++++++ stripe/_http_client.py | 18 ++++++++++++++++++ tests/test_http_client.py | 17 +++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 .hark/changes/2026-11-06_jar_reject-pycurl-header-injection.change.md diff --git a/.hark/changes/2026-11-06_jar_reject-pycurl-header-injection.change.md b/.hark/changes/2026-11-06_jar_reject-pycurl-header-injection.change.md new file mode 100644 index 000000000..401ef8606 --- /dev/null +++ b/.hark/changes/2026-11-06_jar_reject-pycurl-header-injection.change.md @@ -0,0 +1,6 @@ +--- +title: "Reject header values containing CR, LF or NUL in the pycurl HTTP client" +semver_level: patch +jira_tickets_closed: +- RUN_DEVSDK-3683 +--- diff --git a/stripe/_http_client.py b/stripe/_http_client.py index 65c3ed684..6f0815e91 100644 --- a/stripe/_http_client.py +++ b/stripe/_http_client.py @@ -827,6 +827,23 @@ def close(self): pass +def _assert_no_header_injection(headers: Mapping[str, str]) -> None: + """Rejects CR, LF and NUL in a header name or value. + + pycurl's HTTPHEADER option takes raw "name: value" strings, assembled + here by string formatting rather than passed to a validating library + call. A CR or LF in either half would let the remainder be parsed as an + additional header line. + """ + for key, value in headers.items(): + for part in (key, value): + if "\r" in part or "\n" in part or "\0" in part: + raise ValueError( + "Header names and values may not contain CR, LF or " + "NUL characters." + ) + + class PycurlClient(HTTPClient): class _ParsedProxy(TypedDict, total=False): http: Optional["ParseResult"] @@ -957,6 +974,7 @@ def _request_internal( self._curl.setopt(self.pycurl.NOSIGNAL, 1) self._curl.setopt(self.pycurl.CONNECTTIMEOUT, 30) self._curl.setopt(self.pycurl.TIMEOUT, 80) + _assert_no_header_injection(headers) self._curl.setopt( self.pycurl.HTTPHEADER, ["%s: %s" % (k, v) for k, v in dict(headers).items()], diff --git a/tests/test_http_client.py b/tests/test_http_client.py index 8b3ab71fb..0c6eaa557 100644 --- a/tests/test_http_client.py +++ b/tests/test_http_client.py @@ -1149,6 +1149,23 @@ def _check_call( return _check_call + def test_request_rejects_header_injection( + self, mocked_request_lib, make_request: MakeReqFunc, mock_response + ): + mock_response(mocked_request_lib, '{"foo": "baz"}', 200) + + hostile_headers = [ + {"Stripe-Context": "acct_123\r\nX-Injected: 1"}, + {"Stripe-Request-Trigger": "event=evt_1\nX-Injected: 1"}, + {"Authorization": "Bearer sk_test_123\r\n"}, + {"Authorization": "Bearer sk_test_123\0"}, + {"X-Injected\r\nEvil": "value"}, + ] + + for headers in hostile_headers: + with pytest.raises(ValueError): + make_request("get", self.valid_url, headers, None) + class TestPycurlClientHttpProxy(TestPycurlClient): PROXY = "http://user:withPwd@slap:8888/"