Skip to content
Myr

Only send HTTPDigestAuth on 4xx challenges

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Matthew Medal · committed by Matthew Medal · parent 23057db · Browse files · 2 files+43 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Resolves: #3772

Diff stats

2 files+43 added−0 removed

  • tests:+37 added−0 removed
  • other:+6 added−0 removed

2 files+43 added−0 removed

modified:requests/auth.py+6 added
225 unmodified lines
226227 :rtype: requests.Response228 """229230 # If response is not 4xx, do not auth231 # See https://github.com/kennethreitz/requests/issues/3772232 if not 400 <= r.status_code < 500:233 self._thread_local.num_401_calls = 1234 return r235236 if self._thread_local.pos is not None:237 # Rewind the file position indicator of the body to where238 # it was to resend the request.239 r.request.body.seek(self._thread_local.pos)
55 unmodified lines
modified:tests/test_lowlevel.py+37 added
134 unmodified lines
135 assert r.history[0].status_code == 401136 close_server.set()137138139def test_digestauth_only_on_4xx():140 """Ensure we only send digestauth on 4xx challenges.141142 See https://github.com/kennethreitz/requests/issues/3772.143 """144 text_200_chal = (b'HTTP/1.1 200 OK\r\n'145 b'Content-Length: 0\r\n'146 b'WWW-Authenticate: Digest nonce="6bf5d6e4da1ce66918800195d6b9130d"'147 b', opaque="372825293d1c26955496c80ed6426e9e", '148 b'realm="me@kennethreitz.com", qop=auth\r\n\r\n')149150 auth = requests.auth.HTTPDigestAuth('user', 'pass')151152 def digest_response_handler(sock):153 # Respond to GET with a 200 containing www-authenticate header.154 request_content = consume_socket_content(sock, timeout=0.5)155 assert request_content.startswith(b"GET / HTTP/1.1")156 sock.send(text_200_chal)157158 # Verify the client didn't respond with auth.159 request_content = consume_socket_content(sock, timeout=0.5)160 assert request_content == b''161162 return request_content163164 close_server = threading.Event()165 server = Server(digest_response_handler, wait_to_close_event=close_server)166167 with server as (host, port):168 url = 'http://{0}:{1}/'.format(host, port)169 r = requests.get(url, auth=auth)170 # Verify server didn't receive auth from us.171 assert r.status_code == 200172 assert len(r.history) == 0173 close_server.set()174175176_schemes_by_var_prefix = [177 ('http', ['http']),178 ('https', ['https']),179 ('all', ['http', 'https']),
27 unmodified lines