Skip to content

Return user paths relative to effective user's homedir #105

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

terminalmage
Copy link

@terminalmage terminalmage commented May 3, 2018

Since os.path.expanduser() is used to expand the ~ to the home directory, paths which use ~ will always return a path relative to the home directory of the user under which the python process is running. However, if the euid has been changed, this means that these paths will be incorrect, leading to problems when the non-privleged user tries to write to these paths. For example:

>>> import appdirs
>>> import os
>>> os.geteuid()
0
>>> os.seteuid(1000)
>>> os.geteuid()
1000
>>> appdirs.user_cache_dir('foo')
'/root/.cache/foo'
>>> appdirs.user_data_dir('foo')
'/root/.local/share/foo'
>>> appdirs.user_config_dir('foo')
'/root/.config/foo'
>>> appdirs.user_state_dir('foo')
'/root/.local/state/foo'
>>>

This PR fixes that issue by attempting to get the username matching the euid, and using that username (e.g. ~user/.cache) when invoking os.path.expanduser(). With the fix, the funcs now return the correct paths relative to the effective user's homedir:

>>> import appdirs
>>> import os
>>> os.geteuid()
0
>>> os.seteuid(1000)
>>> os.geteuid()
1000
>>> appdirs.user_cache_dir('foo')
'/home/erik/.cache/foo'
>>> appdirs.user_data_dir('foo')
'/home/erik/.local/share/foo'
>>> appdirs.user_config_dir('foo')
'/home/erik/.config/foo'
>>> appdirs.user_state_dir('foo')
'/home/erik/.local/state/foo'
>>>

Since `os.path.expanduser()` is used to expand the `~` to the home
directory, paths which use `~` will always return a path relative to the
home directory of the user under which the python process is running.
However, if the euid has been changed, this means that these paths will
be incorrect, leading to problems when the non-privleged user tries to
write to these paths.

This commit fixes that issue by attempting to get the username matching
the euid, and using that username (e.g. `~user/.cache`) when invoking
`os.path.expanduser()`.
Copy link

@Kriskras99 Kriskras99 left a comment

Choose a reason for hiding this comment

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

Looks great, some small style issues

appdirs.py Outdated
@@ -41,6 +46,12 @@
system = sys.platform


def _effective_user():

Choose a reason for hiding this comment

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

This function should probably be placed at the bottom of the file, because all the support functions are located there.

appdirs.py Outdated
path = os.getenv('XDG_CACHE_HOME', os.path.expanduser('~/.cache'))
if appname:
path = os.path.join(path, appname)
user = _effective_user()

Choose a reason for hiding this comment

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

Same here

appdirs.py Outdated
path = os.getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config"))
if appname:
path = os.path.join(path, appname)
user = _effective_user()

Choose a reason for hiding this comment

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

I think it's cleaner to have _effective_user() inside the format just like you did in user_state_dir() and user_log_dir()

appdirs.py Outdated
@@ -395,7 +426,7 @@ def user_log_dir(appname=None, appauthor=None, version=None, opinion=True):
"""
if system == "darwin":
path = os.path.join(
os.path.expanduser('~/Library/Logs'),
os.path.expanduser('~{0}/Library/Logs'.format(_effective_user())),
appname)

Choose a reason for hiding this comment

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

Can you put the round bracket on a new line to match what you did in the other functions, or in the other functions don't put the round bracket on a new line.

@terminalmage
Copy link
Author

@Kriskras99 Thanks for the review, I've made the requested changes.

@Kriskras99
Copy link

Looks great. Now it's up to @zoofood.

@zoofood
Copy link
Contributor

zoofood commented May 7, 2018

Thanks guys, definitely time to get these merged and a new release out. Appreciate all the efforts!

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 this pull request may close these issues.

3 participants