-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: quote values containing hash (#) in .env file serialization #7380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chirag-bruno
wants to merge
2
commits into
usebruno:main
Choose a base branch
from
chirag-bruno:fix/env-hash-character-escaping
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 2 additions & 11 deletions
13
packages/bruno-app/src/components/Environments/DotEnvFileEditor/utils.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import { jsonToDotenv } from './jsonToDotenv'; | ||
| import * as dotenv from 'dotenv'; | ||
|
|
||
| // Helper to parse .env content using dotenv package | ||
| const dotenvToJson = (content: string): Record<string, string> => { | ||
| return dotenv.parse(Buffer.from(content)); | ||
| }; | ||
|
|
||
| describe('jsonToDotenv', () => { | ||
| describe('basic serialization', () => { | ||
| test('it should serialize simple variables', () => { | ||
| const variables = [ | ||
| { name: 'FOO', value: 'bar' }, | ||
| { name: 'BAZ', value: 'qux' } | ||
| ]; | ||
| const output = jsonToDotenv(variables); | ||
| expect(output).toBe('FOO=bar\nBAZ=qux'); | ||
| }); | ||
|
|
||
| test('it should filter out variables with empty names', () => { | ||
| const variables = [ | ||
| { name: 'VALID', value: 'value' }, | ||
| { name: '', value: 'ignored' }, | ||
| { name: ' ', value: 'also ignored' } | ||
| ]; | ||
| const output = jsonToDotenv(variables); | ||
| expect(output).toBe('VALID=value'); | ||
| }); | ||
|
|
||
| test('it should handle empty values', () => { | ||
| const variables = [ | ||
| { name: 'EMPTY', value: '' }, | ||
| { name: 'UNDEFINED', value: undefined } | ||
| ]; | ||
| const output = jsonToDotenv(variables); | ||
| expect(output).toBe('EMPTY=\nUNDEFINED='); | ||
| }); | ||
|
|
||
| test('it should return empty string for empty array', () => { | ||
| expect(jsonToDotenv([])).toBe(''); | ||
| }); | ||
|
|
||
| test('it should return empty string for non-array input', () => { | ||
| expect(jsonToDotenv(null as any)).toBe(''); | ||
| expect(jsonToDotenv(undefined as any)).toBe(''); | ||
| expect(jsonToDotenv({} as any)).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('special character handling', () => { | ||
| test('it should quote values containing hash (#)', () => { | ||
| const variables = [ | ||
| { name: 'PASSWORD', value: 'ABC#DEF' }, | ||
| { name: 'SECRET', value: 'key#123' } | ||
| ]; | ||
| const output = jsonToDotenv(variables); | ||
| expect(output).toBe('PASSWORD="ABC#DEF"\nSECRET="key#123"'); | ||
| }); | ||
|
|
||
| test('it should quote values containing newlines and escape them', () => { | ||
| const variables = [{ name: 'MULTILINE', value: 'line1\nline2' }]; | ||
| const output = jsonToDotenv(variables); | ||
| expect(output).toBe('MULTILINE="line1\\nline2"'); | ||
| }); | ||
|
|
||
| test('it should quote and escape values containing double quotes', () => { | ||
| const variables = [{ name: 'QUOTED', value: 'say "hello"' }]; | ||
| const output = jsonToDotenv(variables); | ||
| expect(output).toBe('QUOTED="say \\"hello\\""'); | ||
| }); | ||
|
|
||
| test('it should quote values containing single quotes', () => { | ||
| const variables = [{ name: 'APOSTROPHE', value: 'it\'s fine' }]; | ||
| const output = jsonToDotenv(variables); | ||
| expect(output).toBe('APOSTROPHE="it\'s fine"'); | ||
| }); | ||
|
|
||
| test('it should quote and escape values containing backslashes', () => { | ||
| const variables = [{ name: 'PATH', value: 'C:\\Users\\name' }]; | ||
| const output = jsonToDotenv(variables); | ||
| expect(output).toBe('PATH="C:\\\\Users\\\\name"'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('round-trip with dotenvToJson', () => { | ||
| test('it should preserve simple values through round-trip', () => { | ||
| const variables = [ | ||
| { name: 'FOO', value: 'bar' }, | ||
| { name: 'BAZ', value: 'qux123' } | ||
| ]; | ||
| const serialized = jsonToDotenv(variables); | ||
| const parsed = dotenvToJson(serialized); | ||
| expect(parsed.FOO).toBe('bar'); | ||
| expect(parsed.BAZ).toBe('qux123'); | ||
| }); | ||
|
|
||
| test('it should preserve values with hash (#) through round-trip', () => { | ||
| const variables = [ | ||
| { name: 'PASSWORD', value: 'ABC#DEF' }, | ||
| { name: 'API_KEY', value: 'key#123#456' }, | ||
| { name: 'HASH_START', value: '#startsWithHash' }, | ||
| { name: 'HASH_SPACE', value: 'value # comment-like' } | ||
| ]; | ||
| const serialized = jsonToDotenv(variables); | ||
| const parsed = dotenvToJson(serialized); | ||
| expect(parsed.PASSWORD).toBe('ABC#DEF'); | ||
| expect(parsed.API_KEY).toBe('key#123#456'); | ||
| expect(parsed.HASH_START).toBe('#startsWithHash'); | ||
| expect(parsed.HASH_SPACE).toBe('value # comment-like'); | ||
| }); | ||
|
|
||
| test('it should preserve values with single quotes through round-trip', () => { | ||
| const variables = [{ name: 'APOSTROPHE', value: 'it\'s working' }]; | ||
| const serialized = jsonToDotenv(variables); | ||
| const parsed = dotenvToJson(serialized); | ||
| expect(parsed.APOSTROPHE).toBe('it\'s working'); | ||
| }); | ||
|
|
||
| test('it should preserve empty values through round-trip', () => { | ||
| const variables = [{ name: 'EMPTY', value: '' }]; | ||
| const serialized = jsonToDotenv(variables); | ||
| const parsed = dotenvToJson(serialized); | ||
| expect(parsed.EMPTY).toBe(''); | ||
| }); | ||
|
|
||
| test('it should handle complex real-world passwords', () => { | ||
| const variables = [ | ||
| { name: 'DB_PASSWORD', value: 'P@ss#w0rd!123' }, | ||
| { name: 'API_SECRET', value: 'abc#def$ghi%jkl' }, | ||
| { name: 'JWT_SECRET', value: 'secret-key#with-special_chars' } | ||
| ]; | ||
| const serialized = jsonToDotenv(variables); | ||
| const parsed = dotenvToJson(serialized); | ||
| expect(parsed.DB_PASSWORD).toBe('P@ss#w0rd!123'); | ||
| expect(parsed.API_SECRET).toBe('abc#def$ghi%jkl'); | ||
| expect(parsed.JWT_SECRET).toBe('secret-key#with-special_chars'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| export interface DotenvVariable { | ||
| name: string; | ||
| value?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Serializes an array of environment variables to .env file format. | ||
| * | ||
| * This is the inverse of dotenvToJson - it converts a variables array | ||
| * back to .env file content that can be parsed by the dotenv package. | ||
| * | ||
| * Values containing special characters are wrapped in double quotes: | ||
| * - newlines (\n): would break the line-based format | ||
| * - double quotes ("): need escaping | ||
| * - single quotes ('): need escaping | ||
| * - backslashes (\): need escaping | ||
| * - hash (#): would be interpreted as comment start by dotenv parser | ||
| */ | ||
| export const jsonToDotenv = (variables: DotenvVariable[]): string => { | ||
| if (!Array.isArray(variables)) { | ||
| return ''; | ||
| } | ||
|
|
||
| return variables | ||
| .filter((v) => v.name && v.name.trim() !== '') | ||
| .map((v) => { | ||
| const value = v.value || ''; | ||
| // If value contains special characters, wrap in quotes | ||
| if (value.includes('\n') || value.includes('"') || value.includes('\'') || value.includes('\\') || value.includes('#')) { | ||
| // Escape backslashes first, then double quotes, then newlines | ||
| const escapedValue = value.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n'); | ||
| return `${v.name}="${escapedValue}"`; | ||
| } | ||
| return `${v.name}=${value}`; | ||
| }) | ||
| .join('\n'); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.