From de4c97c74b75842eb601d9c0afb80227d38f52d0 Mon Sep 17 00:00:00 2001 From: Zoey Date: Thu, 19 Aug 2021 12:48:22 -0700 Subject: [PATCH] Remove referrer information from external links --- src/lib.rs | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 81a6a21..69d7c0b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ pub mod serving; use std::{ collections::HashMap, path::{Path, PathBuf}, + str::FromStr, }; use anyhow::Context; @@ -16,6 +17,7 @@ use lol_html::{element, html_content::ContentType, HtmlRewriter, Settings}; use pulldown_cmark::{Options, Parser}; use serde::{Deserialize, Serialize}; use walkdir::WalkDir; +use warp::hyper::Uri; const PAGES_PATH: &str = "pages"; const TEMPLATES_PATH: &str = "templates"; @@ -247,23 +249,37 @@ impl<'a> SiteBuilder<'a> { let mut output = Vec::new(); let mut rewriter = HtmlRewriter::new( Settings { - element_content_handlers: vec![element!("head", |el| { - el.prepend(r#""#, ContentType::Html); - el.append(&format!("{}", title), ContentType::Html); - if self.serving { - el.append( - &format!(r#""#, STATIC_PATH), - ContentType::Html, - ); - } else { - el.append( - &format!(r#""#, &self.site.config.base_url), - ContentType::Html, - ); - } + element_content_handlers: vec![ + element!("head", |el| { + el.prepend(r#""#, ContentType::Html); + el.append(&format!("{}", title), ContentType::Html); + if self.serving { + el.append( + &format!(r#""#, STATIC_PATH), + ContentType::Html, + ); + } else { + el.append( + &format!(r#""#, &self.site.config.base_url), + ContentType::Html, + ); + } - Ok(()) - })], + Ok(()) + }), + element!("a", |el| { + if let Some(href) = el.get_attribute("href") { + if let Ok(uri) = Uri::from_str(&href) { + if uri.host().is_some() { + el.set_attribute("rel", "noopener noreferrer")?; + el.set_attribute("target", "_blank")?; + } + } + } + + Ok(()) + }), + ], ..Default::default() }, |c: &[u8]| output.extend_from_slice(c),