Skip to content

Commit

Permalink
Fix bug in route name validation (#5051)
Browse files Browse the repository at this point in the history
* Fix bug in route name validation

* Add changelog

* Fix test end error msg

* Add test
  • Loading branch information
asvetlov committed Oct 16, 2020
1 parent e3945f3 commit 5692019
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/4691.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the register_resource function to validate route name before splitting it so that route name can include python keywords.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ Thomas Forbes
Thomas Grainger
Tolga Tezel
Tomasz Trebski
Toshiaki Tanaka
Trinh Hoang Nhu
Vadim Suharnikov
Vaibhav Sagar
Expand Down
6 changes: 5 additions & 1 deletion aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,11 @@ def register_resource(self, resource: AbstractResource) -> None:
if name is not None:
parts = self.NAME_SPLIT_RE.split(name)
for part in parts:
if not part.isidentifier() or keyword.iskeyword(part):
if keyword.iskeyword(part):
raise ValueError(f'Incorrect route name {name!r}, '
'python keywords cannot be used '
'for route name')
if not part.isidentifier():
raise ValueError('Incorrect route name {!r}, '
'the name should be a sequence of '
'python identifiers separated '
Expand Down
5 changes: 5 additions & 0 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,11 @@ def test_invalid_route_name(router) -> None:
router.add_get('/', make_handler(), name='invalid name')


def test_invalid_route_name(router) -> None:
with pytest.raises(ValueError):
router.add_get('/', make_handler(), name='class') # identifier


def test_frozen_router(router) -> None:
router.freeze()
with pytest.raises(RuntimeError):
Expand Down

0 comments on commit 5692019

Please sign in to comment.