Commit 939cdfa

Anton Golub <antongolub@antongolub.com>
2025-03-29 16:18:17
docs: highlight double quoting issue (#1164)
1 parent d89e3e1
Changed files (2)
docs/quotes.md
@@ -8,7 +8,15 @@ const name = 'foo & bar'
 await $`mkdir ${name}`
 ```
 
-Zx automatically escapes and quotes anything within `${...}`, so there's no need for additional quotes.
+
+> [!WARNING]  
+> Zx automatically escapes and quotes anything within `${...}`, so there's no need for additional quotes. Moreover, this may result in an **unsafe injection**.
+> ```ts
+> const args = ['param && echo bar']
+> const p = $`echo --foo=$'${args}'`
+> (await p).stdout // '--foo=$param\nbar\n'
+> ```
+
 
 The following examples produce the same, correct result:
 
test/core.test.js
@@ -102,6 +102,12 @@ describe('core', () => {
       assert.equal((await $`echo ${bar}`).stdout.trim(), bar)
     })
 
+    test('broken quoting', async () => {
+      const args = ['param && echo bar']
+      const p = $`echo --foo=$'${args}'`
+      assert.equal((await p).stdout, '--foo=$param\nbar\n')
+    })
+
     test('undefined and empty string correctly quoted', async () => {
       assert.equal((await $`echo -n ${undefined}`).toString(), 'undefined')
       assert.equal((await $`echo -n ${''}`).toString(), '')