Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/translators.txt
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,5 @@ Tahsin Ahmed (tahsinahmed2012) :: Bengali
bojan_che :: Serbian (Cyrillic)
setiawan setiawan (culture.setiawan) :: Indonesian
Donald Mac Kenzie (kiuman) :: Norwegian Bokmal
Gabriel Silver (GabrielBSilver) :: Hebrew
Tomas Darius Davainis (Tomasdd) :: Lithuanian
21 changes: 21 additions & 0 deletions app/Exports/ZipExports/ZipExportReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public function readData(): array
{
$this->open();

$info = $this->zip->statName('data.json');
if ($info === false) {
throw new ZipExportException(trans('errors.import_zip_cant_decode_data'));
}

$maxSize = max(intval(config()->get('app.upload_limit')), 1) * 1000000;
if ($info['size'] > $maxSize) {
throw new ZipExportException(trans('errors.import_zip_data_too_large'));
}

// Validate json data exists, including metadata
$jsonData = $this->zip->getFromName('data.json') ?: '';
$importData = json_decode($jsonData, true);
Expand All @@ -73,6 +83,17 @@ public function fileExists(string $fileName): bool
return $this->zip->statName("files/{$fileName}") !== false;
}

public function fileWithinSizeLimit(string $fileName): bool
{
$fileInfo = $this->zip->statName("files/{$fileName}");
if ($fileInfo === false) {
return false;
}

$maxSize = max(intval(config()->get('app.upload_limit')), 1) * 1000000;
return $fileInfo['size'] <= $maxSize;
}

/**
* @return false|resource
*/
Expand Down
8 changes: 7 additions & 1 deletion app/Exports/ZipExports/ZipFileReferenceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public function __construct(
) {
}


/**
* @inheritDoc
*/
Expand All @@ -23,6 +22,13 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
$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)) {
Expand Down
6 changes: 6 additions & 0 deletions app/Exports/ZipExports/ZipImportRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ protected function exportTagsToInputArray(array $exportTags): array

protected function zipFileToUploadedFile(string $fileName, ZipExportReader $reader): UploadedFile
{
if (!$reader->fileWithinSizeLimit($fileName)) {
throw new ZipImportException([
"File $fileName exceeds app upload limit."
]);
}

$tempPath = tempnam(sys_get_temp_dir(), 'bszipextract');
$fileStream = $reader->streamFile($fileName);
$tempStream = fopen($tempPath, 'wb');
Expand Down
5 changes: 3 additions & 2 deletions app/Search/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ public function searchForSelector(Request $request, QueryPopular $queryPopular)

// Search for entities otherwise show most popular
if ($searchTerm !== false) {
$searchTerm .= ' {type:' . implode('|', $entityTypes) . '}';
$entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20)['results'];
$options = SearchOptions::fromString($searchTerm);
$options->setFilter('type', implode('|', $entityTypes));
$entities = $this->searchRunner->searchEntities($options, 'all', 1, 20)['results'];
} else {
$entities = $queryPopular->run(20, 0, $entityTypes);
}
Expand Down
8 changes: 8 additions & 0 deletions app/Search/SearchOptionSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,12 @@ public function nonNegated(): self
$values = array_values(array_filter($this->options, fn (SearchOption $option) => !$option->negated));
return new self($values);
}

/**
* @return self<T>
*/
public function limit(int $limit): self
{
return new self(array_slice(array_values($this->options), 0, $limit));
}
}
22 changes: 22 additions & 0 deletions app/Search/SearchOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static function fromString(string $search): self
{
$instance = new self();
$instance->addOptionsFromString($search);
$instance->limitOptions();
return $instance;
}

Expand Down Expand Up @@ -87,6 +88,8 @@ public static function fromRequest(Request $request): self
$instance->filters = $instance->filters->merge($extras->filters);
}

$instance->limitOptions();

return $instance;
}

Expand Down Expand Up @@ -147,6 +150,25 @@ protected function addOptionsFromString(string $searchString): void
$this->filters = $this->filters->merge(new SearchOptionSet($terms['filters']));
}

/**
* Limit the amount of search options to reasonable levels.
* Provides higher limits to logged-in users since that signals a slightly
* higher level of trust.
*/
protected function limitOptions(): void
{
$userLoggedIn = !user()->isGuest();
$searchLimit = $userLoggedIn ? 10 : 5;
$exactLimit = $userLoggedIn ? 4 : 2;
$tagLimit = $userLoggedIn ? 8 : 4;
$filterLimit = $userLoggedIn ? 10 : 5;

$this->searches = $this->searches->limit($searchLimit);
$this->exacts = $this->exacts->limit($exactLimit);
$this->tags = $this->tags->limit($tagLimit);
$this->filters = $this->filters->limit($filterLimit);
}

/**
* Decode backslash escaping within the input string.
*/
Expand Down
64 changes: 32 additions & 32 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lang/ar/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
'import_zip_cant_read' => 'لم أتمكن من قراءة المِلَفّ المضغوط -ZIP-.',
'import_zip_cant_decode_data' => 'لم نتمكن من العثور على محتوى المِلَفّ المضغوط data.json وفك تشفيره.',
'import_zip_no_data' => 'لا تتضمن بيانات المِلَفّ المضغوط أي محتوى متوقع للكتاب أو الفصل أو الصفحة.',
'import_zip_data_too_large' => 'ZIP data.json content exceeds the configured application maximum upload size.',
'import_validation_failed' => 'فشل التحقق من صحة استيراد المِلَفّ المضغوط بسبب الأخطاء التالية:',
'import_zip_failed_notification' => 'فشل استيراد المِلَفّ المضغوط.',
'import_perms_books' => 'أنت تفتقر إلى الصلاحيات المطلوبة لإنشاء الكتب.',
Expand Down
1 change: 1 addition & 0 deletions lang/ar/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
'uploaded' => 'تعذر تحميل الملف. قد لا يقبل الخادم ملفات بهذا الحجم.',

