mirror of
https://github.com/BookStackApp/BookStack.git
synced 2026-02-25 03:10:24 +03:00
Checks files within the ZIP again the app upload file limit before using/streaming/extracting, to help ensure that they do no exceed what might be expected on that instance, and to prevent disk exhaustion via things like super high compression ratio files. Thanks to Jeong Woo Lee (eclipse07077-ljw) for reporting.
44 lines
1.2 KiB
PHP
44 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Exports\ZipExports;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
class ZipFileReferenceRule implements ValidationRule
|
|
{
|
|
public function __construct(
|
|
protected ZipValidationHelper $context,
|
|
protected array $acceptedMimes,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
if (!$this->context->zipReader->fileExists($value)) {
|
|
$fail('validation.zip_file')->translate();
|
|
}
|
|
|
|
if (!$this->context->zipReader->fileWithinSizeLimit($value)) {
|
|
$fail('validation.zip_file_size')->translate([
|
|
'attribute' => $value,
|
|
'size' => config('app.upload_limit'),
|
|
]);
|
|
}
|
|
|
|
if (!empty($this->acceptedMimes)) {
|
|
$fileMime = $this->context->zipReader->sniffFileMime($value);
|
|
if (!in_array($fileMime, $this->acceptedMimes)) {
|
|
$fail('validation.zip_file_mime')->translate([
|
|
'attribute' => $attribute,
|
|
'validTypes' => implode(',', $this->acceptedMimes),
|
|
'foundType' => $fileMime
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|