Skip to content

Commit 5f55310

Browse files
committed
oauth: narrow on exception handling
It is generally not advisable to use "except:" in python, because it would catch "KeyboardInterrupt" and "SystemExit", meant only to be caught at main() level. Only catch the ones we really expect to see, let others propagate up the stack. Also, when computing the HMAC, we shall not retry w/o hashlib in the case that first hashing has gone wrong. This confuses the error message between a missing import and a bad supplied hash.
1 parent e7774ee commit 5f55310

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

oauth/oauth.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def set_parameter(self, parameter, value):
183183
def get_parameter(self, parameter):
184184
try:
185185
return self.parameters[parameter]
186-
except:
186+
except KeyError:
187187
raise OAuthError('Parameter not found: %s' % parameter)
188188

189189
def _get_timestamp_nonce(self):
@@ -224,7 +224,7 @@ def get_normalized_parameters(self):
224224
try:
225225
# Exclude the signature if it exists.
226226
del params['oauth_signature']
227-
except:
227+
except KeyError:
228228
pass
229229
# Escape key values before sorting.
230230
key_values = [(escape(_utf8_str(k)), escape(_utf8_str(v))) \
@@ -278,7 +278,7 @@ def from_request(http_method, http_url, headers=None, parameters=None,
278278
# Get the parameters from the header.
279279
header_params = OAuthRequest._split_header(auth_header)
280280
parameters.update(header_params)
281-
except:
281+
except Exception:
282282
raise OAuthError('Unable to parse OAuth parameters from '
283283
'Authorization header.')
284284

@@ -450,7 +450,7 @@ def _get_version(self, oauth_request):
450450
"""Verify the correct version request for this server."""
451451
try:
452452
version = oauth_request.get_parameter('oauth_version')
453-
except:
453+
except Exception:
454454
version = VERSION
455455
if version and version != self.version:
456456
raise OAuthError('OAuth version %s not supported.' % str(version))
@@ -461,12 +461,12 @@ def _get_signature_method(self, oauth_request):
461461
try:
462462
signature_method = oauth_request.get_parameter(
463463
'oauth_signature_method')
464-
except:
464+
except Exception:
465465
signature_method = SIGNATURE_METHOD
466466
try:
467467
# Get the signature method object.
468468
signature_method = self.signature_methods[signature_method]
469-
except:
469+
except KeyError:
470470
signature_method_names = ', '.join(self.signature_methods.keys())
471471
raise OAuthError('Signature method %s not supported try one of the '
472472
'following: %s' % (signature_method, signature_method_names))
@@ -498,7 +498,7 @@ def _check_signature(self, oauth_request, consumer, token):
498498
signature_method = self._get_signature_method(oauth_request)
499499
try:
500500
signature = oauth_request.get_parameter('oauth_signature')
501-
except:
501+
except Exception:
502502
raise OAuthError('Missing signature.')
503503
# Validate the signature.
504504
valid_sig = signature_method.check_signature(oauth_request, consumer,
@@ -629,7 +629,7 @@ def build_signature(self, oauth_request, consumer, token):
629629
try:
630630
import hashlib # 2.5
631631
hashed = hmac.new(key, raw, hashlib.sha1)
632-
except:
632+
except ImportError:
633633
import sha # Deprecated
634634
hashed = hmac.new(key, raw, sha)
635635

0 commit comments

Comments
 (0)