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

Fix bindings #348

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on: # yamllint disable-line rule:truthy
branches: [master, release-*]
paths:
- '**.md'
workflow_dispatch:

jobs:
quality:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/interoperability.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ on: # yamllint disable-line rule:truthy
paths-ignore:
- '**.md'
- '**.yml'
workflow_dispatch:

jobs:
edugain:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on: # yamllint disable-line rule:truthy
branches: [master, release-*]
paths-ignore:
- '**.md'
workflow_dispatch
workflow_dispatch:

jobs:
linter:
Expand Down Expand Up @@ -285,8 +285,8 @@ jobs:
runs-on: [ubuntu-latest]
if: |
always() &&
needs.coverage.result == 'success' &&
(needs.unit-tests-linux == 'success' || needs.coverage == 'skipped')
needs.coverage.result == 'success' ||
(needs.unit-tests-linux == 'success' && needs.coverage == 'skipped')

steps:
- uses: geekyeggo/delete-artifact@v2
Expand Down
29 changes: 29 additions & 0 deletions src/SAML2/Binding.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
*/
abstract class Binding
{
/**
* The RelayState associated with the message.
*
* @var string|null
*/
protected ?string $relayState = null;

/**
* The destination of messages.
*
Expand Down Expand Up @@ -99,9 +106,9 @@
}

$query = $request->getParsedBody();
if (array_key_exists('SAMLRequest', $query) || array_key_exists('SAMLResponse', $query)) {

Check failure on line 109 in src/SAML2/Binding.php

View workflow job for this annotation

GitHub Actions / Quality control

PossiblyInvalidArgument

src/SAML2/Binding.php:109:53: PossiblyInvalidArgument: Argument 2 of array_key_exists expects array<array-key, mixed>, but possibly different type array<array-key, mixed>|null|object provided (see https://psalm.dev/092)

Check failure on line 109 in src/SAML2/Binding.php

View workflow job for this annotation

GitHub Actions / Quality control

PossiblyInvalidArgument

src/SAML2/Binding.php:109:97: PossiblyInvalidArgument: Argument 2 of array_key_exists expects array<array-key, mixed>, but possibly different type array<array-key, mixed>|null|object provided (see https://psalm.dev/092)

Check failure on line 109 in src/SAML2/Binding.php

View workflow job for this annotation

GitHub Actions / Quality control

PossiblyInvalidArgument

src/SAML2/Binding.php:109:53: PossiblyInvalidArgument: Argument 2 of array_key_exists expects array<array-key, mixed>, but possibly different type array<array-key, mixed>|null|object provided (see https://psalm.dev/092)

Check failure on line 109 in src/SAML2/Binding.php

View workflow job for this annotation

GitHub Actions / Quality control

PossiblyInvalidArgument

src/SAML2/Binding.php:109:97: PossiblyInvalidArgument: Argument 2 of array_key_exists expects array<array-key, mixed>, but possibly different type array<array-key, mixed>|null|object provided (see https://psalm.dev/092)
return new HTTPPost();
} elseif (array_key_exists('SAMLart', $query)) {

Check failure on line 111 in src/SAML2/Binding.php

View workflow job for this annotation

GitHub Actions / Quality control

PossiblyInvalidArgument

src/SAML2/Binding.php:111:55: PossiblyInvalidArgument: Argument 2 of array_key_exists expects array<array-key, mixed>, but possibly different type array<array-key, mixed>|null|object provided (see https://psalm.dev/092)

Check failure on line 111 in src/SAML2/Binding.php

View workflow job for this annotation

GitHub Actions / Quality control

PossiblyInvalidArgument

src/SAML2/Binding.php:111:55: PossiblyInvalidArgument: Argument 2 of array_key_exists expects array<array-key, mixed>, but possibly different type array<array-key, mixed>|null|object provided (see https://psalm.dev/092)
return new HTTPArtifact();
} else {
/**
Expand Down Expand Up @@ -149,6 +156,28 @@
}


/**
* Set the RelayState associated with he message.
*
* @param string|null $relayState The RelayState.
*/
public function setRelayState(string $relayState = null): void
{
$this->relayState = $relayState;
}


/**
* Get the RelayState associated with the message.
*
* @return string|null The RelayState.
*/
public function getRelayState(): ?string
{
return $this->relayState;
}


/**
* Override the destination of a message.
*
Expand Down
4 changes: 2 additions & 2 deletions src/SAML2/HTTPArtifact.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@

$params = ['SAMLart' => $artifact];

$relayState = $message->getRelayState();
$relayState = $this->getRelayState();
if ($relayState !== null) {
$params['RelayState'] = $relayState;
}

/** @psalm-suppress UndefinedClass */
$httpUtils = new HTTP();
return $httpUtils->addURLparameters($destination, $params);

Check failure on line 95 in src/SAML2/HTTPArtifact.php

View workflow job for this annotation

GitHub Actions / Quality control

UndefinedClass

src/SAML2/HTTPArtifact.php:95:16: UndefinedClass: Class, interface or enum named SimpleSAML\Utils\HTTP does not exist (see https://psalm.dev/019)

Check failure on line 95 in src/SAML2/HTTPArtifact.php

View workflow job for this annotation

GitHub Actions / Quality control

UndefinedClass

src/SAML2/HTTPArtifact.php:95:16: UndefinedClass: Class, interface or enum named SimpleSAML\Utils\HTTP does not exist (see https://psalm.dev/019)
}


Expand Down Expand Up @@ -165,7 +165,7 @@
/**
* Set the request attributes
*/
$issuer = new Issuer($this->spMetadata->getString('entityid'));

Check failure on line 168 in src/SAML2/HTTPArtifact.php

View workflow job for this annotation

GitHub Actions / Quality control

UndefinedClass

src/SAML2/HTTPArtifact.php:168:30: UndefinedClass: Class, interface or enum named SimpleSAML\Configuration does not exist (see https://psalm.dev/019)

Check failure on line 168 in src/SAML2/HTTPArtifact.php

View workflow job for this annotation

GitHub Actions / Quality control

UndefinedClass

src/SAML2/HTTPArtifact.php:168:30: UndefinedClass: Class, interface or enum named SimpleSAML\Configuration does not exist (see https://psalm.dev/019)

// Construct the ArtifactResolve Request
$ar = new ArtifactResolve($query['SAMLart'], null, $issuer, null, '2.0', $endpoint['Location']);
Expand Down Expand Up @@ -194,7 +194,7 @@
$samlResponse->addValidator([get_class($this), 'validateSignature'], $artifactResponse);

if (isset($query['RelayState'])) {
$samlResponse->setRelayState($query['RelayState']);
$this->setRelayState($query['RelayState']);
}

return $samlResponse;
Expand Down
4 changes: 2 additions & 2 deletions src/SAML2/HTTPPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function send(AbstractMessage $message): ResponseInterface
} else {
$destination = $this->destination;
}
$relayState = $message->getRelayState();
$relayState = $this->getRelayState();

$msgStr = $message->toXML();

Expand Down Expand Up @@ -110,7 +110,7 @@ public function receive(ServerRequestInterface $request): AbstractMessage
}

if (array_key_exists('RelayState', $query)) {
$msg->setRelayState($query['RelayState']);
$this->setRelayState($query['RelayState']);
}

return $msg;
Expand Down
43 changes: 4 additions & 39 deletions src/SAML2/HTTPRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getRedirectURL(AbstractMessage $message): string
$destination = $this->destination;
}

$relayState = $message->getRelayState();
$relayState = $this->getRelayState();
$msgStr = $message->toXML();

Utils::getContainer()->debugMessage($msgStr, 'out');
Expand Down Expand Up @@ -127,10 +127,8 @@ public function receive(ServerRequestInterface $request): AbstractMessage

if (array_key_exists('SAMLRequest', $query)) {
$message = $query['SAMLRequest'];
$signedQuery = 'SAMLRequest=' . urlencode($query['SAMLRequest']);
} elseif (array_key_exists('SAMLResponse', $query)) {
$message = $query['SAMLResponse'];
$signedQuery = 'SAMLResponse=' . urlencode($query['SAMLResponse']);
} else {
throw new Exception('Missing SAMLRequest or SAMLResponse parameter.');
}
Expand All @@ -151,44 +149,11 @@ public function receive(ServerRequestInterface $request): AbstractMessage

$document = DOMDocumentFactory::fromString($message);
Utils::getContainer()->debugMessage($document->documentElement, 'in');
$message = MessageFactory::fromXML($document->documentElement);

$msg = MessageFactory::fromXML($document->documentElement);
if (array_key_exists('RelayState', $query)) {
$message->setRelayState($query['RelayState']);
$signedQuery .= '&RelayState=' . urlencode($query['RelayState']);
$this->setRelayState($query['RelayState']);
}

if (!array_key_exists('Signature', $query)) {
return $message;
}

/**
* 3.4.5.2 - SAML Bindings
*
* If the message is signed, the Destination XML attribute in the root SAML element of the protocol
* message MUST contain the URL to which the sender has instructed the user agent to deliver the
* message.
*/
Assert::notNull($message->getDestination()); // Validation of the value must be done upstream

if (!array_key_exists('SigAlg', $query)) {
throw new Exception('Missing signature algorithm.');
} else {
$signedQuery .= '&SigAlg=' . urlencode($query['SigAlg']);
}

$container = ContainerSingleton::getInstance();
$blacklist = $container->getBlacklistedEncryptionAlgorithms();
$verifier = (new SignatureAlgorithmFactory($blacklist))->getAlgorithm(
$query['SigAlg'],
// TODO: Need to use the key from the metadata
PEMCertificatesMock::getPublicKey(PEMCertificatesMock::SELFSIGNED_PUBLIC_KEY),
);

if ($verifier->verify($signedQuery, base64_decode($query['Signature'])) === false) {
throw new SignatureVerificationFailedException('Failed to verify signature.');
}

return $message;
return $msg;
}
}
34 changes: 0 additions & 34 deletions src/SAML2/XML/samlp/AbstractMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ abstract class AbstractMessage extends AbstractSamlpElement implements SignableE
use SignedElementTrait;


