From e4079aa746e861c5d39f2812520e33c4d17d1007 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 14 Jul 2012 16:55:21 +1200 Subject: [PATCH] Add an ~a filter expression, matching an asset content type in responses. Asset content types are Javascript, images, Flash and CSS. This is useful because doing a quick "!~a" while auditing an app will filter out the majority of the static asset cruft, letting you focus on what matters. --- libmproxy/filt.py | 27 ++++++++++++++++++++++++++- test/test_filt.py | 6 ++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/libmproxy/filt.py b/libmproxy/filt.py index 4314f2d59..233cbb62e 100644 --- a/libmproxy/filt.py +++ b/libmproxy/filt.py @@ -24,6 +24,12 @@ Patterns are matched against "name: value" strings. Field names are all-lowercase. + ~a Asset content-type in response. Asset content types are: + text/javascript + application/x-javascript + text/css + image/* + application/x-shockwave-flash ~h rex Header line in either request or response ~hq rex Header in request ~hs rex Header in response @@ -95,6 +101,24 @@ def _check_content_type(expr, o): return False +class FAsset(_Action): + code = "a" + help = "Match asset in response: CSS, Javascript, Flash, images." + ASSET_TYPES = [ + "text/javascript", + "application/x-javascript", + "text/css", + "image/.*", + "application/x-shockwave-flash" + ] + def __call__(self, f): + if f.response: + for i in self.ASSET_TYPES: + if _check_content_type(i, f.response): + return True + return False + + class FContentType(_Rex): code = "t" help = "Content-type header" @@ -258,6 +282,7 @@ class FNot(_Token): filt_unary = [ FReq, FResp, + FAsset, FErr ] filt_rex = [ @@ -322,7 +347,7 @@ bnf = _make() def parse(s): try: return bnf.parseString(s, parseAll=True)[0] - except pp.ParseException: + except pp.ParseException, v: return None except ValueError: return None diff --git a/test/test_filt.py b/test/test_filt.py index 1cea34c41..4e059196d 100644 --- a/test/test_filt.py +++ b/test/test_filt.py @@ -112,6 +112,12 @@ class TestMatching: def q(self, q, o): return filt.parse(q)(o) + def test_asset(self): + s = self.resp() + assert not self.q("~a", s) + s.response.headers["content-type"] = ["text/javascript"] + assert self.q("~a", s) + def test_fcontenttype(self): q = self.req() s = self.resp()