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;
|
2017-10-05 12:35:01 +02:00
|
|
|
mod reader;
|
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};
|
2017-10-05 12:35:01 +02:00
|
|
|
pub use self::reader::Reader;
|
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
|
|
|
|
2017-07-11 13:45:23 +02:00
|
|
|
use std::iter;
|
2017-08-17 16:05:26 +02:00
|
|
|
use itertools::Itertools;
|
2017-10-05 12:35:01 +02:00
|
|
|
use hyper::header;
|
2017-07-04 16:04:09 +02:00
|
|
|
use {apps, address, Embeddable};
|
2016-07-07 09:42:49 +02:00
|
|
|
|
2016-10-19 11:02:21 +02:00
|
|
|
/// Adds security-related headers to the Response.
|
2018-02-15 11:05:20 +01:00
|
|
|
pub fn add_security_headers(headers: &mut header::Headers, embeddable_on: Embeddable, allow_js_eval: bool) {
|
2017-10-05 12:35:01 +02:00
|
|
|
headers.set_raw("X-XSS-Protection", "1; mode=block");
|
|
|
|
headers.set_raw("X-Content-Type-Options", "nosniff");
|
2016-10-19 11:02:21 +02:00
|
|
|
|
|
|
|
// Embedding header:
|
2017-07-04 16:04:09 +02:00
|
|
|
if let None = embeddable_on {
|
2017-10-05 12:35:01 +02:00
|
|
|
headers.set_raw("X-Frame-Options", "SAMEORIGIN");
|
2016-10-19 11:02:21 +02:00
|
|
|
}
|
2017-06-28 09:12:02 +02:00
|
|
|
|
|
|
|
// Content Security Policy headers
|
2017-10-05 12:35:01 +02:00
|
|
|
headers.set_raw("Content-Security-Policy", String::new()
|
2018-01-19 10:38:00 +01:00
|
|
|
// Restrict everything to the same origin by default.
|
|
|
|
+ "default-src 'self';"
|
2017-06-28 09:12:02 +02:00
|
|
|
// Allow connecting to WS servers and HTTP(S) servers.
|
|
|
|
// We could be more restrictive and allow only RPC server URL.
|
2017-10-05 12:35:01 +02:00
|
|
|
+ "connect-src http: https: ws: wss:;"
|
2017-06-28 09:12:02 +02:00
|
|
|
// Allow framing any content from HTTP(S).
|
|
|
|
// Again we could only allow embedding from RPC server URL.
|
|
|
|
// (deprecated)
|
2017-10-05 12:35:01 +02:00
|
|
|
+ "frame-src 'self' http: https:;"
|
2017-06-28 09:12:02 +02:00
|
|
|
// Allow framing and web workers from HTTP(S).
|
2017-10-05 12:35:01 +02:00
|
|
|
+ "child-src 'self' http: https:;"
|
2017-06-28 09:12:02 +02:00
|
|
|
// We allow data: blob: and HTTP(s) images.
|
|
|
|
// We could get rid of wildcarding HTTP and only allow RPC server URL.
|
2017-07-04 16:04:09 +02:00
|
|
|
// (http required for local dapps icons)
|
2017-10-05 12:35:01 +02:00
|
|
|
+ "img-src 'self' 'unsafe-inline' data: blob: http: https:;"
|
2017-06-28 09:12:02 +02:00
|
|
|
// Allow style from data: blob: and HTTPS.
|
2017-10-05 12:35:01 +02:00
|
|
|
+ "style-src 'self' 'unsafe-inline' data: blob: https:;"
|
2017-06-28 09:12:02 +02:00
|
|
|
// Allow fonts from data: and HTTPS.
|
2017-10-05 12:35:01 +02:00
|
|
|
+ "font-src 'self' data: https:;"
|
2018-01-19 10:38:00 +01:00
|
|
|
// Disallow objects
|
|
|
|
+ "object-src 'none';"
|
|
|
|
// Allow scripts
|
2017-10-05 12:35:01 +02:00
|
|
|
+ {
|
2017-08-10 18:32:10 +02:00
|
|
|
let script_src = embeddable_on.as_ref()
|
|
|
|
.map(|e| e.extra_script_src.iter()
|
|
|
|
.map(|&(ref host, port)| address(host, port))
|
|
|
|
.join(" ")
|
|
|
|
).unwrap_or_default();
|
2018-02-15 11:05:20 +01:00
|
|
|
let eval = if allow_js_eval { " 'unsafe-eval'" } else { "" };
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
&format!(
|
2018-02-15 11:05:20 +01:00
|
|
|
"script-src 'self' {}{};",
|
|
|
|
script_src,
|
|
|
|
eval
|
2017-10-05 12:35:01 +02:00
|
|
|
)
|
|
|
|
}
|
2017-08-10 18:32:10 +02:00
|
|
|
// Same restrictions as script-src with additional
|
2017-07-14 18:50:19 +02:00
|
|
|
// blob: that is required for camera access (worker)
|
2018-01-19 10:38:00 +01:00
|
|
|
+ "worker-src 'self' https: blob:;"
|
2017-06-28 09:12:02 +02:00
|
|
|
// Run in sandbox mode (although it's not fully safe since we allow same-origin and script)
|
2017-10-05 12:35:01 +02:00
|
|
|
+ "sandbox allow-same-origin allow-forms allow-modals allow-popups allow-presentation allow-scripts;"
|
2018-01-19 10:38:00 +01:00
|
|
|
// Disallow submitting forms from any dapps
|
2017-10-05 12:35:01 +02:00
|
|
|
+ "form-action 'none';"
|
2017-06-28 09:12:02 +02:00
|
|
|
// Never allow mixed content
|
2017-10-05 12:35:01 +02:00
|
|
|
+ "block-all-mixed-content;"
|
2017-06-28 09:12:02 +02:00
|
|
|
// Specify if the site can be embedded.
|
2017-10-05 12:35:01 +02:00
|
|
|
+ &match embeddable_on {
|
2017-07-04 16:04:09 +02:00
|
|
|
Some(ref embed) => {
|
|
|
|
let std = address(&embed.host, embed.port);
|
|
|
|
let proxy = format!("{}.{}", apps::HOME_PAGE, embed.dapps_domain);
|
|
|
|
let domain = format!("*.{}:{}", embed.dapps_domain, embed.port);
|
|
|
|
|
2017-07-11 13:45:23 +02:00
|
|
|
let mut ancestors = vec![std, domain, proxy]
|
|
|
|
.into_iter()
|
|
|
|
.chain(embed.extra_embed_on
|
|
|
|
.iter()
|
2017-08-10 18:32:10 +02:00
|
|
|
.map(|&(ref host, port)| address(host, port))
|
2017-07-11 13:45:23 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let ancestors = if embed.host == "127.0.0.1" {
|
2017-07-04 16:04:09 +02:00
|
|
|
let localhost = address("localhost", embed.port);
|
2017-07-11 13:45:23 +02:00
|
|
|
ancestors.chain(iter::once(localhost)).join(" ")
|
2017-07-04 16:04:09 +02:00
|
|
|
} else {
|
2017-07-11 13:45:23 +02:00
|
|
|
ancestors.join(" ")
|
|
|
|
};
|
|
|
|
|
|
|
|
format!("frame-ancestors {};", ancestors)
|
2017-06-28 09:12:02 +02:00
|
|
|
},
|
|
|
|
None => format!("frame-ancestors 'self';"),
|
2017-10-05 12:35:01 +02:00
|
|
|
}
|
|
|
|
);
|
2016-07-07 09:42:49 +02:00
|
|
|
}
|