namespace wcf\action;
use Laminas\Diactoros\Response;
+use Laminas\Diactoros\Response\EmptyResponse;
use Laminas\Diactoros\Stream;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
throw new PermissionDeniedException();
}
+ $eTag = \sprintf(
+ '"%d-%s"',
+ $file->fileID,
+ \substr($file->fileHash, 0, 8),
+ );
+
+ $httpIfNoneMatch = $_SERVER['HTTP_IF_NONE_MATCH'] ?? '';
+ if ($httpIfNoneMatch === $eTag) {
+ return new EmptyResponse(304);
+ }
+
$processor->trackDownload($file);
$filename = $file->getPathname();
default => ContentDisposition::Attachment,
};
- return $response->withHeader('content-type', $mimeType)
+ // Prevent <script> execution in the context of the community's domain if
+ // an attacker somehow bypasses 'content-disposition: attachment' for non-inline
+ // MIME-Types. One possibility might be a package extending $inlineMimeTypes
+ // in an unsafe fashion.
+ //
+ // Allow style-src 'unsafe-inline', because otherwise the integrated PDF viewer
+ // of Safari will fail to apply its own trusted stylesheet.
+ $response = $response
+ ->withHeader('content-security-policy', "default-src 'none'; style-src 'unsafe-inline';")
+ ->withHeader('x-content-type-options', 'nosniff');
+
+ $lifetimeInSeconds = $processor->getFileCacheDuration($file)->lifetimeInSeconds;
+ if ($lifetimeInSeconds !== null) {
+ $expiresAt = \sprintf(
+ '%s GMT',
+ \gmdate('D, d M Y H:i:s', $lifetimeInSeconds)
+ );
+ $maxAge = \sprintf(
+ 'max-age=%d, private',
+ $lifetimeInSeconds ?: 0,
+ );
+
+ $response = $response
+ ->withHeader('Expires', $expiresAt)
+ ->withHeader('Cache-control', $maxAge);
+ }
+
+ return $response
+ ->withHeader('content-type', $mimeType)
->withHeader(
'content-disposition',
$contentDisposition->forFilename($file->filename),
- );
+ )
+ ->withHeader('ETag', $eTag);
}
}
return ['*'];
}
+ #[\Override]
+ public function getFileCacheDuration(File $file): FileCacheDuration
+ {
+ return FileCacheDuration::oneYear();
+ }
+
#[\Override]
public function getResizeConfiguration(): ResizeConfiguration
{
return;
}
+ // Side effect: Renew the lifetime of a temporary attachment in case
+ // the user is still writing their message, preventing it
+ // from vanishing prematurely.
+ if ($attachment->tmpHash) {
+ (new AttachmentEditor($attachment))->update([
+ 'uploadTime' => \TIME_NOW,
+ ]);
+
+ // Do not update the download counter for temporary attachments.
+ return;
+ }
+
(new AttachmentEditor($attachment))->update([
'downloads' => $attachment->downloads,
'lastDownloadTime' => \TIME_NOW,
]);
}
+ #[\Override]
+ public function getFileCacheDuration(File $file): FileCacheDuration
+ {
+ $attachment = Attachment::findByFileID($file->fileID);
+ if ($attachment?->tmpHash === '') {
+ return FileCacheDuration::oneYear();
+ }
+
+ return FileCacheDuration::shortLived();
+ }
+
private function getAttachmentHandlerFromContext(array $context): ?AttachmentHandler
{
try {
--- /dev/null
+<?php
+
+namespace wcf\system\file\processor;
+
+/**
+ * Specifies the maximum cache lifetime of a file in the browser.
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.1
+ */
+final class FileCacheDuration
+{
+ public static function shortLived(): self
+ {
+ return new self(5 * 60);
+ }
+
+ public static function oneYear(): self
+ {
+ return new self(365 * 86_400);
+ }
+
+ public static function doNotCache(): self
+ {
+ return new self(null);
+ }
+
+ public static function customDuration(int $seconds): self
+ {
+ if ($seconds < 1) {
+ throw new \OutOfBoundsException('The custom duration must be a positive integer greater than zero.');
+ }
+
+ return new self($seconds);
+ }
+
+ public function allowCaching(): bool
+ {
+ return $this->lifetimeInSeconds !== null;
+ }
+
+ private function __construct(
+ public readonly ?int $lifetimeInSeconds,
+ ) {
+ }
+}
*/
public function getAllowedFileExtensions(array $context): array;
+ /**
+ * Limits how long a file may be cached by the browser. Should use a low
+ * value for files that are not persisted yet.
+ */
+ public function getFileCacheDuration(File $file): FileCacheDuration;
+
/**
* Controls the client-side resizing of some types of images before they are
* being uploaded to the server.