2020-03-23 00:03:33 +01:00
|
|
|
use crate::html;
|
2019-09-29 23:15:49 +02:00
|
|
|
use html5ever::serialize::{serialize, SerializeOpts};
|
2020-01-07 05:22:28 +01:00
|
|
|
use reqwest::blocking::Client;
|
2019-10-23 00:33:22 +02:00
|
|
|
use std::collections::HashMap;
|
2019-09-29 23:15:49 +02:00
|
|
|
|
2020-03-23 03:08:41 +01:00
|
|
|
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
|
|
|
|
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
|
|
|
|
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
|
|
|
|
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║
|
|
|
|
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
|
|
|
|
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
|
2019-09-29 23:15:49 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-03-23 03:08:41 +01:00
|
|
|
fn passing_basic() {
|
2019-10-23 00:33:22 +02:00
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
2019-09-29 23:15:49 +02:00
|
|
|
let html = "<div><P></P></div>";
|
2020-03-23 00:03:33 +01:00
|
|
|
let dom = html::html_to_dom(&html);
|
2019-09-29 23:15:49 +02:00
|
|
|
let url = "http://localhost";
|
|
|
|
|
|
|
|
let opt_no_css: bool = false;
|
|
|
|
let opt_no_frames: bool = false;
|
|
|
|
let opt_no_js: bool = false;
|
|
|
|
let opt_no_images: bool = false;
|
|
|
|
let opt_silent = true;
|
2019-12-10 05:48:46 +01:00
|
|
|
|
2020-01-07 05:22:28 +01:00
|
|
|
let client = Client::new();
|
2019-09-29 23:15:49 +02:00
|
|
|
|
2020-03-23 00:03:33 +01:00
|
|
|
html::walk_and_embed_assets(
|
2019-10-23 00:33:22 +02:00
|
|
|
cache,
|
2019-12-10 05:48:46 +01:00
|
|
|
&client,
|
2019-09-29 23:15:49 +02:00
|
|
|
&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></head><body><div><p></p></div></body></html>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-23 03:08:41 +01:00
|
|
|
fn passing_ensure_no_recursive_iframe() {
|
2019-09-29 23:15:49 +02:00
|
|
|
let html = "<div><P></P><iframe src=\"\"></iframe></div>";
|
2020-03-23 00:03:33 +01:00
|
|
|
let dom = html::html_to_dom(&html);
|
2019-09-29 23:15:49 +02:00
|
|
|
let url = "http://localhost";
|
2019-10-23 00:33:22 +02:00
|
|
|
let cache = &mut HashMap::new();
|
2019-09-29 23:15:49 +02:00
|
|
|
|
|
|
|
let opt_no_css: bool = false;
|
|
|
|
let opt_no_frames: bool = false;
|
|
|
|
let opt_no_js: bool = false;
|
|
|
|
let opt_no_images: bool = false;
|
|
|
|
let opt_silent = true;
|
2019-12-10 05:48:46 +01:00
|
|
|
|
2020-01-07 05:22:28 +01:00
|
|
|
let client = Client::new();
|
2019-09-29 23:15:49 +02:00
|
|
|
|
2020-03-23 00:03:33 +01:00
|
|
|
html::walk_and_embed_assets(
|
2019-10-23 00:33:22 +02:00
|
|
|
cache,
|
2019-12-10 05:48:46 +01:00
|
|
|
&client,
|
2019-09-29 23:15:49 +02:00
|
|
|
&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></head><body><div><p></p><iframe src=\"\"></iframe></div></body></html>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-24 06:06:31 +01:00
|
|
|
#[test]
|
2020-03-23 03:08:41 +01:00
|
|
|
fn passing_ensure_no_recursive_frame() {
|
2020-02-24 06:06:31 +01:00
|
|
|
let html = "<frameset><frame src=\"\"></frameset>";
|
2020-03-23 00:03:33 +01:00
|
|
|
let dom = html::html_to_dom(&html);
|
2020-02-24 06:06:31 +01:00
|
|
|
let url = "http://localhost";
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
|
|
|
let opt_no_css: bool = false;
|
|
|
|
let opt_no_frames: bool = false;
|
|
|
|
let opt_no_js: bool = false;
|
|
|
|
let opt_no_images: bool = false;
|
|
|
|
let opt_silent = true;
|
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
2020-03-23 00:03:33 +01:00
|
|
|
html::walk_and_embed_assets(
|
2020-02-24 06:06:31 +01:00
|
|
|
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></head><frameset><frame src=\"\"></frameset></html>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-29 23:15:49 +02:00
|
|
|
#[test]
|
2020-03-23 03:08:41 +01:00
|
|
|
fn passing_no_css() {
|
2019-09-29 23:15:49 +02:00
|
|
|
let html = "<link rel=\"stylesheet\" href=\"main.css\">\
|
|
|
|
<style>html{background-color: #000;}</style>\
|
|
|
|
<div style=\"display: none;\"></div>";
|
2020-03-23 00:03:33 +01:00
|
|
|
let dom = html::html_to_dom(&html);
|
2019-09-29 23:15:49 +02:00
|
|
|
let url = "http://localhost";
|
2019-10-23 00:33:22 +02:00
|
|
|
let cache = &mut HashMap::new();
|
2019-09-29 23:15:49 +02:00
|
|
|
|
|
|
|
let opt_no_css: bool = true;
|
|
|
|
let opt_no_frames: bool = false;
|
|
|
|
let opt_no_js: bool = false;
|
|
|
|
let opt_no_images: bool = false;
|
|
|
|
let opt_silent = true;
|
2020-01-07 05:22:28 +01:00
|
|
|
let client = Client::new();
|
2019-09-29 23:15:49 +02:00
|
|
|
|
2020-03-23 00:03:33 +01:00
|
|
|
html::walk_and_embed_assets(
|
2019-10-23 00:33:22 +02:00
|
|
|
cache,
|
2019-12-10 05:48:46 +01:00
|
|
|
&client,
|
2019-09-29 23:15:49 +02:00
|
|
|
&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>\
|
|
|
|
<link rel=\"stylesheet\" href=\"\">\
|
|
|
|
<style></style>\
|
|
|
|
</head>\
|
|
|
|
<body>\
|
|
|
|
<div></div>\
|
|
|
|
</body>\
|
|
|
|
</html>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-23 03:08:41 +01:00
|
|
|
fn passing_no_images() {
|
2019-09-29 23:15:49 +02:00
|
|
|
let html = "<link rel=\"icon\" href=\"favicon.ico\">\
|
|
|
|
<div><img src=\"http://localhost/assets/mono_lisa.png\" /></div>";
|
2020-03-23 00:03:33 +01:00
|
|
|
let dom = html::html_to_dom(&html);
|
2019-09-29 23:15:49 +02:00
|
|
|
let url = "http://localhost";
|
2019-10-23 00:33:22 +02:00
|
|
|
let cache = &mut HashMap::new();
|
2019-09-29 23:15:49 +02:00
|
|
|
|
|
|
|
let opt_no_css: bool = false;
|
|
|
|
let opt_no_frames: bool = false;
|
|
|
|
let opt_no_js: bool = false;
|
|
|
|
let opt_no_images: bool = true;
|
|
|
|
let opt_silent = true;
|
2019-12-10 05:48:46 +01:00
|
|
|
|
2020-01-07 05:22:28 +01:00
|
|
|
let client = Client::new();
|
2019-09-29 23:15:49 +02:00
|
|
|
|
2020-03-23 00:03:33 +01:00
|
|
|
html::walk_and_embed_assets(
|
2019-10-23 00:33:22 +02:00
|
|
|
cache,
|
2019-12-10 05:48:46 +01:00
|
|
|
&client,
|
2019-09-29 23:15:49 +02:00
|
|
|
&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>(),
|
2020-04-03 09:30:52 +02:00
|
|
|
format!(
|
|
|
|
"<html>\
|
2019-09-29 23:15:49 +02:00
|
|
|
<head>\
|
|
|
|
<link rel=\"icon\" href=\"\">\
|
|
|
|
</head>\
|
|
|
|
<body>\
|
|
|
|
<div>\
|
2020-04-03 09:30:52 +02:00
|
|
|
<img src=\"{empty_image}\">\
|
2019-09-29 23:15:49 +02:00
|
|
|
</div>\
|
|
|
|
</body>\
|
2020-04-03 09:30:52 +02:00
|
|
|
</html>",
|
|
|
|
empty_image = empty_image!()
|
|
|
|
)
|
2019-09-29 23:15:49 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-05 10:56:09 +01:00
|
|
|
#[test]
|
2020-03-23 03:08:41 +01:00
|
|
|
fn passing_no_body_background_images() {
|
2020-03-05 10:56:09 +01:00
|
|
|
let html = "<body background=\"no/such/image.png\" background=\"no/such/image2.png\"></body>";
|
2020-03-23 00:03:33 +01:00
|
|
|
let dom = html::html_to_dom(&html);
|
2020-03-05 10:56:09 +01:00
|
|
|
let url = "http://localhost";
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
|
|
|
let opt_no_css: bool = false;
|
|
|
|
let opt_no_frames: bool = false;
|
|
|
|
let opt_no_js: bool = false;
|
|
|
|
let opt_no_images: bool = true;
|
|
|
|
let opt_silent = true;
|
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
2020-03-23 00:03:33 +01:00
|
|
|
html::walk_and_embed_assets(
|
2020-03-05 10:56:09 +01:00
|
|
|
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></head><body></body></html>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-29 23:15:49 +02:00
|
|
|
#[test]
|
2020-03-23 03:08:41 +01:00
|
|
|
fn passing_no_frames() {
|
2020-02-24 06:06:31 +01:00
|
|
|
let html = "<frameset><frame src=\"http://trackbook.com\"></frameset>";
|
2020-03-23 00:03:33 +01:00
|
|
|
let dom = html::html_to_dom(&html);
|
2020-02-24 06:06:31 +01:00
|
|
|
let url = "http://localhost";
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
|
|
|
let opt_no_css: bool = false;
|
|
|
|
let opt_no_frames: bool = true;
|
|
|
|
let opt_no_js: bool = false;
|
|
|
|
let opt_no_images: bool = false;
|
|
|
|
let opt_silent = true;
|
|
|
|
let client = Client::new();
|
|
|
|
|
2020-03-23 00:03:33 +01:00
|
|
|
html::walk_and_embed_assets(
|
2020-02-24 06:06:31 +01:00
|
|
|
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></head><frameset><frame src=\"\"></frameset></html>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-23 03:08:41 +01:00
|
|
|
fn passing_no_iframes() {
|
2019-09-29 23:15:49 +02:00
|
|
|
let html = "<iframe src=\"http://trackbook.com\"></iframe>";
|
2020-03-23 00:03:33 +01:00
|
|
|
let dom = html::html_to_dom(&html);
|
2019-09-29 23:15:49 +02:00
|
|
|
let url = "http://localhost";
|
2019-10-23 00:33:22 +02:00
|
|
|
let cache = &mut HashMap::new();
|
2019-09-29 23:15:49 +02:00
|
|
|
|
|
|
|
let opt_no_css: bool = false;
|
|
|
|
let opt_no_frames: bool = true;
|
|
|
|
let opt_no_js: bool = false;
|
|
|
|
let opt_no_images: bool = false;
|
|
|
|
let opt_silent = true;
|
2020-01-07 05:22:28 +01:00
|
|
|
let client = Client::new();
|
2019-09-29 23:15:49 +02:00
|
|
|
|
2020-03-23 00:03:33 +01:00
|
|
|
html::walk_and_embed_assets(
|
2019-10-23 00:33:22 +02:00
|
|
|
cache,
|
2019-12-10 05:48:46 +01:00
|
|
|
&client,
|
2019-09-29 23:15:49 +02:00
|
|
|
&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></head><body><iframe src=\"\"></iframe></body></html>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-23 03:08:41 +01:00
|
|
|
fn passing_no_js() {
|
2019-09-29 23:15:49 +02:00
|
|
|
let html = "<div onClick=\"void(0)\">\
|
|
|
|
<script src=\"http://localhost/assets/some.js\"></script>\
|
|
|
|
<script>alert(1)</script>\
|
|
|
|
</div>";
|
2020-03-23 00:03:33 +01:00
|
|
|
let dom = html::html_to_dom(&html);
|
2019-09-29 23:15:49 +02:00
|
|
|
let url = "http://localhost";
|
2019-10-23 00:33:22 +02:00
|
|
|
let cache = &mut HashMap::new();
|
2019-09-29 23:15:49 +02:00
|
|
|
|
|
|
|
let opt_no_css: bool = false;
|
|
|
|
let opt_no_frames: bool = false;
|
|
|
|
let opt_no_js: bool = true;
|
|
|
|
let opt_no_images: bool = false;
|
|
|
|
let opt_silent = true;
|
2019-12-10 05:48:46 +01:00
|
|
|
|
2020-01-07 05:22:28 +01:00
|
|
|
let client = Client::new();
|
2019-09-29 23:15:49 +02:00
|
|
|
|
2020-03-23 00:03:33 +01:00
|
|
|
html::walk_and_embed_assets(
|
2019-10-23 00:33:22 +02:00
|
|
|
cache,
|
2019-12-10 05:48:46 +01:00
|
|
|
&client,
|
2019-09-29 23:15:49 +02:00
|
|
|
&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></head><body><div><script src=\"\"></script>\
|
|
|
|
<script></script></div></body></html>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-26 15:44:01 +01:00
|
|
|
#[test]
|
2020-03-23 03:08:41 +01:00
|
|
|
fn passing_with_no_integrity() {
|
2019-12-26 15:44:01 +01:00
|
|
|
let html = "<title>No integrity</title>\
|
|
|
|
<link integrity=\"sha384-...\" rel=\"something\"/>\
|
|
|
|
<script integrity=\"sha384-...\" src=\"some.js\"></script>";
|
2020-03-23 00:03:33 +01:00
|
|
|
let dom = html::html_to_dom(&html);
|
2019-12-26 15:44:01 +01:00
|
|
|
let url = "http://localhost";
|
|
|
|
let cache = &mut HashMap::new();
|
2020-01-07 05:22:28 +01:00
|
|
|
let client = Client::new();
|
2019-12-26 15:44:01 +01:00
|
|
|
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;
|
|
|
|
|
2020-03-23 00:03:33 +01:00
|
|
|
html::walk_and_embed_assets(
|
2019-12-26 15:44:01 +01:00
|
|
|
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>"
|
|
|
|
);
|
|
|
|
}
|