2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-04-14 20:38:48 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-07-06 11:24:29 +02:00
|
|
|
//! Hyper handlers implementations.
|
2016-04-14 20:38:48 +02:00
|
|
|
|
2016-07-06 11:24:29 +02:00
|
|
|
mod content;
|
2016-12-27 11:15:02 +01:00
|
|
|
mod echo;
|
2016-08-18 12:19:09 +02:00
|
|
|
mod fetch;
|
2016-12-27 11:15:02 +01:00
|
|
|
mod redirect;
|
|
|
|
mod streaming;
|
2016-07-06 11:24:29 +02:00
|
|
|
|
|
|
|
pub use self::content::ContentHandler;
|
2016-12-27 11:15:02 +01:00
|
|
|
pub use self::echo::EchoHandler;
|
|
|
|
pub use self::fetch::{ContentFetcherHandler, ContentValidator, FetchControl, ValidatorResponse};
|
2016-07-06 11:24:29 +02:00
|
|
|
pub use self::redirect::Redirection;
|
2016-12-27 11:15:02 +01:00
|
|
|
pub use self::streaming::StreamingHandler;
|
2016-07-07 09:42:49 +02:00
|
|
|
|
|
|
|
use url::Url;
|
|
|
|
use hyper::{server, header, net, uri};
|
2016-11-09 19:41:47 +01:00
|
|
|
use address;
|
2016-07-07 09:42:49 +02:00
|
|
|
|
2016-10-19 11:02:21 +02:00
|
|
|
/// Adds security-related headers to the Response.
|
2016-11-09 19:41:47 +01:00
|
|
|
pub fn add_security_headers(headers: &mut header::Headers, embeddable_on: Option<(String, u16)>) {
|
2016-10-19 11:02:21 +02:00
|
|
|
headers.set_raw("X-XSS-Protection", vec![b"1; mode=block".to_vec()]);
|
|
|
|
headers.set_raw("X-Content-Type-Options", vec![b"nosniff".to_vec()]);
|
|
|
|
|
|
|
|
// Embedding header:
|
2017-06-28 09:12:02 +02:00
|
|
|
if let Some(ref embeddable_on) = embeddable_on {
|
|
|
|
headers.set_raw("X-Frame-Options", vec![
|
|
|
|
format!("ALLOW-FROM http://{}", address(embeddable_on)).into_bytes()
|
|
|
|
]);
|
2016-10-19 11:02:21 +02:00
|
|
|
} else {
|
|
|
|
// TODO [ToDr] Should we be more strict here (DENY?)?
|
|
|
|
headers.set_raw("X-Frame-Options", vec![b"SAMEORIGIN".to_vec()]);
|
|
|
|
}
|
2017-06-28 09:12:02 +02:00
|
|
|
|
|
|
|
// Content Security Policy headers
|
|
|
|
headers.set_raw("Content-Security-Policy", vec![
|
|
|
|
// Allow connecting to WS servers and HTTP(S) servers.
|
|
|
|
// We could be more restrictive and allow only RPC server URL.
|
|
|
|
b"connect-src http: https: ws: wss:;".to_vec(),
|
|
|
|
// Allow framing any content from HTTP(S).
|
|
|
|
// Again we could only allow embedding from RPC server URL.
|
|
|
|
// (deprecated)
|
|
|
|
b"frame-src 'self' http: https:;".to_vec(),
|
|
|
|
// Allow framing and web workers from HTTP(S).
|
|
|
|
b"child-src 'self' http: https:;".to_vec(),
|
|
|
|
// We allow data: blob: and HTTP(s) images.
|
|
|
|
// We could get rid of wildcarding HTTP and only allow RPC server URL.
|
|
|
|
// (http require for local dapps icons)
|
|
|
|
b"img-src 'self' 'unsafe-inline' data: blob: http: https:;".to_vec(),
|
|
|
|
// Allow style from data: blob: and HTTPS.
|
|
|
|
b"style-src 'self' 'unsafe-inline' data: blob: https:;".to_vec(),
|
|
|
|
// Allow fonts from data: and HTTPS.
|
|
|
|
b"font-src 'self' data: https:;".to_vec(),
|
|
|
|
// Allow inline scripts and scripts eval (webpack/jsconsole)
|
|
|
|
b"script-src 'self' 'unsafe-inline' 'unsafe-eval';".to_vec(),
|
|
|
|
// Restrict everything else to the same origin.
|
|
|
|
b"default-src 'self';".to_vec(),
|
|
|
|
// Run in sandbox mode (although it's not fully safe since we allow same-origin and script)
|
|
|
|
b"sandbox allow-same-origin allow-forms allow-modals allow-popups allow-presentation allow-scripts;".to_vec(),
|
|
|
|
// Disallow subitting forms from any dapps
|
|
|
|
b"form-action 'none';".to_vec(),
|
|
|
|
// Never allow mixed content
|
|
|
|
b"block-all-mixed-content;".to_vec(),
|
|
|
|
// Specify if the site can be embedded.
|
|
|
|
match embeddable_on {
|
|
|
|
Some((ref host, ref port)) if host == "127.0.0.1" => {
|
|
|
|
format!("frame-ancestors {} {};", address(&(host.to_owned(), *port)), address(&("localhost".to_owned(), *port)))
|
|
|
|
},
|
|
|
|
Some(ref embed) => format!("frame-ancestors {};", address(embed)),
|
|
|
|
None => format!("frame-ancestors 'self';"),
|
|
|
|
}.into_bytes(),
|
|
|
|
]);
|
2016-10-19 11:02:21 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 11:15:02 +01:00
|
|
|
|
2016-10-19 11:02:21 +02:00
|
|
|
/// Extracts URL part from the Request.
|
2016-07-07 09:42:49 +02:00
|
|
|
pub fn extract_url(req: &server::Request<net::HttpStream>) -> Option<Url> {
|
2016-12-27 11:15:02 +01:00
|
|
|
convert_uri_to_url(req.uri(), req.headers().get::<header::Host>())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts URL given URI and Host header.
|
|
|
|
pub fn convert_uri_to_url(uri: &uri::RequestUri, host: Option<&header::Host>) -> Option<Url> {
|
|
|
|
match *uri {
|
2016-07-07 09:42:49 +02:00
|
|
|
uri::RequestUri::AbsoluteUri(ref url) => {
|
|
|
|
match Url::from_generic_url(url.clone()) {
|
|
|
|
Ok(url) => Some(url),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
},
|
2016-12-27 11:15:02 +01:00
|
|
|
uri::RequestUri::AbsolutePath { ref path, ref query } => {
|
|
|
|
let query = match *query {
|
|
|
|
Some(ref query) => format!("?{}", query),
|
|
|
|
None => "".into(),
|
|
|
|
};
|
2016-07-07 09:42:49 +02:00
|
|
|
// Attempt to prepend the Host header (mandatory in HTTP/1.1)
|
2016-12-27 11:15:02 +01:00
|
|
|
let url_string = match host {
|
2016-07-07 09:42:49 +02:00
|
|
|
Some(ref host) => {
|
2016-12-27 11:15:02 +01:00
|
|
|
format!("http://{}:{}{}{}", host.hostname, host.port.unwrap_or(80), path, query)
|
2016-07-07 09:42:49 +02:00
|
|
|
},
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
match Url::parse(&url_string) {
|
|
|
|
Ok(url) => Some(url),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|