@@ -289,14 +289,14 @@ public function store(Request $laravel_request, FileReceiver $receiver, ProcessR
289289 } catch (FileIsTooBig $ e ) {
290290 return response ()->json ([
291291 'errors ' => [
292- 'file ' => ['file may not be greater than ' . (config ('media-library.max_file_size ' ) / 1024 ) . ' kilobytes ' ]
293- ]
292+ 'file ' => ['file may not be greater than ' . (config ('media-library.max_file_size ' ) / 1024 ) . ' kilobytes ' ],
293+ ],
294294 ], 422 );
295295 } catch (Exception $ e ) {
296296 return response ()->json ([
297297 'errors ' => [
298- 'message ' => $ e ->getMessage ()
299- ]
298+ 'message ' => $ e ->getMessage (),
299+ ],
300300 ], 500 );
301301 }
302302 }
@@ -440,15 +440,148 @@ public function destroy(Request $laravel_request, ProcessRequest $request, $file
440440 return response ([], 204 );
441441 }
442442
443+ /**
444+ * Validate uploaded file for security and type restrictions
445+ *
446+ * @param UploadedFile $file
447+ * @param array $errors
448+ * @return array
449+ */
443450 private function validateFile (UploadedFile $ file , &$ errors )
444451 {
445- if (strtolower ($ file ->getClientOriginalExtension () === 'pdf ' )) {
452+ // Explicitly reject archive files for security
453+ $ this ->rejectArchiveFiles ($ file , $ errors );
454+
455+ // Validate file extension if enabled
456+ if (config ('files.enable_extension_validation ' , true )) {
457+ $ this ->validateFileExtension ($ file , $ errors );
458+ }
459+
460+ // Validate MIME type if enabled
461+ if (config ('files.enable_mime_validation ' , true )) {
462+ $ this ->validateMimeType ($ file , $ errors );
463+ }
464+
465+ // Validate specific file types (e.g., PDF for JavaScript content)
466+ if (strtolower ($ file ->getClientOriginalExtension ()) === 'pdf ' ) {
446467 $ this ->validatePDFFile ($ file , $ errors );
447468 }
448469
449470 return $ errors ;
450471 }
451472
473+ /**
474+ * Explicitly reject archive files for security reasons
475+ *
476+ * @param UploadedFile $file
477+ * @param array $errors
478+ * @return void
479+ */
480+ private function rejectArchiveFiles (UploadedFile $ file , &$ errors )
481+ {
482+ $ dangerousExtensions = [
483+ 'zip ' , 'rar ' , '7z ' , 'tar ' , 'gz ' , 'bz2 ' , 'xz ' , 'lzma ' ,
484+ 'cab ' , 'ar ' , 'iso ' , 'dmg ' , 'pkg ' , 'deb ' , 'rpm ' ,
485+ ];
486+
487+ $ fileExtension = strtolower ($ file ->getClientOriginalExtension ());
488+
489+ if (in_array ($ fileExtension , $ dangerousExtensions )) {
490+ $ errors [] = __ ('File extension not allowed ' , [
491+ 'extension ' => $ fileExtension ,
492+ ]);
493+ }
494+
495+ // Also check MIME types for archive files
496+ $ dangerousMimeTypes = [
497+ 'application/zip ' ,
498+ 'application/x-rar-compressed ' ,
499+ 'application/x-7z-compressed ' ,
500+ 'application/x-tar ' ,
501+ 'application/gzip ' ,
502+ 'application/x-bzip2 ' ,
503+ 'application/x-xz ' ,
504+ 'application/x-lzma ' ,
505+ 'application/vnd.ms-cab-compressed ' ,
506+ 'application/x-iso9660-image ' ,
507+ ];
508+
509+ $ fileMimeType = $ file ->getMimeType ();
510+
511+ if (in_array ($ fileMimeType , $ dangerousMimeTypes )) {
512+ $ errors [] = __ ('Mime type not allowed ' , [
513+ 'mime_type ' => $ fileMimeType ,
514+ ]);
515+ }
516+ }
517+
518+ /**
519+ * Validate file extension against allowed extensions
520+ *
521+ * @param UploadedFile $file
522+ * @param array $errors
523+ * @return void
524+ */
525+ private function validateFileExtension (UploadedFile $ file , &$ errors )
526+ {
527+ $ allowedExtensions = config ('files.allowed_extensions ' , [
528+ 'pdf ' , 'doc ' , 'docx ' , 'xls ' , 'xlsx ' , 'ppt ' , 'pptx ' ,
529+ 'txt ' , 'csv ' , 'jpg ' , 'jpeg ' , 'png ' , 'gif ' , 'mp3 ' , 'mp4 ' ,
530+ ]);
531+
532+ $ fileExtension = strtolower ($ file ->getClientOriginalExtension ());
533+
534+ if (!in_array ($ fileExtension , $ allowedExtensions )) {
535+ $ errors [] = __ ('File extension not allowed ' , [
536+ 'extension ' => $ fileExtension ,
537+ 'allowed ' => implode (', ' , $ allowedExtensions ),
538+ ]);
539+ }
540+ }
541+
542+ /**
543+ * Validate MIME type against allowed MIME types
544+ *
545+ * @param UploadedFile $file
546+ * @param array $errors
547+ * @return void
548+ */
549+ private function validateMimeType (UploadedFile $ file , &$ errors )
550+ {
551+ $ allowedMimeTypes = config ('files.allowed_mime_types ' , [
552+ // Documents
553+ 'application/pdf ' ,
554+ 'application/msword ' ,
555+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document ' ,
556+ 'application/vnd.ms-excel ' ,
557+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ' ,
558+ 'application/vnd.ms-powerpoint ' ,
559+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation ' ,
560+ 'text/plain ' ,
561+ 'text/csv ' ,
562+
563+ // Images
564+ 'image/jpeg ' ,
565+ 'image/png ' ,
566+ 'image/gif ' ,
567+
568+ // Audio
569+ 'audio/mpeg ' ,
570+
571+ // Video
572+ 'video/mp4 ' ,
573+ ]);
574+
575+ $ fileMimeType = $ file ->getMimeType ();
576+
577+ if (!in_array ($ fileMimeType , $ allowedMimeTypes )) {
578+ $ errors [] = __ ('Mime type not allowed ' , [
579+ 'mime_type ' => $ fileMimeType ,
580+ 'allowed ' => implode (', ' , $ allowedMimeTypes ),
581+ ]);
582+ }
583+ }
584+
452585 private function validatePDFFile (UploadedFile $ file , &$ errors )
453586 {
454587 $ text = $ file ->get ();
0 commit comments