Skip to content

Commit

Permalink
Added exception handling for invalid setCookie commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Meenan committed Jul 22, 2020
1 parent e306739 commit 97803c6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 63 deletions.
27 changes: 15 additions & 12 deletions internal/devtools_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,18 +508,21 @@ def process_command(self, command):
self.task['user_agent_string'] = command['target']
elif command['command'] == 'setcookie':
if 'target' in command and 'value' in command:
url = command['target'].strip()
cookie = command['value']
pos = cookie.find(';')
if pos > 0:
cookie = cookie[:pos]
pos = cookie.find('=')
if pos > 0:
name = cookie[:pos].strip()
value = cookie[pos + 1:].strip()
if len(name) and len(value) and len(url):
self.devtools.send_command('Network.setCookie',
{'url': url, 'name': name, 'value': value})
try:
url = command['target'].strip()
cookie = command['value']
pos = cookie.find(';')
if pos > 0:
cookie = cookie[:pos]
pos = cookie.find('=')
if pos > 0:
name = cookie[:pos].strip()
value = cookie[pos + 1:].strip()
if len(name) and len(value) and len(url):
self.devtools.send_command('Network.setCookie',
{'url': url, 'name': name, 'value': value})
except Exception:
logging.exception('Error setting cookie')
elif command['command'] == 'setlocation':
try:
if 'target' in command and command['target'].find(',') > 0:
Expand Down
53 changes: 28 additions & 25 deletions internal/microsoft_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,31 +886,34 @@ def process_command(self, command):
self.task['user_agent_string'] = command['target']
elif command['command'] == 'setcookie':
if 'target' in command and 'value' in command:
url = command['target'].strip()
cookie = command['value']
pos = cookie.find(';')
if pos > 0:
cookie = cookie[:pos]
pos = cookie.find('=')
if pos > 0:
name = cookie[:pos].strip()
value = cookie[pos+1:].strip()
if len(name) and len(value) and len(url):
try:
self.driver.add_cookie({'url': url, 'name': name, 'value': value})
except Exception:
logging.exception('Error adding cookie')
try:
import win32inet # pylint: disable=import-error
cookie_string = cookie
if cookie.find('xpires') == -1:
expires = datetime.utcnow() + timedelta(days=30)
expires_string = expires.strftime("%a, %d %b %Y %H:%M:%S GMT")
cookie_string += '; expires={0}'.format(expires_string)
logging.debug("Setting cookie: %s", cookie_string)
win32inet.InternetSetCookie(url, None, cookie_string)
except Exception as err:
logging.exception("Error setting cookie: %s", str(err))
try:
url = command['target'].strip()
cookie = command['value']
pos = cookie.find(';')
if pos > 0:
cookie = cookie[:pos]
pos = cookie.find('=')
if pos > 0:
name = cookie[:pos].strip()
value = cookie[pos+1:].strip()
if len(name) and len(value) and len(url):
try:
self.driver.add_cookie({'url': url, 'name': name, 'value': value})
except Exception:
logging.exception('Error adding cookie')
try:
import win32inet # pylint: disable=import-error
cookie_string = cookie
if cookie.find('xpires') == -1:
expires = datetime.utcnow() + timedelta(days=30)
expires_string = expires.strftime("%a, %d %b %Y %H:%M:%S GMT")
cookie_string += '; expires={0}'.format(expires_string)
logging.debug("Setting cookie: %s", cookie_string)
win32inet.InternetSetCookie(url, None, cookie_string)
except Exception as err:
logging.exception("Error setting cookie: %s", str(err))
except Exception:
logging.exception('Error setting cookie')

def navigate(self, url):
"""Navigate to the given URL"""
Expand Down
25 changes: 14 additions & 11 deletions internal/safari_ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,17 +1159,20 @@ def process_command(self, command):
self.task['user_agent_string'] = command['target']
elif command['command'] == 'setcookie':
if 'target' in command and 'value' in command:
url = command['target'].strip()
cookie = command['value']
pos = cookie.find(';')
if pos > 0:
cookie = cookie[:pos]
pos = cookie.find('=')
if pos > 0:
name = cookie[:pos].strip()
value = cookie[pos + 1:].strip()
if len(name) and len(value) and len(url):
self.ios.set_cookie(url, name, value)
try:
url = command['target'].strip()
cookie = command['value']
pos = cookie.find(';')
if pos > 0:
cookie = cookie[:pos]
pos = cookie.find('=')
if pos > 0:
name = cookie[:pos].strip()
value = cookie[pos + 1:].strip()
if len(name) and len(value) and len(url):
self.ios.set_cookie(url, name, value)
except Exception:
logging.exception('Error setting cookie')
elif command['command'] == 'clearcache':
self.ios.clear_cache()
elif command['command'] == 'addheader':
Expand Down
33 changes: 18 additions & 15 deletions internal/webpagetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,21 +710,24 @@ def build_script(self, job, task):
task['overrideHosts'] = {}
task['overrideHosts'][target] = value
elif command == 'setcookie' and target is not None and value is not None:
url = target
cookie = value
pos = cookie.find(';')
if pos > 0:
cookie = cookie[:pos]
pos = cookie.find('=')
if pos > 0:
cookie_name = cookie[:pos].strip()
cookie_value = cookie[pos + 1:].strip()
if len(cookie_name) and len(cookie_value) and len(url):
if 'cookies' not in task:
task['cookies'] = []
task['cookies'].append({'url': url,
'name': cookie_name,
'value': cookie_value})
try:
url = target
cookie = value
pos = cookie.find(';')
if pos > 0:
cookie = cookie[:pos]
pos = cookie.find('=')
if pos > 0:
cookie_name = cookie[:pos].strip()
cookie_value = cookie[pos + 1:].strip()
if len(cookie_name) and len(cookie_value) and len(url):
if 'cookies' not in task:
task['cookies'] = []
task['cookies'].append({'url': url,
'name': cookie_name,
'value': cookie_value})
except Exception:
logging.exception('Error setting cookie')
# commands that get pre-processed
elif command == 'setuseragent' and target is not None:
job['uastring'] = target
Expand Down

0 comments on commit 97803c6

Please sign in to comment.