惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
U
Unit 42
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
G
Google Developers Blog
Recent Announcements
Recent Announcements
B
Blog RSS Feed
罗磊的独立博客
博客园 - Franky
J
Java Code Geeks
S
SegmentFault 最新的问题
D
DataBreaches.Net
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
腾讯CDC
博客园_首页
美团技术团队
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
The Cloudflare Blog
aimingoo的专栏
aimingoo的专栏

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(prompts): reject unsafe template indexes · openclaw/o...
steipete · 2026-05-29 · via Recent Commits to openclaw:main

File tree

  • packages/agent-core/src/harness

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,20 @@

1+

import { describe, expect, it } from "vitest";

2+

import { substituteArgs } from "./prompt-templates.js";

3+
4+

describe("prompt template argument substitution", () => {

5+

it("rejects unsafe positional placeholders", () => {

6+

expect(substituteArgs("$9007199254740992", ["first", "second"])).toBe("");

7+

});

8+
9+

it("rejects unsafe slice starts and lengths", () => {

10+

const args = ["alpha", "beta", "gamma"];

11+
12+

expect(substituteArgs("${@:9007199254740992}", args)).toBe("");

13+

expect(substituteArgs("${@:1:9007199254740992}", args)).toBe("");

14+

});

15+
16+

it("preserves zero slice compatibility", () => {

17+

expect(substituteArgs("${@:0:0}", ["alpha", "beta"])).toBe("");

18+

expect(substituteArgs("${@:0:1}", ["alpha", "beta"])).toBe("alpha");

19+

});

20+

});

Original file line numberDiff line numberDiff line change

@@ -287,19 +287,38 @@ export function parseCommandArgs(argsString: string): string[] {

287287

return args;

288288

}

289289
290+

function parseSafeNonNegativeInteger(raw: string): number | undefined {

291+

const parsed = Number(raw);

292+

return Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : undefined;

293+

}

294+
290295

/** Substitute prompt template placeholders (`$1`, `$@`, `$ARGUMENTS`, `${@:N}`, `${@:N:L}`) with command arguments. */

291296

export function substituteArgs(content: string, args: string[]): string {

292297

let result = content;

293-

result = result.replace(/\$(\d+)/g, (_, num: string) => args[Number.parseInt(num, 10) - 1] ?? "");

298+

result = result.replace(/\$(\d+)/g, (_, num: string) => {

299+

const parsed = parseSafeNonNegativeInteger(num);

300+

if (parsed === undefined || parsed <= 0) {

301+

return "";

302+

}

303+

return args[parsed - 1] ?? "";

304+

});

294305

result = result.replace(

295306

/\$\{@:(\d+)(?::(\d+))?\}/g,

296307

(_, startStr: string, lengthStr?: string) => {

297-

let start = Number.parseInt(startStr, 10) - 1;

308+

const parsedStart = parseSafeNonNegativeInteger(startStr);

309+

if (parsedStart === undefined) {

310+

return "";

311+

}

312+

let start = parsedStart - 1;

298313

if (start < 0) {

299314

start = 0;

300315

}

301316

if (lengthStr) {

302-

return args.slice(start, start + Number.parseInt(lengthStr, 10)).join(" ");

317+

const length = parseSafeNonNegativeInteger(lengthStr);

318+

if (length === undefined) {

319+

return "";

320+

}

321+

return args.slice(start, start + length).join(" ");

303322

}

304323

return args.slice(start).join(" ");

305324

},

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,20 @@

1+

import { describe, expect, it } from "vitest";

2+

import { substituteArgs } from "./prompt-templates.js";

3+
4+

describe("prompt template argument substitution", () => {

5+

it("rejects unsafe positional placeholders", () => {

6+

expect(substituteArgs("$9007199254740992", ["first", "second"])).toBe("");

7+

});

8+
9+

it("rejects unsafe slice starts and lengths", () => {

10+

const args = ["alpha", "beta", "gamma"];

11+
12+

expect(substituteArgs("${@:9007199254740992}", args)).toBe("");

13+

expect(substituteArgs("${@:1:9007199254740992}", args)).toBe("");

14+

});

15+
16+

it("preserves zero slice compatibility", () => {

17+

expect(substituteArgs("${@:0:0}", ["alpha", "beta"])).toBe("");

18+

expect(substituteArgs("${@:0:1}", ["alpha", "beta"])).toBe("alpha");

19+

});

20+

});

Original file line numberDiff line numberDiff line change

@@ -54,6 +54,11 @@ export function parseCommandArgs(argsString: string): string[] {

5454

return args;

5555

}

5656
57+

function parseSafeNonNegativeInteger(raw: string): number | undefined {

58+

const parsed = Number(raw);

59+

return Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : undefined;

60+

}

61+
5762

/**

5863

* Substitute argument placeholders in template content

5964

* Supports:

@@ -71,21 +76,32 @@ export function substituteArgs(content: string, args: string[]): string {

7176

// Replace $1, $2, etc. with positional args FIRST (before wildcards)

7277

// This prevents wildcard replacement values containing $<digit> patterns from being re-substituted

7378

result = result.replace(/\$(\d+)/g, (_, num) => {

74-

const index = Number.parseInt(num, 10) - 1;

79+

const parsed = parseSafeNonNegativeInteger(num);

80+

if (parsed === undefined || parsed <= 0) {

81+

return "";

82+

}

83+

const index = parsed - 1;

7584

return args[index] ?? "";

7685

});

7786
7887

// Replace ${@:start} or ${@:start:length} with sliced args (bash-style)

7988

// Process BEFORE simple $@ to avoid conflicts

8089

result = result.replace(/\$\{@:(\d+)(?::(\d+))?\}/g, (_, startStr, lengthStr) => {

81-

let start = Number.parseInt(startStr, 10) - 1; // Convert to 0-indexed (user provides 1-indexed)

90+

const parsedStart = parseSafeNonNegativeInteger(startStr);

91+

if (parsedStart === undefined) {

92+

return "";

93+

}

94+

let start = parsedStart - 1; // Convert to 0-indexed (user provides 1-indexed)

8295

// Treat 0 as 1 (bash convention: args start at 1)

8396

if (start < 0) {

8497

start = 0;

8598

}

8699
87100

if (lengthStr) {

88-

const length = Number.parseInt(lengthStr, 10);

101+

const length = parseSafeNonNegativeInteger(lengthStr);

102+

if (length === undefined) {

103+

return "";

104+

}

89105

return args.slice(start, start + length).join(" ");

90106

}

91107

return args.slice(start).join(" ");