mirror of
https://github.com/shuaiplus/nodewarden.git
synced 2026-08-04 22:40:11 +00:00
fix rate limit reset bypasses
This commit is contained in:
@@ -122,6 +122,16 @@ function readBodyValue(body: Record<string, string>, names: string[]): string |
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function sha256Hex(value: string): Promise<string> {
|
||||||
|
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(value));
|
||||||
|
return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0')).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loginRateLimitKey(clientIdentifier: string, grantType: string, subject: string): Promise<string> {
|
||||||
|
const subjectHash = await sha256Hex(`${grantType}:${String(subject || '').trim() || 'unknown'}`);
|
||||||
|
return `${clientIdentifier}:login:${grantType}:${subjectHash}`;
|
||||||
|
}
|
||||||
|
|
||||||
async function getStoredYubicoCredentials(storage: StorageService, env: Env): Promise<YubicoApiCredentials | null> {
|
async function getStoredYubicoCredentials(storage: StorageService, env: Env): Promise<YubicoApiCredentials | null> {
|
||||||
const fromEnv = yubicoCredentialsFromEnv(env);
|
const fromEnv = yubicoCredentialsFromEnv(env);
|
||||||
if (fromEnv) return fromEnv;
|
if (fromEnv) return fromEnv;
|
||||||
@@ -343,13 +353,13 @@ export async function handleToken(request: Request, env: Env): Promise<Response>
|
|||||||
const twoFactorToken = readBodyValue(body, ['twoFactorToken', 'TwoFactorToken']);
|
const twoFactorToken = readBodyValue(body, ['twoFactorToken', 'TwoFactorToken']);
|
||||||
const twoFactorProvider = readBodyValue(body, ['twoFactorProvider', 'TwoFactorProvider']);
|
const twoFactorProvider = readBodyValue(body, ['twoFactorProvider', 'TwoFactorProvider']);
|
||||||
const twoFactorRemember = readBodyValue(body, ['twoFactorRemember', 'TwoFactorRemember']);
|
const twoFactorRemember = readBodyValue(body, ['twoFactorRemember', 'TwoFactorRemember']);
|
||||||
const loginIdentifier = clientIdentifier;
|
|
||||||
const deviceInfo = readAuthRequestDeviceInfo(body, request);
|
const deviceInfo = readAuthRequestDeviceInfo(body, request);
|
||||||
|
|
||||||
if (!email || !passwordHash) {
|
if (!email || !passwordHash) {
|
||||||
// Bitwarden clients expect OAuth-style error fields.
|
// Bitwarden clients expect OAuth-style error fields.
|
||||||
return identityErrorResponse('Email and password are required', 'invalid_request', 400);
|
return identityErrorResponse('Email and password are required', 'invalid_request', 400);
|
||||||
}
|
}
|
||||||
|
const loginIdentifier = await loginRateLimitKey(clientIdentifier, grantType, email);
|
||||||
|
|
||||||
// Check login lockout before user lookup to reduce user-enumeration signal
|
// Check login lockout before user lookup to reduce user-enumeration signal
|
||||||
const loginCheck = await rateLimit.checkLoginAttempt(loginIdentifier);
|
const loginCheck = await rateLimit.checkLoginAttempt(loginIdentifier);
|
||||||
@@ -608,7 +618,8 @@ export async function handleToken(request: Request, env: Env): Promise<Response>
|
|||||||
: baseResponse;
|
: baseResponse;
|
||||||
|
|
||||||
} else if (grantType === 'webauthn') {
|
} else if (grantType === 'webauthn') {
|
||||||
const loginIdentifier = clientIdentifier;
|
const token = String(body.token || '').trim();
|
||||||
|
const loginIdentifier = await loginRateLimitKey(clientIdentifier, grantType, token || 'missing-token');
|
||||||
const loginCheck = await rateLimit.checkLoginAttempt(loginIdentifier);
|
const loginCheck = await rateLimit.checkLoginAttempt(loginIdentifier);
|
||||||
if (!loginCheck.allowed) {
|
if (!loginCheck.allowed) {
|
||||||
return identityErrorResponse(
|
return identityErrorResponse(
|
||||||
@@ -618,7 +629,6 @@ export async function handleToken(request: Request, env: Env): Promise<Response>
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = String(body.token || '').trim();
|
|
||||||
let deviceResponse: unknown = body.deviceResponse;
|
let deviceResponse: unknown = body.deviceResponse;
|
||||||
if (typeof deviceResponse === 'string') {
|
if (typeof deviceResponse === 'string') {
|
||||||
try {
|
try {
|
||||||
@@ -736,11 +746,12 @@ export async function handleToken(request: Request, env: Env): Promise<Response>
|
|||||||
const scope = body.scope;
|
const scope = body.scope;
|
||||||
const deviceInfo = readAuthRequestDeviceInfo(body, request);
|
const deviceInfo = readAuthRequestDeviceInfo(body, request);
|
||||||
|
|
||||||
const loginIdentifier = clientIdentifier;
|
|
||||||
const parmValid = checkClientCredentialsParam(clientId, clientSecret, scope);
|
const parmValid = checkClientCredentialsParam(clientId, clientSecret, scope);
|
||||||
if (!parmValid) {
|
if (!parmValid) {
|
||||||
return identityErrorResponse('Parameter error', 'invalid_request', 400);
|
return identityErrorResponse('Parameter error', 'invalid_request', 400);
|
||||||
}
|
}
|
||||||
|
const uid = clientId.slice(5);
|
||||||
|
const loginIdentifier = await loginRateLimitKey(clientIdentifier, grantType, uid);
|
||||||
|
|
||||||
// Check login lockout before user lookup to reduce user-enumeration signal
|
// Check login lockout before user lookup to reduce user-enumeration signal
|
||||||
const loginCheck = await rateLimit.checkLoginAttempt(loginIdentifier);
|
const loginCheck = await rateLimit.checkLoginAttempt(loginIdentifier);
|
||||||
@@ -752,7 +763,6 @@ export async function handleToken(request: Request, env: Env): Promise<Response>
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const uid = clientId.slice(5);
|
|
||||||
const user = await storage.getUserById(uid);
|
const user = await storage.getUserById(uid);
|
||||||
if (!user) {
|
if (!user) {
|
||||||
await rateLimit.recordFailedLogin(loginIdentifier);
|
await rateLimit.recordFailedLogin(loginIdentifier);
|
||||||
@@ -895,7 +905,7 @@ export async function handleToken(request: Request, env: Env): Promise<Response>
|
|||||||
passwordHashB64,
|
passwordHashB64,
|
||||||
password,
|
password,
|
||||||
rateLimit,
|
rateLimit,
|
||||||
`${clientIdentifier}:send-password`
|
clientIdentifier
|
||||||
);
|
);
|
||||||
if ('error' in result) {
|
if ('error' in result) {
|
||||||
return result.error;
|
return result.error;
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ export async function handleAccessSend(request: Request, env: Env, accessId: str
|
|||||||
if (!clientIdentifier) {
|
if (!clientIdentifier) {
|
||||||
return errorResponse('Client IP is required', 403);
|
return errorResponse('Client IP is required', 403);
|
||||||
}
|
}
|
||||||
sendPasswordLimitIpKey = sendPasswordLimitKey(clientIdentifier);
|
sendPasswordLimitIpKey = sendPasswordLimitKey(clientIdentifier, send.id);
|
||||||
sendPasswordRateLimit = new RateLimitService(env.DB);
|
sendPasswordRateLimit = new RateLimitService(env.DB);
|
||||||
const sendPasswordCheck = await sendPasswordRateLimit.checkLoginAttempt(sendPasswordLimitIpKey);
|
const sendPasswordCheck = await sendPasswordRateLimit.checkLoginAttempt(sendPasswordLimitIpKey);
|
||||||
if (!sendPasswordCheck.allowed) {
|
if (!sendPasswordCheck.allowed) {
|
||||||
@@ -142,7 +142,7 @@ export async function handleAccessSendFile(
|
|||||||
if (!clientIdentifier) {
|
if (!clientIdentifier) {
|
||||||
return errorResponse('Client IP is required', 403);
|
return errorResponse('Client IP is required', 403);
|
||||||
}
|
}
|
||||||
sendPasswordLimitIpKey = sendPasswordLimitKey(clientIdentifier);
|
sendPasswordLimitIpKey = sendPasswordLimitKey(clientIdentifier, send.id);
|
||||||
sendPasswordRateLimit = new RateLimitService(env.DB);
|
sendPasswordRateLimit = new RateLimitService(env.DB);
|
||||||
const sendPasswordCheck = await sendPasswordRateLimit.checkLoginAttempt(sendPasswordLimitIpKey);
|
const sendPasswordCheck = await sendPasswordRateLimit.checkLoginAttempt(sendPasswordLimitIpKey);
|
||||||
if (!sendPasswordCheck.allowed) {
|
if (!sendPasswordCheck.allowed) {
|
||||||
@@ -328,7 +328,7 @@ export async function issueSendAccessToken(
|
|||||||
passwordHashB64?: string | null,
|
passwordHashB64?: string | null,
|
||||||
password?: string | null,
|
password?: string | null,
|
||||||
rateLimit?: RateLimitService,
|
rateLimit?: RateLimitService,
|
||||||
sendPasswordLimitIpKey?: string
|
clientIdentifier?: string
|
||||||
): Promise<{ token: string } | { error: Response }> {
|
): Promise<{ token: string } | { error: Response }> {
|
||||||
const jwt = getSafeJwtSecret(env);
|
const jwt = getSafeJwtSecret(env);
|
||||||
if (!jwt.ok) {
|
if (!jwt.ok) {
|
||||||
@@ -373,6 +373,9 @@ export async function issueSendAccessToken(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sendPasswordLimitIpKey =
|
||||||
|
rateLimit && clientIdentifier ? sendPasswordLimitKey(clientIdentifier, send.id) : null;
|
||||||
|
|
||||||
if (send.passwordHash) {
|
if (send.passwordHash) {
|
||||||
if (rateLimit && sendPasswordLimitIpKey) {
|
if (rateLimit && sendPasswordLimitIpKey) {
|
||||||
const sendPasswordCheck = await rateLimit.checkLoginAttempt(sendPasswordLimitIpKey);
|
const sendPasswordCheck = await rateLimit.checkLoginAttempt(sendPasswordLimitIpKey);
|
||||||
|
|||||||
@@ -434,8 +434,8 @@ export type PublicSendAccessValidationResult =
|
|||||||
| { ok: true }
|
| { ok: true }
|
||||||
| { ok: false; response: Response; reason: 'email_auth_unsupported' | 'password_missing' | 'invalid_password' };
|
| { ok: false; response: Response; reason: 'email_auth_unsupported' | 'password_missing' | 'invalid_password' };
|
||||||
|
|
||||||
export function sendPasswordLimitKey(clientIdentifier: string): string {
|
export function sendPasswordLimitKey(clientIdentifier: string, sendId: string): string {
|
||||||
return `${clientIdentifier}:${SEND_PASSWORD_LIMIT_SCOPE}`;
|
return `${clientIdentifier}:${SEND_PASSWORD_LIMIT_SCOPE}:${String(sendId || '').trim() || 'unknown-send'}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendPasswordLockMessage(retryAfterSeconds: number): string {
|
function sendPasswordLockMessage(retryAfterSeconds: number): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user