Skip to content

Commit

Permalink
Block javascript in HTML media files
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Oct 9, 2021
1 parent dd7fc1b commit fc90412
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/Factories/ImageFactory.php
Expand Up @@ -27,6 +27,7 @@
use Fisharebest\Webtrees\Mime;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Webtrees;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Imagick;
use Intervention\Image\Constraint;
use Intervention\Image\Exception\NotReadableException;
Expand Down Expand Up @@ -98,7 +99,7 @@ public function fileResponse(FilesystemOperator $filesystem, string $path, bool
$filename = $download ? addcslashes(basename($path), '"') : '';

return $this->imageResponse($filesystem->read($path), $mime_type, $filename);
} catch (FileNotFoundException $ex) {
} catch (UnableToReadFile | FilesystemException $ex) {
return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND);
}
}
Expand Down Expand Up @@ -343,8 +344,10 @@ protected function imageResponse(string $data, string $mime_type, string $filena
->withHeader('X-Image-Exception', 'SVG image blocked due to XSS.');
}

// HTML files may contain javascript, so use content-security-policy to disable it.
$response = response($data)
->withHeader('content-type', $mime_type);
->withHeader('content-type', $mime_type)
->withHeader('content-security-policy', 'script-src none');

if ($filename === '') {
return $response;
Expand Down

0 comments on commit fc90412

Please sign in to comment.