Skip to content

Commit 259d863

Browse files
authored
Gateway: harden cron.runs jobId path handling (#24038) thanks @Takhoffman
Verified: - pnpm install --frozen-lockfile - pnpm build - pnpm check - pnpm test:macmini Co-authored-by: Takhoffman <[email protected]> Co-authored-by: Tak Hoffman <[email protected]>
1 parent 45febec commit 259d863

6 files changed

Lines changed: 79 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Docs: https://docs.openclaw.ai
7676
- Cron/Service: execute manual `cron.run` jobs outside the cron lock (while still persisting started/finished state atomically) so `cron.list` and `cron.status` remain responsive during long forced runs. (#23628) Thanks @dsgraves.
7777
- Cron/Timer: keep a watchdog recheck timer armed while `onTimer` is actively executing so the scheduler continues polling even if a due-run tick stalls for an extended period. (#23628) Thanks @dsgraves.
7878
- Cron/Run log: clean up settled per-path run-log write queue entries so long-running cron uptime does not retain stale promise bookkeeping in memory.
79+
- Cron/Run log: harden `cron.runs` run-log path resolution by rejecting path-separator `id`/`jobId` inputs and enforcing reads within the per-cron `runs/` directory.
7980
- Cron/Isolation: force fresh session IDs for isolated cron runs so `sessionTarget="isolated"` executions never reuse prior run context. (#23470) Thanks @echoVic.
8081
- Plugins/Install: strip `workspace:*` devDependency entries from copied plugin manifests before `npm install --omit=dev`, preventing `EUNSUPPORTEDPROTOCOL` install failures for npm-published channel plugins (including Feishu and MS Teams).
8182
- Feishu/Plugins: restore bundled Feishu SDK availability for global installs and strip `openclaw: workspace:*` from plugin `devDependencies` during plugin-version sync so npm-installed Feishu plugins do not fail dependency install. (#23611, #23645, #23603)

‎src/cron/run-log.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ describe("cron run log", () => {
2525
expect(p.endsWith(path.join(os.tmpdir(), "cron", "runs", "job-1.jsonl"))).toBe(true);
2626
});
2727

28+
it("rejects unsafe job ids when resolving run log path", () => {
29+
const storePath = path.join(os.tmpdir(), "cron", "jobs.json");
30+
expect(() => resolveCronRunLogPath({ storePath, jobId: "../job-1" })).toThrow(
31+
/invalid cron run log job id/i,
32+
);
33+
expect(() => resolveCronRunLogPath({ storePath, jobId: "nested/job-1" })).toThrow(
34+
/invalid cron run log job id/i,
35+
);
36+
expect(() => resolveCronRunLogPath({ storePath, jobId: "..\\job-1" })).toThrow(
37+
/invalid cron run log job id/i,
38+
);
39+
});
40+
2841
it("appends JSONL and prunes by line count", async () => {
2942
await withRunLogDir("openclaw-cron-log-", async (dir) => {
3043
const logPath = path.join(dir, "runs", "job-1.jsonl");

‎src/cron/run-log.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,27 @@ export type CronRunLogEntry = {
1919
nextRunAtMs?: number;
2020
} & CronRunTelemetry;
2121

22+
function assertSafeCronRunLogJobId(jobId: string): string {
23+
const trimmed = jobId.trim();
24+
if (!trimmed) {
25+
throw new Error("invalid cron run log job id");
26+
}
27+
if (trimmed.includes("/") || trimmed.includes("\\") || trimmed.includes("\0")) {
28+
throw new Error("invalid cron run log job id");
29+
}
30+
return trimmed;
31+
}
32+
2233
export function resolveCronRunLogPath(params: { storePath: string; jobId: string }) {
2334
const storePath = path.resolve(params.storePath);
2435
const dir = path.dirname(storePath);
25-
return path.join(dir, "runs", `${params.jobId}.jsonl`);
36+
const runsDir = path.resolve(dir, "runs");
37+
const safeJobId = assertSafeCronRunLogJobId(params.jobId);
38+
const resolvedPath = path.resolve(runsDir, `${safeJobId}.jsonl`);
39+
if (!resolvedPath.startsWith(`${runsDir}${path.sep}`)) {
40+
throw new Error("invalid cron run log job id");
41+
}
42+
return resolvedPath;
2643
}
2744

2845
const writesByPath = new Map<string, Promise<void>>();

‎src/gateway/protocol/cron-validators.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,11 @@ describe("cron protocol validators", () => {
4646
expect(validateCronRunsParams({ id: "job-1", limit: 0 })).toBe(false);
4747
expect(validateCronRunsParams({ jobId: "job-2", limit: 0 })).toBe(false);
4848
});
49+
50+
it("rejects cron.runs path traversal ids", () => {
51+
expect(validateCronRunsParams({ id: "../job-1" })).toBe(false);
52+
expect(validateCronRunsParams({ id: "nested/job-1" })).toBe(false);
53+
expect(validateCronRunsParams({ jobId: "..\\job-2" })).toBe(false);
54+
expect(validateCronRunsParams({ jobId: "nested\\job-2" })).toBe(false);
55+
});
4956
});

‎src/gateway/protocol/schema/cron.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ function cronIdOrJobIdParams(extraFields: Record<string, TSchema>) {
5959
]);
6060
}
6161

62+
const CronRunLogJobIdSchema = Type.String({
63+
minLength: 1,
64+
// Prevent path traversal via separators in cron.runs id/jobId.
65+
pattern: "^[^/\\\\]+$",
66+
});
67+
68+
function cronRunsIdOrJobIdParams(extraFields: Record<string, TSchema>) {
69+
return Type.Union([
70+
Type.Object(
71+
{
72+
id: CronRunLogJobIdSchema,
73+
...extraFields,
74+
},
75+
{ additionalProperties: false },
76+
),
77+
Type.Object(
78+
{
79+
jobId: CronRunLogJobIdSchema,
80+
...extraFields,
81+
},
82+
{ additionalProperties: false },
83+
),
84+
]);
85+
}
86+
6287
export const CronScheduleSchema = Type.Union([
6388
Type.Object(
6489
{
@@ -241,7 +266,7 @@ export const CronRunParamsSchema = cronIdOrJobIdParams({
241266
mode: Type.Optional(Type.Union([Type.Literal("due"), Type.Literal("force")])),
242267
});
243268

244-
export const CronRunsParamsSchema = cronIdOrJobIdParams({
269+
export const CronRunsParamsSchema = cronRunsIdOrJobIdParams({
245270
limit: Type.Optional(Type.Integer({ minimum: 1, maximum: 5000 })),
246271
});
247272

‎src/gateway/server-methods/cron.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,20 @@ export const cronHandlers: GatewayRequestHandlers = {
214214
);
215215
return;
216216
}
217-
const logPath = resolveCronRunLogPath({
218-
storePath: context.cronStorePath,
219-
jobId,
220-
});
217+
let logPath: string;
218+
try {
219+
logPath = resolveCronRunLogPath({
220+
storePath: context.cronStorePath,
221+
jobId,
222+
});
223+
} catch {
224+
respond(
225+
false,
226+
undefined,
227+
errorShape(ErrorCodes.INVALID_REQUEST, "invalid cron.runs params: invalid id"),
228+
);
229+
return;
230+
}
221231
const entries = await readCronRunLogEntries(logPath, {
222232
limit: p.limit,
223233
jobId,

0 commit comments

Comments
 (0)