|
3 | 3 | namespace ProcessMaker\Http\Controllers\Api; |
4 | 4 |
|
5 | 5 | use Exception; |
6 | | -use Illuminate\Contracts\Routing\ResponseFactory; |
7 | 6 | use Illuminate\Http\JsonResponse; |
8 | 7 | use Illuminate\Http\Request; |
9 | 8 | use Illuminate\Http\Resources\Json\ResourceCollection; |
10 | 9 | use Illuminate\Http\Response; |
11 | 10 | use Illuminate\Http\UploadedFile; |
12 | | -use Illuminate\Support\Facades\Auth; |
13 | | -use Illuminate\Support\Facades\Storage; |
14 | 11 | use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException; |
15 | 12 | use Pion\Laravel\ChunkUpload\Handler\AbstractHandler; |
16 | | -use Pion\Laravel\ChunkUpload\Handler\HandlerFactory; |
17 | 13 | use Pion\Laravel\ChunkUpload\Receiver\FileReceiver; |
18 | 14 | use ProcessMaker\Events\FilesAccessed; |
19 | 15 | use ProcessMaker\Events\FilesCreated; |
20 | 16 | use ProcessMaker\Events\FilesDeleted; |
21 | 17 | use ProcessMaker\Events\FilesDownloaded; |
22 | 18 | use ProcessMaker\Http\Controllers\Controller; |
23 | 19 | use ProcessMaker\Http\Resources\ApiCollection; |
24 | | -use ProcessMaker\Http\Resources\ApiResource; |
25 | 20 | use ProcessMaker\Models\Media; |
26 | 21 | use ProcessMaker\Models\ProcessRequest; |
27 | 22 | use ProcessMaker\Models\TaskDraft; |
| 23 | +use ProcessMaker\Traits\ValidatesFileTrait; |
28 | 24 | use Spatie\MediaLibrary\MediaCollections\Exceptions\FileIsTooBig; |
29 | 25 |
|
30 | 26 | class ProcessRequestFileController extends Controller |
31 | 27 | { |
| 28 | + use ValidatesFileTrait; |
| 29 | + |
32 | 30 | /** |
33 | 31 | * A whitelist of attributes that should not be |
34 | 32 | * sanitized by our SanitizeInput middleware. |
@@ -439,126 +437,4 @@ public function destroy(Request $laravel_request, ProcessRequest $request, $file |
439 | 437 |
|
440 | 438 | return response([], 204); |
441 | 439 | } |
442 | | - |
443 | | - /** |
444 | | - * Validate uploaded file for security and type restrictions |
445 | | - * |
446 | | - * @param UploadedFile $file |
447 | | - * @param array $errors |
448 | | - * @return array |
449 | | - */ |
450 | | - private function validateFile(UploadedFile $file, &$errors) |
451 | | - { |
452 | | - // Explicitly reject archive files for security |
453 | | - if (config('files.enable_dangerous_validation')) { |
454 | | - $this->rejectArchiveFiles($file, $errors); |
455 | | - } |
456 | | - |
457 | | - // Validate file extension if enabled |
458 | | - if (config('files.enable_extension_validation')) { |
459 | | - $this->validateFileExtension($file, $errors); |
460 | | - } |
461 | | - |
462 | | - // Validate MIME type vs extension if enabled |
463 | | - if (config('files.enable_mime_validation')) { |
464 | | - $this->validateExtensionMimeTypeMatch($file, $errors); |
465 | | - } |
466 | | - |
467 | | - // Validate specific file types (e.g., PDF for JavaScript content) |
468 | | - if (strtolower($file->getClientOriginalExtension()) === 'pdf') { |
469 | | - $this->validatePDFFile($file, $errors); |
470 | | - } |
471 | | - |
472 | | - return $errors; |
473 | | - } |
474 | | - |
475 | | - /** |
476 | | - * Explicitly reject archive files for security reasons |
477 | | - * |
478 | | - * @param UploadedFile $file |
479 | | - * @param array $errors |
480 | | - * @return void |
481 | | - */ |
482 | | - private function rejectArchiveFiles(UploadedFile $file, &$errors) |
483 | | - { |
484 | | - $dangerousExtensions = config('files.dangerous_extensions'); |
485 | | - |
486 | | - $fileExtension = strtolower($file->getClientOriginalExtension()); |
487 | | - |
488 | | - if (in_array($fileExtension, $dangerousExtensions)) { |
489 | | - $errors['message'] = __('Uploaded file type is not allowed'); |
490 | | - |
491 | | - return; |
492 | | - } |
493 | | - |
494 | | - // Also check MIME types for archive files |
495 | | - $dangerousMimeTypes = config('files.dangerous_mime_types'); |
496 | | - |
497 | | - $fileMimeType = $file->getMimeType(); |
498 | | - |
499 | | - if (in_array($fileMimeType, $dangerousMimeTypes)) { |
500 | | - $errors['message'] = __('Uploaded mime file type is not allowed'); |
501 | | - } |
502 | | - } |
503 | | - |
504 | | - /** |
505 | | - * Validate that file extension matches the MIME type |
506 | | - * |
507 | | - * @param UploadedFile $file |
508 | | - * @param array $errors |
509 | | - * @return void |
510 | | - */ |
511 | | - private function validateExtensionMimeTypeMatch(UploadedFile $file, &$errors) |
512 | | - { |
513 | | - $fileExtension = strtolower($file->getClientOriginalExtension()); |
514 | | - $fileMimeType = $file->getMimeType(); |
515 | | - |
516 | | - // Get extension to MIME type mapping from configuration |
517 | | - $extensionMimeMap = config('files.extension_mime_map'); |
518 | | - |
519 | | - // Check if extension exists in our map |
520 | | - if (!isset($extensionMimeMap[$fileExtension])) { |
521 | | - $errors['message'] = __('File extension not allowed'); |
522 | | - |
523 | | - return; |
524 | | - } |
525 | | - |
526 | | - // Check if MIME type matches any of the expected types for this extension |
527 | | - if (!in_array($fileMimeType, $extensionMimeMap[$fileExtension])) { |
528 | | - $errors['message'] = __('The file extension does not match the actual file content'); |
529 | | - } |
530 | | - } |
531 | | - |
532 | | - /** |
533 | | - * Validate file extension against allowed extensions |
534 | | - * |
535 | | - * @param UploadedFile $file |
536 | | - * @param array $errors |
537 | | - * @return void |
538 | | - */ |
539 | | - private function validateFileExtension(UploadedFile $file, &$errors) |
540 | | - { |
541 | | - $allowedExtensions = config('files.allowed_extensions'); |
542 | | - $fileExtension = strtolower($file->getClientOriginalExtension()); |
543 | | - |
544 | | - if (!in_array($fileExtension, $allowedExtensions)) { |
545 | | - $errors['message'] = __('File extension not allowed'); |
546 | | - } |
547 | | - } |
548 | | - |
549 | | - private function validatePDFFile(UploadedFile $file, &$errors) |
550 | | - { |
551 | | - $text = $file->get(); |
552 | | - |
553 | | - $jsKeywords = ['/JavaScript', '<< /S /JavaScript']; |
554 | | - |
555 | | - foreach ($jsKeywords as $keyword) { |
556 | | - if (strpos($text, $keyword) !== false) { |
557 | | - $errors[] = __('Dangerous PDF file content'); |
558 | | - break; |
559 | | - } |
560 | | - } |
561 | | - |
562 | | - return $errors; |
563 | | - } |
564 | 440 | } |
0 commit comments