Repository navigation
fix(refunds): verify refund PIN asynchronously outside write transaction (#1227) - #1228
Merged
Merged
Conversation
Contributor
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
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 |
# Conflicts: # package.json
# Conflicts: # package.json
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1227 (Item 14 from the remediation backlog).
Problem
In
main/routes/refunds.tsandmain/services/refund.ts, manager/owner PIN verification for late refunds (>1 hour within the same business day) was executed synchronously viabcrypt.compareSyncinsidewithTxn.bcrypt.compareSyncwith 10 salt rounds blocks the Node.js event loop for ~70ms per attempt. During this time, the entire POS main process was unresponsive to incoming network requests, WebSocket heartbeats, and UI IPC events.withTxn), SQLite's single-writer lock was held across the entire 70ms cryptographic computation, stalling concurrent writes (order creation, payments, kitchen status updates).Solution
checkIsLateRefundandvalidateRefundPreconditionsinmain/services/refund.tsso business precondition checks (bill existence, completed order, business day eligibility, split allocation, refundable balance) run before PIN evaluation.main/routes/refunds.ts, idempotent replays are checked outsidewithTxnfirst.verifyPinAsyncoutside the database write transaction, yielding the event loop between candidate checks.createRefundaspreVerifiedApprover.withTxn,createRefundauthoritatively verifies the candidate approver is still active (is_active === 1), holds an eligible role (managerorowner), and has matchingpin_hash === preVerified.verifiedPinHash. If state changed concurrently, it fails closed with HTTP 403.withRefundPinLockmutex to prevent parallel bcrypt bursts from exhausting rate limits.tests/async-refund-pin-event-loop.test.tswith 19 tests verifying event loop ticks during verification, concurrent WAL database writes while bcrypt executes, transaction rollback on stale approver state, rate-limiting, and idempotency replay.test:refundsandpackage.json; script coverage passes with 0 unreachables.Verification
npm run test:async-refund-pin-event-loop: 19/19 passednpm run test:refunds: 81/81 passednpm run test:refund-completed-orders: 18/18 passednpm run test:script-coverage: passednpm run lint: passednpm run build: passednode scripts/ci/check-changed-format.cjs --base origin/main: passed