diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index e665d9cb43..0eab71cbd8 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -364,7 +364,9 @@ Http::post('/v1/account') } // null when the policy did not look, false when it looked and found nothing - $passwordPwned = $pwnedPasswords->isEnabled() ? !$pwnedPasswords->isValid($password) : null; + $passwordPwned = ($project->getAttribute('auths', [])['passwordPwned']['enabled'] ?? true) + ? !$pwnedPasswords->isValid($password) + : null; if ($passwordPwned) { throw new Exception(Exception::USER_PASSWORD_PWNED); } @@ -1049,7 +1051,9 @@ Http::post('/v1/account/sessions/email') $authorization->addRole(Role::user($user->getId())->toString()); - if ($pwnedPasswords->checksSessions()) { + $pwnedPolicy = $project->getAttribute('auths', [])['passwordPwned'] ?? []; + + if (($pwnedPolicy['enabled'] ?? true) && ($pwnedPolicy['sessions'] ?? false)) { // The outcome is recorded either way; only a forced reset needs an answer, so an outage never blocks a plain sign-in $passwordPwned = !$pwnedPasswords->isValid($password); @@ -1060,7 +1064,7 @@ Http::post('/v1/account/sessions/email') ])); } - if ($passwordPwned && $pwnedPasswords->blocksUsers()) { + if ($passwordPwned && ($pwnedPolicy['users'] ?? false)) { throw new Exception(Exception::USER_PASSWORD_RESET_REQUIRED); } } @@ -3511,7 +3515,9 @@ Http::patch('/v1/account/password') } // null when the policy did not look, false when it looked and found nothing - $passwordPwned = $pwnedPasswords->isEnabled() ? !$pwnedPasswords->isValid($password) : null; + $passwordPwned = ($project->getAttribute('auths', [])['passwordPwned']['enabled'] ?? true) + ? !$pwnedPasswords->isValid($password) + : null; if ($passwordPwned) { throw new Exception(Exception::USER_PASSWORD_PWNED); } @@ -3615,7 +3621,9 @@ Http::patch('/v1/account/email') } } - $passwordPwned = $pwnedPasswords->isEnabled() ? !$pwnedPasswords->isValid($password) : null; + $passwordPwned = ($project->getAttribute('auths', [])['passwordPwned']['enabled'] ?? true) + ? !$pwnedPasswords->isValid($password) + : null; if ($passwordPwned) { throw new Exception(Exception::USER_PASSWORD_PWNED); } @@ -3789,7 +3797,9 @@ Http::patch('/v1/account/phone') } } - $passwordPwned = $pwnedPasswords->isEnabled() ? !$pwnedPasswords->isValid($password) : null; + $passwordPwned = ($project->getAttribute('auths', [])['passwordPwned']['enabled'] ?? true) + ? !$pwnedPasswords->isValid($password) + : null; if ($passwordPwned) { throw new Exception(Exception::USER_PASSWORD_PWNED); } @@ -4237,7 +4247,9 @@ Http::put('/v1/account/recovery') } // null when the policy did not look, false when it looked and found nothing - $passwordPwned = $pwnedPasswords->isEnabled() ? !$pwnedPasswords->isValid($password) : null; + $passwordPwned = ($project->getAttribute('auths', [])['passwordPwned']['enabled'] ?? true) + ? !$pwnedPasswords->isValid($password) + : null; if ($passwordPwned) { throw new Exception(Exception::USER_PASSWORD_PWNED); } diff --git a/app/init/resources/request.php b/app/init/resources/request.php index ca3b559721..21a02cd696 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -677,21 +677,19 @@ return function (Container $context): void { return; }, ['user', 'store', 'proofForToken']); - $context->set('pwnedPasswords', function (Document $project, Cache $cache) { + $context->set('pwnedPasswords', function (Cache $cache) { $dsn = new DSN(System::getEnv('_APP_PWNED_PASSWORDS_DSN', 'hibp://localhost')); - $policy = $project->getAttribute('auths', [])['passwordPwned'] ?? []; - return match ($dsn->getScheme()) { - 'hibp' => new PasswordPwnedHIBP($policy, $cache), - 'appwrite' => new PasswordPwnedAppwrite($policy, $dsn), + 'hibp' => new PasswordPwnedHIBP($cache), + 'appwrite' => new PasswordPwnedAppwrite($dsn), // Reports almost every password as safe, so it must never be reachable on a real server 'mock' => Http::isProduction() ? throw new Exception(Exception::GENERAL_SERVER_ERROR, 'The mock breach validator cannot be used in production.') - : new PasswordPwnedMock($policy), + : new PasswordPwnedMock(), default => throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Unknown _APP_PWNED_PASSWORDS_DSN scheme: ' . $dsn->getScheme()), }; - }, ['project', 'cache']); + }, ['cache']); $context->set('dbForProject', function (DatabaseFactory $databaseFactory, Database $dbForPlatform, Document $project, Response $response, Publisher $publisher, Event $queueForEvents, FunctionPublisher $publisherForFunctions, Webhook $queueForWebhooks, Realtime $queueForRealtime, UsageContext $usage, Request $request) { if ($project->isEmpty() || $project->getId() === 'console') { diff --git a/src/Appwrite/Auth/Validator/PasswordPwned.php b/src/Appwrite/Auth/Validator/PasswordPwned.php index 649d21948d..225c38102d 100644 --- a/src/Appwrite/Auth/Validator/PasswordPwned.php +++ b/src/Appwrite/Auth/Validator/PasswordPwned.php @@ -6,59 +6,19 @@ use Appwrite\Extend\Exception; use Utopia\Fetch\Client; /** - * Validates a password against the project's `password-pwned` policy. + * Validates that a password has not been exposed in a known data breach. * - * Subclasses differ in who answers the question and in what leaves the server, - * so read the one you are configuring. `_APP_PWNED_PASSWORDS_DSN` picks it, and - * the DSN scheme matches the subclass. + * Whether to ask at all is the caller's decision, read from the project's + * `passwordPwned` policy, the same way the personal data and history checks + * work. Subclasses differ in who answers the question and in what leaves the + * server, so read the one you are configuring. `_APP_PWNED_PASSWORDS_DSN` picks + * it, and the DSN scheme matches the subclass. */ abstract class PasswordPwned extends Password { private const CONNECT_TIMEOUT = 3 * 1000; // milliseconds private const REQUEST_TIMEOUT = 5 * 1000; // milliseconds - protected bool $enabled; - protected bool $sessions; - protected bool $users; - - /** - * @param array $policy the project's `passwordPwned` auth settings - */ - public function __construct(array $policy = [], bool $allowEmpty = false) - { - parent::__construct($allowEmpty); - - $this->enabled = (bool) ($policy['enabled'] ?? true); - $this->sessions = (bool) ($policy['sessions'] ?? false); - $this->users = (bool) ($policy['users'] ?? false); - } - - /** - * Whether the project enforces the policy at all. - */ - public function isEnabled(): bool - { - return $this->enabled; - } - - /** - * Whether passwords are checked when a session is created. - */ - public function checksSessions(): bool - { - return $this->enabled && $this->sessions; - } - - /** - * Whether users signing in with a breached password are blocked until they reset it. - * - * Only takes effect when sessions are checked. - */ - public function blocksUsers(): bool - { - return $this->users; - } - /** * Get Description. * @@ -74,9 +34,6 @@ abstract class PasswordPwned extends Password /** * Is valid. * - * A password the policy did not look at is valid, so callers that need to - * tell "clean" from "never checked" ask `isEnabled()` first. - * * @param mixed $value * * @return bool @@ -92,10 +49,6 @@ abstract class PasswordPwned extends Password return true; } - if (!$this->enabled) { - return true; - } - return !$this->isPwned($value); } diff --git a/src/Appwrite/Auth/Validator/PasswordPwned/Appwrite.php b/src/Appwrite/Auth/Validator/PasswordPwned/Appwrite.php index a2199a50d5..b6a103e8c4 100644 --- a/src/Appwrite/Auth/Validator/PasswordPwned/Appwrite.php +++ b/src/Appwrite/Auth/Validator/PasswordPwned/Appwrite.php @@ -32,13 +32,8 @@ class Appwrite extends PasswordPwned protected string $secret; protected Client $client; - /** - * @param array $policy - */ - public function __construct(array $policy, DSN $dsn, ?Client $client = null) + public function __construct(DSN $dsn, ?Client $client = null) { - parent::__construct($policy); - $scheme = $dsn->getParam('tls') === 'true' ? 'https' : 'http'; $port = $dsn->getPort() !== null ? ':' . $dsn->getPort() : ''; $path = $dsn->getPath(); diff --git a/src/Appwrite/Auth/Validator/PasswordPwned/HIBP.php b/src/Appwrite/Auth/Validator/PasswordPwned/HIBP.php index 086c5a5f6f..e1fa1ffe9a 100644 --- a/src/Appwrite/Auth/Validator/PasswordPwned/HIBP.php +++ b/src/Appwrite/Auth/Validator/PasswordPwned/HIBP.php @@ -27,13 +27,8 @@ class HIBP extends PasswordPwned protected Client $client; protected string $endpoint; - /** - * @param array $policy - */ - public function __construct(array $policy = [], ?Cache $cache = null, ?Client $client = null, string $endpoint = self::ENDPOINT) + public function __construct(?Cache $cache = null, ?Client $client = null, string $endpoint = self::ENDPOINT) { - parent::__construct($policy); - $this->cache = $cache; $this->client = $this->client($client); $this->endpoint = \rtrim($endpoint, '/'); diff --git a/src/Appwrite/Platform/Modules/Users/Http/Users/Create.php b/src/Appwrite/Platform/Modules/Users/Http/Users/Create.php index 61c46bf062..54dd362269 100644 --- a/src/Appwrite/Platform/Modules/Users/Http/Users/Create.php +++ b/src/Appwrite/Platform/Modules/Users/Http/Users/Create.php @@ -74,7 +74,7 @@ class Create extends Base public function action(string $userId, ?string $email, ?string $phone, ?string $password, ?string $name, Response $response, Document $project, Database $dbForProject, Hooks $hooks, array $plan, PasswordPwned $pwnedPasswords): void { - $passwordPwned = empty($password) || !$pwnedPasswords->isEnabled() + $passwordPwned = empty($password) || !($project->getAttribute('auths', [])['passwordPwned']['enabled'] ?? true) ? null : !$pwnedPasswords->isValid($password); if ($passwordPwned) { diff --git a/src/Appwrite/Platform/Modules/Users/Http/Users/Password/Update.php b/src/Appwrite/Platform/Modules/Users/Http/Users/Password/Update.php index b39fdc6a82..2efba854a0 100644 --- a/src/Appwrite/Platform/Modules/Users/Http/Users/Password/Update.php +++ b/src/Appwrite/Platform/Modules/Users/Http/Users/Password/Update.php @@ -86,7 +86,7 @@ class Update extends Action } } - $passwordPwned = \strlen($password) === 0 || !$pwnedPasswords->isEnabled() + $passwordPwned = \strlen($password) === 0 || !($project->getAttribute('auths', [])['passwordPwned']['enabled'] ?? true) ? null : !$pwnedPasswords->isValid($password); if ($passwordPwned) { diff --git a/tests/unit/Auth/Validator/PasswordPwned/AppwriteTest.php b/tests/unit/Auth/Validator/PasswordPwned/AppwriteTest.php index 43ad793fd2..55f8447469 100644 --- a/tests/unit/Auth/Validator/PasswordPwned/AppwriteTest.php +++ b/tests/unit/Auth/Validator/PasswordPwned/AppwriteTest.php @@ -34,14 +34,6 @@ final class AppwriteTest extends TestCase $this->assertTrue($this->validator($fetch)->isValid(self::PASSWORD)); } - public function testDisabledPolicyNeverAsksTheService(): void - { - $fetch = new DetectionFetch(body: '{"leaked":true}'); - - $this->assertTrue((new Appwrite(['enabled' => false], new DSN(self::DSN), new Client($fetch)))->isValid(self::PASSWORD)); - $this->assertCount(0, $fetch->requests); - } - public function testThePasswordTravelsInAJwtTheServiceCanOpen(): void { $fetch = new DetectionFetch(body: '{"leaked":false}'); @@ -87,7 +79,7 @@ final class AppwriteTest extends TestCase foreach ($cases as $dsn => $expected) { $fetch = new DetectionFetch(body: '{"leaked":false}'); - (new Appwrite([], new DSN($dsn), new Client($fetch)))->isValid(self::PASSWORD); + (new Appwrite(new DSN($dsn), new Client($fetch)))->isValid(self::PASSWORD); $this->assertSame($expected, $fetch->requests[0]['url'], $dsn); } @@ -143,7 +135,7 @@ final class AppwriteTest extends TestCase private function validator(DetectionFetch $fetch): Appwrite { - return new Appwrite([], new DSN(self::DSN), new Client($fetch)); + return new Appwrite(new DSN(self::DSN), new Client($fetch)); } } diff --git a/tests/unit/Auth/Validator/PasswordPwned/HIBPTest.php b/tests/unit/Auth/Validator/PasswordPwned/HIBPTest.php index 4b5e70ec52..d14636122f 100644 --- a/tests/unit/Auth/Validator/PasswordPwned/HIBPTest.php +++ b/tests/unit/Auth/Validator/PasswordPwned/HIBPTest.php @@ -74,21 +74,13 @@ final class HIBPTest extends TestCase $this->assertStringEndsWith(\substr($hash, 0, 5), $url); } - public function testDisabledPolicyNeverAsksTheService(): void - { - $fetch = new RangeFetch(body: $this->range([self::LEAKED => 42])); - - $this->assertTrue((new HIBP(['enabled' => false], null, new Client($fetch), self::ENDPOINT))->isValid(self::LEAKED)); - $this->assertCount(0, $fetch->urls); - } - public function testRangeIsCachedPerPrefix(): void { $fetch = new RangeFetch(body: $this->range([self::LEAKED => 42])); $cache = new Cache(new Memory()); - $this->assertFalse((new HIBP([], $cache, new Client($fetch), self::ENDPOINT))->isValid(self::LEAKED)); - $this->assertFalse((new HIBP([], $cache, new Client($fetch), self::ENDPOINT))->isValid(self::LEAKED)); + $this->assertFalse((new HIBP($cache, new Client($fetch), self::ENDPOINT))->isValid(self::LEAKED)); + $this->assertFalse((new HIBP($cache, new Client($fetch), self::ENDPOINT))->isValid(self::LEAKED)); $this->assertCount(1, $fetch->urls); } @@ -98,8 +90,8 @@ final class HIBPTest extends TestCase $fetch = new RangeFetch(body: $this->range([self::LEAKED => 42])); $cache = new Cache(new Memory()); - (new HIBP([], $cache, new Client($fetch), 'https://one.test/range'))->isValid(self::LEAKED); - (new HIBP([], $cache, new Client($fetch), 'https://two.test/range'))->isValid(self::LEAKED); + (new HIBP($cache, new Client($fetch), 'https://one.test/range'))->isValid(self::LEAKED); + (new HIBP($cache, new Client($fetch), 'https://two.test/range'))->isValid(self::LEAKED); $this->assertCount(2, $fetch->urls); } @@ -108,8 +100,8 @@ final class HIBPTest extends TestCase { $fetch = new RangeFetch(body: ''); - (new HIBP([], null, new Client($fetch), 'https://server.test/range'))->isValid(self::LEAKED); - (new HIBP([], null, new Client($fetch), 'https://other.test/range/'))->isValid(self::LEAKED); + (new HIBP(null, new Client($fetch), 'https://server.test/range'))->isValid(self::LEAKED); + (new HIBP(null, new Client($fetch), 'https://other.test/range/'))->isValid(self::LEAKED); $this->assertStringStartsWith('https://server.test/range/', $fetch->urls[0]); $this->assertStringStartsWith('https://other.test/range/', $fetch->urls[1]); @@ -139,7 +131,7 @@ final class HIBPTest extends TestCase public function testFailedLookupIsNotCached(): void { $fetch = new RangeFetch(failure: new \RuntimeException('connection refused')); - $validator = new HIBP([], new Cache(new Memory()), new Client($fetch), self::ENDPOINT); + $validator = new HIBP(new Cache(new Memory()), new Client($fetch), self::ENDPOINT); foreach ([1, 2] as $attempt) { try { @@ -154,7 +146,7 @@ final class HIBPTest extends TestCase private function validator(RangeFetch $fetch): HIBP { - return new HIBP([], null, new Client($fetch), self::ENDPOINT); + return new HIBP(null, new Client($fetch), self::ENDPOINT); } /** diff --git a/tests/unit/Auth/Validator/PasswordPwnedTest.php b/tests/unit/Auth/Validator/PasswordPwnedTest.php index 5dfbdcc133..b5e95ef59f 100644 --- a/tests/unit/Auth/Validator/PasswordPwnedTest.php +++ b/tests/unit/Auth/Validator/PasswordPwnedTest.php @@ -8,38 +8,13 @@ use Appwrite\Auth\Validator\PasswordPwned\Mock; use PHPUnit\Framework\TestCase; /** - * The policy decisions every breach validator shares, exercised through the one + * The behaviour every breach validator shares, exercised through the one * subclass that needs no service behind it. */ final class PasswordPwnedTest extends TestCase { private const BREACHED = Mock::BREACHED[0]; - public function testEnabledByDefaultWithoutSessionChecks(): void - { - $validator = new Mock(); - - $this->assertTrue($validator->isEnabled()); - $this->assertFalse($validator->checksSessions()); - $this->assertFalse($validator->blocksUsers()); - } - - public function testPolicyFlags(): void - { - $validator = new Mock(['enabled' => true, 'sessions' => true, 'users' => true]); - - $this->assertTrue($validator->isEnabled()); - $this->assertTrue($validator->checksSessions()); - $this->assertTrue($validator->blocksUsers()); - } - - public function testSessionChecksNeedAnEnabledPolicy(): void - { - $validator = new Mock(['enabled' => false, 'sessions' => true, 'users' => true]); - - $this->assertFalse($validator->checksSessions()); - } - public function testBreachedPasswordIsInvalid(): void { $validator = new Mock(); @@ -48,13 +23,6 @@ final class PasswordPwnedTest extends TestCase $this->assertTrue($validator->isValid('a-password-nobody-leaked')); } - public function testDisabledPolicyAcceptsEverything(): void - { - $validator = new Mock(['enabled' => false]); - - $this->assertTrue($validator->isValid(self::BREACHED)); - } - public function testBasePasswordRulesStillApply(): void { $validator = new Mock(); @@ -66,7 +34,7 @@ final class PasswordPwnedTest extends TestCase public function testAllowEmptyAcceptsAnEmptyPassword(): void { - $this->assertTrue((new Mock([], allowEmpty: true))->isValid('')); - $this->assertFalse((new Mock([], allowEmpty: true))->isValid(self::BREACHED)); + $this->assertTrue((new Mock(allowEmpty: true))->isValid('')); + $this->assertFalse((new Mock(allowEmpty: true))->isValid(self::BREACHED)); } }