Skip to content
Open
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
26 changes: 25 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,42 @@ function tokenizeCommand(input) {
const ch = input[i];

if (quote) {
if (quote === "'") {
if (ch === '\\' && input[i + 1] === quote) {
current += quote;
i++;
continue;
}
if (ch === quote) {
quote = null;
continue;
Comment on lines +69 to +71

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve escaped apostrophes in single-quoted args

The new single-quote branch closes on every ' character, so inputs that rely on \' now break with Unclosed quote instead of producing a literal apostrophe. For example, --args-json '{"prompt":"don\'t"}' (a common pattern when wrapping JSON in single quotes) parsed before this change but now fails, which can break prompt/JSON payloads containing apostrophes in existing pipelines.

Useful? React with 👍 / 👎.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}
current += ch;
continue;
}

// Double-quoted mode: preserve unknown escapes (\n, \t, etc) while
// unescaping only shell-like quote/backslash escapes.
if (ch === '\\') {
const next = input[i + 1];
if (next) {
if (next === '"' || next === '\\' || next === '$' || next === '`') {
current += next;
i++;
continue;
}
if (next === '\n') {
i++;
continue;
}
current += ch;
continue;
}

if (ch === quote) {
quote = null;
continue;
}

current += ch;
continue;
}
Expand Down
23 changes: 23 additions & 0 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,26 @@ test('parsePipeline keeps quoted pipes', () => {
assert.equal(p.length, 2);
assert.deepEqual(p[0].args._, ['echo', 'a|b']);
});

test('parsePipeline preserves JSON escapes in double-quoted args', () => {
const p = parsePipeline('openclaw.invoke --tool llm-task --action json --args-json "{\\"prompt\\":\\"line1\\\\nline2\\",\\"schema\\":{\\"type\\":\\"object\\"}}"');
assert.equal(p.length, 1);
const raw = p[0].args['args-json'];
const parsed = JSON.parse(raw);
assert.equal(parsed.prompt, 'line1\nline2');
assert.equal(parsed.schema.type, 'object');
});

test('parsePipeline keeps single-quoted args literal', () => {
const p = parsePipeline("openclaw.invoke --args-json '{\"x\":\"a\\\\nb\"}'");
assert.equal(p.length, 1);
assert.equal(p[0].args['args-json'], '{"x":"a\\\\nb"}');
});

test('parsePipeline preserves escaped apostrophes in single-quoted args', () => {
const p = parsePipeline("openclaw.invoke --args-json '{\"prompt\":\"don\\'t\"}'");
assert.equal(p.length, 1);
const raw = p[0].args['args-json'];
const parsed = JSON.parse(raw);
assert.equal(parsed.prompt, "don't");
});