feat(auth): throttle logins, close sessions on password change, password policy
All checks were successful
Deploy Gameservers Control / check (push) Successful in 6s
Deploy Gameservers Control / deploy (push) Has been skipped

- limit login attempts per IP (5 per 15 min by default, counted before the
  password check so parallel requests cannot exceed it), 429 + Retry-After,
  failed logins logged with the client IP
- TRUSTED_PROXIES env to honour X-Forwarded-For from the reverse proxy only
- changing or regenerating a group password closes that group's other
  sessions; deleting a group closes all of its sessions
- new group passwords: 12 to 256 characters and not shared with another
  group (login opens the first matching group)
- PATCH /groups/:id/password no longer echoes the password
- purge expired sessions every 10 minutes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-12-02 12:34:11 +01:00
parent 55be6bf034
commit 7d89a4e646
10 changed files with 280 additions and 16 deletions

View file

@ -12,6 +12,7 @@ fs.mkdirSync(path.join(tmpRoot, "server-images"), { recursive: true });
const { createApp } = await import("../src/app.js");
const { hashPassword } = await import("../src/services/crypto.js");
const { pruneLogs } = await import("../src/services/storage.js");
const { resetLoginThrottle } = await import("../src/services/loginThrottle.js");
const servers = [
{
@ -290,7 +291,7 @@ test("admin can create, update, regenerate, and delete a group", async () => {
body: { password: "replacement-password" },
});
assert.equal(manualPassword.status, 200);
assert.equal(manualPassword.body.password, "replacement-password");
assert.equal(manualPassword.body.password, undefined);
const replacementLoginToken = await login("replacement-password");
assert.ok(replacementLoginToken);
@ -408,6 +409,123 @@ test("prunes launch logs older than seven days", () => {
assert.equal(logs[0].groupId, "new");
});
test("rejects weak or already used group passwords", async () => {
const token = await login(adminLoginPassword);
const tooShort = await request("/api/admin/groups", {
method: "POST",
token,
body: { name: "Short", password: "short-pw" },
});
assert.equal(tooShort.status, 400);
const duplicateOnCreate = await request("/api/admin/groups", {
method: "POST",
token,
body: { name: "Copycat", password: playerPassword },
});
assert.equal(duplicateOnCreate.status, 400);
const duplicateOnUpdate = await request("/api/admin/groups/players/password", {
method: "PATCH",
token,
body: { password: adminLoginPassword },
});
assert.equal(duplicateOnUpdate.status, 400);
const sameGroup = await request("/api/admin/groups/players/password", {
method: "PATCH",
token,
body: { password: playerPassword },
});
assert.equal(sameGroup.status, 200);
const groups = await request("/api/admin/groups", { token });
assert.deepEqual(
groups.body.groups.map((group) => group.id).sort(),
["admin", "players"]
);
});
test("changing or regenerating a group password closes its other sessions", async () => {
const adminToken = await login(adminLoginPassword);
const created = await request("/api/admin/groups", {
method: "POST",
token: adminToken,
body: { name: "Session Group", password: "session-group-pw-1" },
});
const groupId = created.body.group.id;
const firstToken = await login("session-group-pw-1");
assert.equal((await request("/api/servers", { token: firstToken })).status, 200);
const changed = await request(`/api/admin/groups/${groupId}/password`, {
method: "PATCH",
token: adminToken,
body: { password: "session-group-pw-2" },
});
assert.equal(changed.status, 200);
assert.equal((await request("/api/servers", { token: firstToken })).status, 401);
const secondToken = await login("session-group-pw-2");
const regenerated = await request(`/api/admin/groups/${groupId}/regenerate-password`, {
method: "POST",
token: adminToken,
});
assert.equal(regenerated.status, 200);
assert.equal((await request("/api/servers", { token: secondToken })).status, 401);
const thirdToken = await login(regenerated.body.password);
await request(`/api/admin/groups/${groupId}`, { method: "DELETE", token: adminToken });
assert.equal((await request("/api/servers", { token: thirdToken })).status, 401);
});
test("admin keeps the session used to change the admin password", async () => {
const currentToken = await login(adminLoginPassword);
const otherToken = await login(adminLoginPassword);
const changed = await request("/api/admin/groups/admin/password", {
method: "PATCH",
token: currentToken,
body: { password: "temporary-admin-pw" },
});
assert.equal(changed.status, 200);
assert.equal((await request("/api/session", { token: currentToken })).status, 200);
assert.equal((await request("/api/session", { token: otherToken })).status, 401);
const restored = await request("/api/admin/groups/admin/password", {
method: "PATCH",
token: currentToken,
body: { password: adminLoginPassword },
});
assert.equal(restored.status, 200);
});
test("blocks login after too many attempts, even with spoofed X-Forwarded-For", async () => {
resetLoginThrottle();
try {
for (let attempt = 1; attempt <= 5; attempt += 1) {
const response = await request("/api/login", {
method: "POST",
body: { password: `wrong-${attempt}` },
headers: { "X-Forwarded-For": `203.0.113.${attempt}` },
});
assert.equal(response.status, 401);
}
const blocked = await request("/api/login", {
method: "POST",
body: { password: playerPassword },
headers: { "X-Forwarded-For": "203.0.113.99" },
});
assert.equal(blocked.status, 429);
} finally {
resetLoginThrottle();
}
assert.ok(await login(playerPassword));
});
async function login(password) {
const response = await request("/api/login", {
method: "POST",
@ -417,8 +535,8 @@ async function login(password) {
return response.body.token;
}
async function request(pathname, { method = "GET", token, body } = {}) {
const headers = {};
async function request(pathname, { method = "GET", token, body, headers: extraHeaders = {} } = {}) {
const headers = { ...extraHeaders };
if (token) headers.Authorization = `Bearer ${token}`;
if (body && !(body instanceof FormData)) headers["Content-Type"] = "application/json";