fix(sales): route TrackShip delivery poll through status-transition path (closes audit-C2)#193
Conversation
…on path Audit finding C2 (onetwo3d-ims-erl0): checkDeliveryStatus marked orders DELIVERED via a raw db.salesOrder.update — no transition guard, no side effects. A delivery detected by the cron skipped everything the manual DELIVERED path does (status_changed activity log, WooCommerce status push, cache revalidation), and could overwrite an order that had moved to CANCELLED/REFUNDED between the SHIPPED query and the write. Now routed through applySalesOrderStatusTransition with the internal bypass token (cron has no session, so the permission check is skipped but the guard + side effects run). The state machine rejects DELIVERED from a non-deliverable status, so a since-cancelled order is no longer silently overwritten — the cron logs delivery_status_skipped and moves on. Extracted the delivery-marking into markOrderDelivered with injected transition + log deps so the routing and skip-on-reject behaviour are unit-tested without the DB or external API (2 tests). workflows.md documents that cron-driven DELIVERED runs the same guard + side effects as manual. Closes onetwo3d-ims-erl0.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
…state guard (review hardening)
Codex adversarial pass found the original C2 fix did NOT actually
protect against the stale-state race it claimed to. The internal bypass
token makes validateManualSalesOrderStatusTransition short-circuit
{ success: true }, skipping BOTH the permission check AND the
state-machine guard — so a SHIPPED→CANCELLED race between the poll's
query and the write would still overwrite CANCELLED with DELIVERED, and
the docs claim was false.
Root fix: added a narrower skipPermissionCheck option to
applySalesOrderStatusTransition that skips ONLY the permission check;
the state-machine guard (pre-lock AND under-lock via
updateSalesOrderStatusUnderLock) still runs. The WooCommerce status-sync
path keeps the bypass token (skip-both) unchanged — it legitimately
forces mapped statuses and already handles rejection. TrackShip now uses
skipPermissionCheck, so a since-cancelled/refunded order is genuinely
rejected under the lock and logged as delivery_status_skipped.
Also from the review:
- Hoisted the applySalesOrderStatusTransition dynamic import above the
per-order loop (was re-evaluated each delivered order).
- Wrapped the per-order mark in try/catch so one order's failure (e.g.
a transient log write) no longer aborts the whole cron batch.
- Dropped the cast in the test; deps.transition type now
matches the real signature (extra/options optional), so a future
signature drift is caught.
Docs corrected: the cron skips permission only, not the state guard.
2/2 helper tests, 93/93 sales-domain tests.
Codex adversarial review — findings + fixes (commit 39d9c9b)Codex was at its usage cap, so the adversarial pass ran by hand with equal rigour. It found that the original commit did not actually deliver the protection it claimed — a HIGH that I'd otherwise have shipped. HIGH — the guard was being bypassed, not enforced. The internal bypass token makes Fix: added a narrower MEDIUM — LOW — dynamic import inside the loop (fixed). Hoisted LOW — one order's failure aborted the batch (fixed). Wrapped the per-order mark in try/catch so a transient failure (e.g. a log write) no longer drops the remaining orders in the run. Verified-clean angles (no action needed)
Validation after hardening
|
Summary
Closes
onetwo3d-ims-erl0(audit finding C2, Wave 1).The gap:
lib/trackship.ts:checkDeliveryStatusmarked ordersDELIVEREDwith a rawdb.salesOrder.update— no transition guard, no side effects. A cron-detected delivery skipped everything the manual DELIVERED path runs (thestatus_changedactivity log, the WooCommerce status push, cache revalidation), and could silently overwrite an order that had moved to CANCELLED/REFUNDED between the SHIPPED query and the write. Verified directly during the audit.Fix
Delivery marking now routes through
applySalesOrderStatusTransition(id, 'DELIVERED', undefined, { internalBypassToken }):sales.processpermission check — but the transition guard and all side effects still run.DELIVEREDfromSHIPPED/COMPLETED; an order that became CANCELLED/REFUNDED in the meantime is rejected, and the cron logsdelivery_status_skipped(WARNING) instead of forcing the write.Extracted the marking into
markOrderDelivered(target, deps)with injectedtransition+logso the routing and skip-on-reject behaviour are unit-testable without the DB or external API.Validation
npx tsx --test tests/trackship-mark-delivered.test.ts— 2/2 pass (routes through transition with the bypass token; skips + warns on rejection)npm run type-check— cleannpm run lint— cleanNotes
docs/workflows.mdnow documents that cron-driven DELIVERED runs the same guard + side effects as a manual delivery.deliveredcount returned bycheckDeliveryStatusnow reflects only orders that actually transitioned (rejected ones are not counted).🤖 Generated with Claude Code