Skip to content

added validation around code_challenge_method to reject non S256 values#1599

Draft
bwang-icf wants to merge 11 commits into
masterfrom
brandon/BB2-4805-invalid-code-challenge
Draft

added validation around code_challenge_method to reject non S256 values#1599
bwang-icf wants to merge 11 commits into
masterfrom
brandon/BB2-4805-invalid-code-challenge

Conversation

@bwang-icf
Copy link
Copy Markdown
Contributor

JIRA Ticket:
BB2-4805

What Does This PR Do?

Rejects any non S256 code challenge method calls

What Should Reviewers Watch For?

We already have some validation in the form of check_required_params and such within this file. Would it make sense to condense some of these?

Validation

Spin up a local instance and attempt an auth flow. Copy the authorize url and change the S256 value to something else. Observe a failed request and then attempt a regular workflow and see that it passes.

What Security Implications Does This PR Have?

Please indicate if this PR does any of the following:

  • Adds any new software dependencies
  • Modifies any security controls
  • Adds new transmission or storage of data
  • Any other changes that could possibly affect security?
  • Yes, one or more of the above security implications apply. This PR must not be merged without the ISSO or team
    security engineer's approval.

Any Migrations?

  • Yes, there are migrations
    • The migrations should be run PRIOR to the code being deployed
    • The migrations should be run AFTER the code is deployed
    • There is a more complicated migration plan (downtime,
      etc)
  • No migrations

Copy link
Copy Markdown
Contributor

@jimmyfagan jimmyfagan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just some comments to hopefully make this a bit cleaner hopefully.

Comment thread apps/dot_ext/views/authorization.py Outdated
Comment on lines +297 to +298
# Validating before medicare login step that code_challenge_method is correct as per BB2-4805
# We could consider adding other checks at this step as well. Open to the team's suggestions
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer keeping these kinds of comments out of the code, feel free to start convos in the PR, but I don't think these comments are useful to potentially stay in the code for a while after merging.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed for sure.

Comment thread apps/dot_ext/views/authorization.py Outdated
Comment on lines +362 to +364
validation_error = self._validate_code_challenge_method(code_challenge_method)
if validation_error:
return validation_error
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like this is probably better coming after the _check_for_required_parameters call. Check that the required params exist before checking for validity of values.

Comment thread apps/dot_ext/views/authorization.py Outdated
Comment thread apps/dot_ext/views/authorization.py Outdated
Comment on lines +186 to +200
def _validate_code_challenge_method(self, code_challenge_method):
"""Validate code_challenge_method is S256 if provided.

Returns None if valid, JsonResponse error if invalid.
"""
if code_challenge_method and code_challenge_method != 'S256':
return JsonResponse(
{
'status_code': HTTPStatus.BAD_REQUEST,
'error': 'invalid_request',
'error_description': f'code_challenge_method must be S256, got: {code_challenge_method}',
},
status=HTTPStatus.BAD_REQUEST,
)
return None
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we can move this to _check_for_required_params similar to what we do when we confirm the existence of state and then validate it in an elif. And then maybe we can add a call to _check_for_required_params to dispatch so we do these checks pre-login as you suggested. How does that sound? If we do that, it might invalidate one of my other comments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it can be there. That answers my other comment from before about where else that check might live. I'll change it so it's part of the required_param check

Comment thread apps/dot_ext/views/authorization.py Outdated
return validation_error
kwargs['code_challenge'] = request.POST.get('code_challenge')
kwargs['code_challenge_method'] = request.POST.get('code_challenge_method')
kwargs['code_challenge_method'] = code_challenge_method
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this necessary? Kind of unclear to me why any of the kwargs need to be manually set like this to be honest.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think otherwise the PKCE params don't show up in the super().post. It needs to be set within kwargs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it needed in super().post? Seems like it was working fine without this before

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be reverted back, but I was mostly just following what was currently within the code. I think currently the code in master does set the kwargs here, but I can definitely try to run this without setting kwargs and see what happens and remove these if they're really not needed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these indeed are not needed anymore, so we're good removing this. Some chance for related refactors in form_valid to uncomment the lines 398-399, and then removing the ifs for those values beneath that. Seems like these are all kind of remnants of when PKCE was first added, so some cleanup in this area would be nice while we're at it.

Copy link
Copy Markdown
Contributor

@jimmyfagan jimmyfagan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, and seems to work as expected. Left a few more comments, should be good for a final approval after those last couple notes.

Comment thread apps/dot_ext/views/authorization.py Outdated
Comment on lines +362 to +365
code_challenge_method = request.POST.get('code_challenge_method')
validation_error = self._validate_code_challenge_method(code_challenge_method)
if validation_error:
return validation_error
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we replace _validate_code_challenge_method with _check_for_required_params here too? If so, then let's get _validate_code_challenge_method removed altogether.

Comment thread apps/dot_ext/views/authorization.py Outdated
return validation_error
kwargs['code_challenge'] = request.POST.get('code_challenge')
kwargs['code_challenge_method'] = request.POST.get('code_challenge_method')
kwargs['code_challenge_method'] = code_challenge_method
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these indeed are not needed anymore, so we're good removing this. Some chance for related refactors in form_valid to uncomment the lines 398-399, and then removing the ifs for those values beneath that. Seems like these are all kind of remnants of when PKCE was first added, so some cleanup in this area would be nice while we're at it.

@bwang-icf bwang-icf marked this pull request as draft May 28, 2026 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants