Skip to content

Commit bb4fef2

Browse files
committed
throw a meaningful exception when parsing dotenv files with BOM
1 parent 974e6b8 commit bb4fef2

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Dotenv.php

+7-1
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
}

Tests/DotenvTest.php

+10
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
}

Tests/fixtures/file_with_bom

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FOO=BAR

0 commit comments

Comments
 (0)