Skip to content

Commit

Permalink
Fix multipart parsing bug
Browse files Browse the repository at this point in the history
Escape special regex characters from the boundary before placing into
the regexs used to locate the boundaries in the multipart data.
  • Loading branch information
pgjones committed May 14, 2021
1 parent c3236f1 commit 3f8a95f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -12,6 +12,8 @@ Unreleased
- Fix some types that weren't available in Python 3.6.0. :issue:`2123`
- ``cached_property`` is generic over its return type, properties
decorated with it report the correct type. :issue:`2113`
- Fix multipart parsing bug when boundary contains special regex
characters. :issue:`2125`


Version 2.0.0
Expand Down
4 changes: 2 additions & 2 deletions src/werkzeug/sansio/multipart.py
Expand Up @@ -101,7 +101,7 @@ def __init__(
# group to understand if it is an epilogue boundary.
self.preamble_re = re.compile(
br"%s?--%s(--[^\S\n\r]*%s?|[^\S\n\r]*%s)"
% (LINE_BREAK, boundary, LINE_BREAK, LINE_BREAK),
% (LINE_BREAK, re.escape(boundary), LINE_BREAK, LINE_BREAK),
re.MULTILINE,
)
# A boundary must include a line break prefix and suffix, and
Expand All @@ -110,7 +110,7 @@ def __init__(
# understand if it is an epilogue boundary.
self.boundary_re = re.compile(
br"%s--%s(--[^\S\n\r]*%s?|[^\S\n\r]*%s)"
% (LINE_BREAK, boundary, LINE_BREAK, LINE_BREAK),
% (LINE_BREAK, re.escape(boundary), LINE_BREAK, LINE_BREAK),
re.MULTILINE,
)

Expand Down
8 changes: 4 additions & 4 deletions tests/sansio/test_multipart.py
Expand Up @@ -10,18 +10,18 @@


def test_decoder_simple() -> None:
boundary = b"---------------------------9704338192090380615194531385"
boundary = b"---------------------------9704338192090380615194531385$"
decoder = MultipartDecoder(boundary)
data = """
-----------------------------9704338192090380615194531385
-----------------------------9704338192090380615194531385$
Content-Disposition: form-data; name="fname"
ß∑œß∂ƒå∂
-----------------------------9704338192090380615194531385
-----------------------------9704338192090380615194531385$
Content-Disposition: form-data; name="lname"; filename="bob"
asdasd
-----------------------------9704338192090380615194531385--
-----------------------------9704338192090380615194531385$--
""".replace(
"\n", "\r\n"
).encode(
Expand Down

0 comments on commit 3f8a95f

Please sign in to comment.