| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\CeremonyStep; |
| 6 |
|
| 7 |
use function count; |
| 8 |
use function in_array; |
| 9 |
use InvalidArgumentException; |
| 10 |
use function is_array; |
| 11 |
use function is_string; |
| 12 |
use function sprintf; |
| 13 |
use function trigger_deprecation; |
| 14 |
use Webauthn\AuthenticationExtensions\AuthenticationExtensions; |
| 15 |
use Webauthn\AuthenticatorAssertionResponse; |
| 16 |
use Webauthn\AuthenticatorAttestationResponse; |
| 17 |
use Webauthn\CredentialRecord; |
| 18 |
use Webauthn\Exception\AuthenticatorResponseVerificationException; |
| 19 |
use Webauthn\PublicKeyCredentialCreationOptions; |
| 20 |
use Webauthn\PublicKeyCredentialRequestOptions; |
| 21 |
use Webauthn\PublicKeyCredentialSource; |
| 22 |
|
| 23 |
final readonly class CheckAllowedOrigins implements CeremonyStep |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private array $fullOrigins; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private array $rawOrigins; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function __construct( |
| 44 |
array $allowedOrigins, |
| 45 |
private bool $allowSubdomains = false, |
| 46 |
private array $securedRelyingPartyId = [], |
| 47 |
) { |
| 48 |
$fullOrigins = []; |
| 49 |
$rawOrigins = []; |
| 50 |
foreach ($allowedOrigins as $allowedOrigin) { |
| 51 |
$parsed = parse_url($allowedOrigin); |
| 52 |
$parsed !== false || throw new InvalidArgumentException(sprintf('Invalid origin: %s', $allowedOrigin)); |
| 53 |
if (isset($parsed['scheme'], $parsed['host'])) { |
| 54 |
$fullOrigins[] = self::buildOrigin($parsed['scheme'], $parsed['host'], $parsed['port'] ?? null); |
| 55 |
} elseif (isset($parsed['scheme'])) { |
| 56 |
$rawOrigins[] = $allowedOrigin; |
| 57 |
} else { |
| 58 |
|
| 59 |
$host = $parsed['host'] ?? $allowedOrigin; |
| 60 |
$fullOrigins[] = self::buildOrigin('https', $host, null); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$this->fullOrigins = array_unique($fullOrigins); |
| 65 |
$this->rawOrigins = array_unique($rawOrigins); |
| 66 |
} |
| 67 |
|
| 68 |
public function process( |
| 69 |
CredentialRecord $credentialRecord, |
| 70 |
AuthenticatorAssertionResponse|AuthenticatorAttestationResponse $authenticatorResponse, |
| 71 |
PublicKeyCredentialRequestOptions|PublicKeyCredentialCreationOptions $publicKeyCredentialOptions, |
| 72 |
?string $userHandle, |
| 73 |
string $host |
| 74 |
): void { |
| 75 |
if ($credentialRecord instanceof PublicKeyCredentialSource) { |
| 76 |
trigger_deprecation( |
| 77 |
'web-auth/webauthn-lib', |
| 78 |
'5.3', |
| 79 |
'Passing a PublicKeyCredentialSource to "%s::process()" is deprecated, pass a CredentialRecord instead.', |
| 80 |
self::class |
| 81 |
); |
| 82 |
} |
| 83 |
$authData = $authenticatorResponse instanceof AuthenticatorAssertionResponse ? $authenticatorResponse->authenticatorData : $authenticatorResponse->attestationObject->authData; |
| 84 |
$C = $authenticatorResponse->clientDataJSON; |
| 85 |
|
| 86 |
if (in_array($C->origin, $this->rawOrigins, true)) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$parsedOrigin = parse_url($C->origin); |
| 91 |
is_array($parsedOrigin) || throw AuthenticatorResponseVerificationException::create( |
| 92 |
'Invalid origin. Unable to parse the origin.' |
| 93 |
); |
| 94 |
$originHost = $parsedOrigin['host'] ?? $C->origin; |
| 95 |
|
| 96 |
$hasAllowedOrigins = count($this->fullOrigins) !== 0 || count($this->rawOrigins) !== 0; |
| 97 |
|
| 98 |
if ($hasAllowedOrigins) { |
| 99 |
|
| 100 |
if (isset($parsedOrigin['scheme'], $parsedOrigin['host'])) { |
| 101 |
$normalizedOrigin = self::buildOrigin( |
| 102 |
$parsedOrigin['scheme'], |
| 103 |
$parsedOrigin['host'], |
| 104 |
$parsedOrigin['port'] ?? null |
| 105 |
); |
| 106 |
if (in_array($normalizedOrigin, $this->fullOrigins, true)) { |
| 107 |
return; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
$isSubDomain = $this->isSubdomainOfFullOrigins($parsedOrigin); |
| 113 |
|
| 114 |
if ($this->allowSubdomains && $isSubDomain) { |
| 115 |
return; |
| 116 |
} |
| 117 |
if (! $this->allowSubdomains && $isSubDomain) { |
| 118 |
throw AuthenticatorResponseVerificationException::create('Invalid origin. Subdomains are not allowed.'); |
| 119 |
} |
| 120 |
throw AuthenticatorResponseVerificationException::create( |
| 121 |
'Invalid origin. Not in the list of allowed origins.' |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
$rpId = $publicKeyCredentialOptions->rpId ?? $publicKeyCredentialOptions->rp->id ?? $host; |
| 126 |
$facetId = $this->getFacetId($rpId, $publicKeyCredentialOptions->extensions, $authData->extensions); |
| 127 |
|
| 128 |
if (! in_array($facetId, $this->securedRelyingPartyId, true)) { |
| 129 |
$scheme = $parsedOrigin['scheme'] ?? ''; |
| 130 |
$scheme === 'https' || throw AuthenticatorResponseVerificationException::create( |
| 131 |
'Invalid scheme. HTTPS required.' |
| 132 |
); |
| 133 |
} |
| 134 |
$facetId !== '' || throw AuthenticatorResponseVerificationException::create( |
| 135 |
'Invalid origin. Unable to determine the facet ID.' |
| 136 |
); |
| 137 |
if ($originHost === $facetId) { |
| 138 |
return; |
| 139 |
} |
| 140 |
$isSubDomains = $this->isSubdomainOf($originHost, $facetId); |
| 141 |
if ($this->allowSubdomains && $isSubDomains) { |
| 142 |
return; |
| 143 |
} |
| 144 |
if (! $this->allowSubdomains && $isSubDomains) { |
| 145 |
throw AuthenticatorResponseVerificationException::create('Invalid origin. Subdomains are not allowed.'); |
| 146 |
} |
| 147 |
throw AuthenticatorResponseVerificationException::create('Invalid origin.'); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
private function isSubdomainOfFullOrigins(array $parsedOrigin): bool |
| 154 |
{ |
| 155 |
if (! isset($parsedOrigin['scheme'], $parsedOrigin['host'])) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
$originScheme = $parsedOrigin['scheme']; |
| 160 |
|
| 161 |
$originHost = $parsedOrigin['host']; |
| 162 |
$originPort = $parsedOrigin['port'] ?? null; |
| 163 |
|
| 164 |
foreach ($this->fullOrigins as $fullOrigin) { |
| 165 |
$parsedAllowed = parse_url($fullOrigin); |
| 166 |
if (! is_array($parsedAllowed) || ! isset($parsedAllowed['scheme'], $parsedAllowed['host'])) { |
| 167 |
continue; |
| 168 |
} |
| 169 |
|
| 170 |
$allowedScheme = $parsedAllowed['scheme']; |
| 171 |
|
| 172 |
$allowedHost = $parsedAllowed['host']; |
| 173 |
if ($originScheme !== $allowedScheme) { |
| 174 |
continue; |
| 175 |
} |
| 176 |
$allowedPort = $parsedAllowed['port'] ?? null; |
| 177 |
if ($originPort !== $allowedPort) { |
| 178 |
continue; |
| 179 |
} |
| 180 |
if ($this->isSubdomainOf($originHost, $allowedHost)) { |
| 181 |
return true; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
private static function buildOrigin(string $scheme, string $host, ?int $port): string |
| 189 |
{ |
| 190 |
if ($port === null) { |
| 191 |
return sprintf('%s://%s', $scheme, $host); |
| 192 |
} |
| 193 |
$defaultPorts = [ |
| 194 |
'https' => 443, |
| 195 |
'http' => 80, |
| 196 |
]; |
| 197 |
if (isset($defaultPorts[$scheme]) && $port === $defaultPorts[$scheme]) { |
| 198 |
return sprintf('%s://%s', $scheme, $host); |
| 199 |
} |
| 200 |
return sprintf('%s://%s:%d', $scheme, $host, $port); |
| 201 |
} |
| 202 |
|
| 203 |
private function isSubdomainOf(string $subdomain, string $domain): bool |
| 204 |
{ |
| 205 |
return str_ends_with('.' . $subdomain, '.' . $domain); |
| 206 |
} |
| 207 |
|
| 208 |
private function getFacetId( |
| 209 |
string $rpId, |
| 210 |
AuthenticationExtensions $AuthenticationExtensions, |
| 211 |
?AuthenticationExtensions $authenticationExtensionsClientOutputs |
| 212 |
): string { |
| 213 |
if ($authenticationExtensionsClientOutputs === null |
| 214 |
|| ! $AuthenticationExtensions->has('appid') |
| 215 |
|| ! $authenticationExtensionsClientOutputs->has('appid')) { |
| 216 |
return $rpId; |
| 217 |
} |
| 218 |
|
| 219 |
$appId = $AuthenticationExtensions->get('appid') |
| 220 |
->value; |
| 221 |
$wasUsed = $authenticationExtensionsClientOutputs->get('appid') |
| 222 |
->value; |
| 223 |
|
| 224 |
return (is_string($appId) && $wasUsed === true) ? $appId : $rpId; |
| 225 |
} |
| 226 |
} |