8000 Simplify static path checks and resolve strictly by steverep · Pull Request #8491 · aio-libs/aiohttp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

Simplify static path checks and resolve strictly #8491

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
Jul 11, 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/8491.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simplified path checks for ``UrlDispatcher.add_static()`` method -- by :user:`steverep`.
13 changes: 5 additions & 8 deletions aiohttp/web_urldispatcher.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -535,14 +535,11 @@ def __init__(
) -> None:
super().__init__(prefix, name=name)
try:
directory = Path(directory)
if str(directory).startswith("~"):
directory = Path(os.path.expanduser(str(directory)))
directory = directory.resolve()
if not directory.is_dir():
raise ValueError("Not a directory")
except (FileNotFoundError, ValueError) as error:
raise ValueError(f"No directory exists at '{directory}'") from error
directory = Path(directory).expanduser().resolve(strict=True)
except FileNotFoundError as error:
raise ValueError(f"'{directory}' does not exist") from error
if not directory.is_dir():
raise ValueError(f"'{directory}' is not a directory")
self._directory = directory
self._show_index = show_index
self._chunk_size = chunk_size
Expand Down
15 changes: 15 additions & 0 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,21 @@ def test_route_dynamic(router: Any) -> None:
assert route is route2


def test_add_static_path_checks(router: Any, tmp_path: pathlib.Path) -> None:
"""Test that static paths must exist and be directories."""
with pytest.raises(ValueError, match="does not exist"):
router.add_static("/", tmp_path / "does-not-exist")
with pytest.raises(ValueError, match="is not a directory"):
router.add_static("/", __file__)


def test_add_static_path_resolution(router: Any) -> None:
"""Test that static paths are expanded and absolute."""
res = router.add_static("/", "~/..")
directory = str(res.get_info()["directory"])
assert directory == str(pathlib.Path.home().parent)


def test_add_static(router: Any) -> None:
resource = router.add_static(
"/st", pathlib.Path(aiohttp.__file__).parent, name="static"
Expand Down
9 changes: 0 additions & 9 deletions tests/test_web_sendfile_functional.py
4FE8
Original file line number Diff line number Diff line change
Expand Up @@ -587,15 +587,6 @@ async def test_static_file_directory_traversal_attack(aiohttp_client: Any) -> No
await client.close()


def test_static_route_path_existence_check() -> None:
directory = pathlib.Path(__file__).parent
web.StaticResource("/", directory)

nodirectory = directory / "nonexistent-uPNiOEAg5d"
with pytest.raises(ValueError):
web.StaticResource("/", nodirectory)


async def test_static_file_huge(aiohttp_client: Any, tmp_path: Any) -> None:
file_path = tmp_path / "huge_data.unknown_mime_type"

Expand Down
Loading
0