Skip to content

Commit 093e4f3

Browse files
committed
fix: check input on stringify
relates google/zx#1093
1 parent cca6f73 commit 093e4f3

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

src/main/ts/envapi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const formatValue = (v: string): string => {
7373
if (!q1 && !q2 && !q3 && !s) return v
7474
if (!q1) return `${Q1}${v}${Q1}`
7575
if (!q2) return `${Q2}${v}${Q2}`
76+
if (parse(`V=${Q3}${v}${Q3}`).V !== v) throw new Error(`Invalid value: ${v}`)
7677
return `${Q3}${v}${Q3}`
7778
}
7879

src/test/ts/index.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ JSONSTR='{"foo": "b a r"}'`
7171
)
7272
})
7373

74+
test('stringify checks invalid (imbalanced quote) values', () => {
75+
const V1 = "en_US\"'`\nBASH_ENV=$(id 1>&2)\nx=`"
76+
const V2 = 'foo=\'bar\'\nbaz=\\`"qux\\`\"\"'
77+
78+
assert.throws(() => stringify({ V1 }))
79+
assert.throws(() => stringify({ V2 }))
80+
})
81+
7482
test('accepts buffer input', () => {
7583
const str = 'FOO=BAR\r\nBAz=QUZ'
7684
const env = {

0 commit comments

Comments
 (0)