Skip to content

Commit

Permalink
[PR #8642/e4942771 backport][3.10] Fix response to circular symlinks …
Browse files Browse the repository at this point in the history
…with Python v3.13 (#8648)

Co-authored-by: Steve Repsher <[email protected]>
  • Loading branch information
patchback[bot] and steverep authored Aug 8, 2024
1 parent 2ef14a6 commit 1f92213
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES/8565.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed server checks for circular symbolic links to be compatible with Python 3.13 -- by :user:`steverep`.
4 changes: 3 additions & 1 deletion aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ async def prepare(self, request: "BaseRequest") -> Optional[AbstractStreamWriter
file_path, st, file_encoding = await loop.run_in_executor(
None, self._get_file_path_stat_encoding, accept_encoding
)
except FileNotFoundError:
except OSError:
# Most likely to be FileNotFoundError or OSError for circular
# symlinks in python >= 3.13, so respond with 404.
self.set_status(HTTPNotFound.status_code)
return await super().prepare(request)

Expand Down
9 changes: 5 additions & 4 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
BaseDict = dict

CIRCULAR_SYMLINK_ERROR = (
OSError
(OSError,)
if sys.version_info < (3, 10) and sys.platform.startswith("win32")
else RuntimeError
else (RuntimeError,) if sys.version_info < (3, 13) else ()
)

YARL_VERSION: Final[Tuple[int, ...]] = tuple(map(int, yarl_version.split(".")[:2]))
Expand Down Expand Up @@ -694,8 +694,9 @@ def _resolve_path_to_response(self, unresolved_path: Path) -> StreamResponse:
else:
file_path = unresolved_path.resolve()
file_path.relative_to(self._directory)
except (ValueError, CIRCULAR_SYMLINK_ERROR) as error:
# ValueError for relative check; RuntimeError for circular symlink.
except (ValueError, *CIRCULAR_SYMLINK_ERROR) as error:
# ValueError is raised for the relative check. Circular symlinks
# raise here on resolving for python < 3.13.
raise HTTPNotFound() from error

# if path is a directory, return the contents if permitted. Note the
Expand Down

0 comments on commit 1f92213

Please sign in to comment.