@@ -528,6 +528,17 @@ def http_error_302(self, req, fp, code, msg, headers):
528528
529529 # fix a possible malformed URL
530530 urlparts = urlparse (newurl )
531+
532+ # For security reasons we don't allow redirection to anything other
533+ # than http, https or ftp.
534+
535+ if not urlparts .scheme in ('http' , 'https' , 'ftp' ):
536+ raise HTTPError (newurl , code ,
537+ msg +
538+ " - Redirection to url '%s' is not allowed" %
539+ newurl ,
540+ headers , fp )
541+
531542 if not urlparts .path :
532543 urlparts = list (urlparts )
533544 urlparts [2 ] = "/"
@@ -1864,8 +1875,24 @@ def redirect_internal(self, url, fp, errcode, errmsg, headers, data):
18641875 return
18651876 void = fp .read ()
18661877 fp .close ()
1878+
18671879 # In case the server sent a relative URL, join with original:
18681880 newurl = urljoin (self .type + ":" + url , newurl )
1881+
1882+ urlparts = urlparse (newurl )
1883+
1884+ # For security reasons, we don't allow redirection to anything other
1885+ # than http, https and ftp.
1886+
1887+ # We are using newer HTTPError with older redirect_internal method
1888+ # This older method will get deprecated in 3.3
1889+
1890+ if not urlparts .scheme in ('http' , 'https' , 'ftp' ):
1891+ raise HTTPError (newurl , errcode ,
1892+ errmsg +
1893+ " Redirection to url '%s' is not allowed." % newurl ,
1894+ headers , fp )
1895+
18691896 return self .open (newurl )
18701897
18711898 def http_error_301 (self , url , fp , errcode , errmsg , headers , data = None ):
0 commit comments