mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
Gitea accepted unlimited wrong TOTP passcodes, so anyone holding an account's password could enumerate the 6-digit code in minutes. This allows 5 failed attempts per account, then refuses TOTP until 15 minutes after the last one. - The count lives in the cache, and the global lock spans the check, validation and counting, so new sessions and concurrent requests cannot widen it. With Redis, both are shared across instances - Applies to every TOTP check: the web 2FA page, account recovery and Basic auth with `X-Gitea-OTP` (API, git, packages) - Only wrong or replayed passcodes count, server errors do not - Scratch codes and WebAuthn stay usable as the recovery path while locked - Failures log `Failed authentication attempt for <user> from <ip>`, which the fail2ban filter already matches --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
68 lines
3.0 KiB
TypeScript
68 lines
3.0 KiB
TypeScript
import {createHmac} from 'node:crypto';
|
|
import {env} from 'node:process';
|
|
import {test, expect, type Page} from '@playwright/test';
|
|
import {apiCreateUser, loginUser, logout, randomString, signInWithPassword} from './utils.ts';
|
|
|
|
test('homepage', async ({page}) => {
|
|
await page.goto('/');
|
|
await expect(page.getByRole('img', {name: 'Logo'})).toHaveAttribute('src', '/assets/img/logo.svg');
|
|
});
|
|
|
|
test('login form in tab order and logout', async ({page}) => {
|
|
await page.goto('/user/login');
|
|
await page.getByLabel('Username or Email Address').fill(env.GITEA_TEST_E2E_USER);
|
|
await page.keyboard.press('Tab');
|
|
const password = page.getByLabel('Password');
|
|
await expect(password).toBeFocused();
|
|
await password.fill(env.GITEA_TEST_E2E_PASSWORD);
|
|
await page.keyboard.press('Tab');
|
|
await expect(page.getByLabel('Remember This Device')).toBeFocused();
|
|
await page.keyboard.press('Tab');
|
|
const signIn = page.getByRole('button', {name: 'Sign In', exact: true});
|
|
await expect(signIn).toBeFocused();
|
|
await page.keyboard.press('Tab');
|
|
await expect(page.getByRole('link', {name: 'Forgot password?'})).toBeFocused();
|
|
await signIn.click();
|
|
await expect(page.getByRole('link', {name: 'Sign In'})).toBeHidden();
|
|
await logout(page);
|
|
});
|
|
|
|
function totpCode(secret: string, stepOffset: number) {
|
|
const bits = Array.from(secret, (char) => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.indexOf(char).toString(2).padStart(5, '0')).join('');
|
|
const key = Buffer.from(bits.match(/.{8}/g)!.map((byte) => parseInt(byte, 2)));
|
|
const counter = Buffer.alloc(8);
|
|
counter.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 30000) + stepOffset));
|
|
const hmac = createHmac('sha1', key).update(counter).digest();
|
|
return String((hmac.readUInt32BE(hmac[19] & 15) & 0x7fffffff) % 1000000).padStart(6, '0');
|
|
}
|
|
|
|
async function submitPasscode(page: Page, passcode: string) {
|
|
await page.getByLabel('Passcode').fill(passcode);
|
|
await page.getByRole('button', {name: 'Verify'}).click();
|
|
}
|
|
|
|
test('totp sign-in and attempt limit', async ({page, request}) => {
|
|
const username = `e2e-totp-${randomString(8)}`;
|
|
await apiCreateUser(request, username);
|
|
await loginUser(page, username);
|
|
await page.goto('/user/settings/security/two_factor/enroll');
|
|
const secret = (await page.locator('.user-setting-content img').getAttribute('alt'))!;
|
|
await submitPasscode(page, totpCode(secret, 0));
|
|
await expect(page.getByText('successfully enrolled')).toBeVisible();
|
|
|
|
await signInWithPassword(page, username);
|
|
await page.waitForURL('**/user/two_factor');
|
|
await submitPasscode(page, totpCode(secret, 1));
|
|
await expect(page).not.toHaveURL(/\/user\/(login|two_factor)/);
|
|
|
|
await signInWithPassword(page, username);
|
|
await page.waitForURL('**/user/two_factor');
|
|
for (let attempt = 0; attempt < 5; attempt++) {
|
|
await submitPasscode(page, '0000000');
|
|
await expect(page.getByLabel('Passcode')).toHaveValue('');
|
|
}
|
|
await expect(page.getByText('Your passcode is incorrect')).toBeVisible();
|
|
await submitPasscode(page, '0000000');
|
|
await expect(page.getByText('Too many incorrect passcodes')).toBeVisible();
|
|
});
|