From 3461e90d02605b95fbb263d6638397a0611f6611 Mon Sep 17 00:00:00 2001 From: Christoph Kempen Date: Sun, 25 Oct 2026 22:08:33 +0100 Subject: [PATCH] fix: parse MIME subtypes containing dots and plus signs in full --- src/DocumentRoot.php | 2 +- test/DocumentRootTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/DocumentRoot.php b/src/DocumentRoot.php index a05a822..766315c 100644 --- a/src/DocumentRoot.php +++ b/src/DocumentRoot.php @@ -566,7 +566,7 @@ public function loadMimeFileTypes(string $mimeFile): void $mimeFile = \str_replace('\\', '/', $mimeFile); $contents = $this->filesystem->read($mimeFile); - if (!\preg_match_all('#\s*([a-z0-9]+)\s+([a-z0-9\-]+/[a-z0-9\-]+(?:\+[a-z0-9\-]+)?)#i', $contents, $matches)) { + if (!\preg_match_all('#\s*([a-z0-9]+)\s+([a-z0-9\-]+/[a-z0-9][a-z0-9.+\-]*)#i', $contents, $matches)) { throw new \Exception( "No mime associations found in file: {$mimeFile}" ); diff --git a/test/DocumentRootTest.php b/test/DocumentRootTest.php index f367f4b..9aff9c3 100644 --- a/test/DocumentRootTest.php +++ b/test/DocumentRootTest.php @@ -362,4 +362,36 @@ public function testMimetypeParsing(): void $stream = $response->getBody(); $this->assertSame("", $stream->read()); } + + public function testBundledMimeFileIsParsedInFull(): void + { + $this->root->loadMimeFileTypes(DocumentRoot::DEFAULT_MIME_TYPE_FILE); + + $property = new \ReflectionProperty(DocumentRoot::class, 'mimeFileTypes'); + $types = $property->getValue($this->root); + + $expected = []; + foreach (\file(DocumentRoot::DEFAULT_MIME_TYPE_FILE, \FILE_IGNORE_NEW_LINES | \FILE_SKIP_EMPTY_LINES) as $line) { + $this->assertSame(1, \preg_match('#^\s*(\S+)\s+(\S+)\s*$#', $line, $match), "Malformed line: {$line}"); + $expected[\strtolower($match[1])] = $match[2]; + } + + $this->assertNotEmpty($expected); + $this->assertSame($expected, $types); + } + + public function testDottedPlusAndDashSubtypesAreNotTruncated(): void + { + $this->root->loadMimeFileTypes(DocumentRoot::DEFAULT_MIME_TYPE_FILE); + + $property = new \ReflectionProperty(DocumentRoot::class, 'mimeFileTypes'); + $types = $property->getValue($this->root); + + $this->assertSame('application/vnd.ms-excel', $types['xls']); + $this->assertSame('image/svg+xml', $types['svg']); + $this->assertSame( + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + $types['docx'], + ); + } }