/**
* The RelayState associated with this message.
*
* @var string|null
*/
protected ?string $relayState = null;

/**
* The \DOMDocument we are currently building.
*
Expand Down Expand Up @@ -80,7 +73,6 @@ abstract class AbstractMessage extends AbstractSamlpElement implements SignableE
* @param string|null $destination
* @param string|null $consent
* @param \SimpleSAML\SAML2\XML\samlp\Extensions $extensions
* @param string|null $relayState
*
* @throws \Exception
*/
Expand All @@ -92,15 +84,13 @@ protected function __construct(
protected ?string $destination = null,
protected ?string $consent = null,
?Extensions $extensions = null,
?string $relayState = null,
) {
Assert::nullOrSame($issueInstant?->getTimeZone()->getName(), 'Z', ProtocolViolationException::class);
Assert::nullOrValidNCName($id); // Covers the empty string
Assert::nullOrValidURI($destination); // Covers the empty string
Assert::nullOrValidURI($consent); // Covers the empty string

$this->setExtensions($extensions);
$this->setRelayState($relayState);
}


Expand Down Expand Up @@ -191,30 +181,6 @@ public function isMessageConstructedWithSignature(): bool
}


/**
* Retrieve the RelayState associated with this message.
*
* @return string|null The RelayState, or NULL if no RelayState is given
*/
public function getRelayState(): ?string
{
return $this->relayState;
}


