Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add subresource integrity to css/js #494

Open
unuseless opened this issue Jun 27, 2017 · 4 comments
Open

add subresource integrity to css/js #494

unuseless opened this issue Jun 27, 2017 · 4 comments

Comments

@unuseless
Copy link

Allow the creation of a signature for subresource integrity for the created files.
see: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity

@sar009
Copy link

sar009 commented Feb 4, 2019

Any update?

@vulcan25
Copy link

vulcan25 commented Mar 7, 2019

I'm trying to work out how to make this work with webassets.

This Python code can generate the hash:

import base64
import hashlib
BUF_SIZE = 65536
def checksum(input):
    hash = hashlib.sha256()
    with open(input, 'rb') as f:
        while True:
            data = f.read(BUF_SIZE)
            if not data:
                break
            hash.update(data)
    hash = hash.digest()
    hash_base64 = base64.b64encode(hash).decode()
    return 'sha256-{}'.format(hash_base64)

To test this I use a link copied with SRI tag from cdnjs, and wget the CSS file to my working directory, then:

>>> checksum('bootstrap-grid.css')
'sha256-cCazLItaM+Zz5UEzu9HNzlgWhXlvknCzjdE45LBeTns='

The hash of this file from the cdnjs provided link tag: integrity="sha256-cCazLItaM+Zz5UEzu9HNzlgWhXlvknCzjdE45LBeTns="

This looks good, but I'm not sure how webassets integration would or should work.

It would be perfect if when I define the link tag:

{% assets "css_all" %}
<link rel="stylesheet" media='screen' href="{{ ASSET_URL }}" />
{% endassets %}

I could then reference a variable like ASSET_SNI.

But there's also the question of where I call the code to generate these hashes in the first place, and where to store them.

@VorpalBlade
Copy link
Contributor

Presumably this should be implemented in the jinja2 extension to expose a new variable (or use the EXTRA variable documented there).

VorpalBlade added a commit to VorpalBlade/webassets that referenced this issue May 25, 2019
This is the start of the implementation of miracle2k#494

Currently only tested on Python 3.7, missing unit tests and documentation.

Basic idea is to extend the Bundle.urls() function to return a list of dictionaries {'uri': <same as before>, 'sri': <hash>} but only when requested (to not break existing users). A dict is used instead of a tuple to allow adding other values in the future. Currently only works for local resources.
@VorpalBlade
Copy link
Contributor

I have a POC implementation, which I will extend with unit tests. I will not write any code for django etc, only jinja2 (since it is the only one I can test).

Furthermore, I will not be able to test any versions of Python except 3.7 and 2.7.

VorpalBlade added a commit to VorpalBlade/webassets that referenced this issue May 30, 2019
VorpalBlade added a commit to VorpalBlade/webassets that referenced this issue May 30, 2019
This makes it work better with nested bundles and removes the possibility of mixed settings.

This commit also adds unit tests for the bundle class.

Part of implementing miracle2k#494
VorpalBlade added a commit to VorpalBlade/webassets that referenced this issue May 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants