Skip to content

Commit

Permalink
Make test and patch for when recaptcha challenge is a checkbox.
Browse files Browse the repository at this point in the history
Add method html_is_recaptcha() in IncapSession to be able to test whether the re-captcha challenge is being detected.

Fix issue in IncapSession.incap_recaptcha_blocked where self.get() was using an invalid keyword argument.
  • Loading branch information
ziplokk1 committed May 25, 2017
1 parent 1aab6d4 commit 629e9a9
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions incapsula/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ def incap_blocked(self, content):
incap_iframe = soup.find('iframe', {'src': re.compile('^/_Incapsula_Resource.*')})
return robots_tag and incap_iframe

def html_is_recaptcha(self, html_string):
"""
Check if the html contains recaptcha robot prevention.
:param html_string:
:return:
"""
soup = BeautifulSoup(html_string, 'html.parser')
return bool(soup.find('form', {'id': 'captcha-form'}) or soup.find('div', {'class': 'g-recaptcha'}))

def incap_recaptcha_blocked(self, scheme, host, content):
"""
Get the content from the iframe in a blocked request to determine if the block contains a captcha.
Expand All @@ -238,12 +248,10 @@ def incap_recaptcha_blocked(self, scheme, host, content):

# Send request to get the content of the iframe.
iframe_url = incap_iframe.get('src')
resource = self.get(scheme + '://' + host + iframe_url, incap=True)
resource = self.get(scheme + '://' + host + iframe_url, bypass_crack=True)

# If the element below is found, then the iframe content is for a recaptcha and there's no way around that.
soup = BeautifulSoup(resource.content, 'html.parser')
is_recaptcha = bool(soup.find('form', {'id': 'captcha-form'}))
return is_recaptcha
# If the below method is true, then the iframe content is for a recaptcha and there's no way around that.
return self.html_is_recaptcha(resource.content)

def crack(self, resp, org=None, tries=0):
# Use to hold the original request so that when attempting the new unblocked request, we have a reference
Expand Down

0 comments on commit 629e9a9

Please sign in to comment.