Skip to content
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

StaticResource only matches folder-like URL prefix #5534

Merged
merged 10 commits into from Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`.
asvetlov marked this conversation as resolved.
Show resolved Hide resolved
asvetlov marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -71,6 +71,7 @@ Claudiu Popa
Colin Dunklau
Cong Xu
Damien Nadé
Dan King
Dan Xu
Daniel García
Daniel Grossmann-Kavanagh
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_urldispatcher.py
Expand Up @@ -617,7 +617,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._prefix + "/") and path != self._prefix:
return None, set()

if method not in allowed_methods:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_web_urldispatcher.py
Expand Up @@ -458,3 +458,18 @@ async def test_static_absolute_url(aiohttp_client: Any, tmp_path: Any) -> None:
client = await aiohttp_client(app)
resp = await client.get("/static/" + str(file_path.resolve()))
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)
asvetlov marked this conversation as resolved.
Show resolved Hide resolved

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