Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
Don't traceback on invalid base64 in HAR files
Browse files Browse the repository at this point in the history
  • Loading branch information
vfaronov committed Jul 30, 2016
1 parent 04fce37 commit 723869e
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 7 deletions.
21 changes: 14 additions & 7 deletions httpolice/inputs/har.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,21 @@ def _process_response(data, req, creator, path):

if data['content'].get('text') and status != st.not_modified:
if data['content'].get('encoding', u'').lower() == u'base64':
decoded_body = base64.b64decode(data['content']['text'])
if creator in FIDDLER and req.method == m.CONNECT and \
status.successful and b'Fiddler' in decoded_body:
# Fiddler's HAR export adds a body with debug information
# to CONNECT responses.
resp.body = b''
try:
decoded_body = base64.b64decode(data['content']['text'])
except ValueError:
# Firefox sometimes marks normal, unencoded text as "base64"
# (see ``test/har_data/firefox_gif.har``).
# But let's not try to guess.
pass
else:
resp.decoded_body = decoded_body
if creator in FIDDLER and req.method == m.CONNECT and \
status.successful and b'Fiddler' in decoded_body:
# Fiddler's HAR export adds a body with debug information
# to CONNECT responses.
resp.body = b''
else:
resp.decoded_body = decoded_body

elif 'encoding' not in data['content']:
resp.unicode_body = data['content']['text']
Expand Down
175 changes: 175 additions & 0 deletions test/har_data/firefox_gif.har
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{
"_expected": [],
"log": {
"version": "1.1",
"creator": {
"name": "Firefox",
"version": "47.0"
},
"browser": {
"name": "Firefox",
"version": "47.0"
},
"pages": [
{
"startedDateTime": "2016-07-30T11:24:10.237+03:00",
"id": "page_1",
"title": "New Tab",
"pageTimings": {
"onContentLoad": -1,
"onLoad": -1
}
}
],
"entries": [
{
"pageref": "page_1",
"startedDateTime": "2016-07-30T11:24:10.237+03:00",
"time": 425,
"request": {
"bodySize": 0,
"method": "GET",
"url": "http://idsync.rlcdn.com/377928.gif?partner_uid=31bce42a6e6bea362c1bc6dd70dae50c",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "Host",
"value": "idsync.rlcdn.com"
},
{
"name": "User-Agent",
"value": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0"
},
{
"name": "Accept",
"value": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.5"
},
{
"name": "Accept-Encoding",
"value": "gzip, deflate"
},
{
"name": "Cookie",
"value": "ck1=ck1; rlas3=\"rFNX3AxGY91/VR00MXqNXcmwbwLeVqF7ZLvO1Y2NDUSMyA9jEACu5w==\"; rtn1=6277049d9e63b978dea8b913b3d0ae1887cc235a56d95e0307ae318cb3bb551abdc9eb6fa95486ca17f01efd945183a9; dids203877564=59ef54b0382c99bfa216cbac0130c3220f0fd28e7eb08744dbee649f544abc2fd10684f9ab9dca5883fd3f94c100f4448b3909a05a07994fb9c155552bdc07d085b9ac3a917e0ea7dec644a53399d2dc6aa61cd01034d2e597a77392f17324640d9e683aeef6faf2e5b3f13da5194624e96d0195504724c38f623ea7fef09d0599145c65a3afad26aa10389b2bdce2e12705cc42d21ebc93e4f4055cdcd8c570d365c92b2757e512d2ccdcfe43cbd91a0c0cd2660bf84cea8cc80f631000aee7"
},
{
"name": "Connection",
"value": "keep-alive"
}
],
"cookies": [
{
"name": "ck1",
"value": "ck1"
},
{
"name": "rlas3",
"value": "\"rFNX3AxGY91/VR00MXqNXcmwbwLeVqF7ZLvO1Y2NDUSMyA9jEACu5w==\""
},
{
"name": "rtn1",
"value": "6277049d9e63b978dea8b913b3d0ae1887cc235a56d95e0307ae318cb3bb551abdc9eb6fa95486ca17f01efd945183a9"
},
{
"name": "dids203877564",
"value": "59ef54b0382c99bfa216cbac0130c3220f0fd28e7eb08744dbee649f544abc2fd10684f9ab9dca5883fd3f94c100f4448b3909a05a07994fb9c155552bdc07d085b9ac3a917e0ea7dec644a53399d2dc6aa61cd01034d2e597a77392f17324640d9e683aeef6faf2e5b3f13da5194624e96d0195504724c38f623ea7fef09d0599145c65a3afad26aa10389b2bdce2e12705cc42d21ebc93e4f4055cdcd8c570d365c92b2757e512d2ccdcfe43cbd91a0c0cd2660bf84cea8cc80f631000aee7"
}
],
"queryString": [
{
"name": "partner_uid",
"value": "31bce42a6e6bea362c1bc6dd70dae50c"
}
],
"postData": {
"mimeType": "",
"params": [],
"text": ""
},
"headersSize": 935
},
"response": {
"status": 200,
"statusText": "OK",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "Cache-Control",
"value": "no-cache, no-store"
},
{
"name": "Connection",
"value": "keep-alive"
},
{
"name": "Content-Length",
"value": "43"
},
{
"name": "Content-Type",
"value": "image/gif; charset=ISO-8859-1"
},
{
"name": "Date",
"value": "Sat, 30 Jul 2016 08:24:10 GMT"
},
{
"name": "Expires",
"value": "Thu, 01 Jan 1970 00:00:00 GMT"
},
{
"name": "P3P",
"value": "CP: \"NON DSP COR PSDo SAMo BUS IND UNI COM NAV INT POL PRE\""
},
{
"name": "Set-Cookie",
"value": "rlas3=\"xRh2uQaTjLoawRdcNPAWqsmwbwLeVqF7ZLvO1Y2NDUSMyA9jEACu5w==\";Version=1;Domain=.rlcdn.com;Expires=Thu, 26-Jan-2017 08:24:09 GMT;Max-Age=15551999\nrtn1=6277049d9e63b978dea8b913b3d0ae1887cc235a56d95e0307ae318cb3bb551abdc9eb6fa95486ca17f01efd945183a9;Domain=.rlcdn.com;Expires=Thu, 26-Jan-2017 08:24:10 GMT\ndids203877564=;Domain=.rlcdn.com;Expires=Thu, 01-Jan-1970 00:00:00 GMT\ndids452903882=59ef54b0382c99bfa216cbac0130c3220f0fd28e7eb08744dbee649f544abc2fd10684f9ab9dca5883fd3f94c100f4448b3909a05a07994fb9c155552bdc07d085b9ac3a917e0ea7dec644a53399d2dc6aa61cd01034d2e597a77392f17324640d9e683aeef6faf2e5b3f13da5194624e96d0195504724c38f623ea7fef09d0599145c65a3afad26aa10389b2bdce2e128d3a19ae368e46ee4f4055cdcd8c570d365c92b2757e512d2ccdcfe43cbd91a0c0cd2660bf84cea8cc80f631000aee7;Domain=.rlcdn.com;Expires=Thu, 26-Jan-2017 08:24:10 GMT"
}
],
"cookies": [
{
"name": "rlas3",
"value": "\"xRh2uQaTjLoawRdcNPAWqsmwbwLeVqF7ZLvO1Y2NDUSMyA9jEACu5w==\""
},
{
"name": "rtn1",
"value": "6277049d9e63b978dea8b913b3d0ae1887cc235a56d95e0307ae318cb3bb551abdc9eb6fa95486ca17f01efd945183a9"
},
{
"name": "dids203877564",
"value": ""
},
{
"name": "dids452903882",
"value": "59ef54b0382c99bfa216cbac0130c3220f0fd28e7eb08744dbee649f544abc2fd10684f9ab9dca5883fd3f94c100f4448b3909a05a07994fb9c155552bdc07d085b9ac3a917e0ea7dec644a53399d2dc6aa61cd01034d2e597a77392f17324640d9e683aeef6faf2e5b3f13da5194624e96d0195504724c38f623ea7fef09d0599145c65a3afad26aa10389b2bdce2e128d3a19ae368e46ee4f4055cdcd8c570d365c92b2757e512d2ccdcfe43cbd91a0c0cd2660bf84cea8cc80f631000aee7"
}
],
"content": {
"mimeType": "image/gif; charset=ISO-8859-1",
"size": 43,
"encoding": "base64",
"text": "GIF89a\u0001\u0000\u0001\u0000\u0000\u0000ÿÿÿÿÿÿ!ù\u0004\u0001\n\u0000\u0001\u0000,\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u0000\u0000\u0002\u0002L\u0001\u0000;"
},
"redirectURL": "",
"headersSize": 1131,
"bodySize": 43
},
"cache": {},
"timings": {
"blocked": 0,
"dns": 0,
"connect": 220,
"send": 0,
"wait": 205,
"receive": 0
},
"serverIPAddress": "52.21.125.219",
"connection": "80"
}
]
}
}
6 changes: 6 additions & 0 deletions test/test_har_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ def test_firefox_multiple_set_cookie():
assert cookie2 == b'baz=qux'


def test_firefox_gif():
exchanges = load_from_file('firefox_gif.har')
assert exchanges[0].responses[0].body is Unavailable
assert exchanges[0].responses[0].decoded_body is Unavailable


def test_fiddler_connect():
exchanges = load_from_file('fiddler+ie11_connect.har')
assert exchanges[0].request.target == u'httpbin.org:443'
Expand Down

0 comments on commit 723869e

Please sign in to comment.