Skip to content

Commit

Permalink
Fix PermissionError when loading .netrc (aio-libs#7237) (aio-libs#7378)…
Browse files Browse the repository at this point in the history
… (aio-libs#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 aio-libs#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 aio-libs#7237

Backport of aio-libs#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 <Name> <Surname>.
  * 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."
  • Loading branch information
jgosmann authored Jul 22, 2023
1 parent 9c13a52 commit 8d701c3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
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 @@ -153,6 +153,7 @@ Jake Davis
Jakob Ackermann
Jakub Wilk
Jan Buchar
Jan Gosmann
Jashandeep Sohi
Jens Steinhauser
Jeonghun Lee
Expand Down
7 changes: 5 additions & 2 deletions 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
import datetime
import functools
import inspect
Expand Down Expand Up @@ -226,8 +227,11 @@ def netrc_from_env() -> Optional[netrc.netrc]:
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 Expand Up @@ -742,7 +746,6 @@ def ceil_timeout(delay: Optional[float]) -> async_timeout.Timeout:


class HeadersMixin:

ATTRS = frozenset(["_content_type", "_content_dict", "_stored_content_type"])

_content_type: Optional[str] = None
Expand Down
22 changes: 21 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import platform
import tempfile
from math import isclose, modf
from pathlib import Path
from unittest import mock
from urllib.request import getproxies_environment

Expand Down Expand Up @@ -178,7 +179,6 @@ def test_basic_auth_from_not_url() -> None:


class ReifyMixin:

reify = NotImplemented

def test_reify(self) -> None:
Expand Down Expand Up @@ -763,3 +763,23 @@ def test_repr(self) -> None:
)
def test_parse_http_date(value, expected):
assert parse_http_date(value) == expected


@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()

0 comments on commit 8d701c3

Please sign in to comment.