Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed parsing of `.env` files with Windows (CRLF) line endings so carriage returns no longer leak into multi-line double-quoted values.
10 changes: 10 additions & 0 deletions src/functions/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
`,
want: { FOO: "foo1\nfoo2", BAR: "bar" },
},
{
description: "should normalize CRLF line endings (Windows)",
input: "FOO=foo\r\nBAR=bar\r\nBAZ=baz\r\n",
want: { FOO: "foo", BAR: "bar", BAZ: "baz" },
},
{
description: "should normalize CRLF inside double quoted, multi-line values (Windows)",
input: 'FOO="foo1\r\nfoo2\r\nfoo3"\r\nBAR=bar\r\n',
want: { FOO: "foo1\nfoo2\nfoo3", BAR: "bar" },
},
{
description: "should parse many double quoted values",
input: 'FOO="foo"\nBAR="bar"',
Expand Down Expand Up @@ -495,7 +505,7 @@
{ FOO: "bar", lowercase: "bar" },
{ projectId: "project", functionsSource: tmpdir },
);
} catch (err: any) {

Check warning on line 508 in src/functions/env.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unexpected any. Specify a different type
// no-op
}
expect(env.loadUserEnvs({ projectId: "project", functionsSource: tmpdir })["FOO"]).to.be
Expand Down Expand Up @@ -565,7 +575,7 @@

it("loads envs from .env.<alias> file", () => {
createEnvFiles(tmpdir, {
[`.env.${projectInfo.projectAlias}`]: "FOO=foo\nBAR=bar",

Check warning on line 578 in src/functions/env.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Invalid type "string | undefined" of template literal expression
});

expect(env.loadUserEnvs({ ...projectInfo, functionsSource: tmpdir })).to.be.deep.equal({
Expand Down Expand Up @@ -603,7 +613,7 @@
it("loads envs, preferring ones from .env.<alias>", () => {
createEnvFiles(tmpdir, {
".env": "FOO=bad\nBAR=bar",
[`.env.${projectInfo.projectAlias}`]: "FOO=good",

Check warning on line 616 in src/functions/env.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Invalid type "string | undefined" of template literal expression
});

expect(env.loadUserEnvs({ ...projectInfo, functionsSource: tmpdir })).to.be.deep.equal({
Expand All @@ -615,7 +625,7 @@
it("loads envs, preferring ones from .env.<alias> for emulators too", () => {
createEnvFiles(tmpdir, {
".env": "FOO=bad\nBAR=bar",
[`.env.${projectInfo.projectAlias}`]: "FOO=good",

Check warning on line 628 in src/functions/env.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Invalid type "string | undefined" of template literal expression
});

expect(
Expand Down Expand Up @@ -658,7 +668,7 @@
createEnvFiles(tmpdir, {
".env": "FOO=foo\nBAR=bar",
[`.env.${projectInfo.projectId}`]: "FOO=not-foo",
[`.env.${projectInfo.projectAlias}`]: "FOO=not-foo",

Check warning on line 671 in src/functions/env.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Invalid type "string | undefined" of template literal expression
});

expect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/functions/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
* EFG
* -----BEGIN PUBLIC KEY-----"
*
* See test for more examples.

Check warning on line 113 in src/functions/env.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Expected only 0 line after block description
*
* @return {ParseResult} Result containing parsed key, value pairs and errored lines.
*/
Expand All @@ -118,7 +118,7 @@
const envs: Record<string, string> = {};
const errors: string[] = [];

data = data.replace(/\r\n?/, "\n"); // For Windows support.
data = data.replace(/\r\n?/g, "\n"); // For Windows support.
let match;
while ((match = LINE_RE.exec(data))) {
let [, k, v] = match;
Expand Down Expand Up @@ -201,8 +201,8 @@
for (const key of Object.keys(envs)) {
try {
validateKey(key);
} catch (err: any) {

Check warning on line 204 in src/functions/env.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unexpected any. Specify a different type
logger.debug(`Failed to validate key ${key}: ${err}`);

Check warning on line 205 in src/functions/env.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Invalid type "any" of template literal expression
if (err instanceof KeyValidationError) {
validationErrors.push(err);
} else {
Expand Down Expand Up @@ -248,7 +248,7 @@
}

/**
* Checks if user has specified any environment variables for their functions.

Check warning on line 251 in src/functions/env.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Expected only 0 line after block description
*
* @return True if there are any user-specified environment variables
*/
Expand All @@ -263,7 +263,7 @@
* Identifies one and only one dotenv file to touch using the same rules as loadUserEnvs().
* It is an error to provide a key-value pair which is already in the file.
*/
export function writeUserEnvs(toWrite: Record<string, string>, envOpts: UserEnvsOpts) {

Check warning on line 266 in src/functions/env.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Missing return type on function
if (Object.keys(toWrite).length === 0) {
return;
}
Expand Down
Loading