2424
2525import urllib2
2626
27+ from lib .core .exception import sqlmapConnectionException
28+
2729class SmartRedirectHandler (urllib2 .HTTPRedirectHandler ):
30+ # maximum number of redirections to any single URL
31+ # this is needed because of the state that cookies introduce
32+ max_repeats = 4
33+
34+ # maximum total number of redirections (regardless of URL) before
35+ # assuming we're in a loop
36+ max_redirections = 10
37+
2838 def common_http_redirect (self , result , headers , code ):
2939 if "location" in headers :
3040 result .redurl = headers .getheaders ("location" )[0 ].split ("?" )[0 ]
@@ -36,9 +46,17 @@ def common_http_redirect(self, result, headers, code):
3646 return result
3747
3848 def http_error_301 (self , req , fp , code , msg , headers ):
49+ self .infinite_loop_check (req )
3950 result = urllib2 .HTTPRedirectHandler .http_error_301 (self , req , fp , code , msg , headers )
4051 return self .common_http_redirect (result , headers , code )
4152
4253 def http_error_302 (self , req , fp , code , msg , headers ):
54+ self .infinite_loop_check (req )
4355 result = urllib2 .HTTPRedirectHandler .http_error_302 (self , req , fp , code , msg , headers )
4456 return self .common_http_redirect (result , headers , code )
57+
58+ def infinite_loop_check (self , req ):
59+ if hasattr (req , 'redirect_dict' ) and (req .redirect_dict .get (req .get_full_url (), 0 ) >= self .max_repeats or len (req .redirect_dict ) >= self .max_redirections ):
60+ errMsg = "infinite redirect loop detected (%s). " % ", " .join (item for item in req .redirect_dict .keys ())
61+ errMsg += "please check all provided parameters and/or provide missing ones."
62+ raise sqlmapConnectionException , errMsg
0 commit comments