Skip to content

Commit 8d701c3

Browse files
authored
Fix PermissionError when loading .netrc (#7237) (#7378) (#7395)
## What do these changes do? If no NETRC environment variable is provided and the .netrc path cannot be accessed due to missing permission, a PermissionError was raised instead of returning None. See issue #7237. This PR fixes the issue. If the changes look good, I can also prepare backports. ## Are there changes in behavior for the user? If the .netrc cannot be accessed due to a permission problem (and the `NETRC` environment variable is unset), no `PermissionError` will be raised. Instead it will be silently ignored. ## Related issue number Fixes #7237 Backport of #7378 (cherry picked from commit 0d2e43b) ## Checklist - [x] I think the code is well written - [x] Unit tests for the changes exist - [x] Documentation reflects the changes - [x] If you provide code modification, please add yourself to `CONTRIBUTORS.txt` * The format is &lt;Name&gt; &lt;Surname&gt;. * Please keep alphabetical order, the file is sorted by names. - [x] Add a new news fragment into the `CHANGES` folder * name it `<issue_id>.<type>` for example (588.bugfix) * if you don't have an `issue_id` change it to the pr id after creating the pr * ensure type is one of the following: * `.feature`: Signifying a new feature. * `.bugfix`: Signifying a bug fix. * `.doc`: Signifying a documentation improvement. * `.removal`: Signifying a deprecation or removal of public API. * `.misc`: A ticket has been closed, but it is not of interest to users. * Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."
1 parent 9c13a52 commit 8d701c3

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

CHANGES/7237.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed ``PermissionError`` when .netrc is unreadable due to permissions.

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ Jake Davis
153153
Jakob Ackermann
154154
Jakub Wilk
155155
Jan Buchar
156+
Jan Gosmann
156157
Jashandeep Sohi
157158
Jens Steinhauser
158159
Jeonghun Lee

aiohttp/helpers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import asyncio
44
import base64
55
import binascii
6+
import contextlib
67
import datetime
78
import functools
89
import inspect
@@ -226,8 +227,11 @@ def netrc_from_env() -> Optional[netrc.netrc]:
226227
except netrc.NetrcParseError as e:
227228
client_logger.warning("Could not parse .netrc file: %s", e)
228229
except OSError as e:
230+
netrc_exists = False
231+
with contextlib.suppress(OSError):
232+
netrc_exists = netrc_path.is_file()
229233
# we couldn't read the file (doesn't exist, permissions, etc.)
230-
if netrc_env or netrc_path.is_file():
234+
if netrc_env or netrc_exists:
231235
# only warn if the environment wanted us to load it,
232236
# or it appears like the default file does actually exist
233237
client_logger.warning("Could not read .netrc file: %s", e)
@@ -742,7 +746,6 @@ def ceil_timeout(delay: Optional[float]) -> async_timeout.Timeout:
742746

743747

744748
class HeadersMixin:
745-
746749
ATTRS = frozenset(["_content_type", "_content_dict", "_stored_content_type"])
747750

748751
_content_type: Optional[str] = None

tests/test_helpers.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import platform
66
import tempfile
77
from math import isclose, modf
8+
from pathlib import Path
89
from unittest import mock
910
from urllib.request import getproxies_environment
1011

@@ -178,7 +179,6 @@ def test_basic_auth_from_not_url() -> None:
178179

179180

180181
class ReifyMixin:
181-
182182
reify = NotImplemented
183183

184184
def test_reify(self) -> None:
@@ -763,3 +763,23 @@ def test_repr(self) -> None:
763763
)
764764
def test_parse_http_date(value, expected):
765765
assert parse_http_date(value) == expected
766+
767+
768+
@pytest.fixture
769+
def protected_dir(tmp_path: Path):
770+
protected_dir = tmp_path / "protected"
771+
protected_dir.mkdir()
772+
try:
773+
protected_dir.chmod(0o600)
774+
yield protected_dir
775+
finally:
776+
protected_dir.rmdir()
777+
778+
779+
def test_netrc_from_home_does_not_raise_if_access_denied(
780+
protected_dir: Path, monkeypatch: pytest.MonkeyPatch
781+
):
782+
monkeypatch.setattr(Path, "home", lambda: protected_dir)
783+
monkeypatch.delenv("NETRC", raising=False)
784+
785+
helpers.netrc_from_env()

0 commit comments

Comments
 (0)