Skip to content

Commit cb9b881

Browse files
authored
fix(envvars): added industry standard dotenv parsing regex for adding envvars in settings (#2327)
* fix(envvars): added industry standard dotenv parsing regex for adding envvars in settings * ack PR comments * added more edge cases
1 parent 3c84784 commit cb9b881

File tree

1 file changed

+38
-3
lines changed
  • apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/settings-modal/components/environment

1 file changed

+38
-3
lines changed

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/settings-modal/components/environment/environment.tsx

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,48 @@ export function EnvironmentVariables({ registerBeforeLeaveHandler }: Environment
352352
}, [])
353353

354354
const parseEnvVarLine = useCallback((line: string): UIEnvironmentVariable | null => {
355-
const equalIndex = line.indexOf('=')
355+
const trimmed = line.trim()
356+
357+
if (!trimmed || trimmed.startsWith('#')) return null
358+
359+
const withoutExport = trimmed.replace(/^export\s+/, '')
360+
361+
const equalIndex = withoutExport.indexOf('=')
356362
if (equalIndex === -1 || equalIndex === 0) return null
357363

358-
const potentialKey = line.substring(0, equalIndex).trim()
364+
const potentialKey = withoutExport.substring(0, equalIndex).trim()
359365
if (!ENV_VAR_PATTERN.test(potentialKey)) return null
360366

361-
const value = line.substring(equalIndex + 1).trim()
367+
let value = withoutExport.substring(equalIndex + 1)
368+
369+
const looksLikeBase64Key = /^[A-Za-z0-9+/]+$/.test(potentialKey) && !potentialKey.includes('_')
370+
const valueIsJustPadding = /^=+$/.test(value.trim())
371+
if (looksLikeBase64Key && valueIsJustPadding && potentialKey.length > 20) {
372+
return null
373+
}
374+
375+
const trimmedValue = value.trim()
376+
if (
377+
!trimmedValue.startsWith('"') &&
378+
!trimmedValue.startsWith("'") &&
379+
!trimmedValue.startsWith('`')
380+
) {
381+
const commentIndex = value.search(/\s#/)
382+
if (commentIndex !== -1) {
383+
value = value.substring(0, commentIndex)
384+
}
385+
}
386+
387+
value = value.trim()
388+
389+
if (
390+
(value.startsWith('"') && value.endsWith('"')) ||
391+
(value.startsWith("'") && value.endsWith("'")) ||
392+
(value.startsWith('`') && value.endsWith('`'))
393+
) {
394+
value = value.slice(1, -1)
395+
}
396+
362397
return { key: potentialKey, value, id: generateRowId() }
363398
}, [])
364399

0 commit comments

Comments
 (0)