Skip to content

Security, stability & performance quick wins from codebase audit - #184

Open
Sparksx wants to merge 1 commit into
mainfrom
claude/focused-curie-foea7f
Open

Security, stability & performance quick wins from codebase audit#184
Sparksx wants to merge 1 commit into
mainfrom
claude/focused-curie-foea7f

Conversation

@Sparksx

@Sparksx Sparksx commented Jul 23, 2026

Copy link
Copy Markdown
Owner

Summary

Full codebase audit across security, performance, stability, and functionality — 44 findings identified, 8 quick wins implemented in this PR.

Security (Critical + High fixes)

  • Anti-cheat: currency tamper protectionPUT /api/game/state now validates gold/diamonds/essence can only decrease (client spending) and forgeLevel can only increase. Clients can no longer inflate currency by sending arbitrary values.
  • Atomic refund clawback — Stripe refund webhook now uses a Prisma $transaction with decrement instead of a racy read-then-write that could duplicate or lose gold under concurrent access.
  • Atomic admin currency adjustments — All three admin endpoints (gold/essence/diamonds) now use transactions with increment/decrement instead of read-then-write.

Stability

  • Fix competing shutdown handlers — Removed SIGINT/SIGTERM handlers from prisma.js that raced with index.js shutdown sequence, potentially calling process.exit(0) before HTTP/Socket.IO cleanup.
  • Fix trashItem() state mutationtrashItem() now correctly sets equipment[type] = null, removing the need for home.js to directly mutate state internals (violating model/view separation).
  • Guard spendGold() against negative amounts — Passing a negative amount no longer increases gold.

Performance

  • Monster templates caching — Added Cache-Control: public, max-age=3600 header (matching equipment and player template endpoints).
  • Lightweight clan list query — Created LIST_CLAN_INCLUDE for GET /api/clans that skips loading full player and updatedAt data for every member of every clan in the top-25 list.

Test plan

  • npm test — 524 tests pass
  • npm run lint — 0 errors (pre-existing warnings only)
  • npm run build — clean production build
  • Manual: verify forge upgrade still persists gold spend after page reload
  • Manual: verify cosmetic purchases still deduct gold correctly
  • Manual: verify clan list loads faster on populated server
  • Manual: verify Stripe purchase flow still credits gold

Generated by Claude Code

Security:
- Make gold/diamonds/essence/forgeLevel tamper-resistant: server only
  accepts values that decreased (spending) or forge level that increased,
  preventing clients from inflating currency
- Fix refund gold clawback to use atomic transaction with decrement
  instead of racy read-then-write pattern
- Fix admin gold/essence/diamonds adjustments to use atomic transactions

Stability:
- Remove competing SIGINT/SIGTERM handlers from prisma.js (index.js
  handles shutdown in the correct order: Socket.IO → HTTP → Prisma)
- Fix trashItem() to actually clear the equipment slot, removing the
  need for home.js to directly mutate state internals
- Add negative amount guard to spendGold()

Performance:
- Add Cache-Control header to monster templates endpoint (matches
  equipment and player template endpoints)
- Create lightweight LIST_CLAN_INCLUDE for clan list endpoint, avoiding
  loading full gameState JSON blobs for 25 clans × N members

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PakRpNc4HcG1sRQwy3h249
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants