fix(api): secure /api/archive_singleplayer_game endpoint with JWT validation#3954
fix(api): secure /api/archive_singleplayer_game endpoint with JWT validation#3954berkelmali wants to merge 3 commits into
Conversation
game.end() is async but was called without await inside a try/catch. The try/catch only catches synchronous errors. Added .catch() to the returned promise to prevent unhandled promise rejections from crashing the server.
WalkthroughThis PR introduces two focused improvements to the game server: error handling for completed games now uses promise rejection handlers instead of synchronous exceptions, and the single-player game archive endpoint now requires Bearer token authentication with user identity verification. ChangesGame Server Updates
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/server/Worker.ts (1)
281-290: 💤 Low valueSmall terminology nit: 403 error message says "Unauthorized" but should say "Forbidden".
HTTP 401 is "Unauthorized" (authentication failure), HTTP 403 is "Forbidden" (authorization failure). The message "Unauthorized user for this record" is slightly confusing when returned with status 403.
Consider using "Forbidden" or "Not allowed to archive this record" for clarity.
📝 Suggested terminology fix
return res .status(403) - .json({ error: "Unauthorized user for this record" }); + .json({ error: "Forbidden: cannot archive another user's record" });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/server/Worker.ts` around lines 281 - 290, The response body for the 403 branch uses wording "Unauthorized user for this record" which conflicts with the 403 semantics; update the message returned in the res.status(403).json call to use a "Forbidden" phrasing (e.g., "Forbidden: user not allowed to archive this record" or "Forbidden user for this record") and ensure any log.warn context (tokenUser/recordUser) remains unchanged; locate the check around player.persistentID !== persistentID in the function handling the request and replace only the error string passed to res.status(403).json.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/server/Worker.ts`:
- Around line 281-290: The response body for the 403 branch uses wording
"Unauthorized user for this record" which conflicts with the 403 semantics;
update the message returned in the res.status(403).json call to use a
"Forbidden" phrasing (e.g., "Forbidden: user not allowed to archive this record"
or "Forbidden user for this record") and ensure any log.warn context
(tokenUser/recordUser) remains unchanged; locate the check around
player.persistentID !== persistentID in the function handling the request and
replace only the error string passed to res.status(403).json.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9a65233c-b319-421e-a71a-8e772121c74c
📒 Files selected for processing (2)
src/server/GameManager.tssrc/server/Worker.ts
Resolves #3957
Description:
This PR patches a critical unauthenticated API endpoint at
/api/archive_singleplayer_gameinWorker.ts. The endpoint was accepting game records directly from the request body without any authentication and forwarding them to the central database, enabling anyone to spoof and pollute singleplayer statistics.A
verifyClientTokenauthentication middleware check has been added to the endpoint, ensuring that only authenticated requests from verified clients are processed and archived.Please complete the following:
Please put your Discord username so you can be contacted if a bug or regression is found:
barfires