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

get_netrc_auth should handle os.path.expanduser failing #1846

Merged
merged 2 commits into from
Jan 8, 2014

Conversation

acdha
Copy link
Contributor

@acdha acdha commented Jan 7, 2014

https://github.com/toastdriven/django-haystack/issues/924 has a problem report from a user who appears to be running inside a process which does not have $HOME defined and is running under a UID which is either not in /etc/passwd or does not have permission to access it.

This causes utils.get_netrc_auth to raise an unexpected KeyError. The easiest fix would be to simply add that to the except block at the bottom but that's probably too dangerous – I'd probably handle the KeyError right at the source until this can be fixed upstream.

@acdha
Copy link
Contributor Author

acdha commented Jan 7, 2014

Upstream Python bug report: http://bugs.python.org/issue20164

@Lukasa
Copy link
Member

Lukasa commented Jan 7, 2014

Well this makes me very sad. Thanks for raising this!

Given that upstream is not going to fix this on 2.6 for sure, even if they do implement a fix elsewhere, I think we need to handle this. The correct behaviour is that get_netrc_auth should fail and return nothing.

I'm also inclined to be cautious, so we should restrict the scope of the try...except block.

os.path.expanduser can raise a KeyError when $HOME is not set and the
POSIX getpwuid() call fails, which can happen when running under a UID
which is not in /etc/passwd or when the password file cannot be read.

The upstream bug report http://bugs.python.org/issue20164 is unlikely to
be backported to Python 2.x even if fixed so this change handles
KeyError by skipping netrc authentication.

Closes psf#1846
@acdha
Copy link
Contributor Author

acdha commented Jan 7, 2014

@Lukasa I took a first pass at a patch in 0b41cec

# getpwuid fails. See http://bugs.python.org/issue20164 &
# https://github.com/kennethreitz/requests/issues/1846
return

if os.path.exists(loc) and not netrc_path:
netrc_path = loc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're changing this function, can we avoid this and not netrc_path test by breaking from this loop instead? Saves us some iterations, probably.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed out a80bd77 which does this and also changes the next block to a more obvious bare return rather than returning a variable which can only be None

* Stop iterating as soon as we find a netrc file
* More obvious return None
@Lukasa
Copy link
Member

Lukasa commented Jan 7, 2014

Alright, I'm happy with this. You can ignore TravisCI (I have no idea why it's still running), the relevant CI results are these (all of which passed):

http://ci.kennethreitz.org/job/requests-pr/PYTHON=2.6/167/
http://ci.kennethreitz.org/job/requests-pr/PYTHON=2.7/167/
http://ci.kennethreitz.org/job/requests-pr/PYTHON=3.3/167/
http://ci.kennethreitz.org/job/requests-pr/PYTHON=pypy-1.8/167/

Waiting on @sigmavirus24 to review.

@acdha
Copy link
Contributor Author

acdha commented Jan 7, 2014

Great - thanks for reviewing it!

@kennethreitz
Copy link
Contributor

WHY IS TRAVIS RUNNING AGAIN OH MY GOD

@kennethreitz kennethreitz reopened this Jan 8, 2014
kennethreitz added a commit that referenced this pull request Jan 8, 2014
get_netrc_auth should handle os.path.expanduser failing
@kennethreitz kennethreitz merged commit 14f207f into psf:master Jan 8, 2014
@kennethreitz
Copy link
Contributor

I'm very sad that this requires so many lines of code, but so be it.

@acdha
Copy link
Contributor Author

acdha commented Dec 16, 2018

This will at some point become unnecessary once the upstream fix has been widely deployed – hopefully in less than another 5 years:

https://bugs.python.org/issue10496

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants