ActivityPub-federated video streaming platform using P2P directly in your web browser
Configurable login lockout
Chocobozzz49902b88edbd
diff --git a/config/config-schema.json b/config/config-schema.jsonindex e99cb39ff4..760d87f475 100644--- a/config/config-schema.json+++ b/config/config-schema.json@@ -247,7 +247,7 @@},"additionalProperties": false},- "comments": {+ "create_comment": {"description": "Comment/reply creation, limited per authenticated user","type": "object","properties": {@@ -262,6 +262,23 @@}},"additionalProperties": false+ },+ "login_lockout": {+ "description": "Lock an account (independently of the source IP) after too many password/OTP login failures. Since this is keyed per account rather than per IP, an attacker who knows a username can keep it locked by repeating failed logins once per window; raise max/lower window if this tradeoff is a concern for your instance",+ "type": "object",+ "properties": {+ "window": {+ "description": "Lock duration once the threshold is reached, and the rolling window during which failures accumulate towards it",+ "type": "string",+ "default": "5 minutes"+ },+ "max": {+ "description": "Failed login/OTP attempts allowed before the account is locked",+ "type": "integer",+ "default": 10+ }+ },+ "additionalProperties": false}},"additionalProperties": falsediff --git a/config/default.yaml b/config/default.yamlindex fe0762f38a..b5f4debd82 100644--- a/config/default.yaml+++ b/config/default.yaml@@ -74,6 +74,11 @@ rates_limit:# 60 attempts in 5 minuteswindow: 5 minutesmax: 60+ login_lockout: # Lock an account (independently of the source IP) after too many password/OTP login failures+ # Tradeoff: this is keyed per account, not per IP, so an attacker who knows a username can keep it locked+ # by repeating failed logins once per window. Raise max/lower window if this is a concern for your instance+ window: 5 minutes+ max: 10oauth2:token_lifetime:diff --git a/config/production.yaml.example b/config/production.yaml.exampleindex 441d7d57da..777e3d6375 100644--- a/config/production.yaml.example+++ b/config/production.yaml.example@@ -72,6 +72,11 @@ rates_limit:# 60 attempts in 5 minuteswindow: 5 minutesmax: 60+ login_lockout: # Lock an account (independently of the source IP) after too many password/OTP login failures+ # Tradeoff: this is keyed per account, not per IP, so an attacker who knows a username can keep it locked+ # by repeating failed logins once per window. Raise max/lower window if this is a concern for your instance+ window: 5 minutes+ max: 10oauth2:token_lifetime:diff --git a/config/test.yaml b/config/test.yamlindex 6737e8a653..5334663f07 100644--- a/config/test.yaml+++ b/config/test.yaml@@ -25,6 +25,9 @@ rates_limit:create_comment:window: 10 secondsmax: 500+ login_lockout:+ window: 5 seconds+ max: 10database:hostname: '127.0.0.1'diff --git a/packages/tests/src/api/users/login-lockout.ts b/packages/tests/src/api/users/login-lockout.tsindex a49523f315..7c5e7f5a35 100644--- a/packages/tests/src/api/users/login-lockout.ts+++ b/packages/tests/src/api/users/login-lockout.ts@@ -19,7 +19,7 @@ describe('Test login lockout', function () {let userPassword: stringlet userToken: string- // Sync with LOGIN_LOCKOUT constants (lifetime is 5 seconds on a test instance)+ // Sync with rates_limit.login_lockout in config/test.yaml (window is 5 seconds on a test instance)const maxFailures = 10const lifetimeMs = 5000diff --git a/server/core/initializers/config.ts b/server/core/initializers/config.tsindex d110b68088..6c1ce1c1eb 100644--- a/server/core/initializers/config.ts+++ b/server/core/initializers/config.ts@@ -390,6 +390,10 @@ const CONFIG = {CREATE_COMMENT: {WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.create_comment.window')),MAX: config.get<number>('rates_limit.create_comment.max')+ },+ LOGIN_LOCKOUT: {+ WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.login_lockout.window')),+ MAX: config.get<number>('rates_limit.login_lockout.max')}},TRUST_PROXY: config.get<string[]>('trust_proxy'),diff --git a/server/core/initializers/constants.ts b/server/core/initializers/constants.tsindex 0313db5590..d2facdb157 100644--- a/server/core/initializers/constants.ts+++ b/server/core/initializers/constants.ts@@ -943,11 +943,6 @@ export const USER_PASSWORD_CREATE_LIFETIME = 60000 * 60 * 24 * 7 // 7 daysexport const TWO_FACTOR_AUTH_REQUEST_TOKEN_LIFETIME = 60000 * 10 // 10 minutes-// Lock the account (independently of the source IP) after too many password/OTP login failures-export const LOGIN_LOCKOUT = {- MAX_FAILURES: 10,- LIFETIME: 60000 * 5 // 5 minutes-}export let JWT_TOKEN_USER_EXPORT_FILE_LIFETIME: `${number} minutes` | `${number} seconds` = '15 minutes'export const EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes@@ -1316,7 +1311,6 @@ if (process.env.PRODUCTION_CONSTANTS !== 'true') {REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1CONTACT_FORM_LIFETIME = 1000 // 1 second- LOGIN_LOCKOUT.LIFETIME = 5000 // 5 secondsJOB_ATTEMPTS['email'] = 1diff --git a/server/core/lib/auth/oauth-user.ts b/server/core/lib/auth/oauth-user.tsindex b24b582b49..42f85184ab 100644--- a/server/core/lib/auth/oauth-user.ts+++ b/server/core/lib/auth/oauth-user.ts@@ -11,7 +11,7 @@ import { MUser, MUserDefault } from '@server/types/models/user/user.js'import express from 'express'import { logger } from '../../helpers/logger.js'import { CONFIG } from '../../initializers/config.js'-import { LOGIN_LOCKOUT, OTP } from '../../initializers/constants.js'+import { OTP } from '../../initializers/constants.js'import { OAuthTokenModel } from '../../models/oauth/oauth-token.js'import { UserModel } from '../../models/user/user.js'import { findAvailableLocalActorName } from '../local-actor.js'@@ -80,7 +80,7 @@ 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- if (await Redis.Instance.getLoginFailures(user.id) >= LOGIN_LOCKOUT.MAX_FAILURES) {+ 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.'))}diff --git a/server/core/lib/redis.ts b/server/core/lib/redis.tsindex 7bf7810462..6903ab0b0f 100644--- a/server/core/lib/redis.ts+++ b/server/core/lib/redis.ts@@ -10,7 +10,6 @@ import {AP_CLEANER,CONTACT_FORM_LIFETIME,EMAIL_VERIFY_LIFETIME,- LOGIN_LOCKOUT,RESUMABLE_UPLOAD_SESSION_LIFETIME,TWO_FACTOR_AUTH_REQUEST_TOKEN_LIFETIME,USER_PASSWORD_CREATE_LIFETIME,@@ -197,7 +196,7 @@ class Redis {const key = this.generateLoginFailureKey(userId)const failures = await this.increment(key)- await this.setExpiration(key, LOGIN_LOCKOUT.LIFETIME)+ await this.setExpiration(key, CONFIG.RATES_LIMIT.LOGIN_LOCKOUT.WINDOW_MS)return failures}