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

Commit dcb07df

Browse filesBrowse files
bug #58274 [Dotenv] throw a meaningful exception when parsing dotenv files with BOM (xabbuh)
This PR was merged into the 5.4 branch. Discussion ---------- [Dotenv] throw a meaningful exception when parsing dotenv files with BOM | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #58214 | License | MIT This replaces #58216. Other than in that PR I did not account for UTF-16 and UTF-32 byte-order-mark variants as that would IMO require us to first reliably detect the file encoding. Commits ------- b4f3eaa throw a meaningful exception when parsing dotenv files with BOM
2 parents b2201c3 + b4f3eaa commit dcb07df
Copy full SHA for dcb07df

File tree

3 files changed

+18
-1
lines changed
Filter options

3 files changed

+18
-1
lines changed

‎src/Symfony/Component/Dotenv/Dotenv.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Dotenv/Dotenv.php
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,13 @@ private function doLoad(bool $overrideExistingVars, array $paths): void
568568
throw new PathException($path);
569569
}
570570

571-
$this->populate($this->parse(file_get_contents($path), $path), $overrideExistingVars);
571+
$data = file_get_contents($path);
572+
573+
if ("\xEF\xBB\xBF" === substr($data, 0, 3)) {
574+
throw new FormatException('Loading files starting with a byte-order-mark (BOM) is not supported.', new FormatExceptionContext($data, $path, 1, 0));
575+
}
576+
577+
$this->populate($this->parse($data, $path), $overrideExistingVars);
572578
}
573579
}
574580
}

‎src/Symfony/Component/Dotenv/Tests/DotenvTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Dotenv/Tests/DotenvTest.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,4 +604,14 @@ public function testBootEnv()
604604
$resetContext();
605605
rmdir($tmpdir);
606606
}
607+
608+
public function testExceptionWithBom()
609+
{
610+
$dotenv = new Dotenv();
611+
612+
$this->expectException(FormatException::class);
613+
$this->expectExceptionMessage('Loading files starting with a byte-order-mark (BOM) is not supported.');
614+
615+
$dotenv->load(__DIR__.'/fixtures/file_with_bom');
616+
}
607617
}
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FOO=BAR

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.