관리자 사이트 설정에 "채팅 도배 방지" 켜기/끄기(기본 끄기): 끄면 채팅·입력 중 신호에 속도 제한이 전혀 없고, 켜면 접속마다 초당 50개(한 번에 200개)와 해석 전 원시 한도. 메시지마다 설정을 읽어 저장하면 열린 접속에도 바로 적용. 게임 조작 한도와 잘못된 메시지 차단은 그대로
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,8 @@ export const SiteSettingsSchema = z.object({
|
||||
graceSec: z.number().int().min(10).max(600).default(60),
|
||||
chatEnabledDefault: z.boolean().default(true),
|
||||
chatFilterDefault: z.boolean().default(true),
|
||||
/** Chat flood guard (per-socket chat budget, docs/03 §8). Off: chat lines and 입력 중 signals are never limited. */
|
||||
chatFloodGuard: z.boolean().default(false),
|
||||
/** Animations and the computers' thinking time are divided by this (applies at once, also to running games). */
|
||||
gameSpeed: z.number().min(GAME_SPEED_MIN).max(GAME_SPEED_MAX).default(GAME_SPEED_DEFAULT),
|
||||
});
|
||||
|
||||
@@ -234,6 +234,27 @@ describe('M1 integration scenarios', () => {
|
||||
expect(a.messages.filter((m) => m.t === 'pong').length).toBe(30);
|
||||
});
|
||||
|
||||
test('10c. admin 채팅 도배 방지 (default off) applies at once to open sockets', async () => {
|
||||
const s = start();
|
||||
expect(s.settings.getSite().chatFloodGuard).toBe(false);
|
||||
const a = await client(s, '매크로');
|
||||
const code = await createRoom(s, a);
|
||||
await join(a, code);
|
||||
const lines = () => a.messages.filter((m) => m.t === 'chat').length;
|
||||
// On: 230 lines at once → only the chat budget (200) gets through.
|
||||
s.settings.setSite({ ...s.settings.getSite(), chatFloodGuard: true }, 'test');
|
||||
for (let i = 0; i < 230; i++) a.send({ t: 'chat', text: `켜짐 ${i}` });
|
||||
await a.next('chat', (m) => m.text === '켜짐 199');
|
||||
await sleep(200);
|
||||
expect(lines()).toBe(200);
|
||||
// Off again: no limit at all.
|
||||
s.settings.setSite({ ...s.settings.getSite(), chatFloodGuard: false }, 'test');
|
||||
for (let i = 0; i < 300; i++) a.send({ t: 'chat', text: `꺼짐 ${i}` });
|
||||
await a.next('chat', (m) => m.text === '꺼짐 299');
|
||||
expect(lines()).toBe(500);
|
||||
expect(a.closed).toBeFalsy();
|
||||
});
|
||||
|
||||
test('11. malformed / oversized / schema-invalid messages', async () => {
|
||||
const s = start();
|
||||
const a = await client(s, '이상한값');
|
||||
|
||||
@@ -77,7 +77,14 @@ export function startServer(opts: StartOptions) {
|
||||
});
|
||||
const restored = rooms.restoreAll();
|
||||
log('info', 'rooms restored', restored);
|
||||
const gateway = new Gateway({ rooms, me: publicUser, log, onClose: (id) => users.touch(id), build: () => build });
|
||||
const gateway = new Gateway({
|
||||
rooms,
|
||||
me: publicUser,
|
||||
log,
|
||||
onClose: (id) => users.touch(id),
|
||||
build: () => build,
|
||||
chatFloodGuard: () => settings.getSite().chatFloodGuard,
|
||||
});
|
||||
const removeGuest = (id: string) => {
|
||||
rooms.leaveAll(id);
|
||||
gateway.closeUser(id);
|
||||
|
||||
@@ -22,8 +22,11 @@ describe('gateway flood guard (2026-10-14)', () => {
|
||||
let spy: ReturnType<typeof spyOn> | null = null;
|
||||
afterEach(() => spy?.mockRestore());
|
||||
|
||||
test('a flood is cut off before parsing: at most the raw budget of messages is ever parsed', () => {
|
||||
const gw = new Gateway({ rooms: {} as RoomManager, me: () => null, now: () => 0 });
|
||||
const chatLine = JSON.stringify({ t: 'chat', text: '도배' });
|
||||
const gateway = (guard: boolean) => new Gateway({ rooms: {} as RoomManager, me: () => null, now: () => 0, chatFloodGuard: () => guard });
|
||||
|
||||
test('도배 방지 켜짐: a flood is cut off before parsing: at most the raw budget of messages is ever parsed', () => {
|
||||
const gw = gateway(true);
|
||||
const { ws, closed } = socket(gw);
|
||||
// Keep the socket open for the whole flood (strikes would close it): only the parse count is under test.
|
||||
ws.data.strikes = -1e9;
|
||||
@@ -33,4 +36,34 @@ describe('gateway flood guard (2026-10-14)', () => {
|
||||
expect(spy.mock.calls.length).toBeLessThanOrEqual(250);
|
||||
expect(closed()).toBeNull();
|
||||
});
|
||||
|
||||
test('도배 방지 켜짐: chat past its budget (200 at once) is dropped and a flood closes the socket', () => {
|
||||
const gw = gateway(true);
|
||||
const { ws, closed } = socket(gw);
|
||||
for (let i = 0; i < 1000 && closed() === null; i++) gw.handlers.message(ws, chatLine);
|
||||
expect(closed()).toBe(4429);
|
||||
});
|
||||
|
||||
test('도배 방지 꺼짐 (기본): chat is never limited; other messages keep their budget', () => {
|
||||
const gw = gateway(false);
|
||||
const { ws, closed } = socket(gw);
|
||||
spy = spyOn(JSON, 'parse');
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
gw.handlers.message(ws, chatLine);
|
||||
gw.handlers.message(ws, JSON.stringify({ t: 'typing', on: i % 2 === 0, ch: 'room' }));
|
||||
}
|
||||
expect(spy.mock.calls.length).toBe(2000);
|
||||
expect(ws.data.strikes).toBe(0);
|
||||
expect(closed()).toBeNull();
|
||||
// Junk is still cut off (general budget, then strikes).
|
||||
for (let i = 0; i < 1000 && closed() === null; i++) gw.handlers.message(ws, '{not json');
|
||||
expect(closed()).toBe(4429);
|
||||
});
|
||||
|
||||
test('without the setting wired in, the guard is off', () => {
|
||||
const gw = new Gateway({ rooms: {} as RoomManager, me: () => null });
|
||||
const { ws, closed } = socket(gw);
|
||||
for (let i = 0; i < 1000; i++) gw.handlers.message(ws, chatLine);
|
||||
expect(closed()).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,7 +8,8 @@ import { TokenBucket } from './rate-limit';
|
||||
export interface WsData {
|
||||
userId: string;
|
||||
connId: number;
|
||||
/** Every message, checked before parsing (room for both budgets below), so a flood never costs a JSON parse each. */
|
||||
/** Every message, checked before parsing (room for both budgets below), so a flood never costs a JSON parse each.
|
||||
* Only while the chat flood guard is on: with it off chat is unlimited, so the raw count cannot be capped. */
|
||||
rawBucket: TokenBucket;
|
||||
bucket: TokenBucket;
|
||||
/** Chat lines and "입력 중" signals have their own budget, so typing fast never eats into (or waits on) game moves. */
|
||||
@@ -27,6 +28,8 @@ export interface GatewayDeps {
|
||||
onClose?: (userId: string) => void;
|
||||
/** Web build being served (pages opened with an older one offer to refresh, docs/07 §11.1). */
|
||||
build?: () => string | null;
|
||||
/** Admin setting 채팅 도배 방지 (read per message, so a change applies at once). Off when not given. */
|
||||
chatFloodGuard?: () => boolean;
|
||||
}
|
||||
|
||||
export const MAX_CONNS_PER_USER = 5;
|
||||
@@ -130,7 +133,8 @@ export class Gateway {
|
||||
const strike = () => {
|
||||
if (++ws.data.strikes > 50) ws.close(4429, 'rate limited');
|
||||
};
|
||||
if (!ws.data.rawBucket.take()) return strike();
|
||||
const guard = this.deps.chatFloodGuard?.() ?? false;
|
||||
if (guard && !ws.data.rawBucket.take()) return strike();
|
||||
let json: unknown;
|
||||
let badJson = false;
|
||||
try {
|
||||
@@ -138,8 +142,10 @@ export class Gateway {
|
||||
} catch {
|
||||
badJson = true;
|
||||
}
|
||||
const bucket = !badJson && isChatMessage(json) ? ws.data.chatBucket : ws.data.bucket;
|
||||
if (!bucket.take()) return strike();
|
||||
// Guard off: chat is never limited. Everything else keeps the general budget, and junk is still cut off by strikes.
|
||||
const chat = !badJson && isChatMessage(json);
|
||||
const bucket = chat ? (guard ? ws.data.chatBucket : null) : ws.data.bucket;
|
||||
if (bucket && !bucket.take()) return strike();
|
||||
if (badJson) {
|
||||
this.send(ws, { t: 'error', code: 'bad-json', message: '잘못된 요청이에요.' });
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user