Skip to content

Commit

Permalink
fix: check input on stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Feb 1, 2025
1 parent cca6f73 commit 093e4f3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/ts/envapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const formatValue = (v: string): string => {
if (!q1 && !q2 && !q3 && !s) return v
if (!q1) return `${Q1}${v}${Q1}`
if (!q2) return `${Q2}${v}${Q2}`
if (parse(`V=${Q3}${v}${Q3}`).V !== v) throw new Error(`Invalid value: ${v}`)
return `${Q3}${v}${Q3}`
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/ts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ JSONSTR='{"foo": "b a r"}'`
)
})

test('stringify checks invalid (imbalanced quote) values', () => {
const V1 = "en_US\"'`\nBASH_ENV=$(id 1>&2)\nx=`"
const V2 = 'foo=\'bar\'\nbaz=\\`"qux\\`\"\"'

assert.throws(() => stringify({ V1 }))
assert.throws(() => stringify({ V2 }))
})

test('accepts buffer input', () => {
const str = 'FOO=BAR\r\nBAz=QUZ'
const env = {
Expand Down

0 comments on commit 093e4f3

Please sign in to comment.