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

Fix PermissionError when loading .netrc (#7237) #7378

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/7237.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``PermissionError`` when .netrc is unreadable due to permissions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Jake Davis
Jakob Ackermann
Jakub Wilk
Jan Buchar
Jan Gosmann
Jarno Elonen
Jashandeep Sohi
Jean-Baptiste Estival
Expand Down
6 changes: 5 additions & 1 deletion aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import base64
import binascii
import contextlib

Check notice

Code scanning / CodeQL

Module is imported with 'import' and 'import from' Note

Module 'contextlib' is imported with both 'import' and 'import from'.
import dataclasses
import datetime
import enum
Expand Down Expand Up @@ -213,8 +214,11 @@
except netrc.NetrcParseError as e:
client_logger.warning("Could not parse .netrc file: %s", e)
except OSError as e:
netrc_exists = False
with contextlib.suppress(OSError):
netrc_exists = netrc_path.is_file()
# we couldn't read the file (doesn't exist, permissions, etc.)
if netrc_env or netrc_path.is_file():
if netrc_env or netrc_exists:
# only warn if the environment wanted us to load it,
# or it appears like the default file does actually exist
client_logger.warning("Could not read .netrc file: %s", e)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import platform
import weakref
from math import ceil, modf
from pathlib import Path
from unittest import mock
from urllib.request import getproxies_environment

Expand Down Expand Up @@ -993,6 +994,26 @@ def test_netrc_from_env(expected_username: str):
assert netrc_obj.authenticators("example.com")[0] == expected_username


@pytest.fixture
def protected_dir(tmp_path: Path):
protected_dir = tmp_path / "protected"
protected_dir.mkdir()
try:
protected_dir.chmod(0o600)
yield protected_dir
finally:
protected_dir.rmdir()


def test_netrc_from_home_does_not_raise_if_access_denied(
protected_dir: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.setattr(Path, "home", lambda: protected_dir)
monkeypatch.delenv("NETRC", raising=False)

helpers.netrc_from_env()


@pytest.mark.parametrize(
["netrc_contents", "expected_auth"],
[
Expand Down
Loading