Elhebert / laravel-sri

Subresource Integrity hash generator for laravel
MIT License
40 stars 16 forks source link

$path on v1 branch is always null #75

Closed bellwood closed 3 years ago

bellwood commented 3 years ago

Working to implement this on an older L5.6 site on PHP 7.4 using the v1 branch.

Config has been published and hashes are added:

    'hashes' => [
        'css/app.css' => 'sha384-redacted',
        'js/app.js' => 'sha384-redacted',
    ],

In my blade template:

    <link
        rel="stylesheet"
        href="{{ mix('/css/app.css') }}"
        integrity="{{ Sri::hash('css/app.css') }}"
        crossorigin="anonymous">

No matter what path I give inside Sri::hash() I'm always getting the following:

ErrorException(code: 0): array_key_exists() expects parameter 2 to be array, null given at /redacted/vendor/elhebert/laravel-sri/src/Sri.php:40)

If I debug at the top of the hash function $path is always null.

Elhebert commented 3 years ago

Hello,

Thanks for the report. However we have tests supporting the hash function which passed.

array_key_exists() expects parameter 2 to be array, null given Looking at the error message it seems the issue comes from the $json variable and not the $path.

see: https://github.com/Elhebert/laravel-sri/blob/f49e2c7a7638b769d5eb26ca058d4440301635f9/src/Sri.php#L40

But since you defined your hashes in your config file, it shouldn't try to get the mix-sri.json file. You might want to verify that your config is properly defined. The only reason for it to try to get it from the mix-sri.json is because it can't find the hashes in the config file.

https://github.com/Elhebert/laravel-sri/blob/f49e2c7a7638b769d5eb26ca058d4440301635f9/src/Sri.php#L32-L34

bellwood commented 3 years ago

@Elhebert not sure if I can chalk this up to caching or just PEBKAC but after I removed mix-sri.json from my public path (not yet automating that with your other package) and ran php artisan optimize everything began working.

As part of my debug process, I'd also added a new public method to Sri.php and was able to return the path I fed into it from blade using that method (pre artisan optimize)

I have another older site to implement this on and if I can reproduce the issue I will re-open this issue.

Appreciate the follow up, thank you.