Skip to content

Commit 44a2727

Browse files
committed
Add simple implementation of content-security-policy on scripts / styles
This needs a lot more hooks before it'll actually be a good implementation, but for a start it can help get some feedback on if this is the right way to go about it. Part of servo#4577
1 parent 75c201f commit 44a2727

File tree

9 files changed

+132
-3
lines changed

9 files changed

+132
-3
lines changed

Cargo.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/net_traits/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ test = false
1313
doctest = false
1414

1515
[dependencies]
16+
content-security-policy = {git = "https://github.com/rust-ammonia/rust-content-security-policy"}
1617
cookie = "0.11"
1718
embedder_traits = { path = "../embedder_traits" }
1819
headers-core = "0.1"

components/net_traits/request.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use crate::ReferrerPolicy;
66
use crate::ResourceTimingType;
7+
use content_security_policy as csp;
78
use http::HeaderMap;
89
use hyper::Method;
910
use msg::constellation_msg::PipelineId;
@@ -50,6 +51,27 @@ impl Destination {
5051
*self == Destination::SharedWorker ||
5152
*self == Destination::Worker
5253
}
54+
pub fn to_csp_destination(&self) -> csp::Destination {
55+
match *self {
56+
Destination::None => csp::Destination::None,
57+
Destination::Audio => csp::Destination::Audio,
58+
Destination::Document => csp::Destination::Document,
59+
Destination::Embed => csp::Destination::Embed,
60+
Destination::Font => csp::Destination::Font,
61+
Destination::Image => csp::Destination::Image,
62+
Destination::Manifest => csp::Destination::Manifest,
63+
Destination::Object => csp::Destination::Object,
64+
Destination::Report => csp::Destination::Report,
65+
Destination::Script => csp::Destination::Script,
66+
Destination::ServiceWorker => csp::Destination::ServiceWorker,
67+
Destination::SharedWorker => csp::Destination::SharedWorker,
68+
Destination::Style => csp::Destination::Style,
69+
Destination::Track => csp::Destination::Track,
70+
Destination::Video => csp::Destination::Video,
71+
Destination::Worker => csp::Destination::Worker,
72+
Destination::Xslt => csp::Destination::Xslt,
73+
}
74+
}
5375
}
5476

5577
/// A request [origin](https://fetch.spec.whatwg.org/#concept-request-origin)

components/script/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ bluetooth_traits = {path = "../bluetooth_traits"}
4040
byteorder = "1.0"
4141
canvas_traits = {path = "../canvas_traits"}
4242
caseless = "0.2"
43+
content-security-policy = {git = "https://github.com/rust-ammonia/rust-content-security-policy"}
4344
cookie = "0.11"
4445
chrono = "0.4"
4546
crossbeam-channel = "0.3"

