Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise_for_status leaks HTTP basic auth #5021

Open
calve opened this issue Mar 14, 2019 · 1 comment · May be fixed by #5221
Open

raise_for_status leaks HTTP basic auth #5021

calve opened this issue Mar 14, 2019 · 1 comment · May be fixed by #5221

Comments

@calve
Copy link

calve commented Mar 14, 2019

Calling raise_for_status() output (leaks) the HTTP basic auth password when raising an exception.

Expected Result

>>> import requests
>>> r = requests.get("http://user:somepassw@httpbin.org/status/401")
>>> r.raise_for_status()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: UNAUTHORIZED for url: http://user:<redacted>@httpbin.org/status/401

Actual Result

Here we can see the HTTP password in the resulting exception (somepassw in our case)

>>> import requests
>>> r = requests.get("http://user:somepassw@httpbin.org/status/401")
>>> r.raise_for_status()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: UNAUTHORIZED for url: http://user:somepassw@httpbin.org/status/401

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.8"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.7.2"
  },
  "platform": {
    "release": "4.20.8-arch1-1-ARCH",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.21.0"
  },
  "system_ssl": {
    "version": "1010101f"
  },
  "urllib3": {
    "version": "1.24.1"
  },
  "using_pyopenssl": false
}
@calve
Copy link
Author

calve commented Mar 15, 2019

For future reader, one way to fix this behaviour is to pass the basic auth by kwargs;

>>> import requests
>>> r = requests.get("http://httpbin.org/status/401", auth=("user", "passwd"))
>>> r.raise_for_status()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: UNAUTHORIZED for url: http://httpbin.org/status/401

Still, seeing that python-requests send a Authorization header, it looks like we could do something for the original leak, as requests knows which parts of the URL is the authentification

>>> r = requests.get("http://user:password@httpbin.org/status/401")
>>> r.raise_for_status()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: UNAUTHORIZED for url: http://user:password@httpbin.org/status/401
>>> r.request.headers
{'User-Agent': 'python-requests/2.21.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}

@Overv Overv linked a pull request Oct 2, 2019 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant