Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openrouter/spawn",
"version": "0.30.1",
"version": "0.30.2",
"type": "module",
"bin": {
"spawn": "cli.js"
Expand Down
28 changes: 28 additions & 0 deletions packages/cli/src/shared/agent-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ export interface CloudRunner {
downloadFile(remotePath: string, localPath: string): Promise<void>;
}

// ─── Script template validation ────────────────────────────────────────────

/**
* Validate that a script template string does not contain JS template
* interpolation patterns (`${...}`) before it is base64-encoded for shell
* injection into systemd units or remote commands.
*
* Defense-in-depth: the scripts are currently static string arrays joined
* with `\n`, so they should never contain interpolation markers. This guard
* catches future regressions where a developer might accidentally introduce
* template literal interpolation before encoding.
*
* Note: backticks alone are allowed (used in markdown content for skill
* files), but `${` is always rejected as it indicates JS interpolation.
*/
export function validateScriptTemplate(script: string, label: string): void {
if (/\$\{/.test(script)) {
throw new Error(`Script template "${label}" contains \${} interpolation — refusing to encode`);
}
}

// ─── Install helpers ────────────────────────────────────────────────────────

async function installAgent(
Expand Down Expand Up @@ -550,6 +571,9 @@ export async function startGateway(runner: CloudRunner): Promise<void> {
"WantedBy=multi-user.target",
].join("\n");

validateScriptTemplate(wrapperScript, "gateway-wrapper");
validateScriptTemplate(unitFile, "gateway-unit");

const wrapperB64 = Buffer.from(wrapperScript).toString("base64");
const unitB64 = Buffer.from(unitFile).toString("base64");
if (!/^[A-Za-z0-9+/=]+$/.test(wrapperB64)) {
Expand Down Expand Up @@ -811,6 +835,10 @@ export async function setupAutoUpdate(runner: CloudRunner, agentName: string, up
"WantedBy=timers.target",
].join("\n");

validateScriptTemplate(wrapperScript, "auto-update-wrapper");
validateScriptTemplate(unitFile, "auto-update-unit");
validateScriptTemplate(timerFile, "auto-update-timer");

const wrapperB64 = Buffer.from(wrapperScript).toString("base64");
const unitB64 = Buffer.from(unitFile).toString("base64");
const timerB64 = Buffer.from(timerFile).toString("base64");
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/shared/spawn-skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import type { CloudRunner } from "./agent-setup.js";

import { wrapSshCall } from "./agent-setup.js";
import { validateScriptTemplate, wrapSshCall } from "./agent-setup.js";
import { asyncTryCatchIf, isOperationalError } from "./result.js";
import { logInfo, logWarn } from "./ui.js";

Expand Down Expand Up @@ -158,6 +158,8 @@ export async function injectSpawnSkill(runner: CloudRunner, agentName: string):
return;
}

validateScriptTemplate(config.content, `spawn-skill-${agentName}`);

const b64 = Buffer.from(config.content).toString("base64");
if (!/^[A-Za-z0-9+/=]+$/.test(b64)) {
throw new Error("Unexpected characters in base64 output");
Expand Down
Loading