Skip to content

Tools Fail to Unescape XML Entities (e.g., "&&" Becomes Invalid Shell Syntax) #4530

Closed

Description

@taqtiqa-mark

Description

Disclosure: 1st draft AI generated.

Tool fail to properly unescape XML entities in command arguments, leading to execution errors, or file content corruption. This occurs when commands include chained operators like "&&", which must be escaped as "&&" in the XML function call format to make it valid XML. However, the tool passes the escaped string directly to the shell without unescaping, causing syntax errors.

Also happens with Edit and Write tools where \n replaces newlines and a whole file becomes one line of text.

Environment:

  • Platform: Linux.
  • Tools: Bash, Write, Edit tools in opencode.

Impact:

  • Prevents reliable use of chained Bash commands, breaking workflows like script execution or multi-step operations.

Suggested Fix:

  • In the Tool handlers, add unescaping logic (e.g., replace "&" with "&", "\n" with new line, etc.) before executing the command string in the shell. Reference standard XML entity handling in tool parsers.

Additional Context:

  • This aligns with the safety instructions' XML-inspired format, but the parsing layer appears to miss unescaping.
  • Reproducible in sessions with function calls: Bash, Write, Edit.

Related:
#3868 (comment)

OpenCode version

1.0.77

Steps to reproduce

  1. Invoke the Bash tool with a command like: cd /path && ls (escaped for XML validity).
  2. The tool executes it as "cd /path && ls" in the shell.
  3. Result: Error like "/bin/sh: 1: Syntax error: "&" unexpected".

Expected Behavior:

  • The app should unescape the argument (e.g., "&&" -> "&&") before passing to the shell, allowing valid execution (e.g., "cd /path && ls").

Actual Behavior:

  • Escaped string is passed literally, causing shell syntax errors for any command with "&" (e.g., chaining with "&&").

Screenshot and/or share link

No response

Operating System

Linux

Terminal

Alacritty & Zellij

Activity

  1. taqtiqa-mark commented on Nov 21, 2025

    @taqtiqa-mark
    Author

    Another example:

    $ cat <<'EOF' > /home/node/project/tmp/024-analysis/024-analysis-report.md
    ...
    /bin/sh: 1: Syntax error: "&" unexpected
  2. self-assigned this
    on Nov 21, 2025
  3. rekram1-node commented on Nov 21, 2025

    @rekram1-node
    Contributor

    we shall fix

  4. taqtiqa-mark commented on Nov 28, 2025

    @taqtiqa-mark
    Author

    Hmm, it appears that most failures occur silently, and occasionally you get an error report in the TUI. Mostly the effect can look like the LLM failed to follow an instruction, e.g. save a file containing the output.

  5. taqtiqa-mark commented on Dec 28, 2025

    @taqtiqa-mark
    Author

    we shall fix

    Still seen in v1.0.193

  6. taqtiqa-mark commented on Dec 29, 2025

    @taqtiqa-mark
    Author

    Root cause:

    Custom XML parser extracts textContent literally (no entity decoding like &→& or \n→newline), passing raw strings directly to child_process.spawn(command) in bash tool and string ops/Bun.write in edit/write tools.

    Files:

    • packages/opencode/src/tool/bash.ts (line ~170: spawn(params.command, { – no unescape)
    • packages/opencode/src/tool/edit.ts (uses raw params.oldString/newString)
    • packages/opencode/src/tool/registry.ts (registers tools)
  7. taqtiqa-mark commented on Dec 29, 2025

    @taqtiqa-mark
    Author

    @rekram1-node, will you accept PR that use either fast-xml-parser, xml2js or xmldom, xpath, etc. Or is there a preferred approach?

  8. taqtiqa-mark commented on Feb 12, 2026

    @taqtiqa-mark
    Author

    In the spirit of calling out excellent engineering and design....

    I've found that the Pi Coding Agent is more robust and, for me has none of the issues that I've encountered in OC, or has an elegant way to resolve them.... I'm dropping out of OC issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions