﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31912	Path.resolve(strict=True) raises PermissionError.	leonyxz	Mariusz Felisiak	"Hello,

Python 3.8
FreeBSD 11.3-RELEASE-p12 with custom settings mentioned later.

On vanilla configuration of FreeBSD there is no issue.
Installation of FreeBSD in question has mac_bsdextended turned on ([https://www.freebsd.org/cgi/man.cgi?query=mac_bsdextended&sektion=4&apropos=0&manpath=FreeBSD+12.1-RELEASE+and+Ports])
Using ugidfw ([https://www.freebsd.org/cgi/man.cgi?query=ugidfw&sektion=8&manpath=freebsd-release-ports]), the following rule has been set:
---------------------------------------------------------------

{{{
0 subject not uid root gid nobody object gid wheel type d mode sx
}}}
---------------------------------------------------------------
Attempts to run Django 3.1 result in
---------------------------------------------------------------

{{{
$ python manage.py runserver 0.0.0.0:1337
Traceback (most recent call last):
  File ""manage.py"", line 22, in <module>
    main()
  File ""manage.py"", line 18, in main
    execute_from_command_line(sys.argv)
  File ""/home/userlogin/.virtualenvs/django31/lib/python3.8/site-packages/django/core/management/__init__.py"", line 401, in execute_from_command_line
    utility.execute()
  File ""/home/userlogin/.virtualenvs/django31/lib/python3.8/site-packages/django/core/management/__init__.py"", line 345, in execute
    settings.INSTALLED_APPS
  File ""/home/userlogin/.virtualenvs/django31/lib/python3.8/site-packages/django/conf/__init__.py"", line 83, in __getattr__
    self._setup(name)
  File ""/home/userlogin/.virtualenvs/django31/lib/python3.8/site-packages/django/conf/__init__.py"", line 70, in _setup
    self._wrapped = Settings(settings_module)
  File ""/home/userlogin/.virtualenvs/django31/lib/python3.8/site-packages/django/conf/__init__.py"", line 177, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File ""/usr/local/lib/python3.8/importlib/__init__.py"", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File ""<frozen importlib._bootstrap>"", line 1014, in _gcd_import
  File ""<frozen importlib._bootstrap>"", line 991, in _find_and_load
  File ""<frozen importlib._bootstrap>"", line 975, in _find_and_load_unlocked
  File ""<frozen importlib._bootstrap>"", line 671, in _load_unlocked
  File ""<frozen importlib._bootstrap_external>"", line 783, in exec_module
  File ""<frozen importlib._bootstrap>"", line 219, in _call_with_frames_removed
  File ""/usr/home/userlogin/domains/django31.userlogin.com/public_python/project/settings.py"", line 16, in <module>
    BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
  File ""/usr/local/lib/python3.8/pathlib.py"", line 1159, in resolve
    s = self._flavour.resolve(self, strict=strict)
  File ""/usr/local/lib/python3.8/pathlib.py"", line 355, in resolve
    return _resolve(base, str(path)) or sep
  File ""/usr/local/lib/python3.8/pathlib.py"", line 339, in _resolve
    target = accessor.readlink(newpath)
  File ""/usr/local/lib/python3.8/pathlib.py"", line 439, in readlink
    return os.readlink(path)
PermissionError: [Errno 13] Permission denied: '/usr'
}}}

---------------------------------------------------------------
The issue seems to be comnected with following piece code
[https://github.com/python/cpython/blob/master/Lib/pathlib.py#L342-L346]
---------------------------------------------------------------

{{{
    try:
                    target = accessor.readlink(newpath)
                except OSError as e:
                    if e.errno != EINVAL and strict:
                        raise
                    # Not a symlink, or non-strict mode. We just leave the path
                    # untouched.
                    path = newpath
}}}

---------------------------------------------------------------
With ugidfw enabled, `os.readlink` raises a `PermissionError`, which is unhandled by pathlib."	Bug	closed	Core (Other)	3.1	Release blocker	fixed		Jon Dufresne	Accepted	1	0	0	0	0	0
