diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index e112a13..9f5c36a 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -30,6 +30,7 @@ class Kernel extends HttpKernel */ protected $middlewareGroups = [ 'web' => [ + 'htmlsanitize', \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, @@ -58,6 +59,6 @@ class Kernel extends HttpKernel 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, - 'xss' => \App\Http\Middleware\XssSanitizer::class, + 'htmlsanitize' => \App\Http\Middleware\HtmlSanitizer::class, ]; } diff --git a/app/Http/Middleware/XssSanitizer.php b/app/Http/Middleware/HtmlSanitizer.php similarity index 94% rename from app/Http/Middleware/XssSanitizer.php rename to app/Http/Middleware/HtmlSanitizer.php index 2dce94e..e2c39ad 100644 --- a/app/Http/Middleware/XssSanitizer.php +++ b/app/Http/Middleware/HtmlSanitizer.php @@ -5,7 +5,7 @@ namespace App\Http\Middleware; use App\Lib\ToolboxHelper; use Illuminate\Http\Request; -class XssSanitizer +class HtmlSanitizer { /** * Handle an incoming request. @@ -38,7 +38,6 @@ class XssSanitizer } } else { $input = strip_tags($input); - $input = \filter_var($input, FILTER_SANITIZE_STRING); } }); $request->merge($inputs); diff --git a/app/Http/Requests/Bo/Sliders/UpdateSliderRequest.php b/app/Http/Requests/Bo/Sliders/UpdateSliderRequest.php index 2dae054..52ebb24 100644 --- a/app/Http/Requests/Bo/Sliders/UpdateSliderRequest.php +++ b/app/Http/Requests/Bo/Sliders/UpdateSliderRequest.php @@ -4,15 +4,4 @@ namespace App\Http\Requests\Bo\Sliders; class UpdateSliderRequest extends StoreSliderRequest { - /** - * Get the validation rules that apply to the request. - * - * @return array - */ - public function rules() - { - return \array_merge(parent::rules(), [ - 'picture' => 'sometimes|image|dimensions:width=1400,height=630|mimes:png,jpg,jpeg,gif' - ]); - } } diff --git a/app/Lib/ToolboxHelper.php b/app/Lib/ToolboxHelper.php index 0aff991..dcc71da 100644 --- a/app/Lib/ToolboxHelper.php +++ b/app/Lib/ToolboxHelper.php @@ -205,10 +205,7 @@ class ToolboxHelper $table = $model->getTable(); if (\is_object($image) and $image instanceof \Illuminate\Http\UploadedFile) { /** @var \Illuminate\Http\UploadedFile $image */ - $newPath = self::prepareMoveFile($table, \filter_var( - \strip_tags(\urldecode($image->getClientOriginalName())), - \FILTER_SANITIZE_STRING - )); + $newPath = self::prepareMoveFile($table, $image->getClientOriginalName()); $newPath = str_replace(storage_path('app'), '', $newPath); $image->storePubliclyAs( pathinfo($newPath, \PATHINFO_DIRNAME), @@ -241,6 +238,15 @@ class ToolboxHelper */ private static function prepareMoveFile(string $table, string $filename): string { + // * Security filter + beautifier + $filename = self::sanitizeFileName($filename); + // * Security filter + $filename = \filter_var($filename, \FILTER_SANITIZE_STRING); + // * Slugify filename + $ext = \pathinfo($filename, \PATHINFO_EXTENSION); + $filename = Str::slug(\pathinfo($filename, \PATHINFO_FILENAME)) . + (\strlen($ext) ? ".{$ext}" : ''); + $pathFolder = \storage_path(sprintf('app/public/images/%s', $table)); if (!File::exists($pathFolder)) { File::makeDirectory($pathFolder, 0755, true); @@ -371,4 +377,79 @@ class ToolboxHelper ->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class) ->name('cspReportUri'); } + + /** + * Sanitize File name for uploaded files + * + * @param string $filename The original uploaded client file name. + * @param boolean $beautify Beautify the filename. + * @return string The sanitized file name. + */ + public static function sanitizeFileName(string $filename, bool $beautify = true): string + { + // * Sanitize filename + // phpcs:disable Generic.Files.LineLength.TooLong + $filename = preg_replace( + '~ + [<>:"/\\\|?*]| # file system reserved https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words + [\x00-\x1F]| # control characters http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx + [\x7F\xA0\xAD]| # non-printing characters DEL, NO-BREAK SPACE, SOFT HYPHEN + [#\[\]@!$&\'()+,;=]| # URI reserved https://tools.ietf.org/html/rfc3986#section-2.2 + [{}^\~`] # URL unsafe characters https://www.ietf.org/rfc/rfc1738.txt + ~x', + '-', + $filename + ); + // phpcs:enable + // * Avoids '.' '..' or '.hiddenFiles' . + $filename = Str::of($filename)->ltrim('.-'); + // * Optional beautification + if ($beautify) { + $filename = static::beautifyFilename($filename); + } + // * Maximize filename length to 255 bytes http://serverfault.com/a/9548/44086 + /** @var string */ + $extension = pathinfo($filename, \PATHINFO_EXTENSION); + /** @var string */ + $filename = pathinfo($filename, \PATHINFO_FILENAME); + + $encoding = mb_detect_encoding($filename); + $encoding = $encoding ? $encoding : null; + return \mb_strcut( + $filename, + 0, + 255 - (!empty($extension) ? strlen($extension) + 1 : 0), + $encoding + ) . (!empty($extension) ? ".$extension" : ''); + } + + /** + * Beautify a filename + * + * @param string $filename + * @return string + */ + private static function beautifyFilename(string $filename): string + { + // Reduce consecutive characters. + $filename = preg_replace([ + // * "file name.zip" becomes 'file-name.zip' + '/ +/', + // * "file___name.zip" becomes 'file-name.zip' + '/_+/', + // * "file---name.zip" becomes 'file-name.zip' + '/-+/' + ], '-', $filename); + $filename = preg_replace([ + // * "file--.--.-.--name.zip" becomes "file.name.zip" + '/-*\.-*/', + // * "file...name..zip" becomes "file.name.zip" + '/\.{2,}/' + ], '.', $filename); + // Lowercase for windows/unix interoperability http://support.microsoft.com/kb/100625 . + $filename = mb_strtolower($filename, mb_detect_encoding($filename)); + // * ".file-name.-" becomes "file-name" + $filename = trim($filename, '.-'); + return $filename; + } } diff --git a/app/Traits/Requests/HasPicture.php b/app/Traits/Requests/HasPicture.php index f0d0d62..e83b03d 100644 --- a/app/Traits/Requests/HasPicture.php +++ b/app/Traits/Requests/HasPicture.php @@ -163,7 +163,6 @@ trait HasPicture [\public_path(ltrim($decodedPath, '/'))] ) ]); - $changedPicture = $this->picture; $changedPicture = ltrim($decodedPath, '/'); $this->merge([ 'picture' => $changedPicture diff --git a/routes/web-bo.php b/routes/web-bo.php index 56d2e12..b15010f 100644 --- a/routes/web-bo.php +++ b/routes/web-bo.php @@ -28,7 +28,7 @@ Route::prefix('bo')->name('bo.')->group(function () { Route::resource('tags', TagController::class)->except(['show']); Route::resource('news', NewsController::class) ->except(['show']) - ->withoutMiddleware('xss') - ->middleware('xss:content'); + ->withoutMiddleware('htmlsanitize') + ->middleware('htmlsanitize:content'); }); });