fix(daemon): escape schtasks environment assignments

This commit is contained in:
Peter Steinberger
2026-02-19 15:52:08 +01:00
parent c45f3c5b00
commit dafe52e8cf
2 changed files with 161 additions and 17 deletions

View File

@@ -1,11 +1,5 @@
import fs from "node:fs/promises";
import path from "node:path";
import { splitArgsPreservingQuotes } from "./arg-split.js";
import { resolveGatewayServiceDescription, resolveGatewayWindowsTaskName } from "./constants.js";
import { formatLine, writeFormattedLines } from "./output.js";
import { resolveGatewayStateDir } from "./paths.js";
import { parseKeyValueOutput } from "./runtime-parse.js";
import { execSchtasks } from "./schtasks-exec.js";
import type { GatewayServiceRuntime } from "./service-runtime.js";
import type {
GatewayServiceCommandConfig,
@@ -16,6 +10,12 @@ import type {
GatewayServiceManageArgs,
GatewayServiceRenderArgs,
} from "./service-types.js";
import { splitArgsPreservingQuotes } from "./arg-split.js";
import { resolveGatewayServiceDescription, resolveGatewayWindowsTaskName } from "./constants.js";
import { formatLine, writeFormattedLines } from "./output.js";
import { resolveGatewayStateDir } from "./paths.js";
import { parseKeyValueOutput } from "./runtime-parse.js";
import { execSchtasks } from "./schtasks-exec.js";
function resolveTaskName(env: GatewayServiceEnv): string {
const override = env.OPENCLAW_WINDOWS_TASK_NAME?.trim();
@@ -42,6 +42,61 @@ function quoteCmdArg(value: string): string {
return `"${value.replace(/"/g, '\\"')}"`;
}
function escapeCmdSetAssignmentComponent(value: string): string {
return value.replace(/\^/g, "^^").replace(/%/g, "%%").replace(/!/g, "^!").replace(/"/g, '^"');
}
function unescapeCmdSetAssignmentComponent(value: string): string {
let out = "";
for (let i = 0; i < value.length; i += 1) {
const ch = value[i];
const next = value[i + 1];
if (ch === "^" && (next === "^" || next === '"' || next === "!")) {
out += next;
i += 1;
continue;
}
if (ch === "%" && next === "%") {
out += "%";
i += 1;
continue;
}
out += ch;
}
return out;
}
function parseCmdSetAssignment(line: string): { key: string; value: string } | null {
const raw = line.trim();
if (!raw) {
return null;
}
const quoted = raw.startsWith('"') && raw.endsWith('"') && raw.length >= 2;
const assignment = quoted ? raw.slice(1, -1) : raw;
const index = assignment.indexOf("=");
if (index <= 0) {
return null;
}
const key = assignment.slice(0, index).trim();
const value = assignment.slice(index + 1).trim();
if (!key) {
return null;
}
if (!quoted) {
return { key, value };
}
return {
key: unescapeCmdSetAssignmentComponent(key),
value: unescapeCmdSetAssignmentComponent(value),
};
}
function renderCmdSetAssignment(key: string, value: string): string {
const escapedKey = escapeCmdSetAssignmentComponent(key);
const escapedValue = escapeCmdSetAssignmentComponent(value);
return `set "${escapedKey}=${escapedValue}"`;
}
function resolveTaskUser(env: GatewayServiceEnv): string | null {
const username = env.USERNAME || env.USER || env.LOGNAME;
if (!username) {
@@ -84,14 +139,9 @@ export async function readScheduledTaskCommand(
continue;
}
if (line.toLowerCase().startsWith("set ")) {
const assignment = line.slice(4).trim();
const index = assignment.indexOf("=");
if (index > 0) {
const key = assignment.slice(0, index).trim();
const value = assignment.slice(index + 1).trim();
if (key) {
environment[key] = value;
}
const assignment = parseCmdSetAssignment(line.slice(4));
if (assignment) {
environment[assignment.key] = assignment.value;
}
continue;
}
@@ -157,7 +207,7 @@ function buildTaskScript({
if (!value) {
continue;
}
lines.push(`set ${key}=${value}`);
lines.push(renderCmdSetAssignment(key, value));
}
}
const command = programArguments.map(quoteCmdArg).join(" ");