Skip to content

Commit 0cabc05

Browse files
flaneur2020lepture
authored andcommitted
catch Exception in oauth2.py (lepture#223)
* catch Exception in oauth2.py if the authorize_url is invalid, the /authorize page will response a 500 Internal Error due to the ValueError from validate_authorization_request did not get caught. * add test on /authorized with invalid redirect uri * move test_invalid_redirect_uri to test_code.py * add test on empty scope in post authorize
1 parent 51a7491 commit 0cabc05

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

flask_oauthlib/provider/oauth2.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from werkzeug.utils import import_string
1919
from oauthlib import oauth2
2020
from oauthlib.oauth2 import RequestValidator, Server
21-
from oauthlib.common import to_unicode
21+
from oauthlib.common import to_unicode, add_params_to_uri
2222
from ..utils import extract_params, decode_base64, create_response
2323

2424
__all__ = ('OAuth2Provider', 'OAuth2RequestValidator')
@@ -395,6 +395,11 @@ def decorated(*args, **kwargs):
395395
except oauth2.OAuth2Error as e:
396396
log.debug('OAuth2Error: %r', e)
397397
return redirect(e.in_uri(redirect_uri))
398+
except Exception as e:
399+
log.warning('Exception caught while processing request, %s.' % e)
400+
return redirect(add_params_to_uri(
401+
self.error_uri, {'error': str(e) }
402+
))
398403

399404
else:
400405
redirect_uri = request.values.get(
@@ -409,6 +414,12 @@ def decorated(*args, **kwargs):
409414
except oauth2.OAuth2Error as e:
410415
log.debug('OAuth2Error: %r', e)
411416
return redirect(e.in_uri(redirect_uri))
417+
except Exception as e:
418+
log.warning('Exception caught while processing request, %s.' % e)
419+
return redirect(add_params_to_uri(
420+
self.error_uri, {'error': str(e) }
421+
))
422+
412423

413424
if not isinstance(rv, bool):
414425
# if is a response or redirect
@@ -448,6 +459,12 @@ def confirm_authorization_request(self):
448459
except oauth2.OAuth2Error as e:
449460
log.debug('OAuth2Error: %r', e)
450461
return redirect(e.in_uri(redirect_uri or self.error_uri))
462+
except Exception as e:
463+
log.warning('Exception caught while processing request, %s.' % e)
464+
return redirect(add_params_to_uri(
465+
self.error_uri, {'error': str(e) }
466+
))
467+
451468

452469
def verify_request(self, scopes):
453470
"""Verify current request, get the oauth data.

tests/test_oauth2/test_code.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def test_post_authorize(self):
5050
rv = self.client.post(url, data={'confirm': 'yes'})
5151
assert 'code' in rv.location
5252

53+
url = self.authorize_url + '&scope='
54+
rv = self.client.post(url, data={'confirm': 'yes'})
55+
assert 'error=Scopes+must+be+set' in rv.location
56+
5357
def test_invalid_token(self):
5458
rv = self.client.get('/oauth/token')
5559
assert b'unsupported_grant_type' in rv.data
@@ -70,6 +74,16 @@ def test_invalid_token(self):
7074
assert b'invalid_client' not in rv.data
7175
assert rv.status_code == 401
7276

77+
def test_invalid_redirect_uri(self):
78+
authorize_url = (
79+
'/oauth/authorize?response_type=code&client_id=dev'
80+
'&redirect_uri=http://localhost:8000/authorized'
81+
'&scope=invalid'
82+
)
83+
rv = self.client.get(authorize_url)
84+
assert 'error=' in rv.location
85+
assert 'trying+to+decode+a+non+urlencoded+string' in rv.location
86+
7387
def test_get_token(self):
7488
expires = datetime.utcnow() + timedelta(seconds=100)
7589
grant = Grant(

0 commit comments

Comments
 (0)