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

Verify the GSSP response is valid #307

Merged
merged 2 commits into from
Nov 29, 2023
Merged

Verify the GSSP response is valid #307

merged 2 commits into from
Nov 29, 2023

Conversation

MKodde
Copy link
Member

@MKodde MKodde commented Oct 25, 2023

Improve the GSSP response handling. The response is now actually read from the POST vars, to verify the preconditions. In addition we verify if the Subject NameID from the response matches with the one found in the AuthNRequest.

@MKodde MKodde force-pushed the feature/did-set-subject branch 4 times, most recently from a7a5533 to 8dfe2c1 Compare October 25, 2023 14:27
@MKodde MKodde requested a review from parijke October 26, 2023 06:06
Copy link
Contributor

@parijke parijke left a comment

Choose a reason for hiding this comment

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

Small suggestions

{
$currentSubject = $this->get('subject');
if (!empty($currentSubject)) {
if (strtolower($currentSubject) !== strtolower($subject)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Indention level can 1 less by using &&

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, that was a rookie mistake

@@ -75,6 +75,17 @@ public function sendSecondFactorVerificationAuthnRequest(
$originalRequestId = $this->responseContext->getInResponseTo();
}

$subject = $stateHandler->getSubject();
if (!empty($subject) && strtolower($subjectNameId) !== strtolower($subject)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Here you did it

*/
/** @var ResponseValidator */
private $responseValidator;

public function __construct(
SamlAuthenticationLogger $samlLogger,
LoaResolutionService $loaResolutionService,
Copy link
Contributor

Choose a reason for hiding this comment

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

Candidate for constructor property promotion

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, when we are upgrading to php8 we surely will do that. For now this code must be PHP72 compat.

@@ -105,6 +102,8 @@ public function respond(ResponseContext $responseContext)
}

$secondFactor = $this->secondFactorService->findByUuid($selectedSecondFactorUuid);
$this->responseValidator->validate($request, $secondFactor, $responseContext->getIdentityNameId());

Copy link
Contributor

Choose a reason for hiding this comment

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

When do we use an extra empty line, is that personal preference?

Copy link
Member Author

@MKodde MKodde Oct 26, 2023

Choose a reason for hiding this comment

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

No hard requirements here. Only to not have a newline between the last closing parenthesis' of a PHP Class

    }

}

Yields: ERROR | [x] The closing brace for the class must go on the next line after the body

In this case I wanted to emphasize the validation method.

public function __construct(
SecondFactorTypeService $secondFactorTypeService,
ProviderRepository $providerRepository,
PostBinding $postBinding
Copy link
Contributor

Choose a reason for hiding this comment

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

Candidate for constructor property promotion

Copy link
Member Author

Choose a reason for hiding this comment

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

See previous comment

*/
public function validate(Request $request, SecondFactor $secondFactor, string $nameIdFromState)
{
$secondFactorType = new SecondFactorType($secondFactor->secondFactorType);
Copy link
Contributor

Choose a reason for hiding this comment

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

Hard coupling, maybe use a factory?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is acceptable here. Given it's a (very) simple ValueObject.

@MKodde MKodde force-pushed the feature/did-set-subject branch from 8dfe2c1 to 2ea1031 Compare October 26, 2023 11:47
@MKodde MKodde force-pushed the feature/did-set-subject branch from 2ea1031 to ba05073 Compare October 26, 2023 11:48
$provider->getRemoteIdentityProvider(),
$provider->getServiceProvider()
);
$nameIdFromResponse = $samlResponse->getNameId()->getValue();
Copy link
Member Author

Choose a reason for hiding this comment

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

Peer review: rename to subjectNameId from response?

Copy link
Member Author

Choose a reason for hiding this comment

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

renamed to: subjectNameIdFromResponse


use RuntimeException as BaseRuntimeException;

class RuntimeException extends BaseRuntimeException
Copy link
Member Author

Choose a reason for hiding this comment

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

Peer review feedback: Why not give this a more descriptive name?

Copy link
Member Author

Choose a reason for hiding this comment

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

I ended up renaming this Exception to: ReceivedInvalidSubjectNameIdException

use Surfnet\StepupGateway\SecondFactorOnlyBundle\Exception\RuntimeException;
use Symfony\Component\HttpFoundation\Request;

class ResponseValidator
Copy link
Member Author

Choose a reason for hiding this comment

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

Peer review feedback: decouple this?

1. To check the preconditions
2. To verify that the Subject NameID matches the SecondFactor Identifier
   used to start the authentication.
@MKodde MKodde force-pushed the feature/did-set-subject branch from ba05073 to d20d7d3 Compare October 26, 2023 13:27
@MKodde MKodde merged commit 4eda76b into develop Nov 29, 2023
4 checks passed
@MKodde MKodde deleted the feature/did-set-subject branch November 29, 2023 09:21
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