Skip to content

Commit e1fa354

Browse files
Fix escaped quotes in file write operations (#70)
* Fix escaped quotes in file write operations Remove unnecessary quote escaping when using quoted heredoc delimiter. The quoted delimiter 'SANDBOX_EOF' treats content literally, making the escaping redundant and causing escaped quotes to appear in output. * Add changeset * Fix type errors
1 parent 69b91d1 commit e1fa354

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

.changeset/six-seas-go.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/sandbox": patch
3+
---
4+
5+
Fix escaped quotes in file write operations

packages/sandbox/container_src/handler/exec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { SessionManager } from "../isolation";
2-
import { SessionExecRequest } from "../types";
1+
import type { SessionManager } from "../isolation";
2+
import type { SessionExecRequest } from "../types";
33

44
export async function handleExecuteRequest(
55
req: Request,

packages/sandbox/container_src/handler/session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { SessionManager } from "../isolation";
2-
import { CreateSessionRequest } from "../types";
1+
import type { SessionManager } from "../isolation";
2+
import type { CreateSessionRequest } from "../types";
33

44
export async function handleCreateSession(
55
req: Request,

packages/sandbox/container_src/isolation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,11 @@ export class Session {
423423

424424
// File Operations - Execute as shell commands to inherit session context
425425
async writeFileOperation(path: string, content: string, encoding: string = 'utf-8'): Promise<{ success: boolean; exitCode: number; path: string }> {
426-
// Escape content for safe heredoc usage
427-
const safeContent = content.replace(/'/g, "'\\''");
428-
429426
// Create parent directory if needed, then write file using heredoc
427+
// Note: The quoted heredoc delimiter 'SANDBOX_EOF' prevents variable expansion
428+
// and treats the content literally, so no escaping is required
430429
const command = `mkdir -p "$(dirname "${path}")" && cat > "${path}" << 'SANDBOX_EOF'
431-
${safeContent}
430+
${content}
432431
SANDBOX_EOF`;
433432

434433
const result = await this.exec(command);

0 commit comments

Comments
 (0)