From d027891cec67e190403fc4fa73f17d7a74f02720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Lang?= Date: Sun, 13 Jan 2019 23:27:43 -0300 Subject: Fix command injection when exporting to curl The command generated by `export.clip curl @focus` or `export.file curl @focus /path/to/file` wasn't being properly escaped so it could contain a malicious command instead of just a simple curl. --- test/mitmproxy/addons/test_export.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/addons/test_export.py b/test/mitmproxy/addons/test_export.py index f4bb0f64..5c365135 100644 --- a/test/mitmproxy/addons/test_export.py +++ b/test/mitmproxy/addons/test_export.py @@ -1,4 +1,5 @@ import os +import shlex import pytest import pyperclip @@ -47,23 +48,40 @@ def tcp_flow(): class TestExportCurlCommand: def test_get(self, get_request): - result = """curl -H 'header:qvalue' -H 'content-length:0' 'http://address:22/path?a=foo&a=bar&b=baz'""" + result = """curl -H header:qvalue -H content-length:0 'http://address:22/path?a=foo&a=bar&b=baz'""" assert export.curl_command(get_request) == result def test_post(self, post_request): - result = "curl -H 'content-length:256' -X POST 'http://address:22/path' --data-binary '{}'".format( - str(bytes(range(256)))[2:-1] - ) + post_request.request.content = b'nobinarysupport' + result = "curl -H content-length:15 -X POST http://address:22/path --data-binary nobinarysupport" assert export.curl_command(post_request) == result + def test_fails_with_binary_data(self, post_request): + # shlex.quote doesn't support a bytes object + # see https://github.com/python/cpython/pull/10871 + with pytest.raises(exceptions.CommandError): + export.curl_command(post_request) + def test_patch(self, patch_request): - result = """curl -H 'header:qvalue' -H 'content-length:7' -X PATCH 'http://address:22/path?query=param' --data-binary 'content'""" + result = """curl -H header:qvalue -H content-length:7 -X PATCH 'http://address:22/path?query=param' --data-binary content""" assert export.curl_command(patch_request) == result def test_tcp(self, tcp_flow): with pytest.raises(exceptions.CommandError): export.curl_command(tcp_flow) + def test_escape_single_quotes_in_body(self): + request = tflow.tflow( + req=tutils.treq( + method=b'POST', + headers=(), + content=b"'&#" + ) + ) + command = export.curl_command(request) + assert shlex.split(command)[-2] == '--data-binary' + assert shlex.split(command)[-1] == "'&#" + class TestExportHttpieCommand: def test_get(self, get_request): -- cgit v1.2.3 From eab4174b87c7ba0b7dab2c8d7e0b13253833abe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Lang?= Date: Sun, 13 Jan 2019 23:45:28 -0300 Subject: Fix command injection when exporting to httpie The command generated by `export.clip httpie @focus` or `export.file httpie @focus /path/to/file` wasn't being properly escaped so it could contain a malicious command instead of just a simple httpie call. --- test/mitmproxy/addons/test_export.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/addons/test_export.py b/test/mitmproxy/addons/test_export.py index 5c365135..67d0fc99 100644 --- a/test/mitmproxy/addons/test_export.py +++ b/test/mitmproxy/addons/test_export.py @@ -85,23 +85,40 @@ class TestExportCurlCommand: class TestExportHttpieCommand: def test_get(self, get_request): - result = """http GET http://address:22/path?a=foo&a=bar&b=baz 'header:qvalue' 'content-length:0'""" + result = """http GET 'http://address:22/path?a=foo&a=bar&b=baz' header:qvalue content-length:0""" assert export.httpie_command(get_request) == result def test_post(self, post_request): - result = "http POST http://address:22/path 'content-length:256' <<< '{}'".format( - str(bytes(range(256)))[2:-1] - ) + post_request.request.content = b'nobinarysupport' + result = "http POST http://address:22/path content-length:15 <<< nobinarysupport" assert export.httpie_command(post_request) == result + def test_fails_with_binary_data(self, post_request): + # shlex.quote doesn't support a bytes object + # see https://github.com/python/cpython/pull/10871 + with pytest.raises(exceptions.CommandError): + export.httpie_command(post_request) + def test_patch(self, patch_request): - result = """http PATCH http://address:22/path?query=param 'header:qvalue' 'content-length:7' <<< 'content'""" + result = """http PATCH 'http://address:22/path?query=param' header:qvalue content-length:7 <<< content""" assert export.httpie_command(patch_request) == result def test_tcp(self, tcp_flow): with pytest.raises(exceptions.CommandError): export.httpie_command(tcp_flow) + def test_escape_single_quotes_in_body(self): + request = tflow.tflow( + req=tutils.treq( + method=b'POST', + headers=(), + content=b"'&#" + ) + ) + command = export.httpie_command(request) + assert shlex.split(command)[-2] == '<<<' + assert shlex.split(command)[-1] == "'&#" + class TestRaw: def test_get(self, get_request): -- cgit v1.2.3 From 01ddda75e8aaab542d2c11b9bc9218a94a342f10 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 15 Nov 2019 19:02:59 +0100 Subject: improve curl/httpie export --- test/mitmproxy/addons/test_export.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/addons/test_export.py b/test/mitmproxy/addons/test_export.py index a4c10c1a..1c16afb2 100644 --- a/test/mitmproxy/addons/test_export.py +++ b/test/mitmproxy/addons/test_export.py @@ -42,22 +42,23 @@ def tcp_flow(): class TestExportCurlCommand: def test_get(self, get_request): - result = """curl -H header:qvalue 'http://address:22/path?a=foo&a=bar&b=baz'""" + result = """curl -H 'header: qvalue' 'http://address:22/path?a=foo&a=bar&b=baz'""" assert export.curl_command(get_request) == result def test_post(self, post_request): post_request.request.content = b'nobinarysupport' - result = "curl -H content-length:15 -X POST http://address:22/path --data-binary nobinarysupport" + result = "curl -X POST http://address:22/path -d nobinarysupport" assert export.curl_command(post_request) == result def test_fails_with_binary_data(self, post_request): # shlex.quote doesn't support a bytes object # see https://github.com/python/cpython/pull/10871 + post_request.request.headers["Content-Type"] = "application/json; charset=utf-8" with pytest.raises(exceptions.CommandError): export.curl_command(post_request) def test_patch(self, patch_request): - result = """curl -H header:qvalue -H content-length:7 -X PATCH 'http://address:22/path?query=param' --data-binary content""" + result = """curl -H 'header: qvalue' -X PATCH 'http://address:22/path?query=param' -d content""" assert export.curl_command(patch_request) == result def test_tcp(self, tcp_flow): @@ -73,28 +74,29 @@ class TestExportCurlCommand: ) ) command = export.curl_command(request) - assert shlex.split(command)[-2] == '--data-binary' + assert shlex.split(command)[-2] == '-d' assert shlex.split(command)[-1] == "'&#" class TestExportHttpieCommand: def test_get(self, get_request): - result = """http GET 'http://address:22/path?a=foo&a=bar&b=baz' header:qvalue""" + result = """http GET 'http://address:22/path?a=foo&a=bar&b=baz' 'header: qvalue'""" assert export.httpie_command(get_request) == result def test_post(self, post_request): post_request.request.content = b'nobinarysupport' - result = "http POST http://address:22/path content-length:15 <<< nobinarysupport" + result = "http POST http://address:22/path <<< nobinarysupport" assert export.httpie_command(post_request) == result def test_fails_with_binary_data(self, post_request): # shlex.quote doesn't support a bytes object # see https://github.com/python/cpython/pull/10871 + post_request.request.headers["Content-Type"] = "application/json; charset=utf-8" with pytest.raises(exceptions.CommandError): export.httpie_command(post_request) def test_patch(self, patch_request): - result = """http PATCH 'http://address:22/path?query=param' header:qvalue content-length:7 <<< content""" + result = """http PATCH 'http://address:22/path?query=param' 'header: qvalue' <<< content""" assert export.httpie_command(patch_request) == result def test_tcp(self, tcp_flow): -- cgit v1.2.3 From 484e099eb10897b89d83d4b0441b4455faab9422 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 15 Nov 2019 20:57:03 +0100 Subject: test coverage++ --- test/mitmproxy/addons/test_export.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test') diff --git a/test/mitmproxy/addons/test_export.py b/test/mitmproxy/addons/test_export.py index 1c16afb2..4b905722 100644 --- a/test/mitmproxy/addons/test_export.py +++ b/test/mitmproxy/addons/test_export.py @@ -77,6 +77,14 @@ class TestExportCurlCommand: assert shlex.split(command)[-2] == '-d' assert shlex.split(command)[-1] == "'&#" + def test_strip_unnecessary(self, get_request): + get_request.request.headers.clear() + get_request.request.headers["host"] = "address" + get_request.request.headers[":authority"] = "address" + get_request.request.headers["accept-encoding"] = "br" + result = """curl --compressed 'http://address:22/path?a=foo&a=bar&b=baz'""" + assert export.curl_command(get_request) == result + class TestExportHttpieCommand: def test_get(self, get_request): -- cgit v1.2.3