Skip to content

Commit

Permalink
sanitize passed variables, handle errors from network better, first p…
Browse files Browse the repository at this point in the history
…ass at keywork conflict issues
  • Loading branch information
mahtin committed Jul 28, 2021
1 parent c6b7423 commit 7a0d715
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion CloudFlare/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import json
import requests
import keyword

from .network import CFnetwork
from .logging_helper import CFlogger
Expand Down Expand Up @@ -48,6 +49,15 @@ def __init__(self, config):
self.network = CFnetwork(use_sessions=self.use_sessions)
self.user_agent = user_agent()

if not isinstance(self.email, str):
raise ValueError('email argument not string')
if not isinstance(self.token, str):
raise ValueError('token argument not string')
if not isinstance(self.certtoken, str):
raise ValueError('certtoken argument not string')
if not isinstance(self.base_url, str):
raise ValueError('base url argument not string')

if 'debug' in config and config['debug']:
self.logger = CFlogger(config['debug']).getLogger()
else:
Expand Down Expand Up @@ -201,10 +211,22 @@ def _call_network(self, method, headers, parts,

try:
response = self.network(method, url, headers, params, data, files)
except requests.RequestException as e:
if self.logger:
self.logger.debug('Call: requests exception! "%s"' % (e))
raise CloudFlareAPIError(0, e)
except requests.ConnectionError as e:
if self.logger:
self.logger.debug('Call: requests connection exception! "%s"' % (e))
raise CloudFlareAPIError(0, 'connection error')
except requests.exceptions.Timeout as e:
if self.logger:
self.logger.debug('Call: requests timeout exception! "%s"' % (e))
raise CloudFlareAPIError(0, 'connection timeout')
except Exception as e:
if self.logger:
self.logger.debug('Call: exception! "%s"' % (e))
raise CloudFlareAPIError(0, 'connection failed.')
raise

# Create response_{type|code|data}
try:
Expand Down Expand Up @@ -859,6 +881,9 @@ def add(self, t, p1, p2=None, p3=None):
# should never happen
raise CloudFlareAPIError(0, 'api load type mismatch')

if keyword.iskeyword(name):
## add an extra keywork prefix'ed with underscore so it can used with Python code
setattr(branch, name + '_', f)
if '-' in name:
# dashes (vs underscores) cause issues in Python and other languages
setattr(branch, name.replace('-','_'), f)
Expand Down

0 comments on commit 7a0d715

Please sign in to comment.