Skip to content

Commit a356b94

Browse files
committed
Respect the attachment max file size limit for attachment file downloads
1 parent 27afa01 commit a356b94

3 files changed

Lines changed: 84 additions & 1 deletion

File tree

src/Services/Attachments/AttachmentSubmitHandler.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function __construct(
7878
protected FileTypeFilterTools $filterTools,
7979
protected AttachmentsSettings $settings,
8080
protected readonly SVGSanitizer $SVGSanitizer,
81+
private readonly AttachmentsSettings $attachmentsSettings,
8182
#[Autowire(env: "bool:ALLOW_ATTACHMENT_DOWNLOADS_FROM_LOCALNETWORK")]
8283
private readonly bool $allow_local_network_downloads = false,
8384
)
@@ -399,9 +400,30 @@ protected function downloadURL(Attachment $attachment, bool $secureAttachment):
399400
//Open a temporary file in the attachment folder
400401
$fs->mkdir($attachment_folder);
401402
$fileHandler = fopen($tmp_path, 'wb');
403+
404+
$bytesDownloaded = 0;
405+
$maxSize = $this->attachmentsSettings->getMaxFileSizeInMegabytes() * 1024 * 1024; //Convert to bytes
406+
402407
//Write the downloaded data to file
403408
foreach ($this->httpClient->stream($response) as $chunk) {
404-
fwrite($fileHandler, $chunk->getContent());
409+
$content = $chunk->getContent();
410+
$bytesDownloaded += strlen($content);
411+
412+
//Ensure the size does not get too large to avoid filling up the disk easily.
413+
//If the file is too big, cancel the download and delete the temporary file.
414+
if ($bytesDownloaded > $maxSize) {
415+
$response->cancel();
416+
fclose($fileHandler);
417+
unlink($tmp_path); //Delete the temporary file, because it is too big
418+
419+
throw new AttachmentDownloadException(
420+
sprintf(
421+
'The downloaded file is too big! Maximum size is %d MB!',
422+
$this->settings->getMaxFileSizeInMegabytes()
423+
));
424+
}
425+
426+
fwrite($fileHandler, $content);
405427
}
406428
fclose($fileHandler);
407429

src/Settings/SystemSettings/AttachmentsSettings.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,18 @@ class AttachmentsSettings
6565
envVar: "bool:ATTACHMENT_SHOW_HTML_FILES", envVarMode: EnvVarMode::OVERWRITE
6666
)]
6767
public bool $showHTMLAttachments = false;
68+
69+
public function getMaxFileSizeInMegabytes(): int
70+
{
71+
$size = $this->maxFileSize;
72+
$unit = strtoupper(substr($size, -1));
73+
$value = (int) rtrim($size, 'KMG');
74+
75+
return match ($unit) {
76+
'K' => (int) ceil($value / 1024),
77+
'M' => $value,
78+
'G' => $value * 1024,
79+
default => $value, // No unit specified, assume megabytes
80+
};
81+
}
6882
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
namespace App\Tests\Settings\SystemSettings;
22+
23+
use App\Settings\SystemSettings\AttachmentsSettings;
24+
use App\Tests\SettingsTestHelper;
25+
use PHPUnit\Framework\TestCase;
26+
27+
class AttachmentsSettingsTest extends TestCase
28+
{
29+
30+
public function testGetMaxFileSizeInMegabytes(): void
31+
{
32+
$settings = SettingsTestHelper::createSettingsDummy(AttachmentsSettings::class);
33+
34+
$settings->maxFileSize = '100M';
35+
$this->assertEquals(100, $settings->getMaxFileSizeInMegabytes());
36+
37+
$settings->maxFileSize = '1G';
38+
$this->assertEquals(1024, $settings->getMaxFileSizeInMegabytes());
39+
40+
//We round up to the next megabyte if the file size is smaller than 1 MB
41+
$settings->maxFileSize = '500K';
42+
$this->assertEquals(1, $settings->getMaxFileSizeInMegabytes());
43+
44+
$settings->maxFileSize = '13.5M';
45+
$this->assertEquals(13, $settings->getMaxFileSizeInMegabytes());
46+
}
47+
}

0 commit comments

Comments
 (0)