Skip to content
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

fix(cli): escape single-quotes in dotenv key-values #1235

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ afterEach(() => {
});

describe("replaceAllPlaceholderWithEnv", () => {
test("scriptPlaceholder (1)", () => {
test("scriptPlaceholder with basic code and env values", () => {
// arrange
const code = `JSON.parse('"import_meta_env_placeholder"')`;
const env = {
Expand All @@ -22,7 +22,7 @@ describe("replaceAllPlaceholderWithEnv", () => {
);
});

test("scriptPlaceholder (2)", () => {
test("scriptPlaceholder with slashes for double quotes [1]", () => {
// arrange
const code = `JSON.parse('\\"import_meta_env_placeholder\\"')`;

Expand All @@ -40,7 +40,7 @@ describe("replaceAllPlaceholderWithEnv", () => {
);
});

test("scriptPlaceholder (3)", () => {
test("scriptPlaceholder with slashes for double quotes [2] and single quotes [1]", () => {
// arrange
const code = `JSON.parse(\\'\\\\"import_meta_env_placeholder\\\\"\\')`;

Expand All @@ -58,20 +58,38 @@ describe("replaceAllPlaceholderWithEnv", () => {
);
});

test("scriptPlaceholder (4)", () => {
test("scriptPlaceholder with escaped double quotes", () => {
// arrange
const code = `JSON.parse('"import_meta_env_placeholder"')`;
const env = {
KEY1: '{"child1":"value1"}',
KEY2: '{"child2":"value2"}',
KEY3: '["value1", "value2"]',
};

// act
const result = replaceAllPlaceholderWithEnv({ code, env });

// assert
expect(result).toMatchInlineSnapshot(
`"JSON.parse('{"KEY1":"{\\\\\\"child1\\\\\\":\\\\\\"value1\\\\\\"}","KEY2":"{\\\\\\"child2\\\\\\":\\\\\\"value2\\\\\\"}"}')"`,
`"JSON.parse('{"KEY1":"{\\\\\\"child1\\\\\\":\\\\\\"value1\\\\\\"}","KEY2":"{\\\\\\"child2\\\\\\":\\\\\\"value2\\\\\\"}","KEY3":"[\\\\\\"value1\\\\\\", \\\\\\"value2\\\\\\"]"}')"`
);
});

test("scriptPlaceholder with escaped single quotes", () => {
// arrange
const code = `JSON.parse('"import_meta_env_placeholder"')`;
const env = {
KEY1: "['value3', 'value4']",
KEY2: "This has 'single quotes' inside",
};

// act
const result = replaceAllPlaceholderWithEnv({ code, env });

// assert
expect(result).toMatchInlineSnapshot(
`"JSON.parse('{"KEY1":"[\\\'value3\\\', \\\'value4\\\']","KEY2":"This has \\\'single quotes\\\' inside"}')"`
);
});
});
9 changes: 5 additions & 4 deletions packages/cli/src/replace-all-placeholder-with-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,29 @@ export const replaceAllPlaceholderWithEnv = ({
}): string => {
const escapedEnv: Record<string, string> = {};
for (const key of Object.keys(env)) {
escapedEnv[key] = env[key].replace(/"/g, '\\"');
escapedEnv[key] = env[key].replace(/"/g, '\\"').replace(/'/g, "\\'");
}
const serializedEscapedEnv = serialize(escapedEnv).replace(/\\'/g, "'");
return code
.replace(
createScriptPlaceholderRegExp({
doubleQuoteSlashCount: 2,
singleQuoteSlashCount: 1,
}),
`JSON.parse(\\'${serialize(escapedEnv).replace(/"/g, '\\\\"')}\\')`,
`JSON.parse(\\'${serializedEscapedEnv.replace(/"/g, '\\\\"')}\\')`
)
.replace(
createScriptPlaceholderRegExp({
doubleQuoteSlashCount: 1,
singleQuoteSlashCount: 0,
}),
`JSON.parse('${serialize(escapedEnv).replace(/"/g, '\\"')}')`,
`JSON.parse('${serializedEscapedEnv.replace(/"/g, '\\"')}')`
)
.replace(
createScriptPlaceholderRegExp({
doubleQuoteSlashCount: 0,
singleQuoteSlashCount: 0,
}),
`JSON.parse('${serialize(escapedEnv)}')`,
`JSON.parse('${serializedEscapedEnv}')`
);
};