Skip to content

Commit

Permalink
Cosmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
andresriancho committed Nov 29, 2019
1 parent 0c9bd0f commit 02efaca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
38 changes: 19 additions & 19 deletions w3af/core/data/url/handlers/blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"""
import urllib
import urllib2
import cStringIO
import mimetools
import cStringIO

import w3af.core.controllers.output_manager as om
import w3af.core.data.kb.config as cf
Expand Down Expand Up @@ -54,15 +54,15 @@ def default_open(self, req):
needs to be called. With this we want to indicate that the keepalive
handler will be called.
"""
if self._is_blacklisted(req.url_object):
nncr = new_no_content_resp(req.url_object)
addinfo_inst = http_response_to_httplib(nncr)
return addinfo_inst

# This means: I don't know how to handle this, call the next opener
return None
if not self._is_blacklisted(req.url_object):
# This means: I don't know how to handle this, call the next opener
return None

# Return a 204 response
no_content = new_no_content_resp(req.url_object)
no_content = http_response_to_httplib(no_content)
return no_content

def _is_blacklisted(self, uri):
"""
If the user configured w3af to ignore a URL, we are going to be applying
Expand All @@ -71,20 +71,20 @@ def _is_blacklisted(self, uri):
if uri.uri2url() in self._blacklist_urls:
msg = ('%s was included in the HTTP request blacklist, the scan'
' engine is NOT sending the HTTP request and is instead'
' returning an empty response to the caller.')
' returning an empty response to the plugin.')
om.out.debug(msg % uri)
return True

return False


def http_response_to_httplib(nncr):
header_string = cStringIO.StringIO(str(nncr.get_headers()))
def http_response_to_httplib(no_content):
header_string = cStringIO.StringIO(str(no_content.get_headers()))
headers = mimetools.Message(header_string)

addinfo_inst = urllib.addinfourl(cStringIO.StringIO(nncr.get_body()),
headers,
nncr.get_url().url_string,
code=nncr.get_code())
addinfo_inst.msg = 'No content'
return addinfo_inst
no_content = urllib.addinfourl(cStringIO.StringIO(no_content.get_body()),
headers,
no_content.get_url().url_string,
code=no_content.get_code())
no_content.msg = 'No content'
return no_content
22 changes: 9 additions & 13 deletions w3af/core/data/url/handlers/tests/test_blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from nose.plugins.attrib import attr

import w3af.core.data.kb.config as cf

from w3af.core.controllers.misc.number_generator import consecutive_number_generator
from w3af.core.controllers.ci.moth import get_moth_http
from w3af.core.data.parsers.doc.url import URL
Expand All @@ -43,8 +44,6 @@ def tearDown(self):
cf.cf.save('blacklist_http_request', [])

def test_blacklist_handler_block(self):
"""Verify that the blacklist handler works as expected"""

# Configure the handler
blocked_url = URL(get_moth_http('/abc/def/'))
cf.cf.save('blacklist_http_request', [blocked_url])
Expand All @@ -59,22 +58,20 @@ def test_blacklist_handler_block(self):

@attr('moth')
def test_blacklist_handler_pass(self):
"""Verify that the blacklist handler works as expected"""
opener = urllib2.build_opener(BlacklistHandler)

request = urllib2.Request(get_moth_http())
request.url_object = URL(get_moth_http())
response = opener.open(request)

self.assertEqual(response.code, 200)

def test_handler_order_block(self):
"""Get an instance of the extended urllib and verify that the blacklist
handler still works, even when mixed with all the other handlers."""
# Configure the handler
blocked_url = URL(get_moth_http('/abc/def/'))
cf.cf.save('blacklist_http_request', [blocked_url])


# Get an instance of the extended urllib and verify that the blacklist
# handler still works, even when mixed with all the other handlers.
settings = opener_settings.OpenerSettings()
settings.build_openers()
opener = settings.get_custom_opener()
Expand All @@ -90,13 +87,12 @@ def test_handler_order_block(self):

@attr('moth')
def test_handler_order_pass(self):
"""Get an instance of the extended urllib and verify that the blacklist
handler still works, even when mixed with all the other handlers."""
# Configure the handler
blocked_url = URL(get_moth_http('/abc/def/'))
safe_url = URL(get_moth_http())
cf.cf.save('blacklist_http_request', [blocked_url])


# Get an instance of the extended urllib and verify that the blacklist
# handler still works, even when mixed with all the other handlers.
settings = opener_settings.OpenerSettings()
settings.build_openers()
opener = settings.get_custom_opener()
Expand All @@ -108,4 +104,4 @@ def test_handler_order_pass(self):
response = opener.open(request)

self.assertEqual(response.code, 200)
self.assertEqual(response.id, 1)
self.assertEqual(response.id, 1)

0 comments on commit 02efaca

Please sign in to comment.