Skip to content

Commit

Permalink
Merge pull request #972 from foodcoopshop/remotefile
Browse files Browse the repository at this point in the history
Remotefile security fix
  • Loading branch information
mrothauer committed Nov 2, 2023
2 parents d3d10e3 + 9587958 commit 0d5bec5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,13 @@

Das Format basiert auf [keepachangelog.com](http://keepachangelog.com) und verwendet [Semantic Versioning](http://semver.org/).

## v3.6.1

### Security fix
- Security-Fix f眉r das Netzwerk-Modul [PR#972](https://github.com/foodcoopshop/foodcoopshop/pull/972)

Datum: 02.11.2023 / [Alle 脛nderungen anzeigen](https://github.com/foodcoopshop/foodcoopshop/compare/v3.6.0...v3.6.1)

## v3.6.0

### Neue Datenschutz-Funktionen
Expand Down
2 changes: 1 addition & 1 deletion VERSION.txt
@@ -1 +1 @@
3.6.0
3.6.1
24 changes: 18 additions & 6 deletions plugins/Admin/tests/TestCase/src/Model/Table/ProductsTableTest.php
Expand Up @@ -88,19 +88,31 @@ public function testChangeImageValidImageAndDeleteImage()

public function testChangeImageInvalidImage()
{
$file = WWW_ROOT . '/css/global.css';
$productId = 346;
$products = [
[$productId => Configure::read('App.fullBaseUrl') . '/css/global.css']
[$productId => $file]
];
$exceptionThrown = false;

try {
$this->Product->changeImage($products);
} catch (InvalidParameterException $e) {
$exceptionThrown = true;
} catch (Exception $e) {
$this->assertEquals('file is not an image: ' . $file, $e->getMessage());
}
}

$this->assertSame(true, $exceptionThrown);
public function testChangeImageInvalidDomain()
{
$productId = 346;
$products = [
[$productId => 'https://localhost:8080/img/tests/test-image.jpg']
];

try {
$this->Product->changeImage($products);
} catch (Exception $e) {
$this->assertEquals('invalid host', $e->getMessage());
}
}

public function testChangeImageNonExistingFile()
Expand All @@ -113,7 +125,7 @@ public function testChangeImageNonExistingFile()

try {
$this->Product->changeImage($products);
} catch (InvalidParameterException $e) {
} catch (Exception $e) {
$exceptionThrown = true;
}

Expand Down
16 changes: 15 additions & 1 deletion src/Lib/RemoteFile/RemoteFile.php
Expand Up @@ -18,9 +18,11 @@

class RemoteFile
{
public static function exists(string $remoteFile): bool
public static function exists(string $remoteFile, $allowedHosts = []): bool
{

self::verifyAllowedHosts($allowedHosts, $remoteFile);

$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
Expand All @@ -35,4 +37,16 @@ public static function exists(string $remoteFile): bool

}

private static function verifyAllowedHosts($allowedHosts, $remoteFile)
{
if (empty($allowedHosts)) {
throw new \Exception('allowedHosts must be set');
} else {
$host = parse_url($remoteFile, PHP_URL_HOST);
if (!in_array($host, $allowedHosts)) {
throw new \Exception('invalid host');
}
}
}

}
11 changes: 7 additions & 4 deletions src/Model/Table/ProductsTable.php
Expand Up @@ -1370,7 +1370,10 @@ public function changeImage($products)
}

if (filter_var($imageFromRemoteServer, FILTER_VALIDATE_URL)) {
if (!RemoteFile::exists($imageFromRemoteServer)) {
$syncDomainsTable = FactoryLocator::get('Table')->get('Network.SyncDomains');
$syncDomains = $syncDomainsTable->getActiveSyncDomains()->toArray();
$syncDomains = Hash::extract($syncDomains, '{n}.domain');
if (!RemoteFile::exists($imageFromRemoteServer, $syncDomains)) {
throw new InvalidParameterException('remote image not existing: ' . $imageFromRemoteServer);
}
} else {
Expand All @@ -1380,9 +1383,9 @@ public function changeImage($products)
}
}

$imageSize = getimagesize($imageFromRemoteServer);
if ($imageSize === false) {
throw new InvalidParameterException('file is not not an image: ' . $imageFromRemoteServer);
$mimeContentType = mime_content_type($imageFromRemoteServer);
if (!in_array($mimeContentType, Configure::read('app.allowedImageMimeTypes'))) {
throw new InvalidParameterException('file is not an image: ' . $imageFromRemoteServer);
}
}

Expand Down

0 comments on commit 0d5bec5

Please sign in to comment.