fix: escape env values written to the Compose .env file so $, #, and quotes survive literally

This commit is contained in:
Narciso
2026-08-03 17:57:02 -04:00
parent 46c87a22b0
commit ff39cb51e6
4 changed files with 162 additions and 2 deletions
@@ -7,7 +7,7 @@ import { writeDomainsToCompose } from "../docker/domain";
import {
encodeBase64,
getEnvironmentVariablesObject,
prepareEnvironmentVariables,
prepareEnvironmentVariablesForFile,
} from "../docker/utils";
export type ComposeNested = InferResultType<
@@ -127,7 +127,7 @@ export const getCreateEnvFileCommand = (compose: ComposeNested) => {
envContent += `\nCOMPOSE_PREFIX=${compose.suffix}`;
}
const envFileContent = prepareEnvironmentVariables(
const envFileContent = prepareEnvironmentVariablesForFile(
envContent,
compose.environment.project.env,
compose.environment.env,
+21
View File
@@ -465,6 +465,27 @@ export const prepareEnvironmentVariablesForShell = (
return envVars.map((env) => quote([env]));
};
export const prepareEnvironmentVariablesForFile = (
serviceEnv: string | null,
projectEnv?: string | null,
environmentEnv?: string | null,
): string[] => {
const envVars = prepareEnvironmentVariables(
serviceEnv,
projectEnv,
environmentEnv,
);
return envVars.map((pair) => {
const [key, value] = parseEnvironmentKeyValuePair(pair);
const escapedValue = value
.replace(/\\/g, "\\\\")
.replace(/"/g, '\\"')
.replace(/\$/g, "\\$");
return `${key}="${escapedValue}"`;
});
};
export const parseEnvironmentKeyValuePair = (
pair: string,
): [string, string] => {