Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Don't leak account existence

Chocobozzzc27c7c5fa31f
diff --git a/packages/models/src/server/server-error-code.enum.ts b/packages/models/src/server/server-error-code.enum.ts
index 81a482a72f..53c54e2369 100644
--- a/packages/models/src/server/server-error-code.enum.ts
+++ b/packages/models/src/server/server-error-code.enum.ts
@@ -45,11 +45,6 @@ export const ServerErrorCode = {
MISSING_TWO_FACTOR: 'missing_two_factor',
TOO_LONG_PASSWORD: 'too_long_password',
INVALID_TWO_FACTOR: 'invalid_two_factor',
- /**
- * Too many password/OTP login failures for this account: login is temporarily
- * locked independently of the source IP
- */
- TOO_MANY_LOGIN_FAILURES: 'too_many_login_failures',
ACCOUNT_BLOCKED: 'account_blocked',
EMAIL_NOT_VERIFIED: 'email_not_verified',
diff --git a/packages/tests/src/api/users/login-lockout.ts b/packages/tests/src/api/users/login-lockout.ts
index d4d62fe5b0..2f528be3b8 100644
--- a/packages/tests/src/api/users/login-lockout.ts
+++ b/packages/tests/src/api/users/login-lockout.ts
@@ -30,8 +30,10 @@ describe('Test login lockout', function () {
const maxFailuresPerIP = 3
const lifetimeMs = 5000
- function expectLockout (body: PeerTubeProblemDocument) {
- expect(body.code).to.equal(ServerErrorCode.TOO_MANY_LOGIN_FAILURES)
+ // A locked account must return the exact same error as invalid credentials: a distinct status/code here
+ // would let an attacker use the lockout itself as a username-enumeration oracle
+ function expectGenericInvalidGrant (body: PeerTubeProblemDocument) {
+ expect(body.code).to.equal(ServerErrorCode.INVALID_GRANT)
}
function expectLockedAccountEmailsCount (count: number) {
@@ -69,14 +71,16 @@ describe('Test login lockout', function () {
}
}
+ // Even with the correct password/OTP, a locked account must still be rejected, with a body
+ // indistinguishable from a plain wrong-credentials response (see expectGenericInvalidGrant)
async function expectLockedLogin (password: string, otpToken?: string) {
const { body } = await server.login.loginAndGetResponse({
user: { username: userUsername, password },
otpToken,
- expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429
+ expectedStatus: HttpStatusCode.BAD_REQUEST_400
})
- expectLockout(body as unknown as PeerTubeProblemDocument)
+ expectGenericInvalidGrant(body as unknown as PeerTubeProblemDocument)
}
before(async function () {
@@ -147,9 +151,14 @@ describe('Test login lockout', function () {
expectLockedAccountEmailsCount(0)
})
- it('Should lock the account after too many password failures spread over several IPs', async function () {
+ it('Should lock the account after too many failures without leaking it over HTTP', async function () {
this.timeout(30000)
+ const { body: unknownUserBody } = await server.login.loginAndGetResponse({
+ user: { username: 'a-username-that-does-not-exist', password: 'whatever' },
+ expectedStatus: HttpStatusCode.BAD_REQUEST_400
+ })
+
await failLoginsSpreadOverIPs(maxFailures, 'invalid password')
// Even with the correct password
@@ -158,9 +167,23 @@ describe('Test login lockout', function () {
// And of course with an invalid one
await expectLockedLogin('invalid password')
+ // The locked-account error must be indistinguishable from the "unknown username" one: same status,
+ // same generic code, same message. Otherwise the lockout itself becomes a username-enumeration oracle
+ const { body: lockedBody } = await server.login.loginAndGetResponse({
+ user: { username: userUsername, password: userPassword },
+ expectedStatus: HttpStatusCode.BAD_REQUEST_400
+ })
+
+ const unknownUserProblem = unknownUserBody as unknown as PeerTubeProblemDocument
+ const lockedProblem = lockedBody as unknown as PeerTubeProblemDocument
+
+ expectGenericInvalidGrant(unknownUserProblem)
+ expectGenericInvalidGrant(lockedProblem)
+ expect(lockedProblem.detail).to.equal(unknownUserProblem.detail)
+
await waitJobs(server)
- // Only 1 email, even though 2 more requests hit the account after it was already locked
+ // The lock is real, it is just not observable over HTTP: only the account owner's email reveals it
expectLockedAccountEmailsCount(1)
})
@@ -192,7 +215,7 @@ describe('Test login lockout', function () {
expect(body.code).to.equal(ServerErrorCode.INVALID_TWO_FACTOR)
}
- // Locked, even with the correct password and OTP token
+ // Locked, even with the correct password and OTP token, and still the same generic error
await expectLockedLogin(userPassword, TwoFactorCommand.buildOTP({ secret: otpRequest.secret }).generate())
await wait(lifetimeMs + 2000)
diff --git a/server/core/controllers/api/users/token.ts b/server/core/controllers/api/users/token.ts
index 4c8f701946..e1afb176d8 100644
--- a/server/core/controllers/api/users/token.ts
+++ b/server/core/controllers/api/users/token.ts
@@ -6,7 +6,7 @@ import { CONFIG } from '@server/initializers/config.js'
import { OTP } from '@server/initializers/constants.js'
import { BypassLogin } from '@server/lib/auth/bypass-login.model.js'
import { consumeBypassFromExternalAuth, getAuthNameFromRefreshGrant, getBypassFromPasswordGrant } from '@server/lib/auth/external-auth.js'
-import { MissingTwoFactorError, TooManyLoginFailuresError } from '@server/lib/auth/oauth-errors.js'
+import { MissingTwoFactorError } from '@server/lib/auth/oauth-errors.js'
import { handleOAuthToken } from '@server/lib/auth/oauth-handlers.js'
import { revokeToken } from '@server/lib/auth/oauth-token.js'
import { Hooks } from '@server/lib/plugins/hooks.js'
@@ -124,8 +124,6 @@ async function handleToken (req: express.Request, res: express.Response, next: e
logger.debug('Missing two factor error', { err })
} else if (err instanceof InvalidGrantError) {
logger.debug('Invalid grant', { err })
- } else if (err instanceof TooManyLoginFailuresError) {
- logger.debug('Too many login failures', { err })
} else {
logger.warn('Login error', { err })
}
diff --git a/server/core/lib/auth/oauth-errors.ts b/server/core/lib/auth/oauth-errors.ts
index 99605e36a3..1e75822c95 100644
--- a/server/core/lib/auth/oauth-errors.ts
+++ b/server/core/lib/auth/oauth-errors.ts
@@ -25,11 +25,6 @@ export class InvalidTwoFactorError extends Error {
name = ServerErrorCode.INVALID_TWO_FACTOR
}
-export class TooManyLoginFailuresError extends Error {
- code = HttpStatusCode.TOO_MANY_REQUESTS_429
- name = ServerErrorCode.TOO_MANY_LOGIN_FAILURES
-}
-
export class RegistrationWaitingForApproval extends Error {
code = HttpStatusCode.BAD_REQUEST_400
name = ServerErrorCode.ACCOUNT_WAITING_FOR_APPROVAL
diff --git a/server/core/lib/auth/oauth-user.ts b/server/core/lib/auth/oauth-user.ts
index 123519cd2a..50ade68c7c 100644
--- a/server/core/lib/auth/oauth-user.ts
+++ b/server/core/lib/auth/oauth-user.ts
@@ -28,8 +28,7 @@ import {
MissingTwoFactorError,
RegistrationApprovalRejected,
RegistrationWaitingForApproval,
- TooLongPasswordError,
- TooManyLoginFailuresError
+ TooLongPasswordError
} from './oauth-errors.js'
export async function getUserOrThrow (options: {
@@ -81,8 +80,10 @@ export async function getUserOrThrow (options: {
if (isRootAuthDisabled(user)) throwInvalidGrantError()
// Check the per-account login failures counter so a locked account cannot have its password/OTP brute-forced
+ // Throw the exact same generic error as invalid credentials: a distinct error/status here would let an
+ // attacker use the lockout itself as a username-enumeration oracle (try N failed logins, see if it flips)
if (await Redis.Instance.getLoginFailures(user.id) >= CONFIG.RATES_LIMIT.LOGIN_LOCKOUT.MAX) {
- throw new TooManyLoginFailuresError(req.t('Too many login attempts for this account. Please try again later.'))
+ throwInvalidGrantError()
}
if (isUserPasswordTooLong(password)) {