← mined_oracle

requests_6154

failed WRONG_FIX UNSUBMITTED wrong_fix_unsubmitted(None) · None tool calls · 0 s · psf/requests

Task input

(not found in data/tasks.jsonl)

Tool calls (0)

#ToolArgumentsResult
No trace captured.

Patch

--- a/requests/_internal_utils.py
+++ b/requests/_internal_utils.py
@@ -5,9 +5,20 @@ requests._internal_utils
 Provides utility functions that are consumed internally by Requests
 which depend on extremely few external helpers (such as compat)
 """
+import re
 
 from .compat import builtin_str
 
+_VALID_HEADER_NAME_RE_BYTE = re.compile(rb"^[^:\s][^:\r\n]*$")
+_VALID_HEADER_NAME_RE_STR = re.compile(r"^[^:\s][^:\r\n]*$")
+_VALID_HEADER_VALUE_RE_BYTE = re.compile(rb"^\S[^\r\n]*$|^$")
+_VALID_HEADER_VALUE_RE_STR = re.compile(r"^\S[^\r\n]*$|^$")
+
+HEADER_VALIDATORS = {
+    bytes: (_VALID_HEADER_NAME_RE_BYTE, _VALID_HEADER_VALUE_RE_BYTE),
+    str: (_VALID_HEADER_NAME_RE_STR, _VALID_HEADER_VALUE_RE_STR),
+}
+
 
 def to_native_string(string, encoding="ascii"):
     """Given a string object, regardless of type, returns a representation of
--- a/requests/utils.py
+++ b/requests/utils.py
@@ -25,7 +25,7 @@ from . import certs
 from .__version__ import __version__
 
 # to_native_string is unused here, but imported here for backwards compatibility
-from ._internal_utils import to_native_string  # noqa: F401
+from ._internal_utils import HEADER_VALIDATORS, to_native_string  # noqa: F401
 from .compat import (
     Mapping,
     basestring,
@@ -1024,33 +1024,30 @@ def get_auth_from_url(url):
     return auth
 
 
-# Moved outside of function to avoid recompile every call
-_CLEAN_HEADER_REGEX_BYTE = re.compile(b"^\\S[^\\r\\n]*$|^$")
-_CLEAN_HEADER_REGEX_STR = re.compile(r"^\S[^\r\n]*$|^$")
-
-
 def check_header_validity(header):
-    """Verifies that header value is a string which doesn't contain
-    leading whitespace or return characters. This prevents unintended
-    header injection.
+    """Verifies that header parts don't contain leading whitespace
+    reserved characters, or return characters.
 
     :param header: tuple, in the format (name, value).
     """
     name, value = header
 
-    if isinstance(value, bytes):
-        pat = _CLEAN_HEADER_REGEX_BYTE
-    else:
-        pat = _CLEAN_HEADER_REGEX_STR
-    try:
-        if not pat.match(value):
+    for part in header:
+        if type(part) not in HEADER_VALIDATORS:
             raise InvalidHeader(
-                f"Invalid return character or leading space in header: {name}"
+                f"Header part ({part!r}) from {{{name!r}: {value!r}}} must be "
+                f"of type str or bytes, not {type(part)}"
             )
-    except TypeError:
+
+    _validate_header_part(name, "name", HEADER_VALIDATORS[type(name)][0])
+    _validate_header_part(value, "value", HEADER_VALIDATORS[type(value)][1])
+
+
+def _validate_header_part(header_part, header_kind, validator):
+    if not validator.match(header_part):
         raise InvalidHeader(
-            f"Value for header {{{name}: {value}}} must be of type "
-            f"str or bytes, not {type(value)}"
+            f"Invalid leading whitespace, reserved character(s), or return"
+            f"character(s) in header {header_kind}: {header_part!r}"
         )
 
 

Test output

show
[2026-09-23 20:55:11,362] WARNING in core: flasgger is not installed; serving the static landing page at / and skipping the Swagger UI and /spec.json.
ImportError while loading conftest '/private/tmp/swe_work/mined_oracle/requests_6154/b/workspace/tests/conftest.py'.
tests/__init__.py:5: in <module>
    from urllib3.exceptions import SNIMissingWarning
E   ImportError: cannot import name 'SNIMissingWarning' from 'urllib3.exceptions' (/Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/requests/lib/python3.13/site-packages/urllib3/exceptions.py)