security: fix underpayment exploit and make provably-fair real

Phase 0.1 — Underpayment confirmation bug (CRITICAL):
Payment confirmation gated on `status.confirmed`, which checkPaymentStatus
sets for ANY incoming tx regardless of amount, so sending a single atomic
unit granted a full game/credits. Now gate on `status.complete` (received
>= expected). Underpayers are warned once and the payment stays pending so
a top-up can complete it. Persist `received_amount` for single-game and
credits confirmations for reconciliation. Adds an underpayment regression
test.

Phase 0.2 — Provably-fair was decorative (CRITICAL):
The committed seed had zero effect on the dungeon (created seededRNG was
unused; generation used Math.random + a globally-seeded ROT.RNG). Thread a
per-game seeded RNG derived from the committed seed through dungeon
generation (reseed the Digger via seedToInt + replace all Math.random) and
monster movement (seededShuffle). Add DungeonGenerator.regenerateFromSeed
and layoutFingerprint; /api/verify now regenerates the dungeon from the
seed and returns a fingerprint a player can independently reproduce. Adds a
determinism test suite.

Full suite: 11 suites / 113 tests passing.

Plan and progress tracked in docs/REMEDIATION_PLAN.md.
This commit is contained in:
jwinterm
2026-05-31 09:14:21 -04:00
parent 82fdcc0203
commit 3e6e3b858b
10 changed files with 468 additions and 148 deletions
+70 -7
View File
@@ -1,5 +1,7 @@
const crypto = require('crypto');
const ROT = require('./rot.js');
const { getDifficultyConfig, getMonsterSpawnRoomIndex, getTreasureRoomIndex } = require('./difficultyConfig');
const { createSeededRNG, seedToInt } = require('./provablyFair');
// Environment-based console logging control
const CONSOLE_LOGGING = process.env.NODE_ENV === 'debug' || process.env.NODE_ENV === 'development';
@@ -58,6 +60,55 @@ class DungeonGenerator {
};
}
/**
* PROVABLY FAIR (Phase 0.2): Reproduce a dungeon purely from its committed seed.
* Mirrors the standard generation path used by Game so a verifier can independently
* regenerate the exact layout a player saw. Assumes standard generation options
* (the difficulty preset for `cryptoType`); games started with custom dungeon
* overrides would need those overrides passed in `gameOptions` to match.
* @param {string} seed - The revealed game seed
* @param {string|null} cryptoType - Currency, selects the difficulty preset
* @param {object} gameOptions - Optional overrides matching the original game
* @returns {object} The regenerated dungeon ({ map, rooms, entrance, exit, treasure, torches })
*/
static regenerateFromSeed(seed, cryptoType = null, gameOptions = {}) {
const dungeonConfig = this.getConfig(cryptoType);
const width = gameOptions.width || dungeonConfig.DEFAULT_WIDTH;
const height = gameOptions.height || dungeonConfig.DEFAULT_HEIGHT;
const gameConfig = {
width,
height,
floorVariation: gameOptions.floorVariation !== undefined ? gameOptions.floorVariation : dungeonConfig.FLOOR_VARIATION,
torchEnabled: gameOptions.torchEnabled !== undefined ? gameOptions.torchEnabled : dungeonConfig.TORCH_ENABLED,
torchDensity: gameOptions.torchDensity !== undefined ? gameOptions.torchDensity : dungeonConfig.TORCH_DENSITY,
primaryFloor: gameOptions.primaryFloor || dungeonConfig.PRIMARY_FLOOR,
secondaryFloor: gameOptions.secondaryFloor || dungeonConfig.SECONDARY_FLOOR,
torchTile: gameOptions.torchTile || dungeonConfig.TORCH_TILE,
...gameOptions
};
return this.generate(width, height, {
...gameConfig,
rng: createSeededRNG(seed),
seedInt: seedToInt(seed)
});
}
/**
* Deterministic fingerprint of a dungeon's layout, used to compare a regenerated
* dungeon against the one that was played.
* @param {object} dungeon - A dungeon object from generate()/regenerateFromSeed()
* @returns {string} SHA-256 hex of the layout-defining fields
*/
static layoutFingerprint(dungeon) {
const payload = JSON.stringify({
map: dungeon.map,
entrance: dungeon.entrance,
exit: dungeon.exit,
treasure: dungeon.treasure
});
return crypto.createHash('sha256').update(payload).digest('hex');
}
static generate(width, height, options = {}) {
const defaultOptions = {
floorVariation: DUNGEON_CONFIGS.FLOOR_VARIATION,
@@ -73,6 +124,17 @@ class DungeonGenerator {
console.log(`[DungeonGenerator] Generating dungeon with effective torchDensity: ${config.torchDensity}`);
}
// PROVABLY FAIR (Phase 0.2): when a per-game seeded RNG is supplied, all
// randomness in generation is derived from the committed seed so the dungeon
// is reproducible for verification. `config.rng` drives floor/torch/treasure
// placement; `config.seedInt` deterministically seeds ROT.RNG for the Digger.
// Generation is fully synchronous, so seeding the global ROT.RNG here is safe
// even with concurrent games (no other game can interleave mid-generation).
const rng = (typeof config.rng === 'function') ? config.rng : Math.random;
if (config.seedInt != null) {
ROT.RNG.setSeed(config.seedInt);
}
// Create dungeon using ROT.js Map.Digger
const digger = new ROT.Map.Digger(width, height, {
roomWidth: DUNGEON_CONFIGS.ROOM_WIDTH_RANGE,
@@ -117,8 +179,8 @@ class DungeonGenerator {
const treasureCenter = treasureRoom.getCenter();
// Place treasure slightly off center to make it more interesting
const treasure = [
treasureCenter[0] + Math.floor(Math.random() * 3) - 1,
treasureCenter[1] + Math.floor(Math.random() * 3) - 1
treasureCenter[0] + Math.floor(rng() * 3) - 1,
treasureCenter[1] + Math.floor(rng() * 3) - 1
];
// Ensure the treasure is on a floor tile (check for both floor types)
@@ -150,7 +212,8 @@ class DungeonGenerator {
}
let placedTorchesCount = 0; // Counter for torches placed in this function
const rng = (typeof config.rng === 'function') ? config.rng : Math.random;
// Log the torchDensity being used for this map generation pass
if (CONSOLE_LOGGING) {
console.log(`[DungeonGenerator.enhanceMapWithVariations] Starting enhancement with torchDensity: ${config.torchDensity}`);
@@ -165,14 +228,14 @@ class DungeonGenerator {
if (cell === 0) {
// Floor tile - randomly choose primary or secondary
if (Math.random() < config.floorVariation) {
if (rng() < config.floorVariation) {
enhancedMap[y][x] = config.secondaryFloor;
} else {
enhancedMap[y][x] = config.primaryFloor;
}
} else if (cell === 1) {
// Wall tile - maybe add a torch
if (config.torchEnabled && this.shouldPlaceTorch(basicMap, x, y, config.torchDensity)) {
if (config.torchEnabled && this.shouldPlaceTorch(basicMap, x, y, config.torchDensity, rng)) {
enhancedMap[y][x] = config.torchTile;
placedTorchesCount++; // Increment if a torch is placed
} else {
@@ -188,7 +251,7 @@ class DungeonGenerator {
}
// Determine if a torch should be placed on this wall tile
static shouldPlaceTorch(map, x, y, torchDensity) {
static shouldPlaceTorch(map, x, y, torchDensity, rng = Math.random) {
// Only place torches on walls that are adjacent to floors (for lighting logic)
const height = map.length;
const width = map[0].length;
@@ -202,7 +265,7 @@ class DungeonGenerator {
return nx >= 0 && nx < width && ny >= 0 && ny < height && map[ny][nx] === 0;
});
const randomValue = Math.random();
const randomValue = rng();
const shouldPlace = adjacentToFloor && randomValue < torchDensity;
return shouldPlace;
+18 -7
View File
@@ -5,7 +5,7 @@ const Monster = require('./monster.js');
const DungeonGenerator = require('./dungeon.js');
const LightingAndFov = require('./lightingAndFov.js');
const { getDifficultyConfig, getMonsterSpawnRoomIndex, getTreasureRoomIndex } = require('./difficultyConfig');
const { createGameProof, getPreGameCommitment, getPostGameReveal, createSeededRNG } = require('./provablyFair');
const { createGameProof, getPreGameCommitment, getPostGameReveal, createSeededRNG, seedToInt, seededShuffle } = require('./provablyFair');
// Environment-based console logging control
const CONSOLE_LOGGING = process.env.NODE_ENV === 'debug' || process.env.NODE_ENV === 'development';
@@ -29,7 +29,11 @@ class Game {
// Provably fair: Generate game proof before anything else
this.gameProof = createGameProof(this.id);
// Per-game deterministic RNG derived from the committed seed. ALL game randomness
// (dungeon generation + monster movement) flows through this so the game is
// reproducible from the seed for provably-fair verification.
this.seededRNG = createSeededRNG(this.gameProof.seed);
this.seedInt = seedToInt(this.gameProof.seed);
// Get default configuration from DungeonGenerator
const dungeonConfig = DungeonGenerator.getConfig();
@@ -114,8 +118,14 @@ class Game {
this.monsterMoveAccumulator = 0; // For fractional monster moves
this.fee = 0;
// Generate dungeon with the provided dimensions and config
this.dungeon = DungeonGenerator.generate(this.gameConfig.width, this.gameConfig.height, this.gameConfig);
// Generate dungeon with the provided dimensions and config.
// Pass the per-game seeded RNG + numeric seed so the layout is deterministic
// and verifiable from the committed seed (provably fair).
this.dungeon = DungeonGenerator.generate(this.gameConfig.width, this.gameConfig.height, {
...this.gameConfig,
rng: this.seededRNG,
seedInt: this.seedInt
});
// Place player at entrance
if (this.dungeon.entrance) {
@@ -236,12 +246,13 @@ class Game {
while (this.monsterMoveAccumulator >= 1) {
this.monsterMoveAccumulator -= 1;
// Apply chase aggressiveness (chance to move randomly instead of chasing)
if (Math.random() < chaseAggressiveness) {
this.monster.moveTowardPlayer(this.player, this.dungeon);
// Apply chase aggressiveness (chance to move randomly instead of chasing).
// Use the per-game seeded RNG so monster behaviour is deterministic from the seed.
if (this.seededRNG() < chaseAggressiveness) {
this.monster.moveTowardPlayer(this.player, this.dungeon, this.seededRNG);
} else {
// Random move - still try to move, just not toward player
this.monster.moveTowardPlayer(null, this.dungeon);
this.monster.moveTowardPlayer(null, this.dungeon, this.seededRNG);
}
}
+4 -3
View File
@@ -260,7 +260,7 @@ class GameModeManager {
* @param {object} packageInfo - Package info (credits, bonus, etc.)
* @returns {object} Result with success, creditsAdded, newBalance, alreadyProcessed
*/
async processCreditsPackageConfirmation(socketId, paymentId, packageInfo = null) {
async processCreditsPackageConfirmation(socketId, paymentId, packageInfo = null, receivedAmount = null) {
try {
// Look up user from payment record's user_id (stable across socket reconnects)
// This avoids the bug where socket ID changes if user refreshes during confirmation
@@ -301,10 +301,11 @@ class GameModeManager {
UPDATE payments
SET status = 'confirmed',
credits_purchased = $1,
confirmed_at = NOW()
confirmed_at = NOW(),
received_amount = COALESCE($3, received_amount)
WHERE id = $2 AND status = 'pending'
RETURNING id
`, [creditsToAdd, paymentId]);
`, [creditsToAdd, paymentId, receivedAmount]);
if (paymentUpdateResult.rows.length === 0) {
// Already processed by another instance - not an error
+12 -10
View File
@@ -1,4 +1,5 @@
const ROT = require('./rot.js');
const { seededShuffle } = require('./provablyFair');
/**
* Monster class - Manages monster state and AI with improved pathfinding
@@ -104,7 +105,7 @@ class Monster {
/**
* Improved AI to move toward player using A* pathfinding with limited vision
*/
moveTowardPlayer(player, dungeon) {
moveTowardPlayer(player, dungeon, rng = Math.random) {
if (!dungeon || !dungeon.map) return;
// Check if a tile is passable (floor, stairs, treasure)
@@ -115,7 +116,7 @@ class Monster {
// If no player target, wander randomly
if (!player) {
this._wander(dungeon, isPassable);
this._wander(dungeon, isPassable, rng);
return;
}
@@ -143,7 +144,7 @@ class Monster {
}
// Fallback: direct move toward player
this._moveDirectToward(player.x, player.y, dungeon, isPassable);
this._moveDirectToward(player.x, player.y, dungeon, isPassable, rng);
} else if (this.lastKnownPlayerX !== null) {
// Player not visible but we have last known position - patrol toward it
@@ -153,7 +154,7 @@ class Monster {
// Reached last known position, clear it and wander
this.lastKnownPlayerX = null;
this.lastKnownPlayerY = null;
this._wander(dungeon, isPassable);
this._wander(dungeon, isPassable, rng);
} else {
// Path toward last known position
const path = this.findPath(this.lastKnownPlayerX, this.lastKnownPlayerY, dungeon, isPassable);
@@ -165,19 +166,19 @@ class Monster {
// Can't reach last known position, clear and wander
this.lastKnownPlayerX = null;
this.lastKnownPlayerY = null;
this._wander(dungeon, isPassable);
this._wander(dungeon, isPassable, rng);
}
}
} else {
// No player info - wander randomly
this._wander(dungeon, isPassable);
this._wander(dungeon, isPassable, rng);
}
}
/**
* Simple direct movement toward a target (fallback)
*/
_moveDirectToward(targetX, targetY, dungeon, isPassable) {
_moveDirectToward(targetX, targetY, dungeon, isPassable, rng = Math.random) {
const dx = Math.sign(targetX - this.x);
const dy = Math.sign(targetY - this.y);
@@ -212,15 +213,16 @@ class Monster {
}
// Blocked in preferred directions, try diagonals (A* backup)
this._wander(dungeon, isPassable);
this._wander(dungeon, isPassable, rng);
}
/**
* Random wandering behavior
*/
_wander(dungeon, isPassable) {
_wander(dungeon, isPassable, rng = Math.random) {
const dirs = [[0,1],[1,0],[0,-1],[-1,0]];
const shuffledDirs = ROT.RNG.shuffle(dirs.slice());
// Deterministic shuffle from the per-game seeded RNG (provably fair).
const shuffledDirs = seededShuffle(rng, dirs.slice());
for (const [dx, dy] of shuffledDirs) {
const nx = this.x + dx;
+18 -5
View File
@@ -79,6 +79,19 @@ function createSeededRNG(seed) {
};
}
/**
* Derive a positive integer from a hex seed, suitable for seeding ROT.RNG.
* 13 hex chars = 52 bits, which stays within Number.MAX_SAFE_INTEGER so the
* value is represented exactly. Always returns >= 1.
* @param {string} seed - The game seed (hex)
* @returns {number} A positive integer derived deterministically from the seed
*/
function seedToInt(seed) {
const hex = String(seed).replace(/[^0-9a-fA-F]/g, '').slice(0, 13) || '1';
const n = parseInt(hex, 16);
return Number.isFinite(n) && n > 0 ? n : 1;
}
/**
* Generate a random integer in range [min, max] using seeded RNG
* @param {function} rng - Seeded RNG function
@@ -158,13 +171,12 @@ function getPostGameReveal(proof, gameResult = {}) {
moves: gameResult.moves ?? 0,
duration: gameResult.duration ?? 0
},
verificationUrl: `/verify/${proof.gameId}`,
verificationUrl: `/verify/${proof.seed}`,
instructions: [
"To verify this game was fair:",
`1. Compute SHA-256 of the seed: ${proof.seed}`,
`2. Verify it matches the commitment: ${proof.commitment}`,
"3. Use the seed to regenerate the dungeon layout",
"4. Confirm the dungeon matches what you played"
`1. Compute SHA-256 of the seed and confirm it equals the commitment: ${proof.commitment}`,
"2. The seed deterministically generates the dungeon layout, treasure, and monster behaviour",
`3. Open ${`/verify/${proof.seed}`} to regenerate the dungeon from the seed and compare its layout fingerprint`
]
};
}
@@ -196,6 +208,7 @@ module.exports = {
hashSeed,
verifySeed,
createSeededRNG,
seedToInt,
seededRandomInt,
seededShuffle,
createGameProof,
+18
View File
@@ -1753,6 +1753,24 @@ app.get('/api/verify', (req, res) => {
}
const result = verifyGame(seed, commitment);
// PROVABLY FAIR (Phase 0.2): when the seed matches the commitment, regenerate the
// dungeon from the seed and return a layout fingerprint so the player can confirm
// the layout is a deterministic function of the committed seed (not crafted after payment).
if (result.valid) {
try {
const DungeonGenerator = require('./game/dungeon');
const dungeon = DungeonGenerator.regenerateFromSeed(seed, process.env.CRYPTO_TYPE || 'WOW');
result.layoutFingerprint = DungeonGenerator.layoutFingerprint(dungeon);
result.dungeonSize = { width: dungeon.map[0].length, height: dungeon.map.length };
result.entrance = dungeon.entrance;
result.exit = dungeon.exit;
result.treasure = dungeon.treasure;
} catch (e) {
// Regeneration is best-effort; the hash check above is the primary verification.
}
}
res.json(result);
});
+38 -3
View File
@@ -16,6 +16,8 @@ class PaymentHandlers {
this.broadcastManager = broadcastManager;
this.sessionManager = sessionManager;
this.mempoolNotified = new Set();
// Track underpaid payments we've already warned about (notify once, keep monitoring for top-up)
this.underpaidNotified = new Set();
this.paymentMonitors = new Map();
// Track pending payment metadata per socket (may reuse existing)
this.socketPaymentMap = new Map(); // socketId -> { address, paymentId, amount, cryptoType, createdAt }
@@ -311,6 +313,34 @@ class PaymentHandlers {
socket.emit('queue_joined', { position: (existingIdx === -1 ? this.queueManager.getQueueLength() : existingIdx + 1), message: 'Payment received! Waiting for next block to start game...', currentBlock: this.debugManager.getCurrentBlockHeight ? this.debugManager.getCurrentBlockHeight() : null, nextBlock: this.debugManager.getCurrentBlockHeight ? this.debugManager.getCurrentBlockHeight() + 1 : null });
}
} else if (status.confirmed) {
// SECURITY (Phase 0.1): A confirmed transaction is NOT sufficient to grant a
// game/credits — we must verify the RECEIVED amount covers what was expected.
// checkPaymentStatus() sets `confirmed` for ANY incoming tx on the subaddress,
// while `complete` means totalReceived >= required. Without this gate a player
// could send a single atomic unit and play (or buy credits) for free.
if (!status.complete) {
// Underpaid. Keep monitoring so a later top-up to the same address can
// complete it; warn the user once to avoid spamming on every 2s poll.
if (!this.underpaidNotified.has(paymentRequest.id)) {
this.underpaidNotified.add(paymentRequest.id);
const required = status.required || 0;
const received = status.amount || 0;
const shortfall = Math.max(0, required - received);
console.warn(`[PaymentHandlers] Underpaid payment ${paymentRequest.id}: received ${received}, required ${required} (shortfall ${shortfall})`);
try {
socket.emit('payment_underpaid', {
paymentId: paymentRequest.id,
received,
required,
shortfall
});
} catch (_) {}
this.broadcastManager.sendStatusUpdate(socket.id, 'warning',
`⚠️ UNDERPAYMENT DETECTED\n\nReceived less than the required amount. Send the remaining balance to the SAME address to complete your payment, or wait for it to expire and try again.`);
}
return;
}
// SECURITY: Use in-memory Set as fast-path, but DB is source of truth
// This prevents double-processing on server restart
if (this.confirmedPayments.has(paymentRequest.id)) {
@@ -331,7 +361,8 @@ class PaymentHandlers {
const creditsResult = await this.gameModeManager.processCreditsPackageConfirmation(
socket.id,
paymentRequest.id,
mapping.package
mapping.package,
Math.round(status.amount || 0)
);
if (creditsResult.success) {
socket.emit('credits_update', { balance: creditsResult.newBalance });
@@ -368,10 +399,11 @@ class PaymentHandlers {
const updateResult = await this.gameModeManager.db.query(`
UPDATE payments
SET status = 'confirmed',
confirmed_at = NOW()
confirmed_at = NOW(),
received_amount = $2
WHERE id = $1 AND status = 'pending'
RETURNING id
`, [paymentRequest.id]);
`, [paymentRequest.id, Math.round(status.amount || 0)]);
wasUpdated = updateResult.rows.length > 0;
if (this.debugManager.CONSOLE_LOGGING) {
console.log(`[PaymentHandlers] Payment ${paymentRequest.id} status update: ${wasUpdated ? 'success' : 'already confirmed'}`);
@@ -464,6 +496,9 @@ class PaymentHandlers {
if (mapping) {
this.walletService.stopPaymentMonitoring(mapping.address);
this.socketPaymentMap.delete(socketId);
if (mapping.paymentId != null) {
this.underpaidNotified.delete(mapping.paymentId);
}
if (this.debugManager.CONSOLE_LOGGING) {
console.log(`🛑 Stopped monitoring for socket ${socketId}`);
}