[Bug] Custom tool placeholders inside bash arithmetic are not flagged and can run commands
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
- Use one of these bodies:
echo $(( {{ n }} + 1 )),(( {{ n }} > 3 )) && echo big, or[[ {{ n }} -gt 3 ]] && echo big. - Call
findUnsafePlaceholder(body, 'posix', ['n']). - Render with
renderBody(body, {n: 'a[$(echo INJECTED-$((6*7)) >&2)]'}, shellQuote)and run it withbash -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$(( )),(( )),[[ ]]andletarguments 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 }}andlet x={{ n }}did not run the command in my test.
can you assign this to me @addyCooks
Opened #2388 — placeholders inside $(( )), (( )) and [[ ]] are now rejected by findUnsafePlaceholder.