Skip to content

Commit fb160c4

Browse files
authored
script: Inherit initiator origin when loading about:blank and about:srcdoc (#47630)
The [specification] says that `about:blank` and `about:srcdoc` should inherit their source origin. This means that instead of always creating a new opaque origin in the parser for these kind of pages, we should try harder to preserve the source origin. [specification]: https://html.spec.whatwg.org/multipage/#determining-the-origin Testing: This will cause more tests to pass once #47632 lands. --------- Signed-off-by: Martin Robinson <mrobinson@abandonedwig.info> Signed-off-by: Martin Robinson <martin@abandonedwig.info>
1 parent c406ade commit fb160c4

10 files changed

Lines changed: 48 additions & 41 deletions

File tree

components/script/dom/servoparser/mod.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use script_bindings::script_runtime::temp_cx;
3838
use script_traits::DocumentActivity;
3939
use servo_base::id::{PipelineId, WebViewId};
4040
use servo_config::pref;
41-
use servo_constellation_traits::{LoadOrigin, TargetSnapshotParams};
41+
use servo_constellation_traits::TargetSnapshotParams;
4242
use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
4343
use style::context::QuirksMode as ServoQuirksMode;
4444
use tendril::stream::LossyDecoder;
@@ -936,7 +936,12 @@ pub(crate) struct ParserContext {
936936
/// To report CSP violations to the global that initiated the navigation
937937
parent_info: Option<PipelineId>,
938938
target_snapshot_params: TargetSnapshotParams,
939-
load_origin: LoadOrigin,
939+
/// The source origin this load if this navigation was initiated by script.
940+
/// This is used to ensure that `about:blank` and `about:srcdoc` pages are
941+
/// able to inherit the aliased [`MutableOrigin`] of their initiating pages
942+
/// properly. In the case that the initiator is in another event loop, this
943+
/// will be a non-aliased copy of the origin.
944+
source_origin: Option<MutableOrigin>,
940945
document: Option<Trusted<Document>>,
941946
}
942947

@@ -948,7 +953,7 @@ impl ParserContext {
948953
creation_sandboxing_flag_set: SandboxingFlagSet,
949954
parent_info: Option<PipelineId>,
950955
target_snapshot_params: TargetSnapshotParams,
951-
load_origin: LoadOrigin,
956+
source_origin: Option<MutableOrigin>,
952957
) -> ParserContext {
953958
ParserContext {
954959
parser: None,
@@ -969,7 +974,7 @@ impl ParserContext {
969974
iframe_element_referrer_policy: Default::default(),
970975
},
971976
target_snapshot_params,
972-
load_origin,
977+
source_origin,
973978
document: None,
974979
}
975980
}
@@ -1390,16 +1395,10 @@ impl ParserContext {
13901395
// Step 21.11. Set responseOrigin to the result of determining the origin
13911396
// given response's URL, finalSandboxFlags, and entry's document state's
13921397
// initiator origin.
1393-
let source_origin = match self.load_origin {
1394-
LoadOrigin::Script(ref snapshot) => {
1395-
Some(MutableOrigin::from_snapshot(snapshot.clone()))
1396-
},
1397-
_ => None,
1398-
};
13991398
let origin = determine_the_origin(
14001399
metadata.as_ref().map(|metadata| &metadata.final_url),
14011400
final_sandboxing_flag_set,
1402-
source_origin,
1401+
self.source_origin.clone(),
14031402
);
14041403

14051404
let Some(document) = script_thread.handle_page_headers_available(

components/script/event_loop/script_thread.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4002,24 +4002,54 @@ impl ScriptThread {
40024002
/// argument until a notification is received that the fetch is complete.
40034003
#[servo_tracing::instrument(skip_all)]
40044004
fn pre_page_load(&self, cx: &mut js::context::JSContext, mut incomplete: InProgressLoad) {
4005+
let origin_from_snapshot = || -> Option<MutableOrigin> {
4006+
match incomplete.load_data.load_origin {
4007+
LoadOrigin::Script(ref snapshot) => {
4008+
Some(MutableOrigin::from_snapshot(snapshot.clone()))
4009+
},
4010+
_ => None,
4011+
}
4012+
};
4013+
4014+
let preserved_origin = || -> Option<MutableOrigin> {
4015+
// When loading `about:blank`, `about:srcdoc` and `javascript:`
4016+
// URLs, the specification says that the origin should be aliased
4017+
// from the creator origin. This means that changes to the creator
4018+
// origin via things like `document.domain` are reflected in the
4019+
// child Document. This code attempts to look up the creator
4020+
// Document and alias the origin for these type of pages.
4021+
//
4022+
// TODO: This should be eliminated by not having these types of pages
4023+
// use the parser at at all.
4024+
let creator_pipeline_id = incomplete.load_data.creator_pipeline_id?;
4025+
Some(
4026+
ScriptThread::find_document(creator_pipeline_id)?
4027+
.origin()
4028+
.clone(),
4029+
)
4030+
};
4031+
40054032
let url_str = incomplete.load_data.url.as_str();
40064033
if url_str == "about:blank" || incomplete.load_data.js_eval_result.is_some() {
4007-
self.start_synchronous_page_load(cx, incomplete);
4034+
let source_origin = preserved_origin().or(origin_from_snapshot());
4035+
self.start_synchronous_page_load(cx, incomplete, source_origin);
40084036
return;
40094037
}
40104038
if url_str == "about:srcdoc" {
4011-
self.page_load_about_srcdoc(cx, incomplete);
4039+
let source_origin = preserved_origin().or(origin_from_snapshot());
4040+
self.page_load_about_srcdoc(cx, incomplete, source_origin);
40124041
return;
40134042
}
40144043

4044+
let source_origin = origin_from_snapshot();
40154045
let context = ParserContext::new(
40164046
incomplete.webview_id,
40174047
incomplete.pipeline_id,
40184048
incomplete.load_data.url.clone(),
40194049
incomplete.load_data.creation_sandboxing_flag_set,
40204050
incomplete.parent_info,
40214051
incomplete.target_snapshot_params,
4022-
incomplete.load_data.load_origin.clone(),
4052+
source_origin,
40234053
);
40244054
self.incomplete_parser_contexts
40254055
.0
@@ -4230,6 +4260,7 @@ impl ScriptThread {
42304260
&self,
42314261
cx: &mut js::context::JSContext,
42324262
mut incomplete: InProgressLoad,
4263+
source_origin: Option<MutableOrigin>,
42334264
) {
42344265
let mut context = ParserContext::new(
42354266
incomplete.webview_id,
@@ -4238,7 +4269,7 @@ impl ScriptThread {
42384269
incomplete.load_data.creation_sandboxing_flag_set,
42394270
incomplete.parent_info,
42404271
incomplete.target_snapshot_params,
4241-
incomplete.load_data.load_origin.clone(),
4272+
source_origin,
42424273
);
42434274

42444275
let mut meta = Metadata::default(incomplete.load_data.url.clone());
@@ -4272,6 +4303,7 @@ impl ScriptThread {
42724303
&self,
42734304
cx: &mut js::context::JSContext,
42744305
mut incomplete: InProgressLoad,
4306+
source_origin: Option<MutableOrigin>,
42754307
) {
42764308
let url = ServoUrl::parse("about:srcdoc").unwrap();
42774309
let mut meta = Metadata::default(url.clone());
@@ -4289,7 +4321,6 @@ impl ScriptThread {
42894321
let parent_info = incomplete.parent_info;
42904322
let about_base_url = incomplete.load_data.about_base_url.clone();
42914323
let target_snapshot_params = incomplete.target_snapshot_params;
4292-
let load_origin = incomplete.load_data.load_origin.clone();
42934324
self.incomplete_loads.borrow_mut().push(incomplete);
42944325

42954326
let mut context = ParserContext::new(
@@ -4299,7 +4330,7 @@ impl ScriptThread {
42994330
creation_sandboxing_flag_set,
43004331
parent_info,
43014332
target_snapshot_params,
4302-
load_origin,
4333+
source_origin,
43034334
);
43044335
context.process_response(self, cx, Ok(FetchMetadata::Unfiltered(meta)));
43054336
context.set_policy_container(policy_container.as_ref());

components/script/navigation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ impl InProgressLoad {
192192
/// Create a new InProgressLoad object.
193193
pub(crate) fn new(new_pipeline_info: NewPipelineInfo) -> InProgressLoad {
194194
let url = new_pipeline_info.load_data.url.clone();
195+
195196
InProgressLoad {
196197
pipeline_id: new_pipeline_info.new_pipeline_id,
197198
browsing_context_id: new_pipeline_info.browsing_context_id,

tests/wpt/meta/html/browsers/origin/inheritance/about-blank-iframe.html.ini

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/wpt/meta/html/browsers/origin/inheritance/about-blank-window.html.ini

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/wpt/meta/html/browsers/origin/inheritance/about-srcdoc.html.ini

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/wpt/meta/html/browsers/origin/inheritance/javascript-url.html.ini

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/wpt/meta/html/browsers/origin/relaxing-the-same-origin-restriction/document_domain_setter_srcdoc.html.ini

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/wpt/meta/html/browsers/windows/document-domain-nested-set.window.js.ini

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/wpt/meta/html/browsers/windows/document-domain-nested.window.js.ini

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)