From 4f2e203327d1b8ba74b240a1a8c2a847e99ee8f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 15 Sep 2026 15:26:38 +0200 Subject: [PATCH] refactor(policies): reduce the pwned policy to three options The policy is now `enabled`, `sessions` and `users`. - `endpoint` is gone; lookups always use `_APP_PWNED_PASSWORDS_ENDPOINT`, so a project can no longer point the check at its own service. - `threshold` is gone; a password found in any known breach is rejected. - `failClosed` is gone; an unreachable breach service always surfaces `general_pwned_passwords_unavailable` rather than silently passing. - `forceReset` is renamed to `users`, matching `sessions`. Its meaning is unchanged: a breached password blocks the sign-in until it is reset, and it still only applies when sessions are checked. The outage path is now covered by unit tests only, since a project can no longer be pointed at an unreachable endpoint from an e2e test. Co-Authored-By: Claude Opus 5 (1M context) --- app/controllers/api/account.php | 4 +- app/init/registers.php | 1 - src/Appwrite/Auth/Validator/PasswordPwned.php | 36 ++-- .../Organization/Http/Projects/Create.php | 5 +- .../Project/Http/Project/Policies/Get.php | 5 +- .../Project/Policies/PasswordPwned/Update.php | 36 +--- .../Project/Http/Project/Policies/XList.php | 5 +- .../Modules/Projects/Http/Projects/Create.php | 5 +- .../Response/Model/PolicyPasswordPwned.php | 22 +- tests/e2e/Services/Project/PoliciesBase.php | 54 +---- .../PoliciesPasswordPwnedIntegrationTest.php | 202 ++---------------- .../unit/Auth/Validator/PasswordPwnedTest.php | 67 ++---- 12 files changed, 71 insertions(+), 371 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 38b260b718..9256ab59c7 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1050,7 +1050,7 @@ Http::post('/v1/account/sessions/email') if ($pwnedPasswords->checksSessions()) { // The outcome is recorded either way; only a forced reset needs an answer, so an outage never blocks a plain sign-in - $passwordPwned = $pwnedPasswords->check($password, required: $pwnedPasswords->isForceReset()); + $passwordPwned = $pwnedPasswords->check($password); if ($passwordPwned !== null && $passwordPwned !== $user->getAttribute('passwordPwned')) { $user->setAttribute('passwordPwned', $passwordPwned); @@ -1059,7 +1059,7 @@ Http::post('/v1/account/sessions/email') ])); } - if ($passwordPwned && $pwnedPasswords->isForceReset()) { + if ($passwordPwned && $pwnedPasswords->blocksUsers()) { throw new Exception(Exception::USER_PASSWORD_RESET_REQUIRED); } } diff --git a/app/init/registers.php b/app/init/registers.php index 3c7c39c697..22d49647cb 100644 --- a/app/init/registers.php +++ b/app/init/registers.php @@ -35,7 +35,6 @@ if (!Http::isProduction()) { // Useful for existing tests involving webhooks PublicDomain::allow(['request-catcher-sms']); PublicDomain::allow(['request-catcher-webhook']); - PublicDomain::allow(['localhost']); // Lets the pwned passwords policy point at the mock range endpoint } $register->set('pools', function () { diff --git a/src/Appwrite/Auth/Validator/PasswordPwned.php b/src/Appwrite/Auth/Validator/PasswordPwned.php index d50101fe74..ef2bc680dc 100644 --- a/src/Appwrite/Auth/Validator/PasswordPwned.php +++ b/src/Appwrite/Auth/Validator/PasswordPwned.php @@ -26,27 +26,23 @@ class PasswordPwned extends Password protected bool $enabled; protected string $endpoint; - protected int $threshold; protected bool $sessions; - protected bool $forceReset; - protected bool $failClosed; + protected bool $users; protected ?Cache $cache; protected Client $client; /** * @param array $policy the project's `passwordPwned` auth settings - * @param ?string $endpoint server-wide endpoint, used when the policy sets none + * @param ?string $endpoint the breach API this server is configured to use */ public function __construct(array $policy = [], ?Cache $cache = null, ?string $endpoint = null, ?Client $client = null, bool $allowEmpty = false) { parent::__construct($allowEmpty); $this->enabled = (bool) ($policy['enabled'] ?? true); - $this->endpoint = \rtrim(($policy['endpoint'] ?? '') ?: ($endpoint ?: self::ENDPOINT), '/'); - $this->threshold = \max(1, (int) ($policy['threshold'] ?? 1)); $this->sessions = (bool) ($policy['sessions'] ?? false); - $this->forceReset = (bool) ($policy['forceReset'] ?? false); - $this->failClosed = (bool) ($policy['failClosed'] ?? false); + $this->users = (bool) ($policy['users'] ?? false); + $this->endpoint = \rtrim($endpoint ?: self::ENDPOINT, '/'); $this->cache = $cache; $this->client = $client ?? (new Client()) ->setConnectTimeout(self::CONNECT_TIMEOUT) @@ -72,13 +68,13 @@ class PasswordPwned extends Password } /** - * Whether a breached password blocks sign-in until it is reset. + * Whether users signing in with a breached password are blocked until they reset it. * * Only takes effect when sessions are checked. */ - public function isForceReset(): bool + public function blocksUsers(): bool { - return $this->forceReset; + return $this->users; } /** @@ -94,14 +90,13 @@ class PasswordPwned extends Password } /** - * Whether the password appears in at least `threshold` known breaches. + * Whether the password appears in a known breach. * - * Returns null when the policy is disabled, or when the service could not - * be reached and the answer is not required by a fail-closed policy. + * Returns null only when the policy is disabled. * - * @throws Exception when the answer is required, the policy fails closed, and the service is unreachable + * @throws Exception when the breach service cannot be reached */ - public function check(string $password, bool $required = true): ?bool + public function check(string $password): ?bool { if (!$this->enabled) { return null; @@ -114,14 +109,11 @@ class PasswordPwned extends Password $breaches = $this->range($prefix); if ($breaches === null) { - if ($required && $this->failClosed) { - throw new Exception(Exception::GENERAL_PWNED_PASSWORDS_UNAVAILABLE); - } - - return null; + throw new Exception(Exception::GENERAL_PWNED_PASSWORDS_UNAVAILABLE); } - return ($breaches[$suffix] ?? 0) >= $this->threshold; + // Entries with no breaches are dropped, so any hit means the password is breached + return isset($breaches[$suffix]); } /** diff --git a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php index c000d212ae..10d846791f 100644 --- a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php +++ b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php @@ -93,11 +93,8 @@ class Create extends Action 'personalDataCheck' => false, 'passwordPwned' => [ 'enabled' => true, - 'endpoint' => '', - 'threshold' => 1, 'sessions' => false, - 'forceReset' => false, - 'failClosed' => false, + 'users' => false, ], 'disposableEmails' => false, 'canonicalEmails' => false, diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/Get.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/Get.php index 3a02a71b9f..9984257c66 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/Get.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/Get.php @@ -138,11 +138,8 @@ class Get extends Action 'password-pwned' => [ new Document(\array_merge([ 'enabled' => true, - 'endpoint' => '', - 'threshold' => 1, 'sessions' => false, - 'forceReset' => false, - 'failClosed' => false, + 'users' => false, ], $auths['passwordPwned'] ?? [], [ '$id' => 'password-pwned', ])), diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPwned/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPwned/Update.php index 965cbf3e5e..08b7e5fef8 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPwned/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPwned/Update.php @@ -11,14 +11,8 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Domains\Validator\PublicDomain; use Utopia\Platform\Scope\HTTP; -use Utopia\Validator\AnyOf; use Utopia\Validator\Boolean; -use Utopia\Validator\Multiple; -use Utopia\Validator\Range; -use Utopia\Validator\URL; -use Utopia\Validator\WhiteList; class Update extends Action { @@ -45,7 +39,7 @@ class Update extends Action group: 'policies', name: 'updatePasswordPwnedPolicy', description: <<param('enabled', null, new Boolean(), 'Toggle password pwned policy. Set to true to block passwords exposed in known data breaches, or false to allow them. Default is true. When changing this policy, existing passwords remain valid.', optional: true) - ->param('endpoint', null, new AnyOf([new WhiteList([''], true), new Multiple([new URL(['http', 'https']), new PublicDomain()], Multiple::TYPE_STRING)], AnyOf::TYPE_STRING), 'Custom endpoint of a Have I Been Pwned compatible range API, for example a self-hosted mirror. Pass an empty string to use the server default.', optional: true) - ->param('threshold', null, new Range(1, PHP_INT_MAX), 'Minimum number of known breaches a password must appear in before it is rejected. Default is 1.', optional: true) ->param('sessions', null, new Boolean(), 'Whether passwords are checked when a session is created. When enabled, signing in records whether the password appears in a known data breach. Default is false.', optional: true) - ->param('forceReset', null, new Boolean(), 'Whether signing in with a breached password is blocked until the password is reset. Only applies when sessions are checked. Default is false, which allows the sign-in.', optional: true) - ->param('failClosed', null, new Boolean(), 'Whether passwords are rejected when the breach service cannot be reached. Default is false, which skips the check so an outage never blocks your users. Set to true to reject the password instead.', optional: true) + ->param('users', null, new Boolean(), 'Whether users signing in with a breached password are blocked until they reset it. Only applies when sessions are checked. Default is false, which allows the sign-in.', optional: true) ->inject('response') ->inject('dbForPlatform') ->inject('project') @@ -71,11 +62,8 @@ class Update extends Action public function action( ?bool $enabled, - ?string $endpoint, - ?int $threshold, ?bool $sessions, - ?bool $forceReset, - ?bool $failClosed, + ?bool $users, Response $response, Database $dbForPlatform, Document $project, @@ -85,30 +73,18 @@ class Update extends Action $auths = $project->getAttribute('auths', []); $auths['passwordPwned'] = \array_merge([ 'enabled' => true, - 'endpoint' => '', - 'threshold' => 1, 'sessions' => false, - 'forceReset' => false, - 'failClosed' => false, + 'users' => false, ], $auths['passwordPwned'] ?? []); if ($enabled !== null) { $auths['passwordPwned']['enabled'] = $enabled; } - if ($endpoint !== null) { - $auths['passwordPwned']['endpoint'] = $endpoint; - } - if ($threshold !== null) { - $auths['passwordPwned']['threshold'] = $threshold; - } if ($sessions !== null) { $auths['passwordPwned']['sessions'] = $sessions; } - if ($forceReset !== null) { - $auths['passwordPwned']['forceReset'] = $forceReset; - } - if ($failClosed !== null) { - $auths['passwordPwned']['failClosed'] = $failClosed; + if ($users !== null) { + $auths['passwordPwned']['users'] = $users; } $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/XList.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/XList.php index b13541a8eb..32bbaa2e7d 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/XList.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/XList.php @@ -118,11 +118,8 @@ class XList extends Action ]), new Document(\array_merge([ 'enabled' => true, - 'endpoint' => '', - 'threshold' => 1, 'sessions' => false, - 'forceReset' => false, - 'failClosed' => false, + 'users' => false, ], $auths['passwordPwned'] ?? [], [ '$id' => 'password-pwned', ])), diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php index 6a9e4f28f1..f5a689adeb 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php @@ -90,11 +90,8 @@ class Create extends Action 'personalDataCheck' => false, 'passwordPwned' => [ 'enabled' => true, - 'endpoint' => '', - 'threshold' => 1, 'sessions' => false, - 'forceReset' => false, - 'failClosed' => false, + 'users' => false, ], 'disposableEmails' => false, 'canonicalEmails' => false, diff --git a/src/Appwrite/Utopia/Response/Model/PolicyPasswordPwned.php b/src/Appwrite/Utopia/Response/Model/PolicyPasswordPwned.php index 6fe844740e..d1352e078e 100644 --- a/src/Appwrite/Utopia/Response/Model/PolicyPasswordPwned.php +++ b/src/Appwrite/Utopia/Response/Model/PolicyPasswordPwned.php @@ -21,33 +21,15 @@ class PolicyPasswordPwned extends PolicyBase 'default' => true, 'example' => true, ]) - ->addRule('endpoint', [ - 'type' => self::TYPE_STRING, - 'description' => 'Custom endpoint of a Have I Been Pwned compatible range API. Empty when the server default is used.', - 'default' => '', - 'example' => 'https://api.pwnedpasswords.com/range', - ]) - ->addRule('threshold', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Minimum number of known breaches a password must appear in before it is rejected.', - 'default' => 1, - 'example' => 1, - ]) ->addRule('sessions', [ 'type' => self::TYPE_BOOLEAN, 'description' => 'Whether passwords are checked when a session is created.', 'default' => false, 'example' => false, ]) - ->addRule('forceReset', [ + ->addRule('users', [ 'type' => self::TYPE_BOOLEAN, - 'description' => 'Whether signing in with a breached password is blocked until the password is reset. Only applies when sessions are checked.', - 'default' => false, - 'example' => false, - ]) - ->addRule('failClosed', [ - 'type' => self::TYPE_BOOLEAN, - 'description' => 'Whether passwords are rejected when the breach service cannot be reached. When false, the check is skipped instead.', + 'description' => 'Whether users signing in with a breached password are blocked until they reset it. Only applies when sessions are checked.', 'default' => false, 'example' => false, ]); diff --git a/tests/e2e/Services/Project/PoliciesBase.php b/tests/e2e/Services/Project/PoliciesBase.php index 3a195e6232..a50d8c707b 100644 --- a/tests/e2e/Services/Project/PoliciesBase.php +++ b/tests/e2e/Services/Project/PoliciesBase.php @@ -17,7 +17,7 @@ trait PoliciesBase 'password-history' => ['total'], 'password-strength' => ['min', 'uppercase', 'lowercase', 'number', 'symbols'], 'password-personal-data' => ['enabled'], - 'password-pwned' => ['enabled', 'endpoint', 'threshold', 'sessions', 'forceReset', 'failClosed'], + 'password-pwned' => ['enabled', 'sessions', 'users'], 'session-alert' => ['enabled'], 'session-duration' => ['duration'], 'session-invalidation' => ['enabled'], @@ -650,11 +650,8 @@ trait PoliciesBase $policy = $this->getPolicy('password-pwned'); $this->assertSame(200, $policy['headers']['status-code']); $this->assertSame(true, $policy['body']['enabled']); - $this->assertSame('', $policy['body']['endpoint']); - $this->assertSame(1, $policy['body']['threshold']); $this->assertSame(false, $policy['body']['sessions']); - $this->assertSame(false, $policy['body']['forceReset']); - $this->assertSame(false, $policy['body']['failClosed']); + $this->assertSame(false, $policy['body']['users']); // Cleanup $this->updatePasswordPwnedPolicy(['enabled' => false]); @@ -682,11 +679,8 @@ trait PoliciesBase $this->assertSame(200, $policy['headers']['status-code']); $this->assertSame('password-pwned', $policy['body']['$id']); $this->assertSame(true, $policy['body']['enabled']); - $this->assertSame('', $policy['body']['endpoint']); - $this->assertSame(1, $policy['body']['threshold']); $this->assertSame(false, $policy['body']['sessions']); - $this->assertSame(false, $policy['body']['forceReset']); - $this->assertSame(false, $policy['body']['failClosed']); + $this->assertSame(false, $policy['body']['users']); } public function testUpdatePasswordPwnedPolicySessions(): void @@ -709,11 +703,8 @@ trait PoliciesBase public function testUpdatePasswordPwnedPolicyOptionPreservesOtherOptions(): void { $response = $this->updatePasswordPwnedPolicy([ - 'endpoint' => 'https://example.com/range', - 'threshold' => 5, 'sessions' => true, - 'forceReset' => true, - 'failClosed' => true, + 'users' => true, ]); $this->assertSame(200, $response['headers']['status-code']); @@ -724,11 +715,8 @@ trait PoliciesBase $policy = $this->getPolicy('password-pwned'); $this->assertSame(true, $policy['body']['enabled']); - $this->assertSame('https://example.com/range', $policy['body']['endpoint']); - $this->assertSame(5, $policy['body']['threshold']); $this->assertSame(true, $policy['body']['sessions']); - $this->assertSame(true, $policy['body']['forceReset']); - $this->assertSame(true, $policy['body']['failClosed']); + $this->assertSame(true, $policy['body']['users']); // Cleanup $this->resetPasswordPwnedPolicy(); @@ -744,40 +732,17 @@ trait PoliciesBase $policy = $this->getPolicy('password-pwned'); $this->assertSame(true, $policy['body']['enabled']); - $this->assertSame('', $policy['body']['endpoint']); - $this->assertSame(1, $policy['body']['threshold']); $this->assertSame(false, $policy['body']['sessions']); - $this->assertSame(false, $policy['body']['forceReset']); - $this->assertSame(false, $policy['body']['failClosed']); + $this->assertSame(false, $policy['body']['users']); } public function testUpdatePasswordPwnedPolicyInvalidType(): void { - foreach (['enabled', 'sessions', 'forceReset', 'failClosed'] as $option) { + foreach (['enabled', 'sessions', 'users'] as $option) { $response = $this->updatePasswordPwnedPolicy([$option => 'not-a-boolean']); $this->assertSame(400, $response['headers']['status-code'], $option); } - - $response = $this->updatePasswordPwnedPolicy(['threshold' => 'not-a-number']); - - $this->assertSame(400, $response['headers']['status-code']); - } - - public function testUpdatePasswordPwnedPolicyInvalidThreshold(): void - { - $response = $this->updatePasswordPwnedPolicy(['threshold' => 0]); - - $this->assertSame(400, $response['headers']['status-code']); - } - - public function testUpdatePasswordPwnedPolicyInvalidEndpoint(): void - { - foreach (['not-a-url', 'ftp://example.com/range', 'http://192.168.1.1/range'] as $endpoint) { - $response = $this->updatePasswordPwnedPolicy(['endpoint' => $endpoint]); - - $this->assertSame(400, $response['headers']['status-code'], $endpoint); - } } public function testUpdatePasswordPwnedPolicyWithoutAuth(): void @@ -1493,11 +1458,8 @@ trait PoliciesBase { $this->updatePasswordPwnedPolicy([ 'enabled' => true, - 'endpoint' => '', - 'threshold' => 1, 'sessions' => false, - 'forceReset' => false, - 'failClosed' => false, + 'users' => false, ]); } diff --git a/tests/e2e/Services/Project/PoliciesPasswordPwnedIntegrationTest.php b/tests/e2e/Services/Project/PoliciesPasswordPwnedIntegrationTest.php index e7d8fc57d3..c52e3602a3 100644 --- a/tests/e2e/Services/Project/PoliciesPasswordPwnedIntegrationTest.php +++ b/tests/e2e/Services/Project/PoliciesPasswordPwnedIntegrationTest.php @@ -20,15 +20,8 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope use ProjectCustom; use SideServer; - // Reported as breached by the mock range endpoint, 12345 and 9 times respectively + // Reported as breached by the mock range endpoint private const PWNED_PASSWORD = 'pwned-fixture-common'; - private const RARELY_PWNED_PASSWORD = 'pwned-fixture-rare'; - - // The mock range endpoint the stack already uses through _APP_PWNED_PASSWORDS_ENDPOINT - private const MOCK_ENDPOINT = 'http://localhost/v1/mock/tests/general/pwned-passwords'; - - // Nothing listens here, so lookups fail - private const DEAD_ENDPOINT = 'http://localhost:1/range'; public function testDefaultsOnNewProject(): void { @@ -48,13 +41,10 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope // Breached passwords are rejected out of the box $this->assertTrue($response['body']['enabled']); - $this->assertFalse($response['body']['failClosed']); - $this->assertSame(1, $response['body']['threshold']); - $this->assertSame('', $response['body']['endpoint']); // Sign-in checks stay opt-in $this->assertFalse($response['body']['sessions']); - $this->assertFalse($response['body']['forceReset']); + $this->assertFalse($response['body']['users']); // A breached password is blocked without touching the policy $response = $this->client->call(Client::METHOD_POST, '/users', $headers, [ @@ -318,160 +308,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope $this->updatePolicy(['enabled' => false]); } - public function testThreshold(): void - { - $this->updatePolicy(['enabled' => true, 'threshold' => 10]); - - /** - * Test for SUCCESS - */ - $response = $this->client->call(Client::METHOD_POST, '/users', $this->serverHeaders(), [ - 'userId' => ID::unique(), - 'email' => 'pwned_rare_' . \uniqid() . '@localhost.test', - 'password' => self::RARELY_PWNED_PASSWORD, - 'name' => 'Rarely Pwned User', - ]); - - $this->assertSame(201, $response['headers']['status-code']); - - /** - * Test for FAILURE - */ - $response = $this->client->call(Client::METHOD_POST, '/users', $this->serverHeaders(), [ - 'userId' => ID::unique(), - 'email' => 'pwned_often_' . \uniqid() . '@localhost.test', - 'password' => self::PWNED_PASSWORD, - 'name' => 'Often Pwned User', - ]); - - $this->assertSame(400, $response['headers']['status-code']); - $this->assertSame('password_pwned', $response['body']['type']); - - $this->updatePolicy(['enabled' => false, 'threshold' => 1]); - } - - public function testCustomEndpoint(): void - { - $this->updatePolicy(['enabled' => true, 'endpoint' => self::MOCK_ENDPOINT]); - - /** - * Test for FAILURE - */ - $response = $this->client->call(Client::METHOD_POST, '/users', $this->serverHeaders(), [ - 'userId' => ID::unique(), - 'email' => 'pwned_endpoint_' . \uniqid() . '@localhost.test', - 'password' => self::PWNED_PASSWORD, - 'name' => 'Endpoint User', - ]); - - $this->assertSame(400, $response['headers']['status-code']); - $this->assertSame('password_pwned', $response['body']['type']); - - /** - * Test for SUCCESS - */ - $response = $this->client->call(Client::METHOD_POST, '/users', $this->serverHeaders(), [ - 'userId' => ID::unique(), - 'email' => 'pwned_endpoint_clean_' . \uniqid() . '@localhost.test', - 'password' => $this->cleanPassword(), - 'name' => 'Endpoint Clean User', - ]); - - $this->assertSame(201, $response['headers']['status-code']); - - // An empty endpoint falls back to the server configuration - $this->updatePolicy(['endpoint' => '']); - - $response = $this->client->call(Client::METHOD_POST, '/users', $this->serverHeaders(), [ - 'userId' => ID::unique(), - 'email' => 'pwned_fallback_' . \uniqid() . '@localhost.test', - 'password' => self::PWNED_PASSWORD, - 'name' => 'Fallback User', - ]); - - $this->assertSame(400, $response['headers']['status-code']); - $this->assertSame('password_pwned', $response['body']['type']); - - $this->updatePolicy(['enabled' => false]); - } - - public function testFailClosed(): void - { - $this->updatePolicy(['enabled' => true, 'endpoint' => self::DEAD_ENDPOINT, 'failClosed' => true]); - - /** - * Test for FAILURE - */ - $response = $this->client->call(Client::METHOD_POST, '/users', $this->serverHeaders(), [ - 'userId' => ID::unique(), - 'email' => 'pwned_outage_' . \uniqid() . '@localhost.test', - 'password' => $this->cleanPassword(), - 'name' => 'Outage User', - ]); - - $this->assertSame(503, $response['headers']['status-code']); - $this->assertSame('general_pwned_passwords_unavailable', $response['body']['type']); - - $response = $this->client->call(Client::METHOD_POST, '/account', $this->clientHeaders(), [ - 'userId' => ID::unique(), - 'email' => 'pwned_outage_account_' . \uniqid() . '@localhost.test', - 'password' => $this->cleanPassword(), - 'name' => 'Outage Account', - ]); - - $this->assertSame(503, $response['headers']['status-code']); - $this->assertSame('general_pwned_passwords_unavailable', $response['body']['type']); - - // Forced reset needs the lookup, so sign-in fails closed as well - $this->updatePolicy(['enabled' => false]); - - $email = 'pwned_outage_signin_' . \uniqid() . '@localhost.test'; - $password = $this->cleanPassword(); - - $user = $this->client->call(Client::METHOD_POST, '/users', $this->serverHeaders(), [ - 'userId' => ID::unique(), - 'email' => $email, - 'password' => $password, - 'name' => 'Outage Sign In User', - ]); - - $this->assertSame(201, $user['headers']['status-code']); - - $this->updatePolicy(['enabled' => true, 'sessions' => true, 'forceReset' => true]); - - $response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', $this->clientHeaders(), [ - 'email' => $email, - 'password' => $password, - ]); - - $this->assertSame(503, $response['headers']['status-code']); - $this->assertSame('general_pwned_passwords_unavailable', $response['body']['type']); - - /** - * Test for SUCCESS - */ - $this->updatePolicy(['failClosed' => false]); - - $response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', $this->clientHeaders(), [ - 'email' => $email, - 'password' => $password, - ]); - - $this->assertSame(201, $response['headers']['status-code']); - - $response = $this->client->call(Client::METHOD_POST, '/users', $this->serverHeaders(), [ - 'userId' => ID::unique(), - 'email' => 'pwned_outage_open_' . \uniqid() . '@localhost.test', - 'password' => self::PWNED_PASSWORD, - 'name' => 'Outage Open User', - ]); - - $this->assertSame(201, $response['headers']['status-code']); - - $this->updatePolicy(['enabled' => false, 'endpoint' => '', 'sessions' => false, 'forceReset' => false, 'failClosed' => false]); - } - - public function testForceResetOnSignIn(): void + public function testSignInBlockedUntilReset(): void { $this->updatePolicy(['enabled' => false]); @@ -489,7 +326,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope /** * Test for SUCCESS */ - $this->updatePolicy(['enabled' => true, 'sessions' => true, 'forceReset' => false]); + $this->updatePolicy(['enabled' => true, 'sessions' => true, 'users' => false]); $response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', $this->clientHeaders(), [ 'email' => $email, @@ -501,7 +338,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope /** * Test for FAILURE */ - $this->updatePolicy(['forceReset' => true]); + $this->updatePolicy(['users' => true]); $response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', $this->clientHeaders(), [ 'email' => $email, @@ -538,7 +375,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope $this->assertSame(201, $response['headers']['status-code']); - $this->updatePolicy(['enabled' => false, 'sessions' => false, 'forceReset' => false]); + $this->updatePolicy(['enabled' => false, 'sessions' => false, 'users' => false]); } public function testSessionChecksAreOptional(): void @@ -561,7 +398,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope * Test for SUCCESS */ // Sign-in checks are off by default, so a forced reset has nothing to act on - $this->updatePolicy(['enabled' => true, 'sessions' => false, 'forceReset' => true]); + $this->updatePolicy(['enabled' => true, 'sessions' => false, 'users' => true]); $response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', $this->clientHeaders(), [ 'email' => $email, @@ -577,7 +414,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope $this->assertNull($response['body']['passwordPwned']); // Checking sessions without forcing a reset records the breach and still allows the sign-in - $this->updatePolicy(['sessions' => true, 'forceReset' => false]); + $this->updatePolicy(['sessions' => true, 'users' => false]); $response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', $this->clientHeaders(), [ 'email' => $email, @@ -594,7 +431,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope /** * Test for FAILURE */ - $this->updatePolicy(['forceReset' => true]); + $this->updatePolicy(['users' => true]); $response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', $this->clientHeaders(), [ 'email' => $email, @@ -604,7 +441,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope $this->assertSame(412, $response['headers']['status-code']); $this->assertSame('user_password_reset_required', $response['body']['type']); - $this->updatePolicy(['enabled' => false, 'sessions' => false, 'forceReset' => false]); + $this->updatePolicy(['enabled' => false, 'sessions' => false, 'users' => false]); } public function testUpdateAnonymousAccountEmail(): void @@ -1116,7 +953,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope $this->assertSame(201, $userD['headers']['status-code']); $this->assertNull($userD['body']['passwordPwned']); - $this->updatePolicy(['enabled' => true, 'sessions' => true, 'forceReset' => true]); + $this->updatePolicy(['enabled' => true, 'sessions' => true, 'users' => true]); $response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', $this->clientHeaders(), [ 'email' => $emailD, @@ -1131,21 +968,6 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope $this->assertSame(200, $response['headers']['status-code']); $this->assertTrue($response['body']['passwordPwned']); - // An outage with a fail-open policy leaves the recorded state untouched and lets the sign-in through - $this->updatePolicy(['endpoint' => self::DEAD_ENDPOINT, 'failClosed' => false]); - - $response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', $this->clientHeaders(), [ - 'email' => $emailD, - 'password' => self::PWNED_PASSWORD, - ]); - - $this->assertSame(201, $response['headers']['status-code']); - - $response = $this->client->call(Client::METHOD_GET, '/users/' . $userD['body']['$id'], $this->serverHeaders()); - - $this->assertSame(200, $response['headers']['status-code']); - $this->assertTrue($response['body']['passwordPwned']); - // Filtering on an attribute outside the whitelist is rejected $response = $this->client->call(Client::METHOD_GET, '/users', $this->serverHeaders(), [ 'queries' => [Query::equal('passwordHistory', ['x'])->toString()], @@ -1153,7 +975,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope $this->assertSame(400, $response['headers']['status-code']); - $this->updatePolicy(['enabled' => false, 'endpoint' => '', 'sessions' => false, 'forceReset' => false, 'failClosed' => false]); + $this->updatePolicy(['enabled' => false, 'sessions' => false, 'users' => false]); } /** diff --git a/tests/unit/Auth/Validator/PasswordPwnedTest.php b/tests/unit/Auth/Validator/PasswordPwnedTest.php index 167069e56e..6be2387956 100644 --- a/tests/unit/Auth/Validator/PasswordPwnedTest.php +++ b/tests/unit/Auth/Validator/PasswordPwnedTest.php @@ -25,21 +25,21 @@ final class PasswordPwnedTest extends TestCase $this->assertTrue($validator->isEnabled()); $this->assertFalse($validator->checksSessions()); - $this->assertFalse($validator->isForceReset()); + $this->assertFalse($validator->blocksUsers()); } public function testPolicyFlags(): void { - $validator = new PasswordPwned(['enabled' => true, 'sessions' => true, 'forceReset' => true]); + $validator = new PasswordPwned(['enabled' => true, 'sessions' => true, 'users' => true]); $this->assertTrue($validator->isEnabled()); $this->assertTrue($validator->checksSessions()); - $this->assertTrue($validator->isForceReset()); + $this->assertTrue($validator->blocksUsers()); } public function testSessionChecksNeedAnEnabledPolicy(): void { - $validator = new PasswordPwned(['enabled' => false, 'sessions' => true, 'forceReset' => true]); + $validator = new PasswordPwned(['enabled' => false, 'sessions' => true, 'users' => true]); $this->assertFalse($validator->checksSessions()); } @@ -63,14 +63,6 @@ final class PasswordPwnedTest extends TestCase $this->assertFalse($validator->check('never-leaked-' . \uniqid())); } - public function testCheckNotRequiredReturnsUnknownOnOutage(): void - { - $adapter = new RangeAdapter(failure: new \RuntimeException('connection refused')); - $validator = $this->validator(['failClosed' => true], $adapter); - - $this->assertNull($validator->check(self::LEAKED, required: false)); - } - public function testLeakedPasswordIsRejected(): void { $adapter = new RangeAdapter(body: $this->range([self::LEAKED => 42])); @@ -103,12 +95,11 @@ final class PasswordPwnedTest extends TestCase $this->assertFalse($validator->isValid(self::LEAKED)); } - public function testThresholdToleratesRareBreaches(): void + public function testAnySingleBreachIsEnough(): void { - $adapter = new RangeAdapter(body: $this->range([self::LEAKED => 9])); + $adapter = new RangeAdapter(body: $this->range([self::LEAKED => 1])); - $this->assertTrue($this->validator(['threshold' => 10], $adapter)->isValid(self::LEAKED)); - $this->assertFalse($this->validator(['threshold' => 9], $adapter)->isValid(self::LEAKED)); + $this->assertFalse($this->validator([], $adapter)->isValid(self::LEAKED)); } public function testOnlyTheHashPrefixLeavesTheServer(): void @@ -156,40 +147,23 @@ final class PasswordPwnedTest extends TestCase $this->assertCount(2, $adapter->requests); } - public function testUnreachableServiceSkipsTheCheckByDefault(): void + public function testUnreachableServiceIsReported(): void { $adapter = new RangeAdapter(failure: new \RuntimeException('connection refused')); $validator = $this->validator([], $adapter); - // An outage of the breach service must not stop people setting a password - $this->assertTrue($validator->isValid(self::LEAKED)); - } - - public function testUnexpectedStatusSkipsTheCheckByDefault(): void - { - $adapter = new RangeAdapter(statusCode: 503, body: ''); - $validator = $this->validator([], $adapter); - - $this->assertTrue($validator->isValid(self::LEAKED)); - } - - public function testUnreachableServiceIsRejectedWhenFailingClosed(): void - { - $adapter = new RangeAdapter(failure: new \RuntimeException('connection refused')); - $validator = $this->validator(['failClosed' => true], $adapter); - try { $validator->isValid(self::LEAKED); - $this->fail('Expected the check to fail closed'); + $this->fail('Expected the unreachable service to surface'); } catch (Exception $e) { $this->assertSame(Exception::GENERAL_PWNED_PASSWORDS_UNAVAILABLE, $e->getType()); } } - public function testUnexpectedStatusIsRejectedWhenFailingClosed(): void + public function testUnexpectedStatusIsReported(): void { $adapter = new RangeAdapter(statusCode: 503, body: ''); - $validator = $this->validator(['failClosed' => true], $adapter); + $validator = $this->validator([], $adapter); $this->expectException(Exception::class); @@ -202,8 +176,13 @@ final class PasswordPwnedTest extends TestCase $cache = new Cache(new Memory()); $validator = new PasswordPwned(['enabled' => true], $cache, self::ENDPOINT, new Client($adapter)); - $validator->isValid(self::LEAKED); - $validator->isValid(self::LEAKED); + foreach ([1, 2] as $attempt) { + try { + $validator->isValid(self::LEAKED); + } catch (Exception) { + // The lookup failed, which is the point; it must be retried rather than remembered + } + } $this->assertCount(2, $adapter->requests); } @@ -228,15 +207,15 @@ final class PasswordPwnedTest extends TestCase $this->assertCount(0, $adapter->requests); } - public function testPolicyEndpointOverridesTheServerEndpoint(): void + public function testLookupsGoToTheConfiguredEndpoint(): void { $adapter = new RangeAdapter(body: ''); - (new PasswordPwned(['enabled' => true, 'endpoint' => 'https://policy.test/range/'], null, 'https://server.test/range', new Client($adapter)))->isValid(self::LEAKED); - (new PasswordPwned(['enabled' => true, 'endpoint' => ''], null, 'https://server.test/range', new Client($adapter)))->isValid(self::LEAKED); + (new PasswordPwned(['enabled' => true], null, 'https://server.test/range', new Client($adapter)))->isValid(self::LEAKED); + (new PasswordPwned(['enabled' => true], null, 'https://other.test/range/', new Client($adapter)))->isValid(self::LEAKED); - $this->assertStringStartsWith('https://policy.test/range/', $adapter->requests[0]); - $this->assertStringStartsWith('https://server.test/range/', $adapter->requests[1]); + $this->assertStringStartsWith('https://server.test/range/', $adapter->requests[0]); + $this->assertStringStartsWith('https://other.test/range/', $adapter->requests[1]); } /**