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): trust checkout postMessage events only from the checkout iframe
Abhi Pareekc9859025385f
diff --git a/apps/checkout-portal/src/sdk/youpi-checkout.js b/apps/checkout-portal/src/sdk/youpi-checkout.jsindex 6619dfb..2641b71 100644--- a/apps/checkout-portal/src/sdk/youpi-checkout.js+++ b/apps/checkout-portal/src/sdk/youpi-checkout.js@@ -327,7 +327,9 @@closeBtn.onclick = () => {try {if (iframe && iframe.contentWindow) {- iframe.contentWindow.postMessage({ event: 'youpi.request_cancel' }, '*');+ if (this._iframeOrigin) {+ iframe.contentWindow.postMessage({ event: 'youpi.request_cancel' }, this._iframeOrigin);+ }return;}} catch (err) { }@@ -338,6 +340,9 @@const iframe = document.createElement('iframe');iframe.className = 'youpi-checkout-iframe';iframe.src = targetUrl;+ // Only messages from this exact iframe/origin are trusted (see _handleMessage).+ this._iframe = iframe;+ try { this._iframeOrigin = new URL(targetUrl, window.location.href).origin; } catch { this._iframeOrigin = null; }iframe.allow = 'payment';container.appendChild(closeBtn);@@ -374,7 +379,19 @@backdrop.addEventListener('mouseup', stopOutsideDismiss, true);}+ _isSafeRedirect(url) {+ try {+ const u = new URL(url, window.location.href);+ return u.protocol === 'http:' || u.protocol === 'https:';+ } catch {+ return false;+ }+ }+_handleMessage(e) {+ // Ignore messages from any window other than our checkout iframe (spoofed success events).+ if (!this._iframe || e.source !== this._iframe.contentWindow) return;+ if (!this._iframeOrigin || e.origin !== this._iframeOrigin) return;if (!e.data || typeof e.data !== 'object') return;const event = e.data.event;@@ -439,7 +456,7 @@if (this.options.failedUrl) {const delim = this.options.failedUrl.includes('?') ? '&' : '?';window.location.href = `${this.options.failedUrl}${delim}order_id=${encodeURIComponent(result.order_id)}&reason=${encodeURIComponent(result.reason)}`;- } else if (payload.redirectUrl && payload.redirectUrl !== window.location.href && !payload.redirectUrl.includes('/checkout')) {+ } else if (payload.redirectUrl && this._isSafeRedirect(payload.redirectUrl) && payload.redirectUrl !== window.location.href && !payload.redirectUrl.includes('/checkout')) {window.location.href = payload.redirectUrl;}});@@ -498,7 +515,7 @@if (this.options.cancelledUrl) {const delim = this.options.cancelledUrl.includes('?') ? '&' : '?';window.location.href = `${this.options.cancelledUrl}${delim}order_id=${encodeURIComponent(this.options.orderId || '')}`;- } else if (redirectUrl && redirectUrl !== window.location.href && !redirectUrl.includes('/checkout')) {+ } else if (redirectUrl && this._isSafeRedirect(redirectUrl) && redirectUrl !== window.location.href && !redirectUrl.includes('/checkout')) {window.location.href = redirectUrl;}});diff --git a/apps/checkout-portal/src/sdk/youpi.js b/apps/checkout-portal/src/sdk/youpi.jsindex 0487bd1..9fbafb7 100644--- a/apps/checkout-portal/src/sdk/youpi.js+++ b/apps/checkout-portal/src/sdk/youpi.js@@ -327,7 +327,9 @@closeBtn.onclick = () => {try {if (iframe && iframe.contentWindow) {- iframe.contentWindow.postMessage({ event: 'youpi.request_cancel' }, '*');+ if (this._iframeOrigin) {+ iframe.contentWindow.postMessage({ event: 'youpi.request_cancel' }, this._iframeOrigin);+ }return;}} catch (err) { }@@ -338,6 +340,9 @@const iframe = document.createElement('iframe');iframe.className = 'youpi-checkout-iframe';iframe.src = targetUrl;+ // Only messages from this exact iframe/origin are trusted (see _handleMessage).+ this._iframe = iframe;+ try { this._iframeOrigin = new URL(targetUrl, window.location.href).origin; } catch { this._iframeOrigin = null; }iframe.allow = 'payment';container.appendChild(closeBtn);@@ -372,7 +377,19 @@backdrop.addEventListener('mouseup', stopOutsideDismiss, true);}+ _isSafeRedirect(url) {+ try {+ const u = new URL(url, window.location.href);+ return u.protocol === 'http:' || u.protocol === 'https:';+ } catch {+ return false;+ }+ }+_handleMessage(e) {+ // Ignore messages from any window other than our checkout iframe (spoofed success events).+ if (!this._iframe || e.source !== this._iframe.contentWindow) return;+ if (!this._iframeOrigin || e.origin !== this._iframeOrigin) return;if (!e.data || typeof e.data !== 'object') return;const event = e.data.event;@@ -437,7 +454,7 @@if (this.options.failedUrl) {const delim = this.options.failedUrl.includes('?') ? '&' : '?';window.location.href = `${this.options.failedUrl}${delim}order_id=${encodeURIComponent(result.order_id)}&reason=${encodeURIComponent(result.reason)}`;- } else if (payload.redirectUrl && payload.redirectUrl !== window.location.href && !payload.redirectUrl.includes('/checkout')) {+ } else if (payload.redirectUrl && this._isSafeRedirect(payload.redirectUrl) && payload.redirectUrl !== window.location.href && !payload.redirectUrl.includes('/checkout')) {window.location.href = payload.redirectUrl;}});@@ -496,7 +513,7 @@if (this.options.cancelledUrl) {const delim = this.options.cancelledUrl.includes('?') ? '&' : '?';window.location.href = `${this.options.cancelledUrl}${delim}order_id=${encodeURIComponent(this.options.orderId || '')}`;- } else if (redirectUrl && redirectUrl !== window.location.href && !redirectUrl.includes('/checkout')) {+ } else if (redirectUrl && this._isSafeRedirect(redirectUrl) && redirectUrl !== window.location.href && !redirectUrl.includes('/checkout')) {window.location.href = redirectUrl;}});diff --git a/packages/merchant-sdk/src/__tests__/youpi-universal-integration.test.js b/packages/merchant-sdk/src/__tests__/youpi-universal-integration.test.jsindex 5fb234b..034f9e3 100644--- a/packages/merchant-sdk/src/__tests__/youpi-universal-integration.test.js+++ b/packages/merchant-sdk/src/__tests__/youpi-universal-integration.test.js@@ -73,4 +73,52 @@ describe('Universal YOUPI PG One-Script Integration Tests', () => {assert.equal(metadata.plan, 'enterprise');assert.equal(metadata.seats, 5);});++ test('accepts payment events from the checkout iframe at its own origin', () => {+ globalThis.window = { location: { href: 'https://merchant.test/shop' }, dispatchEvent: () => true };+ try {+ const results = [];+ const checkoutWindow = {};+ const instance = new YoupiCheckout({ apiUrl: 'http://localhost:1', orderId: 'O-2', amount: '10.00', handler: r => results.push(r) });+ instance._iframe = { contentWindow: checkoutWindow };+ instance._iframeOrigin = 'https://pay.example.com';+ instance.close = () => {};++ instance._handleMessage({+ source: checkoutWindow,+ origin: 'https://pay.example.com',+ data: { event: 'youpi.payment_success', data: { orderId: 'O-2', amount: '10.00', utr: '123456789012' } }+ });++ assert.equal(results.length, 1);+ assert.equal(results[0].status, 'SUCCESS');+ assert.equal(results[0].order_id, 'O-2');+ } finally {+ delete globalThis.window;+ }+ });++ test('ignores payment events that do not come from the checkout iframe and origin', () => {+ globalThis.window = { location: { href: 'https://merchant.test/shop' } };+ try {+ let handled = 0;+ const checkoutWindow = {};+ const instance = new YoupiCheckout({ apiUrl: 'http://localhost:1', orderId: 'O-1', amount: '10.00', handler: () => { handled++; } });+ instance._iframe = { contentWindow: checkoutWindow };+ instance._iframeOrigin = 'https://pay.example.com';+ instance.close = () => {};+ const forged = { event: 'youpi.payment_success', data: { order_id: 'O-1', amount_received: '10.00', utr: '123456789012' } };++ instance._handleMessage({ source: {}, origin: 'https://pay.example.com', data: forged });+ instance._handleMessage({ source: checkoutWindow, origin: 'https://evil.example', data: forged });+ assert.equal(handled, 0);++ assert.equal(instance._isSafeRedirect('javascript:alert(1)'), false);+ assert.equal(instance._isSafeRedirect('data:text/html,x'), false);+ assert.equal(instance._isSafeRedirect('https://merchant.test/thanks'), true);+ assert.equal(instance._isSafeRedirect('/thanks'), true);+ } finally {+ delete globalThis.window;+ }+ });});diff --git a/packages/merchant-sdk/src/youpi-checkout.js b/packages/merchant-sdk/src/youpi-checkout.jsindex 960a780..030b79b 100644--- a/packages/merchant-sdk/src/youpi-checkout.js+++ b/packages/merchant-sdk/src/youpi-checkout.js@@ -317,7 +317,9 @@closeBtn.onclick = () => {try {if (iframe && iframe.contentWindow) {- iframe.contentWindow.postMessage({ event: 'youpi.request_cancel' }, '*');+ if (this._iframeOrigin) {+ iframe.contentWindow.postMessage({ event: 'youpi.request_cancel' }, this._iframeOrigin);+ }return;}} catch (err) { }@@ -328,6 +330,9 @@const iframe = document.createElement('iframe');iframe.className = 'youpi-checkout-iframe';iframe.src = targetUrl;+ // Only messages from this exact iframe/origin are trusted (see _handleMessage).+ this._iframe = iframe;+ try { this._iframeOrigin = new URL(targetUrl, window.location.href).origin; } catch { this._iframeOrigin = null; }iframe.allow = 'payment';container.appendChild(closeBtn);@@ -364,7 +369,19 @@backdrop.addEventListener('mouseup', stopOutsideDismiss, true);}+ _isSafeRedirect(url) {+ try {+ const u = new URL(url, window.location.href);+ return u.protocol === 'http:' || u.protocol === 'https:';+ } catch {+ return false;+ }+ }+_handleMessage(e) {+ // Ignore messages from any window other than our checkout iframe (spoofed success events).+ if (!this._iframe || e.source !== this._iframe.contentWindow) return;+ if (!this._iframeOrigin || e.origin !== this._iframeOrigin) return;if (!e.data || typeof e.data !== 'object') return;const event = e.data.event;@@ -429,7 +446,7 @@if (this.options.failedUrl) {const delim = this.options.failedUrl.includes('?') ? '&' : '?';window.location.href = `${this.options.failedUrl}${delim}order_id=${encodeURIComponent(result.order_id)}&reason=${encodeURIComponent(result.reason)}`;- } else if (payload.redirectUrl && payload.redirectUrl !== window.location.href && !payload.redirectUrl.includes('/checkout')) {+ } else if (payload.redirectUrl && this._isSafeRedirect(payload.redirectUrl) && payload.redirectUrl !== window.location.href && !payload.redirectUrl.includes('/checkout')) {window.location.href = payload.redirectUrl;}});@@ -488,7 +505,7 @@if (this.options.cancelledUrl) {const delim = this.options.cancelledUrl.includes('?') ? '&' : '?';window.location.href = `${this.options.cancelledUrl}${delim}order_id=${encodeURIComponent(this.options.orderId || '')}`;- } else if (redirectUrl && redirectUrl !== window.location.href && !redirectUrl.includes('/checkout')) {+ } else if (redirectUrl && this._isSafeRedirect(redirectUrl) && redirectUrl !== window.location.href && !redirectUrl.includes('/checkout')) {window.location.href = redirectUrl;}});diff --git a/packages/merchant-sdk/src/youpi.js b/packages/merchant-sdk/src/youpi.jsindex 0487bd1..9fbafb7 100644--- a/packages/merchant-sdk/src/youpi.js+++ b/packages/merchant-sdk/src/youpi.js@@ -327,7 +327,9 @@closeBtn.onclick = () => {try {if (iframe && iframe.contentWindow) {- iframe.contentWindow.postMessage({ event: 'youpi.request_cancel' }, '*');+ if (this._iframeOrigin) {+ iframe.contentWindow.postMessage({ event: 'youpi.request_cancel' }, this._iframeOrigin);+ }return;}} catch (err) { }@@ -338,6 +340,9 @@const iframe = document.createElement('iframe');iframe.className = 'youpi-checkout-iframe';iframe.src = targetUrl;+ // Only messages from this exact iframe/origin are trusted (see _handleMessage).+ this._iframe = iframe;+ try { this._iframeOrigin = new URL(targetUrl, window.location.href).origin; } catch { this._iframeOrigin = null; }iframe.allow = 'payment';container.appendChild(closeBtn);@@ -372,7 +377,19 @@backdrop.addEventListener('mouseup', stopOutsideDismiss, true);}+ _isSafeRedirect(url) {+ try {+ const u = new URL(url, window.location.href);+ return u.protocol === 'http:' || u.protocol === 'https:';+ } catch {+ return false;+ }+ }+_handleMessage(e) {+ // Ignore messages from any window other than our checkout iframe (spoofed success events).+ if (!this._iframe || e.source !== this._iframe.contentWindow) return;+ if (!this._iframeOrigin || e.origin !== this._iframeOrigin) return;if (!e.data || typeof e.data !== 'object') return;const event = e.data.event;@@ -437,7 +454,7 @@if (this.options.failedUrl) {const delim = this.options.failedUrl.includes('?') ? '&' : '?';window.location.href = `${this.options.failedUrl}${delim}order_id=${encodeURIComponent(result.order_id)}&reason=${encodeURIComponent(result.reason)}`;- } else if (payload.redirectUrl && payload.redirectUrl !== window.location.href && !payload.redirectUrl.includes('/checkout')) {+ } else if (payload.redirectUrl && this._isSafeRedirect(payload.redirectUrl) && payload.redirectUrl !== window.location.href && !payload.redirectUrl.includes('/checkout')) {window.location.href = payload.redirectUrl;}});@@ -496,7 +513,7 @@if (this.options.cancelledUrl) {const delim = this.options.cancelledUrl.includes('?') ? '&' : '?';window.location.href = `${this.options.cancelledUrl}${delim}order_id=${encodeURIComponent(this.options.orderId || '')}`;- } else if (redirectUrl && redirectUrl !== window.location.href && !redirectUrl.includes('/checkout')) {+ } else if (redirectUrl && this._isSafeRedirect(redirectUrl) && redirectUrl !== window.location.href && !redirectUrl.includes('/checkout')) {window.location.href = redirectUrl;}});