Skip to content
Merged
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
32 changes: 22 additions & 10 deletions src/Service/WebauthnService.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ public function verifyRegistration(User $user, string $credentialJson, string $n
try {
$options = $this->serializer->deserialize($optionsJson, PublicKeyCredentialCreationOptions::class, 'json');

$record = AuthenticatorAttestationResponseValidator::create($this->createCeremonyStepManagerFactory()->creationCeremony())
$record = AuthenticatorAttestationResponseValidator::create($this->createCeremonyStepManagerFactory([$this->getAppOrigin()])->creationCeremony())
->check($response, $options, $this->getRpId());
} catch (WebauthnException $exception) {
throw $exception;
} catch (Throwable $exception) {
// Usually a misconfigured RP ID or origin, see the settings webauthn_rp_id and webauthn_origins
$this->logger->warning('WebAuthn registration failed: {message}', ['message' => $exception->getMessage(), 'rpId' => $this->getRpId(), 'origins' => $this->getAllowedOrigins()]);
// Usually a misconfigured RP ID or application URL, see the settings webauthn_rp_id and app_url
$this->logger->warning('WebAuthn registration failed: {message}', ['message' => $exception->getMessage(), 'rpId' => $this->getRpId(), 'origin' => $this->getAppOrigin()]);

throw new WebauthnException(self::INVALID_REGISTRATION, 0, $exception);
}
Expand Down Expand Up @@ -226,7 +226,8 @@ public function verifyAuthentication(User $user, string $credentialJson): bool
return false;
}

return $this->verifyAssertion($user, $publicKeyCredential, $options);
// Only the application URL, so script on another allowed origin (e.g. the webmail) cannot sign in to Userli
return $this->verifyAssertion($user, $publicKeyCredential, $options, [$this->getAppOrigin()]);
}

/**
Expand Down Expand Up @@ -280,7 +281,7 @@ public function verifyExternalAssertion(
return false;
}

return $this->verifyAssertion($user, $publicKeyCredential, $this->createRequestOptions($user, $challenge));
return $this->verifyAssertion($user, $publicKeyCredential, $this->createRequestOptions($user, $challenge), $this->getAllowedOrigins());
}

/**
Expand Down Expand Up @@ -318,7 +319,7 @@ public function getRpId(): string
*/
public function getAllowedOrigins(): array
{
$origins = [$this->toOrigin((string) $this->settingsService->get('app_url'))];
$origins = [$this->getAppOrigin()];

// The webmail (e.g. Roundcube with the Userli plugin) is allowed if it can use the security key domain
$webmailOrigin = $this->toOrigin((string) $this->settingsService->get('webmail_url', ''));
Expand All @@ -339,6 +340,11 @@ public function getAllowedOrigins(): array
))));
}

private function getAppOrigin(): string
{
return $this->toOrigin((string) $this->settingsService->get('app_url'));
}

/**
* Browsers report the origin only (scheme, host, port), while a URL may also contain a path.
*/
Expand All @@ -351,7 +357,10 @@ private function toOrigin(string $url): string
return is_string($scheme) && is_string($host) ? $scheme.'://'.$host.(is_int($port) ? ':'.$port : '') : $url;
}

private function verifyAssertion(User $user, PublicKeyCredential $publicKeyCredential, PublicKeyCredentialRequestOptions $options): bool
/**
* @param string[] $origins
*/
private function verifyAssertion(User $user, PublicKeyCredential $publicKeyCredential, PublicKeyCredentialRequestOptions $options, array $origins): bool
{
$response = $publicKeyCredential->response;
if (!$response instanceof AuthenticatorAssertionResponse) {
Expand All @@ -368,7 +377,7 @@ private function verifyAssertion(User $user, PublicKeyCredential $publicKeyCrede

// The user is known from the first factor, so the user handle of the credential is the expected one
try {
$record = AuthenticatorAssertionResponseValidator::create($this->createCeremonyStepManagerFactory()->requestCeremony())
$record = AuthenticatorAssertionResponseValidator::create($this->createCeremonyStepManagerFactory($origins)->requestCeremony())
->check($this->toCredentialRecord($credential), $response, $options, $this->getRpId(), Base64UrlSafe::decode($credential->getUserHandle()));
} catch (Throwable $throwable) {
$this->logger->notice('WebAuthn assertion failed: {message}', ['message' => $throwable->getMessage(), 'credential' => $credential->getId()]);
Expand Down Expand Up @@ -440,10 +449,13 @@ private function createRequestOptions(User $user, string $challenge): PublicKeyC
);
}

private function createCeremonyStepManagerFactory(): CeremonyStepManagerFactory
/**
* @param string[] $origins
*/
private function createCeremonyStepManagerFactory(array $origins): CeremonyStepManagerFactory
{
$factory = new CeremonyStepManagerFactory();
$factory->setAllowedOrigins($this->getAllowedOrigins());
$factory->setAllowedOrigins($origins);
$factory->setAlgorithmManager(Manager::create()->add(ES256::create(), Ed25519::create(), RS256::create()));

return $factory;
Expand Down
19 changes: 19 additions & 0 deletions tests/Service/WebauthnServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,25 @@ public function testExternalAssertionFailsForInvalidSignature(): void
self::assertFalse($this->verifyExternal($challenge, $assertion));
}

public function testAuthenticationFailsForExternalOrigin(): void
{
$authenticator = new SoftwareAuthenticator();
$this->register($authenticator);

$options = $this->service->createAuthenticationOptions($this->user);

self::assertFalse($this->service->verifyAuthentication($this->user, $authenticator->get($options, self::KEYCLOAK_ORIGIN)));
}

public function testRegistrationFailsForExternalOrigin(): void
{
$options = $this->service->createRegistrationOptions($this->user);

$this->expectException(WebauthnException::class);

$this->service->verifyRegistration($this->user, new SoftwareAuthenticator()->create($options, self::KEYCLOAK_ORIGIN), 'My key');
}

public function testGetAllowedOrigins(): void
{
self::assertSame([self::ORIGIN, self::KEYCLOAK_ORIGIN], $this->service->getAllowedOrigins());
Expand Down
Loading