Skip to content

Commit

Permalink
routing match includes newline
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Mar 16, 2022
1 parent 365ee7e commit d761d0e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ Unreleased
instead of bytes. :pr:`2337`
- ``safe_join`` ensures that the path remains relative if the trusted
directory is the empty string. :pr:`2349`
- Percent-encoded newlines (``%0a``), which are decoded by WSGI
servers, are considered when routing instead of terminating the
match early. :pr:`2350`


Version 2.0.3
Expand Down
9 changes: 8 additions & 1 deletion src/werkzeug/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,11 @@ def foo_with_slug(adapter, id):
``wss://``) requests. By default, rules will only match for HTTP
requests.
.. versionchanged:: 2.1
Percent-encoded newlines (``%0a``), which are decoded by WSGI
servers, are considered when routing instead of terminating the
match early.
.. versionadded:: 1.0
Added ``websocket``.
Expand Down Expand Up @@ -892,7 +897,9 @@ def _build_regex(rule: str) -> None:
else:
tail = ""

regex = f"^{''.join(regex_parts)}{tail}$"
# Use \Z instead of $ to avoid matching before a %0a decoded to
# a \n by WSGI.
regex = rf"^{''.join(regex_parts)}{tail}$\Z"
self._regex = re.compile(regex)

def match(
Expand Down
8 changes: 8 additions & 0 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,3 +1346,11 @@ def test_rule_websocket_methods():
methods=["get", "head", "options", "post"],
)
r.Rule("/ws", endpoint="ws", websocket=True, methods=["get", "head", "options"])


def test_newline_match():
m = r.Map([r.Rule("/hello", endpoint="hello")])
a = m.bind("localhost")

with pytest.raises(r.NotFound):
a.match("/hello\n")

0 comments on commit d761d0e

Please sign in to comment.