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

Fix overshadowing of overlapped subapp prefixes (#3701). #3708

Merged
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/3701.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix overshadowing of overlapped subbaps prefixes.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Eric Sheng
Erich Healy
Eugene Chernyshov
Eugene Naydenov
Eugene Nikolaiev
Eugene Tolmachev
Evert Lammerts
FichteFoll
Expand Down
3 changes: 2 additions & 1 deletion aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@ def get_info(self) -> Dict[str, Any]:
'prefix': self._prefix}

async def resolve(self, request: Request) -> _Resolve:
if not request.url.raw_path.startswith(self._prefix):
if not request.url.raw_path.startswith(self._prefix + '/') and \
request.url.raw_path != self._prefix:
return None, set()
match_info = await self._app.router.resolve(request)
match_info.add_app(self._app)
Expand Down
44 changes: 44 additions & 0 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1246,3 +1246,47 @@ def test_prefixed_subapp_resource_canonical(app) -> None:
subapp = web.Application()
res = subapp.add_subapp(canonical, subapp)
assert res.canonical == canonical


async def test_prefixed_subapp_overlap(app) -> None:
"""
Subapp should not overshadow other subapps with overlapping prefixes
"""
subapp1 = web.Application()
handler1 = make_handler()
subapp1.router.add_get('/a', handler1)
app.add_subapp('/s', subapp1)

subapp2 = web.Application()
handler2 = make_handler()
subapp2.router.add_get('/b', handler2)
app.add_subapp('/ss', subapp2)

match_info = await app.router.resolve(make_mocked_request('GET', '/s/a'))
assert match_info.route.handler is handler1
match_info = await app.router.resolve(make_mocked_request('GET', '/ss/b'))
assert match_info.route.handler is handler2


async def test_prefixed_subapp_empty_route(app) -> None:
subapp = web.Application()
handler = make_handler()
subapp.router.add_get('', handler)
app.add_subapp('/s', subapp)

match_info = await app.router.resolve(make_mocked_request('GET', '/s'))
assert match_info.route.handler is handler
match_info = await app.router.resolve(make_mocked_request('GET', '/s/'))
assert "<MatchInfoError 404: Not Found>" == repr(match_info)


async def test_prefixed_subapp_root_route(app) -> None:
subapp = web.Application()
handler = make_handler()
subapp.router.add_get('/', handler)
app.add_subapp('/s', subapp)

match_info = await app.router.resolve(make_mocked_request('GET', '/s/'))
assert match_info.route.handler is handler
match_info = await app.router.resolve(make_mocked_request('GET', '/s'))
assert "<MatchInfoError 404: Not Found>" == repr(match_info)