Merge pull request #94 from snshn/no-integrity

Get rid of integrity attributes
This commit is contained in:
Sunshine 2019-12-26 10:11:52 -05:00 committed by GitHub
commit 13d2ea1607
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 2 deletions

2
Cargo.lock generated
View file

@ -625,7 +625,7 @@ dependencies = [
[[package]] [[package]]
name = "monolith" name = "monolith"
version = "2.1.0" version = "2.1.1"
dependencies = [ dependencies = [
"base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "monolith" name = "monolith"
version = "2.1.0" version = "2.1.1"
authors = [ authors = [
"Sunshine <sunshine@uberspace.net>", "Sunshine <sunshine@uberspace.net>",
"Mahdi Robatipoor <mahdi.robatipoor@gmail.com>", "Mahdi Robatipoor <mahdi.robatipoor@gmail.com>",

View file

@ -79,6 +79,17 @@ pub fn walk_and_embed_assets(
"link" => { "link" => {
let mut link_type: &str = ""; let mut link_type: &str = "";
// Remove integrity attributes
let mut i = 0;
while i < attrs_mut.len() {
let attr_name = attrs_mut[i].name.local.as_ref();
if attr_name.eq_ignore_ascii_case("integrity") {
attrs_mut.remove(i);
} else {
i += 1;
}
}
for attr in attrs_mut.iter_mut() { for attr in attrs_mut.iter_mut() {
if &attr.name.local == "rel" { if &attr.name.local == "rel" {
if is_icon(&attr.value.to_string()) { if is_icon(&attr.value.to_string()) {
@ -263,6 +274,17 @@ pub fn walk_and_embed_assets(
} }
} }
"script" => { "script" => {
// Remove integrity attributes
let mut i = 0;
while i < attrs_mut.len() {
let attr_name = attrs_mut[i].name.local.as_ref();
if attr_name.eq_ignore_ascii_case("integrity") {
attrs_mut.remove(i);
} else {
i += 1;
}
}
if opt_no_js { if opt_no_js {
// Empty src and inner content of SCRIPT tags // Empty src and inner content of SCRIPT tags
for attr in attrs_mut.iter_mut() { for attr in attrs_mut.iter_mut() {

View file

@ -298,6 +298,45 @@ fn test_walk_and_embed_assets_no_js() {
); );
} }
#[test]
fn test_walk_and_embed_with_no_integrity() {
let html = "<title>No integrity</title>\
<link integrity=\"sha384-...\" rel=\"something\"/>\
<script integrity=\"sha384-...\" src=\"some.js\"></script>";
let dom = html_to_dom(&html);
let url = "http://localhost";
let cache = &mut HashMap::new();
let client = reqwest::Client::new();
let opt_no_css: bool = true;
let opt_no_frames: bool = true;
let opt_no_js: bool = true;
let opt_no_images: bool = true;
let opt_silent = true;
walk_and_embed_assets(
cache,
&client,
&url,
&dom.document,
opt_no_css,
opt_no_js,
opt_no_images,
opt_silent,
opt_no_frames,
);
let mut buf: Vec<u8> = Vec::new();
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
assert_eq!(
buf.iter().map(|&c| c as char).collect::<String>(),
"<html>\
<head><title>No integrity</title><link rel=\"something\"><script src=\"\"></script></head>\
<body></body>\
</html>"
);
}
#[test] #[test]
fn test_stringify_document() { fn test_stringify_document() {
let html = "<div><script src=\"some.js\"></script></div>"; let html = "<div><script src=\"some.js\"></script></div>";