2017 © Pedro Peláez
 

library laravel-sri

Subresource Integrity hash generator for laravel

image

elhebert/laravel-sri

Subresource Integrity hash generator for laravel

  • Thursday, March 22, 2018
  • by Elhebert
  • Repository
  • 1 Watchers
  • 10 Stars
  • 156 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 16 Versions
  • 90 % Grown

The README.md

Laravel Subresource Integrity

Software License StyleCI GitHub Workflow Status Latest Version on Packagist Total Downloads, (*1)

Small Laravel 8+ package that'll generate the integrity hashes for your style and script files., (*2)

For Laravel 5.5+ support, use the v1 branch. For Laravel 6+ support, use the v2 branch., (*3)

About Subresources Integrity

From MDN:, (*4)

Subresource Integrity (SRI) is a security feature that enables browsers to verify that files they fetch (for example, from a CDN) are delivered without unexpected manipulation. It works by allowing you to provide a cryptographic hash that a fetched file must match., (*5)

Troy Hunt wrote an article speaking on the subject, you can read it here, (*6)

Installation

composer require elhebert/laravel-sri

This package uses auto-discovery, so you don't have to do anything. It works out of the box., (*7)

Config

If you want to make changes in the configuration you can publish the config file using, (*8)

php artisan vendor:publish --provider="Elhebert\SubresourceIntegrity\SriServiceProvider"

Content of the configuration

key default value possible values
base_path base_path('/public')
algorithm sha256 sha256, sha384 and sha512
hashes [] (see "How to get a hash)
mix_sri_path public_path('mix-sri.json') (see "How to get a hash)
enabled true
dangerously_allow_third_party_assets false

Usage

To only get a hash, use Sri::hash:, (*9)

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

To generate the HTML for the integrity and the crossorigin attributes, use Sri::html. It accepts two parameters:, (*10)

  • first one is the path;
  • second one (default is false) tells if you want to pass the credentials when fetching the resource.
<link
    href="{{ asset('css/app.css') }}"
    rel="stylesheet"
    {{ Sri::html('css/app.css') }}
/>

Blade Component

Alternatively you can use blade components:, (*11)

<x:sri.link href="css/app.css" rel="stylesheet" />
<!-- is the equivalent of doing -->
<link
    href="{{ asset('css/app.css') }}"
    rel="stylesheet"
    integrity="{{ Sri::hash('css/app.css') }}"
    crossorigin="anonymous"
/>

If you add a mix attributet to the component it'll use mix() instead of asset() to generate the link to the assets:, (*12)

<x:sri.link mix href="css/app.css" rel="stylesheet" />
<!-- is the equivalent of doing -->
<link
    href="{{ mix('css/app.css') }}"
    rel="stylesheet"
    integrity="{{ Sri::hash('css/app.css') }}"
    crossorigin="anonymous"
/>

Improve performance

You should wrap your <link> and <script> tags with the @once directive to ensure that your tags are only rendered once. This will help with performances as it'll avoid a potential re-hashing of the files (in case you want to hash them on the fly)., (*13)

Be careful that this should only be use for production as it won't re-render the html tag. Thus preventing new cache busting id to be added to the path by mix., (*14)

@once
<link
    href="{{ mix('css/app.css') }}"
    rel="stylesheet"
    integrity="{{ Sri::hash('css/app.css') }}"
    crossorigin="anonymous"
/>
<!-- Or using the blade component -->
<x:sri.link mix href="css/app.css" rel="stylesheet" />
@endonce

How to get a hash

Store hashes in the configuration

You can references the assets in the configuration like this:, (*15)

[
    // ...

    'hashes' => [
        'css/app.css' => 'my_super_hash'
        'https://code.jquery.com/jquery-3.3.1.min.js' => 'sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8='
    ]
]

This means, you have to calculate the hashes yourself. To do this, you can use report-uri.io, mozilla hash generator or any other resource available., (*16)

Using a webpack (or Mix) plugin to generate hashes on build

It expect a mix-sri.json file with a similar structure to the mix-manifest.json:, (*17)

{
    "/css/app.css": "my_super_hash",
    "/js/app.js": "my_super_hash"
}

The filename and path can be changed in the configuration at any time., (*18)

Self promotion: I made a Laravel Mix extension laravel-mix-sri for this purpose., (*19)

Generate them on the fly

If it can't find the asset hash in the config file nor in the mix-sri.json file, it'll generate the hash on each reload of the page., (*20)

This method is the least recommended, because it reduce performance and make your page load slower., (*21)

Remote resources

This package also work for remote resources. Be careful that resources like Google Fonts won't work., (*22)




<x:sri.script src="http://code.jquery.com/jquery-3.3.1.min.js"></x:sri-script>

Contributing

Please see CONTRIBUTING for more details., (*23)

License

This project and the Laravel framework are open-sourced software licensed under the MIT license., (*24)

The Versions

22/03 2018

dev-master

9999999-dev

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

22/03 2018

1.4.2

1.4.2.0

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

22/03 2018

dev-feat/11-local-asset-path

dev-feat/11-local-asset-path

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

22/03 2018

dev-feat/travis-slow-composer

dev-feat/travis-slow-composer

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

22/03 2018

1.4.1

1.4.1.0

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

22/03 2018

dev-feat/10-non-static-method

dev-feat/10-non-static-method

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

22/03 2018

dev-analysis-qxRGpA

dev-analysis-qxRGpA

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

22/03 2018

dev-analysis-XplLpV

dev-analysis-XplLpV

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

22/03 2018

dev-analysis-8A4vGG

dev-analysis-8A4vGG

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

22/03 2018

1.4.0

1.4.0.0

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

20/03 2018

1.3.0

1.3.0.0

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

20/03 2018

1.2.1

1.2.1.0

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

16/03 2018

1.2.0

1.2.0.0

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

08/02 2018

1.1.0

1.1.0.0

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

01/02 2018

1.0.2

1.0.2.0

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity

01/02 2018

1.0.0

1.0.0.0

Subresource Integrity hash generator for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dieter Stinglhamber

sri laravel-sri subresource integrity