fix(policies): skip the pwned check on outage instead of rejecting

Review feedback: with the policy now enabled by default, defaulting
`failClosed` to true meant any project that had never configured the
policy would reject password creation, updates and recovery with a 503
whenever the breach service was unreachable.

`failClosed` now defaults to false, so an outage skips the check and
leaves the recorded state untouched. Rejecting on outage stays available
by setting it to true.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matej BačoandClaude Opus 5 committed 2026-09-15 14:43:37 +02:00
1 parent 03bc7c694f
commit ef1e6ab1c8
10 files changed
+39 -30

No files matched your search

@@ -46,7 +46,7 @@ class PasswordPwned extends Password
$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'] ?? true);
$this->failClosed = (bool) ($policy['failClosed'] ?? false);
$this->cache = $cache;
$this->client = $client ?? (new Client())
->setConnectTimeout(self::CONNECT_TIMEOUT)
@@ -97,7 +97,7 @@ class Create extends Action
'threshold' => 1,
'sessions' => false,
'forceReset' => false,
'failClosed' => true,
'failClosed' => false,
],
'disposableEmails' => false,
'canonicalEmails' => false,
@@ -142,7 +142,7 @@ class Get extends Action
'threshold' => 1,
'sessions' => false,
'forceReset' => false,
'failClosed' => true,
'failClosed' => false,
], $auths['passwordPwned'] ?? [], [
'$id' => 'password-pwned',
])),
@@ -60,7 +60,7 @@ class Update extends Action
->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 true. Set to false to skip the check instead.', 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)
->inject('response')
->inject('dbForPlatform')
->inject('project')
@@ -89,7 +89,7 @@ class Update extends Action
'threshold' => 1,
'sessions' => false,
'forceReset' => false,
'failClosed' => true,
'failClosed' => false,
], $auths['passwordPwned'] ?? []);
if ($enabled !== null) {
@@ -122,7 +122,7 @@ class XList extends Action
'threshold' => 1,
'sessions' => false,
'forceReset' => false,
'failClosed' => true,
'failClosed' => false,
], $auths['passwordPwned'] ?? [], [
'$id' => 'password-pwned',
])),
@@ -94,7 +94,7 @@ class Create extends Action
'threshold' => 1,
'sessions' => false,
'forceReset' => false,
'failClosed' => true,
'failClosed' => false,
],
'disposableEmails' => false,
'canonicalEmails' => false,
@@ -48,8 +48,8 @@ class PolicyPasswordPwned extends PolicyBase
->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.',
'default' => true,
'example' => true,
'default' => false,
'example' => false,
]);
}
+6 -6
View File
@@ -654,7 +654,7 @@ trait PoliciesBase
$this->assertSame(1, $policy['body']['threshold']);
$this->assertSame(false, $policy['body']['sessions']);
$this->assertSame(false, $policy['body']['forceReset']);
$this->assertSame(true, $policy['body']['failClosed']);
$this->assertSame(false, $policy['body']['failClosed']);
// Cleanup
$this->updatePasswordPwnedPolicy(['enabled' => false]);
@@ -686,7 +686,7 @@ trait PoliciesBase
$this->assertSame(1, $policy['body']['threshold']);
$this->assertSame(false, $policy['body']['sessions']);
$this->assertSame(false, $policy['body']['forceReset']);
$this->assertSame(true, $policy['body']['failClosed']);
$this->assertSame(false, $policy['body']['failClosed']);
}
public function testUpdatePasswordPwnedPolicySessions(): void
@@ -713,7 +713,7 @@ trait PoliciesBase
'threshold' => 5,
'sessions' => true,
'forceReset' => true,
'failClosed' => false,
'failClosed' => true,
]);
$this->assertSame(200, $response['headers']['status-code']);
@@ -728,7 +728,7 @@ trait PoliciesBase
$this->assertSame(5, $policy['body']['threshold']);
$this->assertSame(true, $policy['body']['sessions']);
$this->assertSame(true, $policy['body']['forceReset']);
$this->assertSame(false, $policy['body']['failClosed']);
$this->assertSame(true, $policy['body']['failClosed']);
// Cleanup
$this->resetPasswordPwnedPolicy();
@@ -748,7 +748,7 @@ trait PoliciesBase
$this->assertSame(1, $policy['body']['threshold']);
$this->assertSame(false, $policy['body']['sessions']);
$this->assertSame(false, $policy['body']['forceReset']);
$this->assertSame(true, $policy['body']['failClosed']);
$this->assertSame(false, $policy['body']['failClosed']);
}
public function testUpdatePasswordPwnedPolicyInvalidType(): void
@@ -1497,7 +1497,7 @@ trait PoliciesBase
'threshold' => 1,
'sessions' => false,
'forceReset' => false,
'failClosed' => true,
'failClosed' => false,
]);
}
@@ -48,7 +48,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope
// Breached passwords are rejected out of the box
$this->assertTrue($response['body']['enabled']);
$this->assertTrue($response['body']['failClosed']);
$this->assertFalse($response['body']['failClosed']);
$this->assertSame(1, $response['body']['threshold']);
$this->assertSame('', $response['body']['endpoint']);
@@ -468,7 +468,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope
$this->assertSame(201, $response['headers']['status-code']);
$this->updatePolicy(['enabled' => false, 'endpoint' => '', 'sessions' => false, 'forceReset' => false, 'failClosed' => true]);
$this->updatePolicy(['enabled' => false, 'endpoint' => '', 'sessions' => false, 'forceReset' => false, 'failClosed' => false]);
}
public function testForceResetOnSignIn(): void
@@ -1153,7 +1153,7 @@ final class PoliciesPasswordPwnedIntegrationTest extends Scope
$this->assertSame(400, $response['headers']['status-code']);
$this->updatePolicy(['enabled' => false, 'endpoint' => '', 'sessions' => false, 'forceReset' => false, 'failClosed' => true]);
$this->updatePolicy(['enabled' => false, 'endpoint' => '', 'sessions' => false, 'forceReset' => false, 'failClosed' => false]);
}
/**
+21 -12
View File
@@ -156,11 +156,28 @@ final class PasswordPwnedTest extends TestCase
$this->assertCount(2, $adapter->requests);
}
public function testUnreachableServiceFailsClosedByDefault(): void
public function testUnreachableServiceSkipsTheCheckByDefault(): 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');
@@ -169,29 +186,21 @@ final class PasswordPwnedTest extends TestCase
}
}
public function testUnexpectedStatusFailsClosedByDefault(): void
public function testUnexpectedStatusIsRejectedWhenFailingClosed(): void
{
$adapter = new RangeAdapter(statusCode: 503, body: '');
$validator = $this->validator([], $adapter);
$validator = $this->validator(['failClosed' => true], $adapter);
$this->expectException(Exception::class);
$validator->isValid(self::LEAKED);
}
public function testUnreachableServiceSkipsTheCheckWhenFailingOpen(): void
{
$adapter = new RangeAdapter(failure: new \RuntimeException('connection refused'));
$validator = $this->validator(['failClosed' => false], $adapter);
$this->assertTrue($validator->isValid(self::LEAKED));
}
public function testFailedLookupIsNotCached(): void
{
$adapter = new RangeAdapter(failure: new \RuntimeException('connection refused'));
$cache = new Cache(new Memory());
$validator = new PasswordPwned(['enabled' => true, 'failClosed' => false], $cache, self::ENDPOINT, new Client($adapter));
$validator = new PasswordPwned(['enabled' => true], $cache, self::ENDPOINT, new Client($adapter));
$validator->isValid(self::LEAKED);
$validator->isValid(self::LEAKED);