'zip_file' => ':attribute بحاجة إلى الرجوع إلى مِلَفّ داخل المِلَفّ المضغوط.',
'zip_file_size' => 'The file :attribute must not exceed :size MB.',
'zip_file_mime' => ':attribute بحاجة إلى الإشارة إلى مِلَفّ من نوع :validTypes، وجدت :foundType.',
'zip_model_expected' => 'عنصر البيانات المتوقع ولكن ":type" تم العثور عليه.',
'zip_unique' => 'يجب أن يكون :attribute فريداً لنوع الكائن داخل المِلَفّ المضغوط.',
Expand Down
1 change: 1 addition & 0 deletions lang/bg/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
'import_zip_cant_read' => 'Could not read ZIP file.',
'import_zip_cant_decode_data' => 'Could not find and decode ZIP data.json content.',
'import_zip_no_data' => 'ZIP file data has no expected book, chapter or page content.',
'import_zip_data_too_large' => 'ZIP data.json content exceeds the configured application maximum upload size.',
'import_validation_failed' => 'Import ZIP failed to validate with errors:',
'import_zip_failed_notification' => 'Failed to import ZIP file.',
'import_perms_books' => 'You are lacking the required permissions to create books.',
Expand Down
1 change: 1 addition & 0 deletions lang/bg/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
'uploaded' => 'Файлът не можа да бъде качен. Сървърът може да не приема файлове с такъв размер.',

'zip_file' => 'The :attribute needs to reference a file within the ZIP.',
'zip_file_size' => 'The file :attribute must not exceed :size MB.',
'zip_file_mime' => 'The :attribute needs to reference a file of type :validTypes, found :foundType.',
'zip_model_expected' => 'Data object expected but ":type" found.',
'zip_unique' => 'The :attribute must be unique for the object type within the ZIP.',
Expand Down
1 change: 1 addition & 0 deletions lang/bn/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
'import_zip_cant_read' => 'Could not read ZIP file.',
'import_zip_cant_decode_data' => 'Could not find and decode ZIP data.json content.',
'import_zip_no_data' => 'ZIP file data has no expected book, chapter or page content.',
'import_zip_data_too_large' => 'ZIP data.json content exceeds the configured application maximum upload size.',
'import_validation_failed' => 'Import ZIP failed to validate with errors:',
'import_zip_failed_notification' => 'Failed to import ZIP file.',
'import_perms_books' => 'You are lacking the required permissions to create books.',
Expand Down
1 change: 1 addition & 0 deletions lang/bn/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
'uploaded' => 'The file could not be uploaded. The server may not accept files of this size.',

'zip_file' => 'The :attribute needs to reference a file within the ZIP.',
'zip_file_size' => 'The file :attribute must not exceed :size MB.',
'zip_file_mime' => 'The :attribute needs to reference a file of type :validTypes, found :foundType.',
'zip_model_expected' => 'Data object expected but ":type" found.',
'zip_unique' => 'The :attribute must be unique for the object type within the ZIP.',
Expand Down
1 change: 1 addition & 0 deletions lang/bs/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
'import_zip_cant_read' => 'Could not read ZIP file.',
'import_zip_cant_decode_data' => 'Could not find and decode ZIP data.json content.',
'import_zip_no_data' => 'ZIP file data has no expected book, chapter or page content.',
'import_zip_data_too_large' => 'ZIP data.json content exceeds the configured application maximum upload size.',
'import_validation_failed' => 'Import ZIP failed to validate with errors:',
'import_zip_failed_notification' => 'Failed to import ZIP file.',
'import_perms_books' => 'You are lacking the required permissions to create books.',
Expand Down
1 change: 1 addition & 0 deletions lang/bs/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
'uploaded' => 'Fajl nije učitan. Server ne prihvata fajlove ove veličine.',

'zip_file' => 'The :attribute needs to reference a file within the ZIP.',
'zip_file_size' => 'The file :attribute must not exceed :size MB.',
'zip_file_mime' => 'The :attribute needs to reference a file of type :validTypes, found :foundType.',
'zip_model_expected' => 'Data object expected but ":type" found.',
'zip_unique' => 'The :attribute must be unique for the object type within the ZIP.',
Expand Down
1 change: 1 addition & 0 deletions lang/ca/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
'import_zip_cant_read' => 'No es pot llegir el fitxer ZIP.',
'import_zip_cant_decode_data' => 'No s\'ha pogut trobar i descodificar el fitxer data.json en el fitxer ZIP.',
'import_zip_no_data' => 'Les dades del fitxer ZIP no contenen cap llibre, capítol o contingut de pàgina.',
'import_zip_data_too_large' => 'ZIP data.json content exceeds the configured application maximum upload size.',
'import_validation_failed' => 'Error en validar la importació del ZIP amb els errors:',
'import_zip_failed_notification' => 'Error en importar l\'arxiu ZIP.',
'import_perms_books' => 'Li falten els permisos necessaris per crear llibres.',
Expand Down
Loading