Skip to content

Commit

Permalink
fix: validate icons to be images (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
keriati authored Jun 6, 2023
1 parent 5d67f57 commit fbd050d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
27 changes: 17 additions & 10 deletions app/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ function format_bytes($bytes, bool $is_drive_size = true, string $beforeunit = '
$btype = ($is_drive_size === true) ? 1000 : 1024;
$labels = ['B', 'KB', 'MB', 'GB', 'TB'];
// use 1000 rather than 1024 to simulate HD size not real size
for ($x = 0; $bytes >= $btype && $x < (count($labels) - 1); $bytes /= $btype, $x++);
for ($x = 0; $bytes >= $btype && $x < (count($labels) - 1); $bytes /= $btype, $x++) ;
if ($labels[$x] == 'TB') {
return round($bytes, 3).$beforeunit.$labels[$x].$afterunit;
return round($bytes, 3) . $beforeunit . $labels[$x] . $afterunit;
} elseif ($labels[$x] == 'GB') {
return round($bytes, 2).$beforeunit.$labels[$x].$afterunit;
return round($bytes, 2) . $beforeunit . $labels[$x] . $afterunit;
} elseif ($labels[$x] == 'MB') {
return round($bytes, 2).$beforeunit.$labels[$x].$afterunit;
return round($bytes, 2) . $beforeunit . $labels[$x] . $afterunit;
} else {
return round($bytes, 0).$beforeunit.$labels[$x].$afterunit;
return round($bytes, 0) . $beforeunit . $labels[$x] . $afterunit;
}
}

Expand All @@ -37,11 +37,11 @@ function str_slug($title, string $separator = '-', string $language = 'en'): str
return Str::slug($title, $separator, $language);
}

if (! function_exists('str_is')) {
if (!function_exists('str_is')) {
/**
* Determine if a given string matches a given pattern.
*
* @param string|array $pattern
* @param string|array $pattern
* @param string $value
* @return bool
*
Expand All @@ -64,7 +64,7 @@ function get_brightness($hex)
// $hex = str_replace('#', '', $hex);
$hex = preg_replace("/[^0-9A-Fa-f]/", '', $hex);
if (strlen($hex) == 3) {
$hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
}

$c_r = hexdec(substr($hex, 0, 2));
Expand Down Expand Up @@ -97,7 +97,7 @@ function getLinkTargetAttribute(): string
if ($target === 'current') {
return '';
} else {
return ' target="'.$target.'"';
return ' target="' . $target . '"';
}
}

Expand All @@ -112,10 +112,17 @@ function className($name)

/**
* @param string $file
* @param string $extension
* @return bool
*/
function isImage(string $file):bool
function isImage(string $file, string $extension): bool
{
$allowedExtensions = ['jpg', 'jpeg', 'png', 'bmp', 'gif', 'svg', 'webp'];

if (!in_array($extension, $allowedExtensions)) {
return false;
}

$tempFileName = tempnam("/tmp", "image-check-");
$handle = fopen($tempFileName, "w");

Expand Down
28 changes: 13 additions & 15 deletions app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function pinToggle($id, $ajax = false, $tag = false)
*/
public function index(Request $request): View
{
$trash = (bool) $request->input('trash');
$trash = (bool)$request->input('trash');

$data['apps'] = Item::ofType('item')->orderBy('title', 'asc')->get();
$data['trash'] = Item::ofType('item')->onlyTrashed()->get();
Expand Down Expand Up @@ -197,6 +197,7 @@ public function edit(int $id): View
* @param Request $request
* @param null $id
* @return Item
* @throws ValidationException
*/
public static function storelogic(Request $request, $id = null): Item
{
Expand All @@ -219,21 +220,18 @@ public static function storelogic(Request $request, $id = null): Item
"verify_peer_name" => false,
),
);

$file = $request->input('icon');
$path_parts = pathinfo($file);
$extension = $path_parts['extension'];

$contents = file_get_contents($request->input('icon'), false, stream_context_create($options));

if (!isImage($contents)) {
if (!isImage($contents, $extension)) {
throw ValidationException::withMessages(['file' => 'Icon must be an image.']);
}

if ($application) {
$icon = $application->icon;
} else {
$file = $request->input('icon');
$path_parts = pathinfo($file);
$icon = md5($contents);
$icon .= '.' . $path_parts['extension'];
}
$path = 'icons/' . $icon;
$path = 'icons/' . ($application ? $application->icon : md5($contents) . '.' . $extension);

// Private apps could have here duplicated icons folder
if (strpos($path, 'icons/icons/') !== false) {
Expand Down Expand Up @@ -340,7 +338,7 @@ public function update(Request $request, int $id): RedirectResponse
public function destroy(Request $request, int $id): RedirectResponse
{
//
$force = (bool) $request->input('force');
$force = (bool)$request->input('force');
if ($force) {
Item::withTrashed()
->where('id', $id)
Expand Down Expand Up @@ -394,11 +392,11 @@ public function appload(Request $request): ?string
$output['custom'] = null;

$app = Application::single($appid);
$output = (array) $app;
$output = (array)$app;

$appdetails = Application::getApp($appid);

if ((bool) $app->enhanced === true) {
if ((bool)$app->enhanced === true) {
// if(!isset($app->config)) { // class based config
$output['custom'] = className($appdetails->name) . '.config';
// }
Expand Down Expand Up @@ -444,7 +442,7 @@ public function testConfig(Request $request)
}

$app_details = new $app();
$app_details->config = (object) $data;
$app_details->config = (object)$data;
$app_details->test();
}

Expand Down
24 changes: 12 additions & 12 deletions tests/Unit/helpers/IsImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ class IsImageTest extends TestCase
/**
* @return void
*/
public function test_isImage_returns_false_when_file_is_not_image()
public function test_returns_true_when_file_is_image()
{
$actual = isImage("<?php ?>");
$file = file_get_contents(__DIR__ . '/fixtures/heimdall-icon-small.png');

$this->assertFalse($actual);
$actual = isImage($file, 'png');

$this->assertTrue($actual);
}

/**
* @return void
*/
public function test_isImage_returns_true_when_file_is_image()
public function test_returns_false_when_file_extension_is_image_but_content_is_not()
{
$file = file_get_contents(__DIR__ . '/fixtures/heimdall-icon-small.png');

$actual = isImage($file);
$actual = isImage("<?php ?>", "png");

$this->assertTrue($actual);
$this->assertFalse($actual);
}

/**
* @return void
*/
public function test_isImage_returns_false_when_file_is_php_but_png()
public function test_returns_false_when_file_extension_is_not_image_but_content_is()
{
$file = file_get_contents(__DIR__ . '/fixtures/heimdall-icon-small-php.php');
$file = file_get_contents(__DIR__ . '/fixtures/heimdall-icon-small.png');

$actual = isImage($file);
$actual = isImage($file, 'php');

$this->assertTrue($actual);
$this->assertFalse($actual);
}
}
Binary file not shown.

0 comments on commit fbd050d

Please sign in to comment.