Skip to content

Commit 40364e7

Browse files
committed
fix: use shlex.quote for safe path escaping in shell commands
1 parent d1067be commit 40364e7

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

docs/cookbooks/codex-coding.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,10 @@ async def main():
242242
max_output_length: int | None = None,
243243
) -> dict:
244244
"""Execute shell commands in a bash session."""
245+
import shlex
245246
# Change to working directory before executing
246-
prefixed_commands = [f"cd {base_path} && {cmd}" for cmd in commands]
247+
safe_path = shlex.quote(base_path)
248+
prefixed_commands = [f"cd {safe_path} && {cmd}" for cmd in commands]
247249
result = await shell_tool(
248250
commands=prefixed_commands,
249251
timeout_ms=timeout_ms,

examples/06_codex_coding_agent.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import argparse
3333
import asyncio
3434
import os
35+
import shlex
3536

3637
from dotenv import load_dotenv
3738
from openai import AsyncOpenAI
@@ -124,7 +125,9 @@ async def shell(
124125
max_output_length: Optional max output length hint
125126
"""
126127
# Change to working directory before executing
127-
prefixed_commands = [f"cd {base_path} && {cmd}" for cmd in commands]
128+
# Use shlex.quote to safely handle paths with spaces or special characters
129+
safe_path = shlex.quote(base_path)
130+
prefixed_commands = [f"cd {safe_path} && {cmd}" for cmd in commands]
128131
result = await shell_tool(
129132
commands=prefixed_commands,
130133
timeout_ms=timeout_ms,

0 commit comments

Comments
 (0)