-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Security: prevent winner stats spoofing and auth-gate singleplayer archive #4004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Radomir-Aksenenko
wants to merge
11
commits into
openfrontio:main
Choose a base branch
from
Radomir-Aksenenko:fix/winner-stats-auth-upstream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9ada38f
fix: prevent fake winner stats and auth-gate singleplayer archive
Radomir-Aksenenko 602ba12
fix: send JWT auth header when archiving singleplayer game
Radomir-Aksenenko 3e07e25
style: fix Prettier formatting in GameServer.ts
Radomir-Aksenenko 2096ab7
test: add ClientSendWinnerSchema validation tests
Radomir-Aksenenko db6aff4
fix: decouple auth lookup from archive upload in LocalServer
Radomir-Aksenenko 1dcf681
Merge branch 'main' into fix/winner-stats-auth-upstream
iiamlewis ef4d5e6
fix: skip singleplayer archive for anonymous users; add security test
Radomir-Aksenenko 00a6195
Merge remote-tracking branch 'openfront/fix/winner-stats-auth-upstrea…
Radomir-Aksenenko 237808b
fix: keep allPlayersStats for local games in onSendWinnerEvent
Radomir-Aksenenko 5c68235
style: fix Prettier formatting in Transport.ts, LocalServer.ts, tests
Radomir-Aksenenko e4ed6bf
test: restore real timers in WinnerSecurity teardown
Radomir-Aksenenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { ClientSendWinnerSchema } from "../src/core/Schemas"; | ||
|
|
||
| describe("ClientSendWinnerSchema", () => { | ||
| // ID must match /^[A-Za-z0-9]{8}$/ | ||
| const id1 = "AAAAAAAA"; | ||
| const id2 = "BBBBBBBB"; | ||
| const validWinner = ["player", id1, id2] as const; | ||
|
|
||
| test("accepts a winner message without allPlayersStats", () => { | ||
| const result = ClientSendWinnerSchema.safeParse({ | ||
| type: "winner", | ||
| winner: validWinner, | ||
| }); | ||
| expect(result.success).toBe(true); | ||
| }); | ||
|
|
||
| test("accepts a winner message with allPlayersStats (singleplayer path)", () => { | ||
| const result = ClientSendWinnerSchema.safeParse({ | ||
| type: "winner", | ||
| winner: validWinner, | ||
| allPlayersStats: { | ||
| [id1]: {}, | ||
| }, | ||
| }); | ||
| expect(result.success).toBe(true); | ||
| }); | ||
|
|
||
| test("accepts a winner message with undefined winner (draw)", () => { | ||
| const result = ClientSendWinnerSchema.safeParse({ | ||
| type: "winner", | ||
| winner: undefined, | ||
| }); | ||
| expect(result.success).toBe(true); | ||
| }); | ||
|
|
||
| test("rejects a message with wrong type", () => { | ||
| const result = ClientSendWinnerSchema.safeParse({ | ||
| type: "not_winner", | ||
| winner: validWinner, | ||
| }); | ||
| expect(result.success).toBe(false); | ||
| }); | ||
|
|
||
| test("rejects a message with invalid winner format", () => { | ||
| const result = ClientSendWinnerSchema.safeParse({ | ||
| type: "winner", | ||
| winner: "invalid", | ||
| }); | ||
| expect(result.success).toBe(false); | ||
| }); | ||
|
|
||
| test("allPlayersStats is absent (undefined) when not provided by sender", () => { | ||
| const result = ClientSendWinnerSchema.safeParse({ | ||
| type: "winner", | ||
| winner: validWinner, | ||
| }); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.allPlayersStats).toBeUndefined(); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| vi.mock("../../src/core/Schemas", async () => { | ||
| const actual = (await vi.importActual("../../src/core/Schemas")) as any; | ||
| return { | ||
| ...actual, | ||
| GameStartInfoSchema: { | ||
| safeParse: (data: any) => ({ success: true, data: data }), | ||
| }, | ||
| ServerPrestartMessageSchema: { | ||
| safeParse: (data: any) => ({ success: true, data: data }), | ||
| }, | ||
| ClientMessageSchema: { | ||
| safeParse: (data: any) => ({ success: true, data: data }), | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock("../../src/server/Archive", () => ({ | ||
| archive: vi.fn(), | ||
| finalizeGameRecord: (r: any) => r, | ||
| })); | ||
|
|
||
| import { GameType } from "../../src/core/game/Game"; | ||
| import { archive } from "../../src/server/Archive"; | ||
| import { Client } from "../../src/server/Client"; | ||
| import { GameServer } from "../../src/server/GameServer"; | ||
|
|
||
| function makeMockWs(ip = "1.2.3.4") { | ||
| const handlers: Record<string, (...args: any[]) => any> = {}; | ||
| return { | ||
| on: (event: string, handler: (...args: any[]) => any) => { | ||
| handlers[event] = handler; | ||
| }, | ||
| removeAllListeners: (_event: string) => {}, | ||
| send: vi.fn(), | ||
| close: vi.fn(), | ||
| readyState: 1, | ||
| trigger: (event: string, ...args: any[]) => handlers[event]?.(...args), | ||
| }; | ||
| } | ||
|
|
||
| function makeClient( | ||
| clientID: string, | ||
| persistentID: string, | ||
| ip = "1.2.3.4", | ||
| ): { client: Client; ws: ReturnType<typeof makeMockWs> } { | ||
| const ws = makeMockWs(ip); | ||
| const client = new Client( | ||
| clientID, | ||
| persistentID, | ||
| null, | ||
| null, | ||
| undefined, | ||
| ip, | ||
| "TestUser", | ||
| null, | ||
| ws as any, | ||
| undefined, | ||
| undefined, | ||
| [], | ||
| ); | ||
| return { client, ws }; | ||
| } | ||
|
|
||
| describe("GameServer - winner message security", () => { | ||
| let mockLogger: any; | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| mockLogger = { | ||
| child: vi.fn().mockReturnThis(), | ||
| info: vi.fn(), | ||
| warn: vi.fn(), | ||
| error: vi.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| vi.clearAllTimers(); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| it("archives with undefined stats regardless of client-supplied allPlayersStats", async () => { | ||
| const game = new GameServer("test-game", mockLogger, Date.now(), { | ||
| gameType: GameType.Private, | ||
| } as any); | ||
|
|
||
| const { client: c1, ws: ws1 } = makeClient("cid-1", "pid-1", "1.2.3.4"); | ||
| const { client: c2, ws: ws2 } = makeClient("cid-2", "pid-2", "5.6.7.8"); | ||
| game.joinClient(c1); | ||
| game.joinClient(c2); | ||
| game.start(); | ||
|
|
||
| // Both clients vote for the same winner and attach fabricated stats. | ||
| // Majority threshold is met so archiveGame() is triggered. | ||
| const fabricatedStats = { | ||
| "cid-1": { kills: 9999, gold: 9999 } as any, | ||
| "cid-2": { kills: 9999, gold: 9999 } as any, | ||
| }; | ||
|
|
||
| await ws1.trigger( | ||
| "message", | ||
| JSON.stringify({ | ||
| type: "winner", | ||
| winner: ["player", "cid-1"], | ||
| allPlayersStats: fabricatedStats, | ||
| }), | ||
| ); | ||
| await ws2.trigger( | ||
| "message", | ||
| JSON.stringify({ | ||
| type: "winner", | ||
| winner: ["player", "cid-1"], | ||
| allPlayersStats: fabricatedStats, | ||
| }), | ||
| ); | ||
|
|
||
| expect(archive).toHaveBeenCalledOnce(); | ||
| const archivedRecord = (archive as ReturnType<typeof vi.fn>).mock | ||
| .calls[0][0]; | ||
| for (const player of archivedRecord.info.players) { | ||
| expect(player.stats).toBeUndefined(); | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
anonymous users do have jwts