mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Implement HSTS fetch step
Implemented step nine of the main fetch. If current URL scheme is 'HTTP' and current URL's host is domain and if current URL's host matched with Known HSTS Host Domain Name Matching results in either a superdomain match with an asserted includeSubDomains directive or a congruent match then we change request scheme to 'https'. This change has been made in method.rs A test case to validate this has been added in fetch.rs. For asserting https scheme, a https localhost was required. For this purpose I have created a self-signed certificate and refactored fetch-context and connector.rs to programmatically trust this certificate for running this test case.
This commit is contained in:
parent
7e3c9e2197
commit
6020b4c15c
8 changed files with 117 additions and 9 deletions
|
@ -18,11 +18,17 @@ use hyper::header::{Encoding, Location, Pragma, Quality, QualityItem, SetCookie,
|
|||
use hyper::header::{Headers, Host, HttpDate, Referer as HyperReferer};
|
||||
use hyper::method::Method;
|
||||
use hyper::mime::{Mime, SubLevel, TopLevel};
|
||||
use hyper::server::{Request as HyperRequest, Response as HyperResponse};
|
||||
use hyper::net::Openssl;
|
||||
use hyper::server::{Request as HyperRequest, Response as HyperResponse, Server};
|
||||
use hyper::status::StatusCode;
|
||||
use hyper::uri::RequestUri;
|
||||
use msg::constellation_msg::TEST_PIPELINE_ID;
|
||||
use net::fetch::cors_cache::CorsCache;
|
||||
use net::fetch::methods::FetchContext;
|
||||
use net::filemanager_thread::FileManager;
|
||||
use net::hsts::HstsEntry;
|
||||
use net::test::HttpState;
|
||||
use net_traits::IncludeSubdomains;
|
||||
use net_traits::NetworkError;
|
||||
use net_traits::ReferrerPolicy;
|
||||
use net_traits::request::{Origin, RedirectMode, Referrer, Request, RequestMode};
|
||||
|
@ -506,6 +512,50 @@ fn test_fetch_with_local_urls_only() {
|
|||
assert!(server_response.is_network_error());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fetch_with_hsts() {
|
||||
static MESSAGE: &'static [u8] = b"";
|
||||
let handler = move |_: HyperRequest, response: HyperResponse| {
|
||||
response.send(MESSAGE).unwrap();
|
||||
};
|
||||
|
||||
let path = resources_dir_path().expect("Cannot find resource dir");
|
||||
let mut cert_path = path.clone();
|
||||
cert_path.push("self_signed_certificate_for_testing.crt");
|
||||
|
||||
let mut key_path = path.clone();
|
||||
key_path.push("privatekey_for_testing.key");
|
||||
|
||||
let ssl = Openssl::with_cert_and_key(cert_path.into_os_string(), key_path.into_os_string())
|
||||
.unwrap();
|
||||
|
||||
let mut server = Server::https("0.0.0.0:0", ssl).unwrap().handle_threads(handler, 1).unwrap();
|
||||
|
||||
let context = FetchContext {
|
||||
state: HttpState::new("self_signed_certificate_for_testing.crt"),
|
||||
user_agent: DEFAULT_USER_AGENT.into(),
|
||||
devtools_chan: None,
|
||||
filemanager: FileManager::new(),
|
||||
};
|
||||
|
||||
{
|
||||
let mut list = context.state.hsts_list.write().unwrap();
|
||||
list.push(HstsEntry::new("localhost".to_owned(), IncludeSubdomains::NotIncluded, None)
|
||||
.unwrap());
|
||||
}
|
||||
let url_string = format!("http://localhost:{}", server.socket.port());
|
||||
let url = ServoUrl::parse(&url_string).unwrap();
|
||||
let origin = Origin::Origin(url.origin());
|
||||
let mut request = Request::new(url, Some(origin), false, None);
|
||||
*request.referrer.borrow_mut() = Referrer::NoReferrer;
|
||||
// Set the flag.
|
||||
request.local_urls_only = false;
|
||||
let response = fetch_with_context(request, &context);
|
||||
let _ = server.close();
|
||||
assert_eq!(response.internal_response.unwrap().url().unwrap().scheme(),
|
||||
"https");
|
||||
}
|
||||
|
||||
fn setup_server_and_fetch(message: &'static [u8], redirect_cap: u32) -> Response {
|
||||
let handler = move |request: HyperRequest, mut response: HyperResponse| {
|
||||
let redirects = match request.uri {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue