From d359120d810d38312d604c2e427b50e596d20c75 Mon Sep 17 00:00:00 2001 From: Djamil Legato Date: Thu, 27 May 2021 12:26:25 -0700 Subject: [PATCH] Fixed SRI trying to calculate remote assets, only ever set integrity for local files. Use the SRI provided by the remote source and manually add it in the `addJs/addCss` call for remote support. (fixes #3358) Also Fixed wrong SRI paths invoked when Grav instance as a sub folder --- CHANGELOG.md | 2 ++ system/src/Grav/Common/Assets/BaseAsset.php | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa9a35e65..1b9ba97b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ * Fixed the first visible child page getting ordering number `999999.` [#3365](https://github.com/getgrav/grav/issues/3365) * Fixed flex pages search using only folder name [#3316](https://github.com/getgrav/grav/issues/3316) * Fixed flex pages using wrong type in `onBlueprintCreated` event [#3157](https://github.com/getgrav/grav/issues/3157) + * Fixed wrong SRI paths invoked when Grav instance as a sub folder [#3358](https://github.com/getgrav/grav/issues/3358) + * Fixed SRI trying to calculate remote assets, only ever set integrity for local files. Use the SRI provided by the remote source and manually add it in the `addJs/addCss` call for remote support. [#3358](https://github.com/getgrav/grav/issues/3358) # v1.7.15 ## 05/19/2021 diff --git a/system/src/Grav/Common/Assets/BaseAsset.php b/system/src/Grav/Common/Assets/BaseAsset.php index 3e079746b..192ea81cd 100644 --- a/system/src/Grav/Common/Assets/BaseAsset.php +++ b/system/src/Grav/Common/Assets/BaseAsset.php @@ -15,6 +15,7 @@ use Grav\Common\Grav; use Grav\Common\Uri; use Grav\Common\Utils; use Grav\Framework\Object\PropertyObject; +use RocketTheme\Toolbox\File\File; use SplFileInfo; /** @@ -182,16 +183,21 @@ abstract class BaseAsset extends PropertyObject public static function integrityHash($input) { $grav = Grav::instance(); + $uri = $grav['uri']; $assetsConfig = $grav['config']->get('system.assets'); - if ( !empty($assetsConfig['enable_asset_sri']) && $assetsConfig['enable_asset_sri'] ) - { - $dataToHash = file_get_contents( GRAV_WEBROOT . $input); + if (!self::isRemoteLink($input) && !empty($assetsConfig['enable_asset_sri']) && $assetsConfig['enable_asset_sri']) { + $input = preg_replace('#^' . $uri->rootUrl() . '#', '', $input); + $asset = File::instance(GRAV_WEBROOT . $input); - $hash = hash('sha256', $dataToHash, true); - $hash_base64 = base64_encode($hash); - return ' integrity="sha256-' . $hash_base64 . '"'; + if ($asset->exists()) { + $dataToHash = $asset->content(); + $hash = hash('sha256', $dataToHash, true); + $hash_base64 = base64_encode($hash); + + return ' integrity="sha256-' . $hash_base64 . '"'; + } } return '';