From b0faca2f215e772e3984764a34ad1e328a4637d6 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 2 May 2020 20:28:19 +0100 Subject: [PATCH] Replace SRI template tag with package --- requirements.txt | 1 + templates/base.html | 8 ++-- website/common/templatetags/__init__.py | 0 website/common/templatetags/sri.py | 51 ------------------------- website/settings.py | 1 + 5 files changed, 6 insertions(+), 55 deletions(-) delete mode 100644 website/common/templatetags/__init__.py delete mode 100644 website/common/templatetags/sri.py diff --git a/requirements.txt b/requirements.txt index 0119575..a4b1d5f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ whitenoise==5.0.1 brotli==1.0.7 django-environ==0.4.5 gunicorn ==20.0.4 +django-sri==0.1.2 diff --git a/templates/base.html b/templates/base.html index a9c4747..495ea83 100644 --- a/templates/base.html +++ b/templates/base.html @@ -15,8 +15,8 @@ {% block title %}{% endblock %} :: TheOrangeOne - {% sri_css "css/font-awesome.min.css" %} - {% sri_css "css/index.css" %} + {% sri "css/font-awesome.min.css" %} + {% sri "css/index.css" %} @@ -51,8 +51,8 @@ {% endblock %} - {% sri_js "js/materialize.min.js" %} - {% sri_js "js/index.js" %} + {% sri "js/materialize.min.js" %} + {% sri "js/index.js" %} {% block extrascripts %}{% endblock %} diff --git a/website/common/templatetags/__init__.py b/website/common/templatetags/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/website/common/templatetags/sri.py b/website/common/templatetags/sri.py deleted file mode 100644 index c8c9b02..0000000 --- a/website/common/templatetags/sri.py +++ /dev/null @@ -1,51 +0,0 @@ -# Based off https://github.com/claudep/django/commit/89aa4c04dbffcbafc05c3e2053b2262be8de4d3d - -import base64 -import hashlib -import os -from functools import lru_cache - -from django import template -from django.conf import settings -from django.templatetags.static import static -from django.utils.safestring import mark_safe - -register = template.Library() - - -@lru_cache -def generate_sha256(path): - with open(path, "r") as f: - body = f.read() - digest = hashlib.sha256(body.encode()).digest() - sha = base64.b64encode(digest).decode() - return "sha256-{}".format(sha) - - -def attrs_to_str(attrs): - return " ".join('{}="{}"'.format(k, v) for k, v in attrs.items()) - - -@register.simple_tag -def sri_js(url): - path = os.path.join(settings.STATIC_ROOT, url) - attrs = { - "src": static(url), - "type": "text/javascript", - "integrity": generate_sha256(path), - "crossorigin": "anonymous", - } - return mark_safe(f"") - - -@register.simple_tag -def sri_css(url): - path = os.path.join(settings.STATIC_ROOT, url) - attrs = { - "href": static(url), - "type": "text/css", - "rel": "stylesheet", - "integrity": generate_sha256(path), - "crossorigin": "anonymous", - } - return mark_safe(f"") diff --git a/website/settings.py b/website/settings.py index 246200c..c657eaa 100644 --- a/website/settings.py +++ b/website/settings.py @@ -38,6 +38,7 @@ INSTALLED_APPS = [ "whitenoise.runserver_nostatic", "django.contrib.staticfiles", "debug_toolbar", + "sri", "website.common", ]