2026-10-24 16:09:45 +08:00
|
|
|
'use strict';
|
2026-12-04 08:58:53 -08:00
|
|
|
const test = require('node:test'),
|
|
|
|
|
assert = require('node:assert/strict'),
|
|
|
|
|
fs = require('node:fs'),
|
|
|
|
|
os = require('node:os'),
|
|
|
|
|
path = require('node:path');
|
|
|
|
|
const { Ledger } = require('../ledger');
|
|
|
|
|
const { createApp, createUser } = require('../server');
|
2026-10-24 16:09:45 +08:00
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('同一来源连续输错超过10次后仍可立即登录,密码与会话保护保持有效', async t => {
|
|
|
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-login-test-'));
|
|
|
|
|
const ledger = new Ledger(':memory:');
|
|
|
|
|
const name = '合成登录用户',
|
|
|
|
|
password = 'synthetic-login-password';
|
|
|
|
|
createUser(ledger, name, 'owner', password);
|
|
|
|
|
const app = createApp({
|
|
|
|
|
mode: 'trial',
|
|
|
|
|
host: '127.0.0.1',
|
|
|
|
|
port: 0,
|
|
|
|
|
dataDir: dir,
|
|
|
|
|
ledger,
|
|
|
|
|
feishuConfig: { enabled: false },
|
|
|
|
|
});
|
|
|
|
|
t.after(async () => {
|
|
|
|
|
await new Promise(resolve => {
|
|
|
|
|
app.server.closeAllConnections();
|
|
|
|
|
app.server.close(resolve);
|
|
|
|
|
});
|
|
|
|
|
ledger.close();
|
|
|
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
|
|
|
});
|
|
|
|
|
await new Promise(resolve => app.server.listen(0, '127.0.0.1', resolve));
|
|
|
|
|
const base = 'http://127.0.0.1:' + app.server.address().port;
|
|
|
|
|
const login = (input, extra = {}) =>
|
|
|
|
|
fetch(base + '/api/login', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json', Origin: base, 'X-Requested-With': 'maimai-ledger', ...extra },
|
|
|
|
|
body: JSON.stringify(input),
|
|
|
|
|
});
|
|
|
|
|
const protectedRequest = cookie => fetch(base + '/api/state', { headers: cookie ? { Cookie: cookie } : {} });
|
2026-10-24 16:09:45 +08:00
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
assert.equal((await protectedRequest()).status, 401);
|
|
|
|
|
for (let i = 0; i < 12; i++) {
|
|
|
|
|
const rejected = await login({ name, password: 'wrong-password' });
|
|
|
|
|
assert.equal(rejected.status, 401, '第' + (i + 1) + '次错误密码仍应由密码校验拒绝');
|
|
|
|
|
assert.deepEqual(await rejected.json(), { error: '账号或密码不正确' });
|
|
|
|
|
assert.equal(rejected.headers.get('set-cookie'), null);
|
2026-10-24 16:09:45 +08:00
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
assert.equal((await protectedRequest()).status, 401);
|
|
|
|
|
assert.equal((await login({ name, password }, { Origin: 'https://evil.example' })).status, 403);
|
|
|
|
|
assert.equal((await login({ name, password }, { 'X-Requested-With': '' })).status, 403);
|
2026-10-24 16:09:45 +08:00
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
const accepted = await login({ name, password });
|
|
|
|
|
assert.equal(accepted.status, 200);
|
|
|
|
|
assert.equal((await accepted.json()).user.name, name);
|
|
|
|
|
const sessionCookie = accepted.headers.get('set-cookie');
|
|
|
|
|
assert.match(sessionCookie, /^mm_session=[a-f0-9]+;/);
|
|
|
|
|
assert.match(sessionCookie, /HttpOnly; SameSite=Strict; Path=\/; Max-Age=28800/);
|
|
|
|
|
assert.equal((await protectedRequest(sessionCookie.split(';')[0])).status, 200);
|
|
|
|
|
assert.equal((await protectedRequest()).status, 401);
|
|
|
|
|
assert.equal((await login({ name: '不存在的合成用户', password })).status, 401);
|
|
|
|
|
assert.equal((await login({ name, password: 'wrong-password' })).status, 401);
|
2026-10-24 16:09:45 +08:00
|
|
|
});
|