"""Third-party assets are pinned (QUA-004).
Two CDNs serve this site: cdnjs for Font Awesome, jsDelivr for FullCalendar.
The CSP names both as allowed origins, so neither would be stopped by it —
whoever controls those hosts controls what runs on every page, and on the
calendar page that includes executable script.
`integrity` pins each file: the browser refuses one that has been altered
since the hash was taken. It does not prove the file was honest at that
moment. Worth being clear about, because SRI is often read as more than it
is.
This is a guard, not a test of behaviour. Its job is to fail the day someone
adds a CDN URL without a hash, or bumps a version and leaves the old hash
behind — the second of which fails *silently* in a browser, since a script
that fails its integrity check simply does not run.
"""
import re
import pytest
TEMPLATES = 'app/templates'
#: Hosts we deliberately load from. Anything else on a template needs a
#: decision, not a hash.
KNOWN_CDNS = ('cdnjs.cloudflare.com', 'cdn.jsdelivr.net')
#: Discord avatars. Not a subresource — an
src built at render time,
#: which cannot carry an integrity hash and executes nothing.
IMAGE_ONLY_HOSTS = ('cdn.discordapp.com',)
def _templates():
import os
for root, _dirs, files in os.walk(TEMPLATES):
for name in files:
if name.endswith('.html'):
path = os.path.join(root, name)
with open(path, encoding='utf-8') as handle:
yield path, handle.read()
def _tags_loading_from(host, markup):
"""Every