A self-hosted UPI & COD payment gateway with zero per-transaction platform fees, universal JavaScript checkout integration, dynamic QR payments, WhatsApp OTP authentication, and automated payment verification.
JavaScript
fix(security): repair PR #35 merge defects (ReDoS + dropped legitimate credits)
rishabh4b6377424360
diff --git a/packages/core/src/__tests__/core.test.js b/packages/core/src/__tests__/core.test.jsindex 3176c1a..7a71af8 100644--- a/packages/core/src/__tests__/core.test.js+++ b/packages/core/src/__tests__/core.test.js@@ -8,6 +8,7 @@ import {AttributionEngine,BankSmsParser} from '../index.js';+import { extractIntelligentAmount } from '../bank-sms-parser.js';import {PaymentStatus,PublicPaymentOutcome,@@ -354,6 +355,17 @@ describe('Bank SMS Parser UTR & Amount Extraction', () => {assert.equal(parsed, null);});+ test('amount extraction is not vulnerable to quadratic backtracking (ReDoS)', () => {+ // The credit patterns chain several whitespace-matching quantifiers. A body such as+ // "credited" followed by a long whitespace run used to take seconds to scan and blocked+ // the payment event loop, so the text is whitespace-collapsed before matching.+ const hostile = 'credited' + ' '.repeat(20000);+ const started = Date.now();+ extractIntelligentAmount(hostile);+ const elapsed = Date.now() - started;+ assert.ok(elapsed < 500, `amount extraction took ${elapsed}ms on a whitespace flood`);+ });+test('rejects bare payment or transfer debit message without credit indicator', () => {const sms = {text: 'Payment of Rs 350.00 sent to merchant@upi on 19-Oct. UPI Ref 998877665544.'diff --git a/packages/core/src/bank-email-parser.js b/packages/core/src/bank-email-parser.jsindex d5d0b97..b376c8b 100644--- a/packages/core/src/bank-email-parser.js+++ b/packages/core/src/bank-email-parser.js@@ -30,22 +30,27 @@ export class BankEmailParser {const combinedText = `${email.subject || ''} ${email.body || ''}`.trim();if (!combinedText) return null;- // Check if email represents an incoming credit/payment- const creditKeywords = /\b(credited|credit|received|deposit|deposited|payment received|upi\/cr)\b/i;+ // Check if email represents an incoming credit/payment. A bare "payment" or "transferred"+ // without credit context is rejected, but "transferred to your account" and "payment of+ // Rs 500" are genuine credits and must still be recognised.+ const creditKeywords = new RegExp(+ '\\b(?:credited|credit|received|deposit(?:ed)?|payment\\s+received|upi\\/cr' ++ '|transferred\\s+to\\s+(?:your\\s+)?(?:a\\/c|account|wallet)' ++ '|payment\\s+of\\s+(?:(?:rs\\b\\.?|inr\\b)\\s*)?[\\d,.]+\\s+(?:has\\s+been\\s+)?made\\s+to\\s+(?:your\\s+)?(?:a\\/c|account|wallet)' ++ '|added\\s+to\\s+(?:your\\s+)?(?:account|wallet))\\b',+ 'i'+ );if (!creditKeywords.test(combinedText)) {return null; // Ignore non-credit alerts}- // Reject debit notifications:+ // Reject debit notifications. A debit that appears before any credit means the message is+ // about money leaving the account; a debit mentioned after a credit (e.g. "Rs 500 credited.+ // Rs 10 debited for charges") must not cancel the credit.const debitIdx = combinedText.search(/\b(?:debited|withdrawn|deducted|spent|paid rs|sent rs)\b/i);const creditIdx = combinedText.search(/\b(?:credited|credit|received|deposit|deposited)\b/i);- if (debitIdx !== -1) {- if (creditIdx === -1 || debitIdx < creditIdx) {- return null;- }- if (/\b(?:debited\s*(?:from|by|for)|is\s+debited|has\s+been\s+debited)\b/i.test(combinedText)) {- return null;- }+ if (debitIdx !== -1 && (creditIdx === -1 || debitIdx < creditIdx)) {+ return null;}// 1. Extract Monetary Amount using Intelligent Multi-Pattern Extractordiff --git a/packages/core/src/bank-sms-parser.js b/packages/core/src/bank-sms-parser.jsindex a8a49d7..94852c6 100644--- a/packages/core/src/bank-sms-parser.js+++ b/packages/core/src/bank-sms-parser.js@@ -12,10 +12,17 @@ import { toPaise } from '@youpi/common';export function extractIntelligentAmount(text) {if (!text || typeof text !== 'string') return null;+ // Collapse runs of whitespace before matching. The amount patterns below chain several+ // whitespace-matching quantifiers (`\s*` ... `\s*` ... `[\s:=-]*`), which is polynomial+ // backtracking over a whitespace run: a body of "credited" followed by ~250 spaces took+ // seconds to scan and blocked the payment event loop. With whitespace collapsed to single+ // spaces the scan is linear and the cost is bounded by the length of the text.+ const normalizedText = text.replace(/\s+/g, ' ');+// 0. Remove balance mentions (e.g. "Avl Bal Rs 10,000", "Bal: INR 5000", "Available limit Rs. 20,000")// to avoid extracting account balance instead of credit amount.const balancePattern = /(?:(?:avl\b|avail\b|available\b|tot\b|total\b|ledger\b|clear\b|curr\b|current\b)\s*(?:bal\b|balance\b|limit\b)?|bal\b|balance\b)[\s:=-]*(?:(?:rs\b\.?|inr\b)|₹)?[\s:=-]*[\d,]+(?:\.\d{1,2})?/gi;- const sanitizedText = text.replace(balancePattern, ' ').trim();+ const sanitizedText = normalizedText.replace(balancePattern, ' ').trim();// Pattern 0: Direct credit proximity phrases (e.g. "credited with Rs. 500", "received ₹200.00", "Rs 499.00 credited")const directCreditMatch = sanitizedText.match(/(?:credited\s*(?:with|by|for)?|received|deposit(?:ed)?)\s*[:=-]?\s*(?:(?:rs\b\.?|inr\b)|₹)?[\s:=-]*([\d,]+(?:\.\d{1,2})?)/i) ||@@ -100,24 +107,29 @@ export class BankSmsParser {const text = (sms.text || '').trim().slice(0, MAX_SCAN_CHARS);if (!text) return null;- // Check if SMS represents an incoming credit. Bare "payment" or "transferred" without credit context is rejected.- const creditKeywords = /\b(credited|received|deposit|deposited|payment received|money received|added to (?:your )?(?:account|wallet)|upi\/cr)\b/i;+ // Check if SMS represents an incoming credit. A bare "payment" or "transferred" without+ // credit context is rejected, but "transferred to your account" and "payment of Rs 500"+ // are genuine credits and must still be recognised.+ const creditKeywords = new RegExp(+ '\\b(?:credited|received|deposit(?:ed)?|payment\\s+received|money\\s+received|upi\\/cr' ++ '|transferred\\s+to\\s+(?:your\\s+)?(?:a\\/c|account|wallet)' ++ '|payment\\s+of\\s+(?:(?:rs\\b\\.?|inr\\b)\\s*)?[\\d,.]+\\s+(?:has\\s+been\\s+)?made\\s+to\\s+(?:your\\s+)?(?:a\\/c|account|wallet)' ++ '|added\\s+to\\s+(?:your\\s+)?(?:account|wallet))\\b',+ 'i'+ );if (!creditKeywords.test(text)) {return null;}// Reject debit notifications:- // If debit indicators exist and appear before credit indicators (e.g. "debited ... credited back in 48h")- // or if the account itself is being debited, reject immediately.+ // A debit indicator that appears before any credit indicator means the message is about+ // money leaving the account (e.g. "debited ... credited back in 48h"), so it is refused.+ // A debit mentioned *after* a credit (e.g. "Rs 500 credited. Rs 10 debited for SMS+ // charges") must not cancel the credit, so ordering is what decides.const debitIdx = text.search(/\b(?:debited|withdrawn|deducted|spent|paid rs|sent rs)\b/i);const creditIdx = text.search(/\b(?:credited|received|deposit|deposited)\b/i);- if (debitIdx !== -1) {- if (creditIdx === -1 || debitIdx < creditIdx) {- return null;- }- if (/\b(?:debited\s*(?:from|by|for)|is\s+debited|has\s+been\s+debited)\b/i.test(text)) {- return null;- }+ if (debitIdx !== -1 && (creditIdx === -1 || debitIdx < creditIdx)) {+ return null;}// 1. Extract Amount using Intelligent Multi-Pattern Extractor