Skip to content

Commit

Permalink
Fix response to circular symlinks with Python v3.13 (#8642)
Browse files Browse the repository at this point in the history
Co-authored-by: J. Nick Koston <nick@koston.org>
  • Loading branch information
steverep and bdraco authored Aug 8, 2024
1 parent 0a88bab commit e494277
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 @@ -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

0 comments on commit e494277

Please sign in to comment.