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

Feature relaystate validation release 20241128 #5

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
3 changes: 2 additions & 1 deletion src/ServiceProvider/AuthFlowServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public function getAuthenticatedUri(ServerRequestEx $request) : XUri;

/**
* @param XUri $returnUri
* @param bool $sha512EncryptionEnabled
* @return XUri
*/
public function getLoginUri(XUri $returnUri) : XUri;
public function getLoginUri(XUri $returnUri, XMLSecurityKey $securityKey = XMLSecurityKey::RSA_SHA1) : XUri;

/**
* @param string $id - user identifier for downstream identity provider
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceProvider/OAuth/OAuthFlowService.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function getAuthenticatedUri(ServerRequestEx $request) : XUri {
return $returnUri !== null ? $returnUri : $this->oauth->getDefaultReturnUri();
}

public function getLoginUri(XUri $returnUri) : XUri {
public function getLoginUri(XUri $returnUri, XMLSecurityKey $securityKey = XMLSecurityKey::RSA_SHA1) : XUri {
$clientId = $this->oauth->getRelyingPartyClientId();
$state = $this->uuidFactory->uuid4()->toString();
$uri = $this->oauth->getIdentityProviderAuthorizationUri()
Expand Down
20 changes: 20 additions & 0 deletions src/ServiceProvider/Saml/Exception/SamlInvalidRelayStateUri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);
/**
* AuthForge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace modethirteen\AuthForge\ServiceProvider\Saml\Exception;

class SamlInvalidRelayStateUri extends SamlException {
}
17 changes: 14 additions & 3 deletions src/ServiceProvider/Saml/RelayStateAuthFlowServiceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use modethirteen\AuthForge\Common\Exception\ServerRequestInterfaceParsedBodyException;
use modethirteen\AuthForge\Common\Http\ServerRequestEx;
use modethirteen\AuthForge\ServiceProvider\Saml\Exception\SamlInvalidRelayStateUri;
use modethirteen\AuthForge\ServiceProvider\Saml\Http\HttpMessageInterface;
use modethirteen\Http\Exception\MalformedPathQueryFragmentException;
use modethirteen\Http\Exception\MalformedUriException;
Expand Down Expand Up @@ -45,9 +46,14 @@ protected function getRedirectUriFromRequestRelayState(SamlConfigurationInterfac
if(!StringEx::isNullOrEmpty($relayState)) {
$logger->debug('Found RelayState', ['RelayState' => $relayState]);
try {
return XUri::isAbsoluteUrl($relayState)
? XUri::newFromString($relayState)
: $saml->getRelayStateBaseUri()->atPath($relayState);
if(XUri::isAbsoluteUrl($relayState)) {
if(!$saml->isValidRelayStateUri($relayState)) {
throw new SamlInvalidRelayStateUri();
}
return XUri::newFromString($relayState);
} else {
return $saml->getRelayStateBaseUri()->atPath($relayState);
}
} catch(MalformedPathQueryFragmentException $e) {
$this->logger->warning('Could not append relative RelayState to service provider base URI, {{Error}}', [
'Error' => $e->getMessage()
Expand All @@ -57,6 +63,11 @@ protected function getRedirectUriFromRequestRelayState(SamlConfigurationInterfac
'Error' => $e->getMessage()
]);
}
catch(SamlInvalidRelayStateUri $e) {
$this->logger->warning('RelayState URI does not match service provider base URI, {{Uri}}', [
'Uri' => $e->getMessage()
]);
}
}
return $saml->getDefaultReturnUri();
}
Expand Down
5 changes: 5 additions & 0 deletions src/ServiceProvider/Saml/SamlConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,9 @@ public function isNameIdFormatEnforcementEnabled() : bool;
* @return bool
*/
public function isStrictValidationRequired() : bool;

/**
* @return bool
*/
public function isValidRelayStateUri(string $uri) : bool;
}
4 changes: 2 additions & 2 deletions src/ServiceProvider/Saml/SamlFlowService.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ public function getAuthenticatedUri(ServerRequestEx $request) : XUri {
* {@inheritDoc}
* @throws SamlFlowServiceException
*/
public function getLoginUri(XUri $returnUri) : XUri {
public function getLoginUri(XUri $returnUri, XMLSecurityKey $securityKey = XMLSecurityKey::RSA_SHA1) : XUri {
try {
return $this->uriFactory->newAuthnRequestUri($returnUri);
return $this->uriFactory->newAuthnRequestUri($returnUri, $securityKey);
} catch(
SamlCannotDeflateOutgoingHttpMessageException |
SamlCannotGenerateSignatureException |
Expand Down
4 changes: 2 additions & 2 deletions src/ServiceProvider/Saml/SamlUriFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function __construct(
* @throws SamlCannotGenerateSignatureException
* @throws SamlCannotLoadCryptoKeyException
*/
public function newAuthnRequestUri(XUri $returnUri) : XUri {
public function newAuthnRequestUri(XUri $returnUri, XMLSecurityKey $securityKey = XMLSecurityKey::RSA_SHA1) : XUri {
$uri = $this->saml->getIdentityProviderSingleSignOnUri();
$returnHref = $returnUri->toString();
$id = $this->newId();
Expand All @@ -95,7 +95,7 @@ public function newAuthnRequestUri(XUri $returnUri) : XUri {
// handle signing
if($this->saml->isAuthnRequestSignatureRequired()) {
$signature = $this->buildRequestSignature($samlRequest, $returnHref);
$parameters['SigAlg'] = XMLSecurityKey::RSA_SHA1;
$parameters['SigAlg'] = $securityKey;
$parameters['Signature'] = $signature;
}
$this->logger->debug('Sending AuthnRequest', [
Expand Down
3 changes: 2 additions & 1 deletion src/ServiceProvider/Saml/SamlUriFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ interface SamlUriFactoryInterface {

/**
* @param XUri $returnUri
* @param XMLSecurityKey $securityKey
* @return XUri
*/
public function newAuthnRequestUri(XUri $returnUri) : XUri;
public function newAuthnRequestUri(XUri $returnUri, XMLSecurityKey $securityKey = XMLSecurityKey::RSA_SHA1) : XUri;

/**
* @param string $username
Expand Down
Loading