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

Sub-resource integrity #521

Merged
merged 4 commits into from
Nov 18, 2019
Merged

Conversation

VorpalBlade
Copy link
Contributor

This pull request implements #494, by adding a flag to the Bundle.urls() method (calculate_sri) that defaults to false. When set to true it changes the return value from a list of URIs to a list of (using syntax based on Python 3's typing module):

{'uri': str, sri: Optional[str]}

For example:

[{'uri': '/a',
  'sri': 'sha384-OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb'}]

This is then used by the jinja2 module to expose a new variable ASSERT_SRI. that can be used something like this:

{%- assets filters="cssutils", output="css/min.css", "css/reset.css", "css/site.css" -%}
    <link rel="stylesheet" href="{{ ASSET_URL }}" integrity="{{ ASSET_SRI }}">
{%- endassets -%}

Note that the SRI is not computed for remote resources, in that case None is returned. This should be handled properly, but obviously it is something that the template write will have to deal with if it can happen in their environment (and they also want to use the integrity features for local resources).

This could be extended in the future to download and calculate the hash for remote resources, but there is an inherent trust/security issue, should that resource change between builds when you don't expect it to, so I'm not sure it would be a good idea.

This has been tested on Python 2.7 and 3.7, I do not have easy ability to test anything else, and there seems to be some issue with failing unit tests in general for this project in the Travis CI setup.

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.
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
miracle2k added a commit that referenced this pull request Nov 18, 2019
@miracle2k miracle2k merged commit 967844f into miracle2k:master Nov 18, 2019
@miracle2k
Copy link
Owner

Sorry for the delay in getting this merged. Good work.

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

Successfully merging this pull request may close these issues.

2 participants