Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rfc7636: validate code challenge format #638

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions authlib/oauth2/rfc7636/challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


CODE_VERIFIER_PATTERN = re.compile(r'^[a-zA-Z0-9\-._~]{43,128}$')
CODE_CHALLENGE_PATTERN = re.compile(r'^[a-zA-Z0-9\-._~]{43,128}$')


def create_s256_code_challenge(code_verifier):
Expand Down Expand Up @@ -76,6 +77,9 @@ def validate_code_challenge(self, grant):
if not challenge:
raise InvalidRequestError('Missing "code_challenge"')

if not CODE_CHALLENGE_PATTERN.match(challenge):
raise InvalidRequestError('Invalid "code_challenge"')

if method and method not in self.SUPPORTED_CODE_CHALLENGE_METHOD:
raise InvalidRequestError('Unsupported "code_challenge_method"')

Expand Down
21 changes: 13 additions & 8 deletions tests/flask/test_oauth2/test_code_challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,23 @@ def test_missing_code_challenge(self):

def test_has_code_challenge(self):
self.prepare_data()
rv = self.client.get(self.authorize_url + '&code_challenge=abc')
rv = self.client.get(self.authorize_url + '&code_challenge=Zhs2POMonIVVHZteWfoU7cSXQSm0YjghikFGJSDI2_s')
self.assertEqual(rv.data, b'ok')

def test_invalid_code_challenge(self):
self.prepare_data()
rv = self.client.get(self.authorize_url + '&code_challenge=abc&code_challenge_method=plain')
self.assertIn(b'Invalid', rv.data)

def test_invalid_code_challenge_method(self):
self.prepare_data()
suffix = '&code_challenge=abc&code_challenge_method=invalid'
suffix = '&code_challenge=Zhs2POMonIVVHZteWfoU7cSXQSm0YjghikFGJSDI2_s&code_challenge_method=invalid'
rv = self.client.get(self.authorize_url + suffix)
self.assertIn(b'Unsupported', rv.data)

def test_supported_code_challenge_method(self):
self.prepare_data()
suffix = '&code_challenge=abc&code_challenge_method=plain'
suffix = '&code_challenge=Zhs2POMonIVVHZteWfoU7cSXQSm0YjghikFGJSDI2_s&code_challenge_method=plain'
rv = self.client.get(self.authorize_url + suffix)
self.assertEqual(rv.data, b'ok')

Expand All @@ -101,7 +106,7 @@ def test_trusted_client_without_code_challenge(self):

def test_missing_code_verifier(self):
self.prepare_data()
url = self.authorize_url + '&code_challenge=foo'
url = self.authorize_url + '&code_challenge=Zhs2POMonIVVHZteWfoU7cSXQSm0YjghikFGJSDI2_s'
rv = self.client.post(url, data={'user_id': '1'})
self.assertIn('code=', rv.location)

Expand All @@ -117,7 +122,7 @@ def test_missing_code_verifier(self):

def test_trusted_client_missing_code_verifier(self):
self.prepare_data('client_secret_basic')
url = self.authorize_url + '&code_challenge=foo'
url = self.authorize_url + '&code_challenge=Zhs2POMonIVVHZteWfoU7cSXQSm0YjghikFGJSDI2_s'
rv = self.client.post(url, data={'user_id': '1'})
self.assertIn('code=', rv.location)

Expand All @@ -133,7 +138,7 @@ def test_trusted_client_missing_code_verifier(self):

def test_plain_code_challenge_invalid(self):
self.prepare_data()
url = self.authorize_url + '&code_challenge=foo'
url = self.authorize_url + '&code_challenge=Zhs2POMonIVVHZteWfoU7cSXQSm0YjghikFGJSDI2_s'
rv = self.client.post(url, data={'user_id': '1'})
self.assertIn('code=', rv.location)

Expand All @@ -150,7 +155,7 @@ def test_plain_code_challenge_invalid(self):

def test_plain_code_challenge_failed(self):
self.prepare_data()
url = self.authorize_url + '&code_challenge=foo'
url = self.authorize_url + '&code_challenge=Zhs2POMonIVVHZteWfoU7cSXQSm0YjghikFGJSDI2_s'
rv = self.client.post(url, data={'user_id': '1'})
self.assertIn('code=', rv.location)

Expand Down Expand Up @@ -206,7 +211,7 @@ def test_s256_code_challenge_success(self):

def test_not_implemented_code_challenge_method(self):
self.prepare_data()
url = self.authorize_url + '&code_challenge=foo'
url = self.authorize_url + '&code_challenge=Zhs2POMonIVVHZteWfoU7cSXQSm0YjghikFGJSDI2_s'
url += '&code_challenge_method=S128'

rv = self.client.post(url, data={'user_id': '1'})
Expand Down
Loading