mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 00:18:26 +00:00
fix(daemon): escape schtasks environment assignments
This commit is contained in:
@@ -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(" ");
|
||||
|
||||
Reference in New Issue
Block a user