nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] Custom tool placeholders inside bash arithmetic are not flagged and can run commands

Open addyCooks opened this issue 1 week ago • 2 comments

Description

findUnsafePlaceholder is meant to reject templates where a quoted value would not stay a single word. It does not know that $(( )), (( )) and [[ ]] evaluate their contents as arithmetic, where an array subscript like a[$(cmd)] runs cmd. A tool whose template uses a placeholder there is accepted, and a model-supplied value can then run a command.

Environment

  • OS: Windows 11 Home 10.0.26300 (bash from Git for Windows)
  • Node version: v22.22.3
  • Nanocoder version: 1.32.0 (built from main at eb7dee57)
  • Provider: Not provider-specific
  • Model: Not model-specific

Steps to Reproduce

  1. Use one of these bodies: echo $(( {{ n }} + 1 )), (( {{ n }} > 3 )) && echo big, or [[ {{ n }} -gt 3 ]] && echo big.
  2. Call findUnsafePlaceholder(body, 'posix', ['n']).
  3. Render with renderBody(body, {n: 'a[$(echo INJECTED-$((6*7)) >&2)]'}, shellQuote) and run it with bash -c.

Expected Behavior

findUnsafePlaceholder returns a message for each body. The tool is rejected, or the value is checked to be a plain number first.

Actual Behavior

findUnsafePlaceholder returns undefined for all three, and bash -c runs the command each time.

Logs/Screenshots

findUnsafePlaceholder -> undefined (all 3 bodies)
bash output contains: INJECTED-42   (all 3 bodies)

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder (main)

Suggested fix:

  • source/custom-tools/template.ts:168 (posixProblem): treat placeholders inside $(( )), (( )), [[ ]] and let arguments as unsafe, with a message that says to assign the value to a variable and validate it.
  • source/custom-tools/template.ts:383 (findUnsafePlaceholder) is where the check is exposed.
  • Add tests for the three bodies above and for the safe form (n={{ n }}; echo $(( n + 1 )), if you decide to keep it).
  • Note echo {{ n }} and let x={{ n }} did not run the command in my test.

addyCooks avatar Nov 23 '26 07:11 addyCooks

can you assign this to me @addyCooks

Vishesh-Verma-07 avatar Nov 24 '26 08:11 Vishesh-Verma-07

Opened #2388 — placeholders inside $(( )), (( )) and [[ ]] are now rejected by findUnsafePlaceholder.

787164510-sudo avatar Nov 24 '26 11:11 787164510-sudo