All Projects → Elhebert → Laravel Sri

Elhebert / Laravel Sri

Licence: mit
Subresource Integrity hash generator for laravel

Projects that are alternatives of or similar to Laravel Sri

Laravel Medialibrary
Associate files with Eloquent models
Stars: ✭ 4,743 (+20521.74%)
Mutual labels:  hacktoberfest, laravel
Core
Simple forum software for building great communities.
Stars: ✭ 5,372 (+23256.52%)
Mutual labels:  hacktoberfest, laravel
Laravel Dompdf
A DOMPDF Wrapper for Laravel
Stars: ✭ 4,978 (+21543.48%)
Mutual labels:  hacktoberfest, laravel
Freek.dev
The sourcecode of freek.dev
Stars: ✭ 407 (+1669.57%)
Mutual labels:  hacktoberfest, laravel
Artisan View
👀 Manage your views in Laravel projects through artisan
Stars: ✭ 708 (+2978.26%)
Mutual labels:  hacktoberfest, laravel
Laravel 8 Simple Cms
Laravel 8 content management system for starters.
Stars: ✭ 444 (+1830.43%)
Mutual labels:  hacktoberfest, laravel
Laravel Mediable
Laravel-Mediable is a package for easily uploading and attaching media files to models with Laravel 5.
Stars: ✭ 541 (+2252.17%)
Mutual labels:  hacktoberfest, laravel
Collision
💥 Collision is a beautiful error reporting tool for command-line applications
Stars: ✭ 3,993 (+17260.87%)
Mutual labels:  hacktoberfest, laravel
Tenancy
Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups.
Stars: ✭ 916 (+3882.61%)
Mutual labels:  hacktoberfest, laravel
Laravel Boilerplate
Laravel Boilerplate / Starter Kit with Gentelella Admin Theme
Stars: ✭ 704 (+2960.87%)
Mutual labels:  hacktoberfest, laravel
Laravel Acl
This package helps you to associate users with permissions and permission groups with laravel framework
Stars: ✭ 404 (+1656.52%)
Mutual labels:  hacktoberfest, laravel
Platform
A modular multilingual CMS built with Laravel 5.
Stars: ✭ 719 (+3026.09%)
Mutual labels:  hacktoberfest, laravel
Laravel Datatables
jQuery DataTables API for Laravel 4|5|6|7|8
Stars: ✭ 4,134 (+17873.91%)
Mutual labels:  hacktoberfest, laravel
Telegram
✈️ Telegram Notifications Channel for Laravel
Stars: ✭ 450 (+1856.52%)
Mutual labels:  hacktoberfest, laravel
Comments
Native comments for your Laravel application.
Stars: ✭ 390 (+1595.65%)
Mutual labels:  hacktoberfest, laravel
Tenanti
[Package] Multi-tenant Database Schema Manager for Laravel
Stars: ✭ 525 (+2182.61%)
Mutual labels:  hacktoberfest, laravel
Laravel Modules
Module Management In Laravel
Stars: ✭ 3,910 (+16900%)
Mutual labels:  hacktoberfest, laravel
Larasail
LaraSail - Set Sail with your Laravel app on DigitalOcean
Stars: ✭ 348 (+1413.04%)
Mutual labels:  hacktoberfest, laravel
Laravel Translatable
A Laravel package for multilingual models
Stars: ✭ 624 (+2613.04%)
Mutual labels:  hacktoberfest, laravel
Orm
A drop-in Doctrine ORM 2 implementation for Laravel 5+ and Lumen
Stars: ✭ 712 (+2995.65%)
Mutual labels:  hacktoberfest, laravel

Laravel Subresource Integrity

Software License StyleCI GitHub Workflow Status Latest Version on Packagist Total Downloads

Small Laravel 8+ package that'll generate the integrity hashes for your style and script files.

For Laravel 5.5+ support, use the v1 branch. For Laravel 6+ support, use the v2 branch.

About Subresources Integrity

From MDN:

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.

Troy Hunt wrote an article speaking on the subject, you can read it here

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.

Config

If you want to make changes in the configuration you can publish the config file using

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:

<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:

  • 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:

<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:

<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).

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.

@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:

[
    // ...

    '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.

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:

{
    "/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.

Self promotion: I made a Laravel Mix extension laravel-mix-sri for this purpose.

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.

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

Remote resources

This package also work for remote resources. Be careful that resources like Google Fonts won't work.

<script
    src="http://code.jquery.com/jquery-3.3.1.min.js"
    integrity="{{ Sri::hash('http://code.jquery.com/jquery-3.3.1.min.js') }}"
    crossorigin="anonymous"
></script>

<!-- or with a blade component -->
<x:sri-script src="http://code.jquery.com/jquery-3.3.1.min.js"></x:sri-script>

Contributing

Please see CONTRIBUTING for more details.

License

This project and the Laravel framework are open-sourced software licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].