b01884
From 4c0e7d69e461a28a254e7c7a27c2768be3163a3d Mon Sep 17 00:00:00 2001
b01884
From: Fraser Tweedale <ftweedal@redhat.com>
b01884
Date: Wed, 7 Nov 2018 17:06:47 +1100
b01884
Subject: [PATCH] rpc: always read response
b01884
b01884
If the server responds 401 and the response body is empty, the
b01884
client raises ResponseNotReady.  This occurs because:
b01884
b01884
1. For a non-200 response, the response read only if the
b01884
   Content-Length header occurs.
b01884
b01884
2. The response must be read before another request (e.g. the
b01884
   follow-up request with WWW-Authenticate header set), and this
b01884
   condition was not met.  For details see
b01884
   https://github.com/python/cpython/blob/v3.6.7/Lib/http/client.py#L1305-L1321.
b01884
b01884
This situation should not arise in regular use, because the client
b01884
either has a session cookie, or, knowing the details of the server
b01884
it is contacting, it establishes the GSS-API context and includes
b01884
the WWW-Authenticate header in the initial request.
b01884
b01884
Nevertheless, this problem has been observed in the wild.  I do not
b01884
know its ordinary cause(s), but one can force the issue by removing
b01884
an authenticated user's session cache from /run/ipa/ccaches, then
b01884
performing a request.
b01884
b01884
Resolve the issue by always reading the response.  It is safe to
b01884
call response.read() regardless of whether the Content-Length header
b01884
appears, or whether the body is empty.
b01884
b01884
Fixes: https://pagure.io/freeipa/issue/7752
b01884
Reviewed-By: Christian Heimes <cheimes@redhat.com>
b01884
---
b01884
 ipalib/rpc.py | 11 +++++++++--
b01884
 1 file changed, 9 insertions(+), 2 deletions(-)
b01884
b01884
diff --git a/ipalib/rpc.py b/ipalib/rpc.py
b01884
index b27f3cef9c..23841d0a4c 100644
b01884
--- a/ipalib/rpc.py
b01884
+++ b/ipalib/rpc.py
b01884
@@ -712,8 +712,15 @@ def single_request(self, host, handler, request_body, verbose=0):
b01884
                     response = h.getresponse()
b01884
 
b01884
                 if response.status != 200:
b01884
-                    if (response.getheader("content-length", 0)):
b01884
-                        response.read()
b01884
+                    # Must read response (even if it is empty)
b01884
+                    # before sending another request.
b01884
+                    #
b01884
+                    # https://docs.python.org/3/library/http.client.html
b01884
+                    #   #http.client.HTTPConnection.getresponse
b01884
+                    #
b01884
+                    # https://pagure.io/freeipa/issue/7752
b01884
+                    #
b01884
+                    response.read()
b01884
 
b01884
                     if response.status == 401:
b01884
                         if not self._auth_complete(response):