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): isolate manual UPI and merchant API auth
surenuxui21aa97cc5ea8
diff --git a/apps/payment-api/src/auth-guard.js b/apps/payment-api/src/auth-guard.jsindex 566cc07..344b4ed 100644--- a/apps/payment-api/src/auth-guard.js+++ b/apps/payment-api/src/auth-guard.js@@ -98,6 +98,5 @@ export function isPaymentOwner(req, session, ctx) {if (typeof ctx.merchantRepo?.verifyApiKey === 'function') {return ctx.merchantRepo.verifyApiKey(merchant, presented.trim());}- if (!merchant.webhookSecret) return false;- return safeEqual(presented.trim(), merchant.webhookSecret);+ return false;}diff --git a/apps/payment-api/src/routes/admin.routes.js b/apps/payment-api/src/routes/admin.routes.jsindex 3eba941..0f6f8ac 100644--- a/apps/payment-api/src/routes/admin.routes.js+++ b/apps/payment-api/src/routes/admin.routes.js@@ -282,6 +282,11 @@ export function registerAdminRoutes(server, ctx) {return;}+ if (session.method === PaymentMethod.MANUAL_UPI) {+ HttpServer.json(res, 409, { error: 'Manual UPI claims must be approved in the merchant order system after checking the receiving bank account.' });+ return;+ }+if ([PaymentStatus.SUCCESS, PaymentStatus.RECONCILED, PaymentStatus.FAILED, PaymentStatus.EXPIRED, PaymentStatus.CANCELLED].includes(session.status)) {HttpServer.json(res, 409, { error: `Cannot confirm a payment in terminal status '${session.status}'.` });return;diff --git a/packages/db/src/__tests__/db.test.js b/packages/db/src/__tests__/db.test.jsindex 689b56b..680a423 100644--- a/packages/db/src/__tests__/db.test.js+++ b/packages/db/src/__tests__/db.test.js@@ -28,6 +28,17 @@ describe('@youpi/db Unit & Integration Tests', () => {let testMerchant;let testAccount;+ test('merchant API authentication accepts only the dedicated API key hash', () => {+ const merchant = merchantRepo.create({+ businessName: 'Key isolation fixture', slug: 'key-isolation-fixture', contactEmail: '[email protected]', contactPhone: '9000000000',+ apiKey: 'mk_live_dedicated_api_credential_123456', webhookSecret: 'independent_webhook_hmac_secret_123456',+ branding: { merchantApiKey: 'legacy_branding_key_123456' }+ });+ assert.equal(merchantRepo.verifyApiKey(merchant, 'mk_live_dedicated_api_credential_123456'), true);+ assert.equal(merchantRepo.verifyApiKey(merchant, 'independent_webhook_hmac_secret_123456'), false);+ assert.equal(merchantRepo.verifyApiKey(merchant, 'legacy_branding_key_123456'), false);+ });+beforeEach(() => {db = new Database({ inMemory: true });const { defaultMerchant } = runMigrations(db);diff --git a/packages/db/src/repositories/merchant.repository.js b/packages/db/src/repositories/merchant.repository.jsindex b37426a..bfcd98a 100644--- a/packages/db/src/repositories/merchant.repository.js+++ b/packages/db/src/repositories/merchant.repository.js@@ -168,8 +168,7 @@ export class MerchantRepository {}/**- * Verifies a candidate merchant API key against the merchant's stored SHA-256 apiKeyHash- * (or webhookSecret for backward compatibility) in constant time.+ * Verifies a candidate merchant API key against the stored SHA-256 apiKeyHash.*/verifyApiKey(merchant, candidateKey) {if (!merchant || !candidateKey || typeof candidateKey !== 'string') return false;@@ -178,12 +177,6 @@ export class MerchantRepository {if (merchant.apiKeyHash && safeEqual(hashApiKey(clean), merchant.apiKeyHash)) {return true;}- if (merchant.branding?.merchantApiKey && safeEqual(clean, merchant.branding.merchantApiKey)) {- return true;- }- if (merchant.webhookSecret && safeEqual(clean, merchant.webhookSecret)) {- return true;- }return false;}diff --git a/packages/merchant-sdk/src/__tests__/merchant-sdk.test.js b/packages/merchant-sdk/src/__tests__/merchant-sdk.test.jsindex 18c6809..d80a7dd 100644--- a/packages/merchant-sdk/src/__tests__/merchant-sdk.test.js+++ b/packages/merchant-sdk/src/__tests__/merchant-sdk.test.js@@ -1,6 +1,6 @@import { test, describe, before, after } from 'node:test';import assert from 'node:assert/strict';-import { Database } from '@youpi/db';+import { Database, MerchantRepository } from '@youpi/db';import { startServer as startPaymentApi } from '../../../../apps/payment-api/src/index.js';import { YoupiPaymentClient } from '../index.js';import { generateHmac } from '@youpi/common';@@ -10,6 +10,7 @@ describe('Merchant SDK Integration Tests', () => {let client;let testDb;const merchantSecret = 'sdk_test_webhook_secret_9988';+ const merchantApiKey = 'mk_live_sdk_api_key_not_the_webhook_secret_9988';before(async () => {process.env.YOUPI_ALLOWED_RETURN_ORIGINS = 'https://store.example';@@ -21,10 +22,12 @@ describe('Merchant SDK Integration Tests', () => {merchantSecret,'00000000-0000-0000-0000-000000000001']);+ new MerchantRepository(testDb).update('00000000-0000-0000-0000-000000000001', { apiKey: merchantApiKey });client = new YoupiPaymentClient({apiUrl: `http://localhost:${apiInstance.port}`,merchantId: '00000000-0000-0000-0000-000000000001',+ apiKey: merchantApiKey,webhookSecret: merchantSecret});});