Upstream/server/commands/recallMovement.js
jvanvik d84b0c9d12 Fix review findings: proto smuggling, trade-leg recall, escrow cap, static headers, rate limits, rankings DoS
Fixed: validator rejects __proto__/constructor keys at any depth (transport capacity bypass); trade and site shipments
cannot be recalled; trade escrow refund capped by Rack hall; CSP/nosniff on static files and connect-src 'self';
IPv6 /64 limiter keys + LRU eviction + per-username login bucket + scrypt concurrency cap; 5 s footprint cache for
rankings; rename uses text.cleanText; 429s no longer pinned in the idempotency store; 30-day notification prune;
units_offline notification on first offline. Accepted items listed in docs/DECISIONS.md R1.
Tested: npm test 182/182 incl. test/review-fixes.test.js (regression tests). Not re-reviewed after the fixes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-11 19:06:51 +02:00

32 lines
2.5 KiB
JavaScript

'use strict';
/* POST /movements/:id/recall. Within the first 90 s (MILITARY.recallWindowMs) an outbound movement can turn around:
* its arrive event is cancelled and a return leg takes the same units and cargo home in as long as they were out. */
const D = require('../../public/js/data.js');
const { GameError } = require('../errors');
const ctxOf = require('../military/ctx');
const U = require('../military/units');
const M = require('../military/movements');
module.exports = function recallMovement(ctx0, { userId, movementId }) {
const ctx = ctxOf(ctx0);
const { db, now } = ctx;
const mv = M.getMovement(db, movementId);
const op = mv && db.get('SELECT id FROM operators WHERE id = ? AND user_id = ?', mv.ownerId, userId);
if (!mv || !op) throw new GameError('not_found', { what: 'movement' });
if (mv.status !== 'out' || mv.type === 'return') throw new GameError('conflict', { what: 'movement cannot be recalled', status: mv.status, type: mv.type });
// Trade legs and backbone shipments are commitments: recalling one would let a side keep both halves of the deal.
if (mv.extra && (mv.extra.tradeId || mv.extra.siteId)) throw new GameError('conflict', { what: 'this shipment cannot be recalled' });
if (now - mv.departAt > D.MILITARY.recallWindowMs) throw new GameError('recall_expired', { departAt: mv.departAt, windowMs: D.MILITARY.recallWindowMs });
const origin = mv.fromTownId ? db.get('SELECT * FROM towns WHERE id = ?', mv.fromTownId) : null;
const dest = U.homeTownFor(db, mv.ownerId, origin && origin.owner_id === mv.ownerId ? origin.id : null);
if (!dest) throw new GameError('conflict', { what: 'no POP to return to' });
db.run("UPDATE events SET status = 'cancelled', done_at = ? WHERE id = ? AND status = 'pending'", now, mv.eventId);
db.run("UPDATE movements SET status = 'recalled' WHERE id = ?", mv.id);
const elapsed = Math.max(1000, now - mv.departAt);
const f = Math.min(1, (now - mv.departAt) / Math.max(1, mv.arriveAt - mv.departAt));
const at = { x: Math.round(mv.from.x + (mv.to.x - mv.from.x) * f), y: Math.round(mv.from.y + (mv.to.y - mv.from.y) * f) };
const extra = Object.assign({}, mv.extra, { recalled: true });
if (mv.type === 'transport') extra.link = 1;
const ret = M.launchReturn(ctx, { worldId: mv.worldId, ownerId: mv.ownerId, fromTownId: null, at, to: dest, units: mv.units, cargo: mv.cargo, arriveAt: now + elapsed, parentId: mv.id, extra });
return { recalled: mv.id, returnMovementId: ret.id, arriveAt: ret.arriveAt };
};