/**
* Set the RelayState associated with this message.
*
* @param string|null $relayState The new RelayState
*/
public function setRelayState(string $relayState = null): void
{
Assert::nullOrNotWhitespaceOnly($relayState);

$this->relayState = $relayState;
}


/**
* Get the XML element.
*
Expand Down
4 changes: 1 addition & 3 deletions src/SAML2/XML/samlp/AbstractStatusResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ abstract class AbstractStatusResponse extends AbstractMessage
* @param string|null $destination
* @param string|null $consent
* @param \SimpleSAML\SAML2\XML\samlp\Extensions|null $extensions
* @param string|null $relayState
*
* @throws \Exception
*/
Expand All @@ -48,11 +47,10 @@ protected function __construct(
?string $destination = null,
?string $consent = null,
?Extensions $extensions = null,
?string $relayState = null,
) {
Assert::nullOrValidNCName($inResponseTo); // Covers the empty string

parent::__construct($issuer, $id, $version, $issueInstant, $destination, $consent, $extensions, $relayState);
parent::__construct($issuer, $id, $version, $issueInstant, $destination, $consent, $extensions);
}


Expand Down
3 changes: 0 additions & 3 deletions src/SAML2/XML/samlp/LogoutResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ final class LogoutResponse extends AbstractStatusResponse
* @param string|null $destination
* @param string|null $consent
* @param \SimpleSAML\SAML2\XML\samlp\Extensions|null $extensions
* @param string|null $relayState
*
* @throws \Exception
*/
Expand All @@ -51,7 +50,6 @@ public function __construct(
?string $destination = null,
?string $consent = null,
?Extensions $extensions = null,
?string $relayState = null,
) {
parent::__construct(
$status,
Expand All @@ -63,7 +61,6 @@ public function __construct(
$destination,
$consent,
$extensions,
$relayState,
);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/SAML2/HTTPPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testResponseParsing(): void
$this->assertInstanceOf(Response::class, $response);
$issuer = $response->getIssuer();
$this->assertEquals('https://engine.test.surfconext.nl/authentication/idp/metadata', $issuer->getContent());
$relay = $response->getRelayState();
$relay = $hp->getRelayState();
$this->assertEquals('relaystate001', $relay);
}

Expand Down Expand Up @@ -161,14 +161,14 @@ public function testSendAuthnResponse(): void
issuer: $issuer,
destination: 'http://example.org/login?success=yes',
);
$response->setRelayState('http://example.org');
$signer = (new SignatureAlgorithmFactory())->getAlgorithm(
C::SIG_RSA_SHA256,
PEMCertificatesMock::getPrivateKey(PEMCertificatesMock::SELFSIGNED_PRIVATE_KEY),
);
$response->sign($signer);

