Skip to content

Commit

Permalink
[PR #5534/9c7f3d37 backport][3.8] StaticResource only matches folder-…
Browse files Browse the repository at this point in the history
…like URL prefix (#6179)

* StaticResource only matches folder-like URL prefix (#5534)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
(cherry picked from commit 9c7f3d3)

* fix

Co-authored-by: Dan King <daniel.zidan.king@gmail.com>
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
  • Loading branch information
3 people committed Oct 30, 2021
1 parent eeaea0c commit a8f01d7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/5250.bugfix
@@ -0,0 +1 @@
StaticResource prefixes no longer match URLs with a non-folder prefix. For example ``routes.static('/foo', '/foo')`` no longer matches the URL ``/foobar``. Previously, this would attempt to load the file ``/foo/ar``.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -74,6 +74,7 @@ Claudiu Popa
Colin Dunklau
Cong Xu
Damien Nadé
Dan King
Dan Xu
Daniel García
Daniel Grossmann-Kavanagh
Expand Down
6 changes: 4 additions & 2 deletions aiohttp/web_urldispatcher.py
Expand Up @@ -513,6 +513,7 @@ def __init__(self, prefix: str, *, name: Optional[str] = None) -> None:
assert prefix in ("", "/") or not prefix.endswith("/"), prefix
super().__init__(name=name)
self._prefix = _requote_path(prefix)
self._prefix2 = self._prefix + "/"

@property
def canonical(self) -> str:
Expand All @@ -523,6 +524,7 @@ def add_prefix(self, prefix: str) -> None:
assert not prefix.endswith("/")
assert len(prefix) > 1
self._prefix = prefix + self._prefix
self._prefix2 = self._prefix + "/"

def raw_match(self, prefix: str) -> bool:
return False
Expand Down Expand Up @@ -634,7 +636,7 @@ async def resolve(self, request: Request) -> _Resolve:
path = request.rel_url.raw_path
method = request.method
allowed_methods = set(self._routes)
if not path.startswith(self._prefix):
if not path.startswith(self._prefix2) and path != self._prefix:
return None, set()

if method not in allowed_methods:
Expand Down Expand Up @@ -750,7 +752,7 @@ def get_info(self) -> _InfoDict:

async def resolve(self, request: Request) -> _Resolve:
if (
not request.url.raw_path.startswith(self._prefix + "/")
not request.url.raw_path.startswith(self._prefix2)
and request.url.raw_path != self._prefix
):
return None, set()
Expand Down
16 changes: 16 additions & 0 deletions tests/test_web_urldispatcher.py
Expand Up @@ -5,6 +5,7 @@
import shutil
import sys
import tempfile
from typing import Any
from unittest import mock
from unittest.mock import MagicMock

Expand Down Expand Up @@ -519,6 +520,21 @@ async def test_static_absolute_url(aiohttp_client, tmpdir) -> None:
assert resp.status == 403


async def test_for_issue_5250(aiohttp_client: Any, tmp_path: Any) -> None:
app = web.Application()
app.router.add_static("/foo", tmp_path)

async def get_foobar(request):
return web.Response(body="success!")

app.router.add_get("/foobar", get_foobar)

client = await aiohttp_client(app)
async with await client.get("/foobar") as resp:
assert resp.status == 200
assert (await resp.text()) == "success!"


@pytest.mark.xfail(
raises=AssertionError,
reason="Regression in v3.7: https://github.com/aio-libs/aiohttp/issues/5621",
Expand Down

0 comments on commit a8f01d7

Please sign in to comment.