components/script/dom/bindings/trace.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use canvas_traits::webgl::{WebGLBufferId, WebGLChan, WebGLContextShareMode, WebG
5151
use canvas_traits::webgl::{WebGLFramebufferId, WebGLMsgSender, WebGLPipeline, WebGLProgramId};
5252
use canvas_traits::webgl::{WebGLReceiver, WebGLRenderbufferId, WebGLSLVersion, WebGLSender};
5353
use canvas_traits::webgl::{WebGLShaderId, WebGLTextureId, WebGLVersion, WebGLVertexArrayId};
54+
use content_security_policy::CspList;
5455
use crossbeam_channel::{Receiver, Sender};
5556
use cssparser::RGBA;
5657
use devtools_traits::{CSSError, TimelineMarkerType, WorkerId};
@@ -166,6 +167,8 @@ unsafe_no_jsmanaged_fields!(*mut JobQueue);
166167

167168
unsafe_no_jsmanaged_fields!(Cow<'static, str>);
168169

170+
unsafe_no_jsmanaged_fields!(CspList);
171+
169172
/// Trace a `JSVal`.
170173
pub fn trace_jsval(tracer: *mut JSTracer, description: &str, val: &Heap<JSVal>) {
171174
unsafe {

components/script/dom/document.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ use crate::stylesheet_set::StylesheetSetRef;
109109
use crate::task::TaskBox;
110110
use crate::task_source::{TaskSource, TaskSourceName};
111111
use crate::timers::OneshotTimerCallback;
112+
use content_security_policy::{self as csp, CspList};
112113
use cookie::Cookie;
113114
use devtools_traits::ScriptToDevtoolsControlMsg;
114115
use dom_struct::dom_struct;
@@ -129,6 +130,7 @@ use msg::constellation_msg::BrowsingContextId;
129130
use net_traits::pub_domains::is_pub_domain;
130131
use net_traits::request::RequestBuilder;
131132
use net_traits::response::HttpsState;
133+
use net_traits::NetworkError;
132134
use net_traits::CookieSource::NonHTTP;
133135
use net_traits::CoreResourceMsg::{GetCookiesForUrl, SetCookiesForUrl};
134136
use net_traits::{FetchResponseMsg, IpcSend, ReferrerPolicy};
@@ -147,7 +149,7 @@ use servo_atoms::Atom;
147149
use servo_config::pref;
148150
use servo_media::{ClientContextId, ServoMedia};
149151
use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
150-
use std::borrow::ToOwned;
152+
use std::borrow::{Cow, ToOwned};
151153
use std::cell::{Cell, Ref, RefMut};
152154
use std::collections::hash_map::Entry::{Occupied, Vacant};
153155
use std::collections::{HashMap, HashSet, VecDeque};
@@ -395,6 +397,9 @@ pub struct Document {
395397
/// where `id` needs to match any of the registered ShadowRoots
396398
/// hosting the media controls UI.
397399
media_controls: DomRefCell<HashMap<String, Dom<ShadowRoot>>>,
400+
/// https://html.spec.whatwg.org/multipage/dom.html#concept-document-csp-list
401+
#[ignore_malloc_size_of = "Defined in rust-content-security-policy"]
402+
csp_list: DomRefCell<Option<CspList>>,
398403
}
399404

400405
#[derive(JSTraceable, MallocSizeOf)]
@@ -1734,6 +1739,10 @@ impl Document {
17341739
request: RequestBuilder,
17351740
fetch_target: IpcSender<FetchResponseMsg>,
17361741
) {
1742+
if self.should_request_be_blocked_by_csp(&request) == csp::CheckResult::Blocked {
1743+
fetch_target.send(FetchResponseMsg::ProcessResponse(Err(NetworkError::LoadCancelled))).unwrap();
1744+
return;
1745+
}
17371746
let mut loader = self.loader.borrow_mut();
17381747
loader.fetch_async(load, request, fetch_target);
17391748
}
@@ -2784,9 +2793,51 @@ impl Document {
27842793
shadow_roots: DomRefCell::new(HashSet::new()),
27852794
shadow_roots_styles_changed: Cell::new(false),
27862795
media_controls: DomRefCell::new(HashMap::new()),
2796+
csp_list: DomRefCell::new(None),
27872797
}
27882798
}
27892799

2800+
pub fn set_csp_list(&self, csp_list: Option<CspList>) {
2801+
*self.csp_list.borrow_mut() = csp_list;
2802+
}
2803+
2804+
pub fn get_csp_list(&self) -> Option<Ref<CspList>> {
2805+
let b = self.csp_list.borrow();
2806+
if b.is_none() { None } else { Some(Ref::map(b, |o| o.as_ref().unwrap())) }
2807+
}
2808+
2809+
pub fn should_request_be_blocked_by_csp(&self, request: &RequestBuilder) -> csp::CheckResult {
2810+
let request = csp::Request {
2811+
url: request.url.clone().into_url(),
2812+
origin: request.origin.clone().into_url_origin(),
2813+
redirect_count: 0,
2814+
destination: request.destination.to_csp_destination(),
2815+
initiator: csp::Initiator::Fetch,
2816+
nonce: String::new(),
2817+
integrity_metadata: String::new(),
2818+
parser_metadata: csp::ParserMetadata::None,
2819+
};
2820+
// TODO: Instead of ignoring violations, report them.
2821+
self.get_csp_list().map(|c| c.should_request_be_blocked(&request).0).unwrap_or(csp::CheckResult::Allowed)
2822+
}
2823+
2824+
pub fn should_elements_inline_type_behavior_be_blocked(
2825+
&self,
2826+
el: &Element,
2827+
type_: csp::InlineCheckType,
2828+
source: &str) -> csp::CheckResult {
2829+
let element = csp::Element {
2830+
nonce: el.get_attribute(&ns!(), &local_name!("nonce"))
2831+
.map(|attr| Cow::Owned(attr.value().to_string())),
2832+
};
2833+
// TODO: Instead of ignoring violations, report them.
2834+
self.get_csp_list().map(|c| c.should_elements_inline_type_behavior_be_blocked(
2835+
&element,
2836+
type_,
2837+
source,
2838+
).0).unwrap_or(csp::CheckResult::Allowed)
2839+
}
2840+
27902841
/// Prevent any JS or layout from running until the corresponding call to
27912842
/// `remove_script_and_layout_blocker`. Used to isolate periods in which
27922843
/// the DOM is in an unstable state and should not be exposed to arbitrary

components/script/dom/globalscope.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use crate::task_source::websocket::WebsocketTaskSource;
3838
use crate::task_source::TaskSourceName;
3939
use crate::timers::{IsInterval, OneshotTimerCallback, OneshotTimerHandle};
4040
use crate::timers::{OneshotTimers, TimerCallback};
41+
use content_security_policy::CspList;
4142
use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId};
4243
use dom_struct::dom_struct;
4344
use ipc_channel::ipc::IpcSender;
@@ -59,7 +60,7 @@ use script_traits::{MsDuration, ScriptToConstellationChan, TimerEvent};
5960
use script_traits::{TimerEventId, TimerSchedulerMsg, TimerSource};
6061
use servo_url::{MutableOrigin, ServoUrl};
6162
use std::borrow::Cow;
62-
use std::cell::Cell;
63+
use std::cell::{Cell, Ref};
6364
use std::collections::hash_map::Entry;
6465
use std::collections::HashMap;
6566
use std::ffi::CString;
@@ -812,6 +813,13 @@ impl GlobalScope {
812813
pub fn get_user_agent(&self) -> Cow<'static, str> {
813814
self.user_agent.clone()
814815
}
816+
817+
pub fn get_csp_list(&self) -> Option<CspList> {
818+
if let Some(window) = self.downcast::<Window>() {
819+
return window.Document().get_csp_list().map(|c| c.clone());
820+
}
821+
None
822+
}
815823
}
816824

817825
fn timestamp_in_ms(time: Timespec) -> u64 {

components/script/dom/htmlscriptelement.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::dom::node::{BindContext, ChildrenMutation, CloneChildrenFlag, Node};
2626
use crate::dom::performanceresourcetiming::InitiatorType;
2727
use crate::dom::virtualmethods::VirtualMethods;
2828
use crate::network_listener::{self, NetworkListener, PreInvoke, ResourceTimingListener};
29+
use content_security_policy::{self as csp, CspList};
2930
use dom_struct::dom_struct;
3031
use encoding_rs::Encoding;
3132
use html5ever::{LocalName, Prefix};
@@ -442,7 +443,13 @@ impl HTMLScriptElement {
442443

443444
// TODO: Step 12: nomodule content attribute
444445

445-
// TODO(#4577): Step 13: CSP.
446+
if !element.has_attribute(&local_name!("src")) &&
447+
doc.should_elements_inline_type_behavior_be_blocked(
448+
&element,
449+
csp::InlineCheckType::Script,
450+
&text) == csp::CheckResult::Blocked {
451+
return;
452+
}
446453

447454
// Step 14.
448455
let for_attribute = element.get_attribute(&ns!(), &local_name!("for"));

components/script/dom/servoparser/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::dom::text::Text;
3535
use crate::dom::virtualmethods::vtable_for;
3636
use crate::network_listener::PreInvoke;
3737
use crate::script_thread::ScriptThread;
38+
use content_security_policy::{self as csp, CspList};
3839
use dom_struct::dom_struct;
3940
use embedder_traits::resources::{self, Resource};
4041
use encoding_rs::Encoding;
@@ -736,6 +737,24 @@ impl FetchResponseListener for ParserContext {
736737
.and_then(|meta| meta.content_type)
737738
.map(Serde::into_inner)
738739
.map(Into::into);
740+
741+
// https://www.w3.org/TR/CSP/#initialize-document-csp
742+
// XXX TODO: DO NOT MERGE INTO MASTER YET
743+
// This only implements the non-local behavior stuff; I need to figure out how to check if a URL is local.
744+
let csp_list = metadata.as_ref().and_then(|m| {
745+
let h = if let Some(h) = m.headers.as_ref() { h } else { return None };
746+
let mut csp = h.get_all("content-security-policy").iter();
747+
// This silently ignores the CSP if it contains invalid Unicode.
748+
// We should probably report an error somewhere.
749+
let c = if let Some(c) = csp.next().and_then(|c| c.to_str().ok()) { c } else { return None };
750+
let mut csp_list = CspList::parse(c, csp::PolicySource::Header, csp::PolicyDisposition::Enforce);
751+
for c in csp {
752+
let c = if let Ok(c) = c.to_str() { c } else { return None };
753+
csp_list.append(CspList::parse(c, csp::PolicySource::Header, csp::PolicyDisposition::Enforce));
754+
}
755+
Some(csp_list)
756+
});
757+
739758
let parser = match ScriptThread::page_headers_available(&self.id, metadata) {
740759
Some(parser) => parser,
741760
None => return,
@@ -744,6 +763,8 @@ impl FetchResponseListener for ParserContext {
744763
return;
745764
}
746765

766+
parser.document.set_csp_list(csp_list);
767+
747768
self.parser = Some(Trusted::new(&*parser));
748769

749770
match content_type {

0 commit comments

Comments
 (0)