$hr = new HTTPPost();
$hr->setRelayState('http://example.org');
$hr->send($response);
}
}
27 changes: 5 additions & 22 deletions tests/SAML2/HTTPRedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function testRequestParsingMoreParams(): void
$hr = new HTTPRedirect();
$samlrequest = $hr->receive($request);
$this->assertInstanceOf(AbstractRequest::class, $samlrequest);
$relaystate = $samlrequest->getRelayState();
$relaystate = $hr->getRelayState();
$this->assertEquals('https://profile.surfconext.nl/', $relaystate);
}

Expand All @@ -140,7 +140,7 @@ public function testSignedRequestParsing(): void
$hr = new HTTPRedirect();
$samlrequest = $hr->receive($request);
$this->assertInstanceOf(AbstractRequest::class, $samlrequest);
$relaystate = $samlrequest->getRelayState();
$relaystate = $hr->getRelayState();
$this->assertEquals(
'https://demo.moo-archive.nl/module.php/admin/test/default-sp',
$relaystate,
Expand All @@ -163,6 +163,7 @@ public function testSignedRequestValidation(): void
$request = $request->withQueryParams($q);

$hr = new HTTPRedirect();
$hr->setRelayState(urlencode($q['RelayState']));
$samlrequest = $hr->receive($request);

// validate with the correct certificate, should verify
Expand Down Expand Up @@ -234,25 +235,6 @@ public function testInvalidEncodingSpecified(): void
}


/**
*/
public function testNoSigAlgSpecified(): void
{
$q = [
'SAMLRequest' => 'nVLBauMwEP0Vo7sjW7FpKpJA2rBsoNuGOruHXhZFHm8EsuRqxtv27yvbWWgvYelFgjfvzbx5zBJVazu56enkHuG5B6TktbUO5VhYsT446RUalE61gJK0rDY/7qSYZbILnrz2ln2QXFYoRAhkvGPJbrtiv7VoygJEoTJ9LOusXDSFuJ4vdH6cxwoIEGUjsrqoFUt+QcCoXLHYKMoRe9g5JOUoQlleprlI8/yQz6W4ksXiiSXbuI1xikbViahDyfkRSM2wD40DmjnL0bSdhcE6Hx7BTd3xqnqoIPw1GmbdqWPJNx80jCGtGIUeWLL5t8mtd9i3EM78n493/zWr9XVvx+58mj39IlUaR/QmKOPq4Dtkyf4c9E1EjPtzOePjREL5/XDYp/uH6sDWy6G3HDML66+5ayO7VlHx2dySf2y9nM7pPprabffeGv02ZNcquux5QEydNiNVUlAODTiKMVvrX24DKIJz8nw9jfx8tOt3',
'RelayState' => 'https://beta.surfnet.nl/simplesaml/module.php/core/authenticate.php?as=Braindrops',
'Signature' => 'b+qe/XGgICOrEL1v9dwuoy0RJtJ/GNAr7gJGYSJzLG0riPKwo7v5CH8GPC2P9IRikaeaNeQrnhBAaf8FCWrO0cLFw4qR6msK9bxRBGk+hIaTUYCh54ETrVCyGlmBneMgC5/iCRvtEW3ESPXCCqt8Ncu98yZmv9LIVyHSl67Se+fbB9sDw3/fzwYIHRMqK2aS8jnsnqlgnBGGOXqIqN3+d/2dwtCfz14s/9odoYzSUv32qfNPiPez6PSNqwhwH7dWE3TlO/jZmz0DnOeQ2ft6qdZEi5ZN5KCV6VmNKpkrLMq6DDPnuwPm/8oCAoT88R2jG7uf9QZB+ArWJKMEhDLsCA==',
];
$request = new ServerRequest('GET', 'http://tnyholm.se');
$request = $request->withQueryParams($q);

$this->expectException(Exception::class);
$this->expectExceptionMessage('Missing signature algorithm');
$hr = new HTTPRedirect();
$hr->receive($request);
}


/**
* test handling of non-deflated data in samlrequest
*/
Expand Down Expand Up @@ -327,8 +309,9 @@ public function testSendAuthnResponse(): void
issuer: $issuer,
destination: 'http://example.org/login?success=yes',
);
$response->setRelayState('http://example.org');

$hr = new HTTPRedirect();
$hr->setRelayState('http://example.org');
$hr->send($response);
}

Expand Down
Loading