Skip to content

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(checkout): let allowlisted merchant sites embed the checkout modal

kaustubhk-06901fd342b5ac
diff --git a/.env.example b/.env.example
index 984c428..3ad1b83 100644
--- a/.env.example
+++ b/.env.example
@@ -23,6 +23,9 @@ BASE_CHECKOUT_URL=http://localhost:7000
BASE_ADMIN_URL=http://localhost:6001
BASE_HELP_URL=http://localhost:3000
BASE_DEMO_STORE_URL=http://localhost:5000
+# Merchant sites allowed to show the checkout in the youpi.js modal (iframe), comma separated.
+# Unset = only the portal's own origin may frame it.
+# CHECKOUT_FRAME_ANCESTORS=https://shop.example.com
# --- DATABASE CONFIGURATION ---
# PostgreSQL connection string:
diff --git a/apps/checkout-portal/src/__tests__/checkout-portal.test.js b/apps/checkout-portal/src/__tests__/checkout-portal.test.js
index c5b8d72..fe8a431 100644
--- a/apps/checkout-portal/src/__tests__/checkout-portal.test.js
+++ b/apps/checkout-portal/src/__tests__/checkout-portal.test.js
@@ -2,6 +2,7 @@ import { test, describe, before, after } from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import { startCheckoutServer } from '../index.js';
+import { frameAncestorOrigins } from '../server.js';
import { generateQrMatrix, generateQrSvg } from '../qr.js';
describe('Checkout Portal Integration Tests', () => {
@@ -85,6 +86,25 @@ describe('Checkout Portal Integration Tests', () => {
assert.equal(res.text.includes('available-offers-card'), false);
});
+ test('GET /checkout/:id can be framed only by origins in CHECKOUT_FRAME_ANCESTORS', async () => {
+ const before = await get('/checkout/pay_test_uuid_12345');
+ assert.equal(before.headers['x-frame-options'], 'SAMEORIGIN');
+ assert.ok(before.headers['content-security-policy'].includes("frame-ancestors 'self';"));
+
+ process.env.CHECKOUT_FRAME_ANCESTORS = 'https://shop.example.com, javascript:alert(1) https://x.test/path *';
+ try {
+ const res = await get('/checkout/pay_test_uuid_12345');
+ assert.equal(res.headers['x-frame-options'], undefined);
+ assert.ok(res.headers['content-security-policy'].includes("frame-ancestors 'self' https://shop.example.com;"));
+ // The QR image stays unframeable.
+ const qr = await get('/checkout/pay_test_uuid_12345/qr.svg');
+ assert.equal(qr.headers['x-frame-options'], 'SAMEORIGIN');
+ } finally {
+ delete process.env.CHECKOUT_FRAME_ANCESTORS;
+ }
+ assert.deepEqual(frameAncestorOrigins('https://a.test http://localhost:5000/'), ['https://a.test', 'http://localhost:5000']);
+ });
+
test('GET /checkout/:id/qr.svg returns dynamic SVG image', async () => {
const res = await get('/checkout/pay_test_uuid_12345/qr.svg');
assert.equal(res.status, 200);
diff --git a/apps/checkout-portal/src/server.js b/apps/checkout-portal/src/server.js
index 4fa3e9f..980f52d 100644
--- a/apps/checkout-portal/src/server.js
+++ b/apps/checkout-portal/src/server.js
@@ -16,6 +16,40 @@ const BASE_SECURITY_HEADERS = Object.freeze({
'Content-Security-Policy': "default-src 'self'; img-src 'self' data: https:; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'; object-src 'none'"
});
+/**
+ * Origins allowed to embed the checkout page, from CHECKOUT_FRAME_ANCESTORS
+ * (comma or space separated, e.g. "https://shop.example.com"). youpi.js opens the
+ * checkout in an iframe on the merchant's own site, which `frame-ancestors 'self'`
+ * blocks whenever the store and the portal are on different origins. Anything that
+ * is not a plain http(s) origin is ignored, so the CSP cannot be widened by accident.
+ */
+export function frameAncestorOrigins(raw = process.env.CHECKOUT_FRAME_ANCESTORS) {
+ const origins = [];
+ for (const entry of String(raw || '').split(/[\s,]+/)) {
+ if (!entry) continue;
+ try {
+ const url = new URL(entry);
+ if ((url.protocol === 'https:' || url.protocol === 'http:') && entry.replace(/\/$/, '') === url.origin) {
+ origins.push(url.origin);
+ }
+ } catch {}
+ }
+ return origins;
+}
+
+/** Headers for the embeddable checkout page: BASE_SECURITY_HEADERS plus any allowed embedders. */
+function checkoutPageSecurityHeaders() {
+ const origins = frameAncestorOrigins();
+ if (!origins.length) return BASE_SECURITY_HEADERS;
+ // X-Frame-Options cannot express an allowlist; CSP frame-ancestors takes over.
+ const { 'X-Frame-Options': _xfo, ...rest } = BASE_SECURITY_HEADERS;
+ return {
+ ...rest,
+ 'Content-Security-Policy': BASE_SECURITY_HEADERS['Content-Security-Policy']
+ .replace("frame-ancestors 'self'", `frame-ancestors 'self' ${origins.join(' ')}`)
+ };
+}
+
/**
* Escapes a value for interpolation into HTML text or attribute context.
* Order ids, merchant names and branding fields are attacker-influenced, so they must
@@ -293,7 +327,7 @@ res.writeHead(200, {
'Pragma': 'no-cache',
'Expires': '0',
'Access-Control-Allow-Origin': '*',
- ...BASE_SECURITY_HEADERS
+ ...checkoutPageSecurityHeaders()
});
res.end(rendered);
return;
diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md
index 9f2a942..9f34a4c 100644
--- a/docs/CONFIGURATION.md
+++ b/docs/CONFIGURATION.md
@@ -27,6 +27,7 @@
| `YOUPI_EXPOSE_DEV_OTP` | unset | Set to `true` for local development to get the OTP in the `send-otp` response. Ignored when `NODE_ENV=production` |
| `YOUPI_ALLOW_DEV_TOOLS` | unset | Developer test-mode / simulator endpoints (`/admin/dev/*`, `simulate-observation`) are refused when `NODE_ENV=production` unless this is `true` |
| `GMAIL_REDIRECT_URI` | derived | Gmail OAuth redirect URI. Defaults to `<gmail host from your configured domain>/oauth/callback` |
+| `CHECKOUT_FRAME_ANCESTORS` | unset | Merchant origins (comma or space separated, e.g. `https://shop.example.com`) allowed to embed `/checkout/:id`. The `youpi.js` modal is an iframe, so a store on a different origin than the checkout portal must be listed here or the browser blocks the modal |
| `ANDROID_HEARTBEAT_TIMEOUT_MS` | `180000` | Inactivity window before Android device is marked DISCONNECTED |
---