Skip to content

Commit

Permalink
Merge pull request #2868 from kira0204/data-crash
Browse files Browse the repository at this point in the history
Fix crashing when editing form with random data, fix #2794
  • Loading branch information
mhils committed Mar 4, 2018
2 parents 618d52a + fb54bb3 commit a2740ee
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
7 changes: 2 additions & 5 deletions mitmproxy/net/http/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,7 @@ def constrain_encoding(self):
def _get_urlencoded_form(self):
is_valid_content_type = "application/x-www-form-urlencoded" in self.headers.get("content-type", "").lower()
if is_valid_content_type:
try:
return tuple(mitmproxy.net.http.url.decode(self.content.decode()))
except ValueError:
pass
return tuple(mitmproxy.net.http.url.decode(self.get_text(strict=False)))
return ()

def _set_urlencoded_form(self, form_data):
Expand All @@ -441,7 +438,7 @@ def _set_urlencoded_form(self, form_data):
This will overwrite the existing content if there is one.
"""
self.headers["content-type"] = "application/x-www-form-urlencoded"
self.content = mitmproxy.net.http.url.encode(form_data, self.content.decode()).encode()
self.content = mitmproxy.net.http.url.encode(form_data, self.get_text(strict=False)).encode()

@property
def urlencoded_form(self):
Expand Down
4 changes: 2 additions & 2 deletions test/mitmproxy/net/http/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,10 @@ def test_get_urlencoded_form(self):
request.headers["Content-Type"] = "application/x-www-form-urlencoded"
assert list(request.urlencoded_form.items()) == [("foobar", "baz")]
request.raw_content = b"\xFF"
assert len(request.urlencoded_form) == 0
assert len(request.urlencoded_form) == 1

def test_set_urlencoded_form(self):
request = treq()
request = treq(content=b"\xec\xed")
request.urlencoded_form = [('foo', 'bar'), ('rab', 'oof')]
assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"
assert request.content
Expand Down

0 comments on commit a2740ee

Please sign in to comment.