forked from mail-in-a-box/mailinabox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use subresource integrity attributes to guard against CDNs being used…
… as an attack vector; drop external resources that we can't protect this way (fonts); fixes mail-in-a-box#234
- Loading branch information
Showing
2 changed files
with
29 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/python3 | ||
# Updates subresource integrity attributes in management/templates/index.html | ||
# to prevent CDN-hosted resources from being used as an attack vector. Run this | ||
# after updating the Bootstrap and jQuery <link> and <script> to compute the | ||
# appropriate hash and insert it into the template. | ||
|
||
import re, urllib.request, hashlib, base64 | ||
|
||
fn = "management/templates/index.html" | ||
|
||
with open(fn, 'r') as f: | ||
content = f.read() | ||
|
||
def make_integrity(url): | ||
resource = urllib.request.urlopen(url).read() | ||
return "sha256-" + base64.b64encode(hashlib.sha256(resource).digest()).decode('ascii') | ||
|
||
content = re.sub( | ||
r'<(link rel="stylesheet" href|script src)="(.*?)" integrity="(.*?)"', | ||
lambda m : '<' + m.group(1) + '="' + m.group(2) + '" integrity="' + make_integrity(m.group(2)) + '"', | ||
content) | ||
|
||
with open(fn, 'w') as f: | ||
f.write(content) |