aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorskjnldsv <skjnldsv@protonmail.com>2025-02-05 11:51:10 +0100
committerskjnldsv <skjnldsv@protonmail.com>2025-03-04 09:29:12 +0100
commit1672a357bd219d0a164e27168bfbcc0e74fe9ca0 (patch)
tree2e10474fc9d1f608c7e39a3df570e03c06c8e16c
parentc4b98bf8e78ce40fdae002369a87d672dfc8b910 (diff)
downloadnextcloud-server-1672a357bd219d0a164e27168bfbcc0e74fe9ca0.tar.gz
nextcloud-server-1672a357bd219d0a164e27168bfbcc0e74fe9ca0.zip
fix: make sure we process mime extensions as string
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
-rw-r--r--core/Command/Maintenance/Mimetype/GenerateMimetypeFileBuilder.php4
-rw-r--r--core/Command/Maintenance/Mimetype/UpdateDB.php4
-rw-r--r--lib/private/Files/Type/Detection.php14
-rw-r--r--tests/lib/Files/Type/DetectionTest.php35
4 files changed, 53 insertions, 4 deletions
diff --git a/core/Command/Maintenance/Mimetype/GenerateMimetypeFileBuilder.php b/core/Command/Maintenance/Mimetype/GenerateMimetypeFileBuilder.php
index 873744e6f94..db2f3e44f1c 100644
--- a/core/Command/Maintenance/Mimetype/GenerateMimetypeFileBuilder.php
+++ b/core/Command/Maintenance/Mimetype/GenerateMimetypeFileBuilder.php
@@ -37,6 +37,10 @@ class GenerateMimetypeFileBuilder {
public function generateFile(array $aliases): string {
// Remove comments
$aliases = array_filter($aliases, static function ($key) {
+ // Single digit extensions will be treated as integers
+ // Let's make sure they are strings
+ // https://github.com/nextcloud/server/issues/42902
+ $key = (string)$key;
return !($key === '' || $key[0] === '_');
}, ARRAY_FILTER_USE_KEY);
diff --git a/core/Command/Maintenance/Mimetype/UpdateDB.php b/core/Command/Maintenance/Mimetype/UpdateDB.php
index 212b1994263..26c0b3aba6e 100644
--- a/core/Command/Maintenance/Mimetype/UpdateDB.php
+++ b/core/Command/Maintenance/Mimetype/UpdateDB.php
@@ -62,6 +62,10 @@ class UpdateDB extends Command {
$totalNewMimetypes = 0;
foreach ($mappings as $ext => $mimetypes) {
+ // Single digit extensions will be treated as integers
+ // Let's make sure they are strings
+ // https://github.com/nextcloud/server/issues/42902
+ $ext = (string)$ext;
if ($ext[0] === '_') {
// comment
continue;
diff --git a/lib/private/Files/Type/Detection.php b/lib/private/Files/Type/Detection.php
index 71b8cb986d7..1ae03794430 100644
--- a/lib/private/Files/Type/Detection.php
+++ b/lib/private/Files/Type/Detection.php
@@ -98,6 +98,8 @@ class Detection implements IMimeTypeDetector {
public function registerType(string $extension,
string $mimetype,
?string $secureMimeType = null): void {
+ // Make sure the extension is a string
+ // https://github.com/nextcloud/server/issues/42902
$this->mimetypes[$extension] = [$mimetype, $secureMimeType];
$this->secureMimeTypes[$mimetype] = $secureMimeType ?: $mimetype;
}
@@ -112,13 +114,17 @@ class Detection implements IMimeTypeDetector {
* @param array $types
*/
public function registerTypeArray(array $types): void {
- $this->mimetypes = array_merge($this->mimetypes, $types);
+ // Register the types,
+ foreach ($types as $extension => $mimeType) {
+ $this->registerType((string)$extension, $mimeType[0], $mimeType[1] ?? null);
+ }
// Update the alternative mimetypes to avoid having to look them up each time.
foreach ($this->mimetypes as $extension => $mimeType) {
- if (str_starts_with($extension, '_comment')) {
+ if (str_starts_with((string)$extension, '_comment')) {
continue;
}
+
$this->secureMimeTypes[$mimeType[0]] = $mimeType[1] ?? $mimeType[0];
if (isset($mimeType[1])) {
$this->secureMimeTypes[$mimeType[1]] = $mimeType[1];
@@ -179,7 +185,7 @@ class Detection implements IMimeTypeDetector {
}
/**
- * @return array
+ * @return array<string, array{string, string|null}>
*/
public function getAllMappings(): array {
$this->loadMappings();
@@ -209,7 +215,7 @@ class Detection implements IMimeTypeDetector {
$extension = strrchr($fileName, '.');
if ($extension !== false) {
$extension = strtolower($extension);
- $extension = substr($extension, 1); //remove leading .
+ $extension = substr($extension, 1); // remove leading .
return $this->mimetypes[$extension][0] ?? 'application/octet-stream';
}
}
diff --git a/tests/lib/Files/Type/DetectionTest.php b/tests/lib/Files/Type/DetectionTest.php
index 079117f5622..37bab1a46c0 100644
--- a/tests/lib/Files/Type/DetectionTest.php
+++ b/tests/lib/Files/Type/DetectionTest.php
@@ -114,6 +114,41 @@ class DetectionTest extends \Test\TestCase {
$this->assertEquals($expected, $result);
}
+ public function dataMimeTypeCustom(): array {
+ return [
+ ['123', 'foobar/123'],
+ ['a123', 'foobar/123'],
+ ['bar', 'foobar/bar'],
+ ];
+ }
+
+ /**
+ * @dataProvider dataMimeTypeCustom
+ *
+ * @param string $ext
+ * @param string $mime
+ */
+ public function testDetectMimeTypeCustom(string $ext, string $mime): void {
+ $confDir = sys_get_temp_dir();
+ file_put_contents($confDir . '/mimetypemapping.dist.json', json_encode([]));
+
+ /** @var IURLGenerator $urlGenerator */
+ $urlGenerator = $this->getMockBuilder(IURLGenerator::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ /** @var LoggerInterface $logger */
+ $logger = $this->createMock(LoggerInterface::class);
+
+ // Create new mapping file
+ file_put_contents($confDir . '/mimetypemapping.dist.json', json_encode([$ext => [$mime]]));
+
+ $detection = new Detection($urlGenerator, $logger, $confDir, $confDir);
+ $mappings = $detection->getAllMappings();
+ $this->assertArrayHasKey($ext, $mappings);
+ $this->assertEquals($mime, $detection->detectPath('foo.' . $ext));
+ }
+
public function dataGetSecureMimeType(): array {
return [
['image/svg+xml', 'text/plain'],