Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Dotenv] throw a meaningful exception when parsing dotenv files with BOM #58274

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

Merged
merged 1 commit into from
Sep 16, 2024
Merged
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
8 changes: 7 additions & 1 deletion 8 src/Symfony/Component/Dotenv/Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,13 @@ private function doLoad(bool $overrideExistingVars, array $paths): void
throw new PathException($path);
}

$this->populate($this->parse(file_get_contents($path), $path), $overrideExistingVars);
$data = file_get_contents($path);

if ("\xEF\xBB\xBF" === substr($data, 0, 3)) {
throw new FormatException('Loading files starting with a byte-order-mark (BOM) is not supported.', new FormatExceptionContext($data, $path, 1, 0));
}

$this->populate($this->parse($data, $path), $overrideExistingVars);
}
}
}
10 changes: 10 additions & 0 deletions 10 src/Symfony/Component/Dotenv/Tests/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -604,4 +604,14 @@ public function testBootEnv()
$resetContext();
rmdir($tmpdir);
}

public function testExceptionWithBom()
{
$dotenv = new Dotenv();

$this->expectException(FormatException::class);
$this->expectExceptionMessage('Loading files starting with a byte-order-mark (BOM) is not supported.');

$dotenv->load(__DIR__.'/fixtures/file_with_bom');
}
}
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Dotenv/Tests/fixtures/file_with_bom
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FOO=BAR
Morty Proxy This is a proxified and sanitized view of the page, visit original site.