Skip to content

EspoCRM – Open Source CRM Application

Image document source no inline

Yuriif7ecf0c47430
diff --git a/application/Espo/EntryPoints/Avatar.php b/application/Espo/EntryPoints/Avatar.php
index 55dfe47302..69a9014d88 100644
--- a/application/Espo/EntryPoints/Avatar.php
+++ b/application/Espo/EntryPoints/Avatar.php
@@ -147,6 +147,7 @@ class Avatar extends Image
}
$this->show(
+ request: $request,
response: $response,
id: $user->getAvatarId(),
size: $size,
diff --git a/application/Espo/EntryPoints/Image.php b/application/Espo/EntryPoints/Image.php
index 0295f7c59b..6ba78ba524 100644
--- a/application/Espo/EntryPoints/Image.php
+++ b/application/Espo/EntryPoints/Image.php
@@ -57,6 +57,15 @@ class Image implements EntryPoint
/** @var ?string[] */
protected $allowedFieldList = null;
+ private const string TYPE_SVG = 'image/svg+xml';
+
+ /**
+ * @var string[]
+ */
+ private array $noInlineTypeList = [
+ self::TYPE_SVG,
+ ];
+
public function __construct(
private FileStorageManager $fileStorageManager,
private FileManager $fileManager,
@@ -75,7 +84,7 @@ class Image implements EntryPoint
throw new BadRequest("No id.");
}
- $this->show($response, $id, $size);
+ $this->show($request, $response, $id, $size);
}
/**
@@ -85,6 +94,7 @@ class Image implements EntryPoint
* @throws ForbiddenSilent
*/
protected function show(
+ Request $request,
Response $response,
string $id,
?string $size,
@@ -149,13 +159,16 @@ class Image implements EntryPoint
$response->setHeader('Content-Type', $fileType);
}
- $fileName = str_replace("\"", "\\\"", $fileName ?? '');
+ $fileName = self::sanitizeFileName($fileName);
- $csp = "default-src 'none'; script-src 'none'; object-src 'none'; form-action 'none'; " .
+ $csp =
+ "default-src 'none'; script-src 'none'; object-src 'none'; form-action 'none'; " .
"style-src 'unsafe-inline'; sandbox;";
+ $disposition = $this->chooseDisposition($fileType, $request);
+
$response
- ->setHeader('Content-Disposition', 'inline;filename="' . $fileName . '"')
+ ->setHeader('Content-Disposition', "$disposition;filename=\"$fileName\"")
->setHeader('Content-Length', (string) $fileSize)
->setHeader('Content-Security-Policy', $csp);
@@ -463,4 +476,26 @@ class Image implements EntryPoint
$targetWidth, $targetHeight, $originalWidth, $originalHeight
);
}
+
+ private function chooseDisposition(?string $fileType, Request $request): string
+ {
+ $disposition = 'inline';
+
+ if (
+ in_array($fileType, $this->noInlineTypeList) &&
+ in_array($request->getHeader('Sec-Fetch-Dest'), [
+ 'document',
+ 'iframe',
+ ])
+ ) {
+ $disposition = 'attachment';
+ }
+
+ return $disposition;
+ }
+
+ private static function sanitizeFileName(?string $fileName): string
+ {
+ return str_replace("\"", "\\\"", $fileName ?? '');
+ }
}
diff --git a/application/Espo/EntryPoints/LogoImage.php b/application/Espo/EntryPoints/LogoImage.php
index abb43c1879..59bb8e4e8e 100644
--- a/application/Espo/EntryPoints/LogoImage.php
+++ b/application/Espo/EntryPoints/LogoImage.php
@@ -54,6 +54,6 @@ class LogoImage extends Image
throw new NotFound("No id.");
}
- $this->show($response, $id, $size);
+ $this->show($request, $response, $id, $size);
}
}