Skip to content

Commit 708c467

Browse files
committed
fix: check donenv names
1 parent 93691d6 commit 708c467

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

src/util.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,14 @@ export const parseBool = (v: string): boolean | string =>
361361
// prettier-ignore
362362
export const parseDotenv = (content: string): NodeJS.ProcessEnv => {
363363
const e: Record<string, string> = {}
364+
const r = /^[a-zA-Z_]+[a-zA-Z0-9_]*$/
364365
let k = ''
365366
let c = ''
366367
let q = ''
367-
const cap = () => {if (c && k) { e[k] = c; c = ''; k = '' }}
368+
const cap = () => { if (c && k) {
369+
if (!r.test(k)) throw new Error(`Invalid identifier: ${k}`)
370+
e[k] = c; c = ''; k = ''
371+
}}
368372

369373
for (const s of content) {
370374
if (s === ' ' && !q) {
@@ -389,6 +393,7 @@ export const parseDotenv = (content: string): NodeJS.ProcessEnv => {
389393
}
390394
c += s
391395
}
396+
cap()
392397

393398
return e
394399
}

test/cli.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe('cli', () => {
150150
assert.ok(p.stderr.endsWith(cwd + '\n'))
151151
})
152152

153-
test('supports `--env` options with file', async () => {
153+
test('supports `--env` option', async () => {
154154
const env = tmpfile(
155155
'.env',
156156
`FOO=BAR

test/util.test.js

+31-28
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,9 @@ describe('util', () => {
140140
assert.equal(toCamelCase('SOME_MORE_BIG_STR'), 'someMoreBigStr')
141141
assert.equal(toCamelCase('kebab-input-str'), 'kebabInputStr')
142142
})
143-
})
144143

145-
test('parseDotenv()', () => {
146-
const multiline = `SIMPLE=xyz123
144+
test('parseDotenv()', () => {
145+
const multiline = `SIMPLE=xyz123
147146
NON_INTERPOLATED='raw text without variable interpolation'
148147
MULTILINE = """
149148
long text here,
@@ -152,32 +151,36 @@ e.g. a private SSH key
152151
ENV=v1\nENV2=v2\n\n\n ENV3 = v3 \n export ENV4=v4
153152
`
154153

155-
assert.deepEqual(parseDotenv(multiline), {
156-
SIMPLE: 'xyz123',
157-
NON_INTERPOLATED: 'raw text without variable interpolation',
158-
MULTILINE: '\nlong text here,\ne.g. a private SSH key\n',
159-
ENV: 'v1',
160-
ENV2: 'v2',
161-
ENV3: 'v3',
162-
ENV4: 'v4',
163-
})
164-
})
165-
166-
describe('readEnvFromFile()', () => {
167-
test('handles correct proccess.env', () => {
168-
const file = tempfile('.env', 'ENV=value1\nENV2=value24')
169-
const env = readEnvFromFile(file)
170-
assert.equal(env.ENV, 'value1')
171-
assert.equal(env.ENV2, 'value24')
172-
assert.ok(env.NODE_VERSION !== '')
154+
assert.deepEqual(parseDotenv(multiline), {
155+
SIMPLE: 'xyz123',
156+
NON_INTERPOLATED: 'raw text without variable interpolation',
157+
MULTILINE: '\nlong text here,\ne.g. a private SSH key\n',
158+
ENV: 'v1',
159+
ENV2: 'v2',
160+
ENV3: 'v3',
161+
ENV4: 'v4',
162+
})
163+
164+
assert.deepEqual(parseDotenv(`FOO=BAR
165+
BAR=FOO+`), {FOO: 'BAR', BAR: 'FOO+'})
173166
})
174167

175-
test('handles correct some env', () => {
176-
const file = tempfile('.env', 'ENV=value1\nENV2=value24')
177-
const env = readEnvFromFile(file, { version: '1.0.0', name: 'zx' })
178-
assert.equal(env.ENV, 'value1')
179-
assert.equal(env.ENV2, 'value24')
180-
assert.equal(env.version, '1.0.0')
181-
assert.equal(env.name, 'zx')
168+
describe('readEnvFromFile()', () => {
169+
test('handles correct proccess.env', () => {
170+
const file = tempfile('.env', 'ENV=value1\nENV2=value24')
171+
const env = readEnvFromFile(file)
172+
assert.equal(env.ENV, 'value1')
173+
assert.equal(env.ENV2, 'value24')
174+
assert.ok(env.NODE_VERSION !== '')
175+
})
176+
177+
test('handles correct some env', () => {
178+
const file = tempfile('.env', 'ENV=value1\nENV2=value24')
179+
const env = readEnvFromFile(file, { version: '1.0.0', name: 'zx' })
180+
assert.equal(env.ENV, 'value1')
181+
assert.equal(env.ENV2, 'value24')
182+
assert.equal(env.version, '1.0.0')
183+
assert.equal(env.name, 'zx')
184+
})
182185
})
183186
})

0 commit comments

Comments
 (0)