Skip to content

Commit fa74697

Browse files
committed
security: fix command injection in Telegram bridge (Fixes #118)
User messages from Telegram were interpolated directly into a shell command string with only single-quote escaping, allowing attackers to execute arbitrary commands via $() or backtick expansion. Fix: pass both the API key and user message through stdin instead of the command string, validate sessionId and SANDBOX_NAME, and change stdio from "ignore" to "pipe" so stdin is writable.
1 parent 241ffb2 commit fa74697

1 file changed

Lines changed: 30 additions & 6 deletions

File tree

‎scripts/telegram-bridge.js‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const ALLOWED_CHATS = process.env.ALLOWED_CHAT_IDS
2828

2929
if (!TOKEN) { console.error("TELEGRAM_BOT_TOKEN required"); process.exit(1); }
3030
if (!API_KEY) { console.error("NVIDIA_API_KEY required"); process.exit(1); }
31+
if (!/^[a-zA-Z0-9_-]+$/.test(SANDBOX)) {
32+
console.error("SANDBOX_NAME contains invalid characters"); process.exit(1);
33+
}
3134

3235
let offset = 0;
3336
const activeSessions = new Map(); // chatId → message history
@@ -85,20 +88,41 @@ async function sendTyping(chatId) {
8588

8689
function runAgentInSandbox(message, sessionId) {
8790
return new Promise((resolve) => {
91+
// Validate sessionId — Telegram chat IDs are numeric
92+
const safeSessionId = sessionId.replace(/[^0-9a-zA-Z_-]/g, "");
93+
if (!safeSessionId) {
94+
resolve("Error: invalid session ID");
95+
return;
96+
}
97+
8898
const sshConfig = execSync(`openshell sandbox ssh-config ${SANDBOX}`, { encoding: "utf-8" });
8999

90100
// Write temp ssh config
91-
const confPath = `/tmp/nemoclaw-tg-ssh-${sessionId}.conf`;
101+
const confPath = `/tmp/nemoclaw-tg-ssh-${safeSessionId}.conf`;
92102
require("fs").writeFileSync(confPath, sshConfig);
93103

94-
const escaped = message.replace(/'/g, "'\\''");
95-
const cmd = `export NVIDIA_API_KEY='${API_KEY}' && nemoclaw-start openclaw agent --agent main --local -m '${escaped}' --session-id 'tg-${sessionId}'`;
96-
97-
const proc = spawn("ssh", ["-T", "-F", confPath, `openshell-${SANDBOX}`, cmd], {
104+
// Pass API key (line 1) and user message (remaining stdin) via stdin
105+
// to prevent shell command injection through message content.
106+
// Variable expansion inside double quotes ("$VAR") is not subject to
107+
// further shell parsing, so the values are treated as literal strings.
108+
const remoteScript = [
109+
"read -r NVIDIA_API_KEY",
110+
"export NVIDIA_API_KEY",
111+
"MSG=$(cat)",
112+
`exec nemoclaw-start openclaw agent --agent main --local -m "$MSG" --session-id "tg-${safeSessionId}"`,
113+
].join(" && ");
114+
115+
const proc = spawn("ssh", ["-T", "-F", confPath, `openshell-${SANDBOX}`, remoteScript], {
98116
timeout: 120000,
99-
stdio: ["ignore", "pipe", "pipe"],
117+
stdio: ["pipe", "pipe", "pipe"],
100118
});
101119

120+
// Write API key on the first line, then the user message.
121+
// This keeps both values out of the shell command string.
122+
proc.stdin.write(API_KEY + "\n");
123+
proc.stdin.write(message);
124+
proc.stdin.end();
125+
102126
let stdout = "";
103127
let stderr = "";
104128

0 commit comments

Comments
 (0)