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(security): launch cloudflared without a shell (remote code execution)

Abhi Pareek834f193b4b7e
diff --git a/apps/payment-api/src/services/tunnel-manager.js b/apps/payment-api/src/services/tunnel-manager.js
index b80957f..abf7a2f 100644
--- a/apps/payment-api/src/services/tunnel-manager.js
+++ b/apps/payment-api/src/services/tunnel-manager.js
@@ -1,5 +1,6 @@
import { spawn } from 'node:child_process';
import { EventEmitter } from 'node:events';
+import { buildCloudflaredInvocation } from '@youpi/common';
/**
* Cloudflare Tunnel Process Manager
@@ -32,7 +33,7 @@ export class TunnelManager extends EventEmitter {
getStatus() {
return {
status: this.status,
- command: this.command,
+ command: this.command, // redacted: never contains the token
pid: this.pid,
startedAt: this.startedAt,
isRunning: this.status === 'RUNNING' || this.status === 'STARTING',
@@ -49,19 +50,16 @@ export class TunnelManager extends EventEmitter {
this.stop();
}
- const trimmed = rawCommand.trim();
- this.command = trimmed;
+ // Only the extracted token is used; the pasted text is never run through a shell.
+ const invocation = buildCloudflaredInvocation(rawCommand);
+ this.command = invocation.redacted;
this.status = 'STARTING';
this.startedAt = new Date().toISOString();
- this.appendLog(`[TunnelManager] Launching tunnel command: ${trimmed}`);
+ this.appendLog(`[TunnelManager] Launching tunnel: ${invocation.redacted}`);
try {
- // Execute via shell (cmd.exe on Windows / sh on Unix) so tokens, flags, and paths execute cleanly
- const isWindows = process.platform === 'win32';
- const shell = isWindows ? (process.env.ComSpec || 'cmd.exe') : '/bin/sh';
- const shellArgs = isWindows ? ['/c', trimmed] : ['-c', trimmed];
-
- const child = spawn(shell, shellArgs, {
+ const child = spawn(invocation.file, invocation.args, {
+ shell: false,
detached: false,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env }
diff --git a/scripts/dev.js b/scripts/dev.js
index 232807d..7efc513 100644
--- a/scripts/dev.js
+++ b/scripts/dev.js
@@ -1,6 +1,7 @@
import { spawn } from 'node:child_process';
import { runMigrations } from '../packages/db/src/migrate.js';
import { startControlCenter } from '../apps/control-center/src/index.js';
+import { startCloudflaredSafely } from '../packages/common/src/tunnel-command.js';
if (!process.env.YOUPI_MODE) process.env.YOUPI_MODE = 'localhost';
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'development';
@@ -21,15 +22,8 @@ const tunnelCmd = process.env.CLOUDFLARE_TUNNEL_COMMAND || branding.cloudflareTu
if (tunnelCmd && tunnelCmd.trim()) {
console.log(`\n[☁️ Cloudflare Tunnel] Executing tunnel command before starting services:`);
- console.log(` > ${tunnelCmd.trim()}`);
- const isWindows = process.platform === 'win32';
- const shell = isWindows ? (process.env.ComSpec || 'cmd.exe') : '/bin/sh';
- const shellArgs = isWindows ? ['/c', tunnelCmd.trim()] : ['-c', tunnelCmd.trim()];
- tunnelProcess = spawn(shell, shellArgs, {
- stdio: 'inherit',
- env: { ...process.env }
- });
- console.log(`[✓] Cloudflare Tunnel process launched (PID: ${tunnelProcess.pid}).\n`);
+ tunnelProcess = startCloudflaredSafely(tunnelCmd, { stdio: 'inherit' });
+ if (tunnelProcess) console.log(`[✓] Cloudflare Tunnel process launched (PID: ${tunnelProcess.pid}).\n`);
// Brief pause to establish socket before services spin up
await new Promise(r => setTimeout(r, 1500));
}
diff --git a/scripts/start.js b/scripts/start.js
index f439021..4a90956 100644
--- a/scripts/start.js
+++ b/scripts/start.js
@@ -5,6 +5,7 @@ import { spawn } from 'node:child_process';
import { runMigrations } from '../packages/db/src/migrate.js';
import { startControlCenter } from '../apps/control-center/src/index.js';
import { resolveRuntimeUrls } from '../packages/common/src/runtime-config.js';
+import { startCloudflaredSafely } from '../packages/common/src/tunnel-command.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -68,15 +69,8 @@ const tunnelCmd = process.env.CLOUDFLARE_TUNNEL_COMMAND || branding.cloudflareTu
if (tunnelCmd && tunnelCmd.trim()) {
console.log(`[☁️ Cloudflare Tunnel] Executing tunnel command before starting services:`);
- console.log(` > ${tunnelCmd.trim()}`);
- const isWindows = process.platform === 'win32';
- const shell = isWindows ? (process.env.ComSpec || 'cmd.exe') : '/bin/sh';
- const shellArgs = isWindows ? ['/c', tunnelCmd.trim()] : ['-c', tunnelCmd.trim()];
- tunnelProcess = spawn(shell, shellArgs, {
- stdio: 'inherit',
- env: { ...process.env }
- });
- console.log(`[✓] Cloudflare Tunnel process launched (PID: ${tunnelProcess.pid}).\n`);
+ tunnelProcess = startCloudflaredSafely(tunnelCmd, { stdio: 'inherit' });
+ if (tunnelProcess) console.log(`[✓] Cloudflare Tunnel process launched (PID: ${tunnelProcess.pid}).\n`);
await new Promise(r => setTimeout(r, 1500));
}