Skip to content

Commit 31f0870

Browse files
committed
Escape env vars when exporting to file
1 parent e7c1337 commit 31f0870

File tree

1 file changed

+12
-1
lines changed
  • packages/cli-v3/src/commands

1 file changed

+12
-1
lines changed

packages/cli-v3/src/commands/env.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,9 @@ async function _envPullCommand(options: z.infer<typeof EnvPullOptions>) {
403403
}
404404
}
405405

406-
const envContent = userVariables.map(([key, value]) => `${key}=${value || ""}`).join("\n");
406+
const envContent = userVariables
407+
.map(([key, value]) => `${key}=${serializeDotenvValue(value)}`)
408+
.join("\n");
407409

408410
$spinner.start(`Writing to ${options.output}`);
409411
const [writeError] = await tryCatch(
@@ -428,3 +430,12 @@ async function _envPullCommand(options: z.infer<typeof EnvPullOptions>) {
428430
const envInfo = branch ? `${env} (${branch})` : env;
429431
outro(`Project: ${projectRef} | Environment: ${envInfo}`);
430432
}
433+
434+
const serializeDotenvValue = (v: unknown): string => {
435+
if (v == null || v === undefined) return "";
436+
437+
const s = String(v);
438+
// Quote when unsafe chars present: whitespace, equals, newlines, comments, quotes, backslashes
439+
const needsQuotes = /[\s#"'`\\=\n\r]/.test(s) || s === "";
440+
return needsQuotes ? JSON.stringify(s) : s;
441+
};

0 commit comments

Comments
 (0)