From 357b48645599574d6b6d9a3f94e3c6bd9a9cf66e Mon Sep 17 00:00:00 2001 From: Alexandrov Sergey Date: Sun, 17 May 2020 13:50:11 +0300 Subject: make is_origin_trustworthy a method of ServoUrl + fix localhost handling --- components/url/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'components/url') diff --git a/components/url/lib.rs b/components/url/lib.rs index 411d3e94683..8a05837a461 100644 --- a/components/url/lib.rs +++ b/components/url/lib.rs @@ -169,6 +169,32 @@ impl ServoUrl { pub fn from_file_path>(path: P) -> Result { Ok(Self::from_url(Url::from_file_path(path)?)) } + + // https://w3c.github.io/webappsec-secure-contexts/#is-origin-trustworthy + pub fn is_origin_trustworthy(&self) -> bool { + // Step 1 + if !self.origin().is_tuple() { + return false; + } + + // Step 3 + if self.scheme() == "https" || self.scheme() == "wss" { + true + // Steps 4-5 + } else if self.host().is_some() { + let host = self.host_str().unwrap(); + // Step 4 + if let Ok(ip_addr) = host.parse::() { + ip_addr.is_loopback() + // Step 5 + } else { + host == "localhost" || host.ends_with(".localhost") + } + // Step 6 + } else { + self.scheme() == "file" + } + } } impl fmt::Display for ServoUrl { -- cgit v1.2.3