Skip to content

Fix response to circular symlinks with Python v3.13 #8642

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

Merged
merged 3 commits into from
Aug 8, 2024
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/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 @@ -188,7 +188,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 @@ -77,9 +77,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 @@ -672,8 +672,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
Loading