Skip to content

Commit 370a5b9

Browse files
adrienvergedjc
authored andcommitted
Fix HTTP auth password encoding (#302)
Username/password encoding in HTTP basic auth is currently broken for non-ASCII password. Example with user `user` and password `unusual-char-é`. With curl it works as expected: ```shell curl -v http://user:unusual-char-%C3%A9@localhost:5984/ ``` ``` > GET / HTTP/1.1 > Authorization: Basic YWxpY2U6YWRyaWVuPTohw6k= > < HTTP/1.1 200 OK ``` But with couchdb-python the string is decoded from `utf-8` then re-encoded into `latin1`, causing an incorrect Authorization header: ```python url = 'http://user:unusual-char-%C3%A9@localhost:5984/' couchdb.Server(url).version() ``` ``` > GET / HTTP/1.1 > Authorization: Basic dXNlcjp1bnVzdWFsLWNoYXIt6Q== > < HTTP/1.1 401 Unauthorized ``` This patch fixes this wrong encoding charset by using `utf-8` (used by default by CouchDB) instead of `latin1`. Closes: #301
1 parent 53b0bb7 commit 370a5b9

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

couchdb/http.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def _try_request():
362362
if num_redirects > self.max_redirects:
363363
raise RedirectLimit('Redirection limit exceeded')
364364
location = resp.getheader('location')
365-
365+
366366
# in case of relative location: add scheme and host to the location
367367
location_split = util.urlsplit(location)
368368

@@ -592,7 +592,7 @@ def _request_json(self, method, path=None, body=None, headers=None, **params):
592592
def extract_credentials(url):
593593
"""Extract authentication (user name and password) credentials from the
594594
given URL.
595-
595+
596596
>>> extract_credentials('http://localhost:5984/_config/')
597597
('http://localhost:5984/_config/', None)
598598
>>> extract_credentials('http://joe:secret@localhost:5984/_config/')
@@ -620,8 +620,8 @@ def basic_auth(credentials):
620620
>>> basic_auth(())
621621
"""
622622
if credentials:
623-
token = b64encode(('%s:%s' % credentials).encode('latin1'))
624-
return ('Basic %s' % token.strip().decode('latin1')).encode('ascii')
623+
token = b64encode(('%s:%s' % credentials).encode('utf-8'))
624+
return ('Basic %s' % token.strip().decode('utf-8')).encode('ascii')
625625

626626

627627
def quote(string, safe=''):

0 commit comments

Comments
 (0)