Change raise_for_status message to hide password in URL#5221
Change raise_for_status message to hide password in URL#5221
Conversation
jackyzha0
left a comment
There was a problem hiding this comment.
Is there anything we need to update in the documentation for this? Other than that, looks good! 👍
barroca
left a comment
There was a problem hiding this comment.
can you add unit test for this change?
|
@jackyzha0 @barroca I'll have time to get back to this next weekend. |
|
I've added a unit test. |
tests/test_requests.py
Outdated
| with pytest.raises(requests.exceptions.HTTPError) as e: | ||
| r.raise_for_status() | ||
|
|
||
| assert 'pass' not in str(e) |
There was a problem hiding this comment.
💡 I'd rather assert e is equals with the string with pass obfuscated, and also add a test that there doesn't obfuscate things that wasn't suppose to, but I'm happy to approve it.
There was a problem hiding this comment.
I've made the test more specific now. How would you implement a test that checks if other things are not obfuscated?
There was a problem hiding this comment.
You can create a test like this:
def test_status_raising_doesnt_hides_other_stuff(self, httpbin):
host = urlparse(httpbin()).netloc
r = requests.get('http://shouldntchange@' + host + '/status/404')
with pytest.raises(requests.exceptions.HTTPError) as e:
r.raise_for_status()
host_without_port = host.split(':')[0]
assert str(e.value) == '404 Client Error: NOT FOUND for url: http://shouldntchange@' + host_without_port + '/status/404'
This is just a validation that the match doesn't put wrong information if the password is not supplied
There was a problem hiding this comment.
Adding that test actually made me find two bugs!
- The host in the exception message no longer included the port, which shouldn't happen.
- Asterisks were included even if there was no password specified.
Thanks for the suggestion.
|
Running into this same issue for requests with |
|
Running into this same issue for requests with |
- reference issue psf/requests#5221 - unless this issue is not resolved, may have to do an alternate approach
This PR fixes #5021 by replacing a password in the URL with asterisks.
Example:
Output: