mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 08:02:59 +00:00
fix(css): preserve referrer state across stylesheet imports
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
(async () => {
|
||||
const controlled = navigator.serviceWorker.controller ? Promise.resolve() :
|
||||
new Promise(resolve => navigator.serviceWorker.addEventListener('controllerchange', resolve, {once:true}));
|
||||
await navigator.serviceWorker.register('/css-import-referrer-worker.js', {scope:'/'});
|
||||
await navigator.serviceWorker.ready;
|
||||
await controlled;
|
||||
const setPolicy = policy => {
|
||||
const meta=document.createElement('meta');
|
||||
meta.name='referrer';meta.content=policy;document.head.append(meta);meta.remove();
|
||||
};
|
||||
const failures=[];
|
||||
globalThis.cssImportFailures=failures;
|
||||
for (const name of ['default','no-referrer','origin','unsafe','multiple','invalid',
|
||||
'redirect','inline','inline-direct','reset','nested','diamond','data','inline-data',
|
||||
'external-data','insert-root','insert-child','synthetic-inherit','request-override']) {
|
||||
setPolicy('no-referrer');
|
||||
const resource=file => new URL('/css-referrer/' + name + '/' + file, location.href).href;
|
||||
const root=resource('root.css'), mid=resource('mid.css');
|
||||
const row=(file,referrer,policy) => ({file,referrer,policy});
|
||||
const expected=[];
|
||||
if (name === 'redirect') expected.push(row('start.css','','no-referrer'));
|
||||
if (!['data','inline-data','inline-direct'].includes(name))
|
||||
expected.push(name === 'request-override' ? row('root.css',location.origin+'/','origin') : row('root.css','','no-referrer'));
|
||||
if (name === 'reset') expected.push(row('mid.css','','no-referrer'),row('leaf.css','','no-referrer'));
|
||||
else if (name === 'nested') expected.push(row('mid.css',root,'unsafe-url'),row('leaf.css','','no-referrer'));
|
||||
else if (name === 'synthetic-inherit') expected.push(row('mid.css',root,'unsafe-url'),row('leaf.css','','no-referrer'));
|
||||
else if (name === 'diamond') expected.push(row('left.css',root,'unsafe-url'),row('right.css',root,'unsafe-url'),
|
||||
row('leaf.css','','no-referrer'));
|
||||
else if (name === 'insert-child') expected.push(row('mid.css',root,'unsafe-url'),row('leaf.css',location.origin+'/','origin'));
|
||||
else if (['no-referrer','insert-root','inline-direct'].includes(name)) expected.push(row('leaf.css','','no-referrer'));
|
||||
else if (name === 'origin') expected.push(row('leaf.css',location.origin+'/','origin'));
|
||||
else if (['unsafe','multiple','redirect'].includes(name)) expected.push(row('leaf.css',root,'unsafe-url'));
|
||||
else if (['data','inline-data','external-data'].includes(name)) expected.push(row('leaf.css','','no-referrer'));
|
||||
else expected.push(row('leaf.css','','no-referrer'));
|
||||
|
||||
const inline=name.startsWith('inline');
|
||||
const element=document.createElement(inline ? 'style' : 'link');
|
||||
let url=name === 'redirect' ? resource('start.css') : root;
|
||||
if (name === 'data' || name === 'inline-data')
|
||||
url='data:text/css,'+encodeURIComponent('@import url("'+resource('leaf.css')+'");');
|
||||
if (name === 'inline-direct') url=resource('leaf.css');
|
||||
if (inline) element.textContent='@import url("'+url+'");';
|
||||
else {element.rel='stylesheet';element.href=url;}
|
||||
if (name === 'request-override') element.referrerPolicy='origin';
|
||||
if (inline) document.head.append(element);
|
||||
else await new Promise((resolve,reject) => {
|
||||
element.onload=resolve;
|
||||
element.onerror=() => reject(new Error(name+': stylesheet failed'));
|
||||
document.head.append(element);
|
||||
});
|
||||
if (name === 'insert-root' || name === 'insert-child') {
|
||||
setPolicy('unsafe-url');
|
||||
const sheet=name === 'insert-root' ? element.sheet : element.sheet.cssRules[0].styleSheet;
|
||||
sheet.insertRule('@import url("'+resource('leaf.css')+'");',0);
|
||||
}
|
||||
let actual=[];
|
||||
for (let attempt=0;attempt<200;attempt++) {
|
||||
actual=await (await fetch('/css-import-observations?case='+name)).json();
|
||||
if (actual.length >= expected.length) break;
|
||||
await new Promise(resolve => setTimeout(resolve,10));
|
||||
}
|
||||
const normalize=rows => rows.map(value => JSON.stringify(value)).sort();
|
||||
if (JSON.stringify(normalize(actual)) !== JSON.stringify(normalize(expected)))
|
||||
failures.push(name+': '+JSON.stringify(actual)+' != '+JSON.stringify(expected));
|
||||
element.remove();
|
||||
}
|
||||
if (failures.length) throw new Error(failures.join('\n'));
|
||||
return true;
|
||||
})();
|
||||
@@ -0,0 +1,28 @@
|
||||
use super::*;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn css_import_requests_use_the_parent_stylesheet_referrer() -> Result<()> {
|
||||
let servers = IntegrityServers::spawn().await?;
|
||||
let browser = Browser::new(AppConfig::default())?;
|
||||
let mut page = browser
|
||||
.fetch(&format!("{}/page.html", servers.origin))
|
||||
.await?;
|
||||
let result = tokio::time::timeout(
|
||||
Duration::from_secs(20),
|
||||
page.evaluate_runtime_expression_with_await_async(
|
||||
include_str!("css_import_referrer.js"),
|
||||
true,
|
||||
),
|
||||
)
|
||||
.await??;
|
||||
assert_eq!(result["value"], true, "{result}");
|
||||
assert!(
|
||||
!servers
|
||||
.requests
|
||||
.lock()
|
||||
.iter()
|
||||
.any(|request| request.path.starts_with("/css-referrer/")),
|
||||
"import requests must pass through the controlling service worker"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
const observations = new Map();
|
||||
self.addEventListener('activate', event => event.waitUntil(clients.claim()));
|
||||
self.addEventListener('fetch', event => {
|
||||
const url = new URL(event.request.url);
|
||||
if (url.pathname === '/css-import-observations') {
|
||||
event.respondWith(new Response(JSON.stringify(observations.get(url.searchParams.get('case')) || []), {
|
||||
headers: {'Content-Type':'application/json'}
|
||||
}));
|
||||
return;
|
||||
}
|
||||
if (!url.pathname.startsWith('/css-referrer/')) return;
|
||||
const [, , name, file] = url.pathname.split('/');
|
||||
const rows = observations.get(name) || [];
|
||||
rows.push({file,referrer:event.request.referrer,policy:event.request.referrerPolicy});
|
||||
observations.set(name, rows);
|
||||
const resource = file => new URL('/css-referrer/' + name + '/' + file, url).href;
|
||||
const imports = file => '@import url("' + resource(file) + '");';
|
||||
if (file === 'start.css') {
|
||||
event.respondWith(Response.redirect(resource('root.css')));
|
||||
return;
|
||||
}
|
||||
let body = '';
|
||||
let policy;
|
||||
if (file === 'root.css') {
|
||||
policy = {
|
||||
'no-referrer':'no-referrer', origin:'origin', unsafe:'unsafe-url',
|
||||
multiple:'origin, invalid, unsafe-url', invalid:'not-a-policy',
|
||||
redirect:'unsafe-url', reset:'no-referrer', nested:'unsafe-url', 'synthetic-inherit':'unsafe-url',
|
||||
diamond:'unsafe-url', 'external-data':'unsafe-url',
|
||||
'insert-root':'no-referrer', 'insert-child':'unsafe-url'
|
||||
}[name];
|
||||
if (['reset','nested','insert-child','synthetic-inherit'].includes(name)) body=imports('mid.css');
|
||||
else if (name === 'diamond') body=imports('left.css')+imports('right.css');
|
||||
else if (name === 'external-data')
|
||||
body='@import url("data:text/css,' + encodeURIComponent(imports('leaf.css')) + '");';
|
||||
else if (name !== 'insert-root') body=imports('leaf.css');
|
||||
} else if (file === 'mid.css') {
|
||||
if (name === 'nested') policy='no-referrer';
|
||||
if (name === 'insert-child') policy='origin';
|
||||
else body=imports('leaf.css');
|
||||
} else if (file === 'left.css' || file === 'right.css') {
|
||||
policy=file === 'left.css' ? 'no-referrer' : 'unsafe-url';
|
||||
body=imports('leaf.css');
|
||||
}
|
||||
const headers={'Content-Type':'text/css','Cache-Control':'no-store'};
|
||||
if (policy !== undefined) headers['Referrer-Policy']=policy;
|
||||
event.respondWith(new Response(body,{headers}));
|
||||
});
|
||||
@@ -49,6 +49,8 @@ mod referrer_policy_inheritance;
|
||||
#[path = "fetch_client_referrer.rs"]
|
||||
mod fetch_client_referrer;
|
||||
|
||||
#[path = "css_import_referrer.rs"]
|
||||
mod css_import_referrer;
|
||||
#[path = "service_worker_referrer.rs"]
|
||||
mod service_worker_referrer;
|
||||
|
||||
@@ -371,6 +373,11 @@ fn fixture_response(
|
||||
format!("{javascript}Cache-Control: no-store\r\n"),
|
||||
include_str!("native_referrer_worker.js").to_owned(),
|
||||
),
|
||||
"/css-import-referrer-worker.js" => (
|
||||
"200 OK",
|
||||
format!("{javascript}Cache-Control: no-store\r\n"),
|
||||
include_str!("css_import_referrer_worker.js").to_owned(),
|
||||
),
|
||||
"/cors.js" | "/cors-null.js" | "/cors-origin.js" => (
|
||||
"200 OK",
|
||||
format!(
|
||||
|
||||
@@ -103,6 +103,7 @@ pub(crate) use stylesheet_runtime::{
|
||||
ConnectedLoadCompletion, LinkedStylesheetImportGraphCompletion,
|
||||
LiveStylesheetImportLoadCompletion, StylesheetImportCompletionAuthority,
|
||||
fetch_complete_stylesheet_import_graph, live_stylesheet_import_responses,
|
||||
stylesheet_import_options,
|
||||
};
|
||||
use stylesheet_runtime::{
|
||||
ConnectedLoadOperation, LinkedStylesheetImportGraphs, QueuedConnectedStyleLoad,
|
||||
@@ -438,6 +439,7 @@ impl ConnectedStyleImportRoot {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct ConnectedLoadNetworkResult {
|
||||
pub(crate) import_options: Option<crate::stylesheet_blocking::StylesheetFetchOptions>,
|
||||
pub(crate) consumed_preload_error: bool,
|
||||
/// Exact request Document, independent of an element's current location.
|
||||
pub(crate) document_owner: Option<crate::frame_owner_model::FrameDocumentTaskOwner>,
|
||||
@@ -2598,6 +2600,7 @@ mod tests {
|
||||
.stylesheet_lifecycle
|
||||
.ready_connected_load_network_results
|
||||
.push_back(ConnectedLoadNetworkResult {
|
||||
import_options: None,
|
||||
consumed_preload_error: false,
|
||||
document_owner: None,
|
||||
stylesheet_fetch: None,
|
||||
@@ -2647,6 +2650,7 @@ mod tests {
|
||||
.stylesheet_lifecycle
|
||||
.ready_connected_load_network_results
|
||||
.push_back(ConnectedLoadNetworkResult {
|
||||
import_options: None,
|
||||
consumed_preload_error: false,
|
||||
document_owner: None,
|
||||
stylesheet_fetch: Some(active_load.fetch().clone()),
|
||||
@@ -2701,6 +2705,7 @@ mod tests {
|
||||
.stylesheet_lifecycle
|
||||
.ready_connected_load_network_results
|
||||
.push_back(ConnectedLoadNetworkResult {
|
||||
import_options: None,
|
||||
consumed_preload_error: false,
|
||||
document_owner: None,
|
||||
stylesheet_fetch: Some(detached_load.fetch().clone()),
|
||||
@@ -2755,6 +2760,7 @@ mod tests {
|
||||
.stylesheet_lifecycle
|
||||
.ready_connected_load_network_results
|
||||
.push_back(ConnectedLoadNetworkResult {
|
||||
import_options: None,
|
||||
consumed_preload_error: false,
|
||||
document_owner: None,
|
||||
stylesheet_fetch: Some(detached_load.fetch().clone()),
|
||||
@@ -3367,6 +3373,7 @@ mod tests {
|
||||
operation,
|
||||
successful: false,
|
||||
network_results: vec![ConnectedLoadNetworkResult {
|
||||
import_options: None,
|
||||
consumed_preload_error: false,
|
||||
document_owner: None,
|
||||
stylesheet_fetch: None,
|
||||
@@ -3463,6 +3470,7 @@ mod tests {
|
||||
operation: first_a,
|
||||
successful: true,
|
||||
network_results: vec![ConnectedLoadNetworkResult {
|
||||
import_options: None,
|
||||
consumed_preload_error: false,
|
||||
document_owner: None,
|
||||
stylesheet_fetch: None,
|
||||
@@ -3537,6 +3545,7 @@ mod tests {
|
||||
operation,
|
||||
successful: true,
|
||||
network_results: vec![ConnectedLoadNetworkResult {
|
||||
import_options: None,
|
||||
consumed_preload_error: false,
|
||||
document_owner: None,
|
||||
stylesheet_fetch: None,
|
||||
@@ -3599,6 +3608,7 @@ mod tests {
|
||||
operation: first_a,
|
||||
successful: true,
|
||||
network_results: vec![ConnectedLoadNetworkResult {
|
||||
import_options: None,
|
||||
consumed_preload_error: false,
|
||||
document_owner: None,
|
||||
stylesheet_fetch: None,
|
||||
@@ -3742,6 +3752,7 @@ mod tests {
|
||||
.stylesheet_lifecycle
|
||||
.ready_connected_load_network_results
|
||||
.push_back(ConnectedLoadNetworkResult {
|
||||
import_options: None,
|
||||
consumed_preload_error: false,
|
||||
document_owner: None,
|
||||
stylesheet_fetch: None,
|
||||
|
||||
@@ -1,6 +1,19 @@
|
||||
use super::*;
|
||||
|
||||
impl LiveStylesheet {
|
||||
pub(crate) fn import_options(
|
||||
&self,
|
||||
) -> Option<crate::stylesheet_blocking::StylesheetFetchOptions> {
|
||||
self.import_options.borrow().clone()
|
||||
}
|
||||
|
||||
pub(crate) fn set_import_options(
|
||||
&self,
|
||||
options: crate::stylesheet_blocking::StylesheetFetchOptions,
|
||||
) {
|
||||
self.import_options.replace(Some(options));
|
||||
}
|
||||
|
||||
pub(crate) fn top_level_import_state(&self) -> (bool, Vec<url::Url>) {
|
||||
let edges = self.import_edges.borrow();
|
||||
let has_import_rules = !edges.is_empty();
|
||||
@@ -30,6 +43,9 @@ impl LiveStylesheet {
|
||||
Some(LiveStylesheetImportRequest {
|
||||
edge_id: edge.id,
|
||||
url: edge.rule.read_with(&guard).url.url()?.as_ref().clone(),
|
||||
import_options: self
|
||||
.import_options()
|
||||
.filter(|options| options.referrer_url().is_some()),
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
|
||||
@@ -959,6 +959,7 @@ enum LiveStylesheetCssomRuntimeState {
|
||||
pub(crate) struct LiveStylesheetImportRequest {
|
||||
pub(crate) edge_id: StylesheetImportEdgeId,
|
||||
pub(crate) url: url::Url,
|
||||
pub(crate) import_options: Option<crate::stylesheet_blocking::StylesheetFetchOptions>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -968,6 +969,7 @@ pub(crate) struct LiveStylesheetImportResponse {
|
||||
pub(crate) css_text: String,
|
||||
pub(crate) successful: bool,
|
||||
pub(crate) origin_clean: bool,
|
||||
pub(crate) import_options: Option<crate::stylesheet_blocking::StylesheetFetchOptions>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -1044,6 +1046,7 @@ pub(crate) struct LiveStylesheet {
|
||||
quirks_mode: QuirksMode,
|
||||
allow_import_rules: AllowImportRules,
|
||||
origin_clean: Cell<bool>,
|
||||
import_options: RefCell<Option<crate::stylesheet_blocking::StylesheetFetchOptions>>,
|
||||
cssom_runtime_state: LiveStylesheetCssomRuntimeState,
|
||||
contents_revision: Cell<u64>,
|
||||
cascade_generation: Cell<u64>,
|
||||
@@ -1382,6 +1385,7 @@ impl LiveStylesheet {
|
||||
quirks_mode,
|
||||
allow_import_rules,
|
||||
origin_clean: Cell::new(true),
|
||||
import_options: RefCell::new(None),
|
||||
cssom_runtime_state,
|
||||
contents_revision: Cell::new(1),
|
||||
cascade_generation: Cell::new(1),
|
||||
|
||||
@@ -235,6 +235,12 @@ impl LiveStylesheetRegistry {
|
||||
LiveStylesheetRuntimeStateKind::IndependentCssom,
|
||||
));
|
||||
child.set_origin_clean(origin_clean);
|
||||
child.set_import_options(
|
||||
crate::stylesheet_blocking::StylesheetFetchOptions::for_import(
|
||||
child.base_url().clone(),
|
||||
None,
|
||||
),
|
||||
);
|
||||
self.entries.borrow_mut().insert(id, Rc::downgrade(&child));
|
||||
if !parent.install_import_child(edge_id, child.clone(), successful) {
|
||||
self.entries.borrow_mut().remove(&id);
|
||||
@@ -268,12 +274,17 @@ impl LiveStylesheetRegistry {
|
||||
.or_insert(response);
|
||||
}
|
||||
let mut data_expansions = 0;
|
||||
Some(self.install_import_graph_for_stylesheet(
|
||||
&root,
|
||||
&responses_by_request_identity,
|
||||
&ancestors,
|
||||
&mut data_expansions,
|
||||
))
|
||||
Some(
|
||||
self.install_import_graph_for_stylesheet(
|
||||
&root,
|
||||
&responses_by_request_identity,
|
||||
&ancestors,
|
||||
&mut data_expansions,
|
||||
root.import_options()
|
||||
.and_then(|options| options.document_referrer_policy().map(str::to_owned))
|
||||
.as_deref(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fn install_import_graph_for_stylesheet(
|
||||
@@ -282,6 +293,7 @@ impl LiveStylesheetRegistry {
|
||||
responses_by_request_identity: &HashMap<url::Url, &LiveStylesheetImportResponse>,
|
||||
ancestors: &HashSet<url::Url>,
|
||||
data_expansions: &mut usize,
|
||||
document_referrer_policy: Option<&str>,
|
||||
) -> bool {
|
||||
enum ImportGraphTraversal {
|
||||
Visit {
|
||||
@@ -358,6 +370,15 @@ impl LiveStylesheetRegistry {
|
||||
css_text,
|
||||
successful: true,
|
||||
origin_clean: true,
|
||||
import_options: Some(
|
||||
crate::stylesheet_blocking::StylesheetFetchOptions::for_import(
|
||||
request.url.clone(),
|
||||
document_referrer_policy.map(str::to_owned),
|
||||
)
|
||||
.with_document_referrer_policy(
|
||||
document_referrer_policy.map(str::to_owned),
|
||||
),
|
||||
),
|
||||
},
|
||||
None => LiveStylesheetImportResponse {
|
||||
request_url: request.url.clone(),
|
||||
@@ -365,6 +386,7 @@ impl LiveStylesheetRegistry {
|
||||
css_text: String::new(),
|
||||
successful: false,
|
||||
origin_clean: true,
|
||||
import_options: None,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
@@ -377,21 +399,23 @@ impl LiveStylesheetRegistry {
|
||||
css_text: String::new(),
|
||||
successful: false,
|
||||
origin_clean: false,
|
||||
import_options: None,
|
||||
})
|
||||
};
|
||||
all_successful &= response.successful;
|
||||
if self
|
||||
.install_import_response(
|
||||
parent.id(),
|
||||
parent.contents_revision(),
|
||||
request.edge_id,
|
||||
&response.css_text,
|
||||
response.response_url.clone(),
|
||||
response.successful,
|
||||
response.origin_clean,
|
||||
)
|
||||
.is_none()
|
||||
{
|
||||
if let Some(child) = self.install_import_response(
|
||||
parent.id(),
|
||||
parent.contents_revision(),
|
||||
request.edge_id,
|
||||
&response.css_text,
|
||||
response.response_url.clone(),
|
||||
response.successful,
|
||||
response.origin_clean,
|
||||
) {
|
||||
if let Some(options) = response.import_options {
|
||||
child.set_import_options(options);
|
||||
}
|
||||
} else {
|
||||
all_successful = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,6 +254,7 @@ fn descendant_import_replacement_invalidates_a_root_graph_snapshot() {
|
||||
root_revision,
|
||||
stale_import_generation,
|
||||
&[LiveStylesheetImportResponse {
|
||||
import_options: None,
|
||||
request_url: stale_leaf_request.url.clone(),
|
||||
response_url: stale_leaf_request.url,
|
||||
css_text: ".stale { color: red; }".to_owned(),
|
||||
@@ -281,6 +282,7 @@ fn import_graph_uses_each_response_url_as_the_nested_parser_base() {
|
||||
);
|
||||
let responses = vec![
|
||||
LiveStylesheetImportResponse {
|
||||
import_options: None,
|
||||
request_url: url::Url::parse("https://example.test/assets/redirected.css").unwrap(),
|
||||
response_url: url::Url::parse("https://cdn.example.test/final/child.css").unwrap(),
|
||||
css_text: "@import './leaf.css'; .child { color: blue; }".to_owned(),
|
||||
@@ -288,6 +290,7 @@ fn import_graph_uses_each_response_url_as_the_nested_parser_base() {
|
||||
origin_clean: true,
|
||||
},
|
||||
LiveStylesheetImportResponse {
|
||||
import_options: None,
|
||||
request_url: url::Url::parse("https://cdn.example.test/final/leaf.css").unwrap(),
|
||||
response_url: url::Url::parse("https://cdn.example.test/final/leaf.css").unwrap(),
|
||||
css_text: ".leaf { color: green; }".to_owned(),
|
||||
@@ -373,6 +376,7 @@ fn deep_import_graph_walks_and_generation_propagation_are_iterative() {
|
||||
root.contents_revision(),
|
||||
generation_before_terminal,
|
||||
&[LiveStylesheetImportResponse {
|
||||
import_options: None,
|
||||
request_url: pending[0].url.clone(),
|
||||
response_url: pending[0].url.clone(),
|
||||
css_text: ".terminal { color: green; }".to_owned(),
|
||||
|
||||
@@ -249,6 +249,7 @@ impl JsContextHost {
|
||||
prepared,
|
||||
),
|
||||
);
|
||||
self.retain_linked_stylesheet_import_options(owner, terminal);
|
||||
installed_stylesheet_count += 1;
|
||||
}
|
||||
let Some(response) = ready_response else {
|
||||
|
||||
@@ -287,6 +287,21 @@ impl JsContextHost {
|
||||
root: crate::document_runtime::ConnectedStyleImportRoot,
|
||||
responses: &[crate::live_stylesheet::LiveStylesheetImportResponse],
|
||||
) -> Option<bool> {
|
||||
let document_policy =
|
||||
self.dom_host()
|
||||
.owner_document_handle(root.owner)
|
||||
.and_then(|document| {
|
||||
crate::context_bootstrap::document_referrer_policy_for_native_document(
|
||||
self, document,
|
||||
)
|
||||
});
|
||||
let stylesheet = self.live_stylesheets.get(root.stylesheet_id)?;
|
||||
stylesheet.set_import_options(
|
||||
stylesheet
|
||||
.import_options()
|
||||
.unwrap_or_default()
|
||||
.with_document_referrer_policy(document_policy),
|
||||
);
|
||||
self.live_stylesheets.install_import_graph(
|
||||
root.stylesheet_id,
|
||||
root.contents_revision,
|
||||
@@ -296,6 +311,29 @@ impl JsContextHost {
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn retain_linked_stylesheet_import_options(
|
||||
&self,
|
||||
owner: DomHandle,
|
||||
terminal: &crate::stylesheet_blocking::StylesheetFetchTerminal,
|
||||
) {
|
||||
let document_policy = self
|
||||
.dom_host()
|
||||
.owner_document_handle(owner)
|
||||
.and_then(|document| {
|
||||
crate::context_bootstrap::document_referrer_policy_for_native_document(
|
||||
self, document,
|
||||
)
|
||||
});
|
||||
let document_options = crate::stylesheet_blocking::StylesheetFetchOptions::default()
|
||||
.with_document_referrer_policy(document_policy);
|
||||
if let Some(options) =
|
||||
crate::document_runtime::stylesheet_import_options(terminal, &document_options)
|
||||
&& let Some(stylesheet) = self.linked_live_stylesheet(owner)
|
||||
{
|
||||
stylesheet.set_import_options(options);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn refresh_live_stylesheet_after_import_graph(
|
||||
&mut self,
|
||||
owner: DomHandle,
|
||||
|
||||
@@ -16,6 +16,7 @@ use crate::{
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct DocumentPreloadResponse {
|
||||
pub(crate) service_worker_response_url: Option<Url>,
|
||||
pub(crate) response: NavigationResponse,
|
||||
pub(crate) from_service_worker: bool,
|
||||
pub(crate) response_filter: Option<AsyncSubresourceFetchResponseFilter>,
|
||||
@@ -266,6 +267,7 @@ mod tests {
|
||||
|
||||
fn response() -> DocumentPreloadResponse {
|
||||
DocumentPreloadResponse {
|
||||
service_worker_response_url: None,
|
||||
response: NavigationResponse::from_text_body(
|
||||
request().url,
|
||||
200,
|
||||
|
||||
@@ -262,6 +262,11 @@ impl RendererBrowserContextRuntime {
|
||||
.await
|
||||
.map_err(crate::network::preloads::ConsumedPreloadError)?;
|
||||
return Ok(Some(ServiceWorkerDirectFetchResponse {
|
||||
response_url: if preload.from_service_worker {
|
||||
preload.service_worker_response_url
|
||||
} else {
|
||||
Some(preload.response.final_url.clone())
|
||||
},
|
||||
response: Box::new(preload.response),
|
||||
response_filter: preload.response_filter,
|
||||
from_network_fallback: !preload.from_service_worker,
|
||||
|
||||
@@ -2548,6 +2548,7 @@ impl ScriptVm {
|
||||
let mut host = self._context_host.borrow_mut();
|
||||
for network_result in results {
|
||||
let crate::document_runtime::ConnectedLoadNetworkResult {
|
||||
import_options,
|
||||
consumed_preload_error,
|
||||
document_owner,
|
||||
stylesheet_fetch,
|
||||
@@ -2611,6 +2612,7 @@ impl ScriptVm {
|
||||
});
|
||||
if is_import_graph_result {
|
||||
let response = crate::live_stylesheet::LiveStylesheetImportResponse {
|
||||
import_options: import_options.clone(),
|
||||
request_url: request_url.clone(),
|
||||
response_url: stylesheet_base_url.clone(),
|
||||
css_text: stylesheet_text.clone(),
|
||||
|
||||
@@ -711,6 +711,8 @@ pub(crate) enum ServiceWorkerDirectFetchResult {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ServiceWorkerDirectFetchResponse {
|
||||
/// The worker response's URL before applying the request-URL fallback.
|
||||
pub(crate) response_url: Option<Url>,
|
||||
pub(crate) response: Box<crate::protocol_types::NavigationResponse>,
|
||||
pub(crate) response_filter: Option<crate::types::AsyncSubresourceFetchResponseFilter>,
|
||||
pub(crate) from_network_fallback: bool,
|
||||
|
||||
@@ -344,6 +344,7 @@ impl ServiceWorkerRuntimeService {
|
||||
body,
|
||||
);
|
||||
ServiceWorkerDirectFetchResult::Response(ServiceWorkerDirectFetchResponse {
|
||||
response_url: Some(navigation_response.final_url.clone()),
|
||||
response: Box::new(navigation_response),
|
||||
response_filter,
|
||||
from_network_fallback: true,
|
||||
@@ -606,8 +607,8 @@ impl ServiceWorkerRuntimeService {
|
||||
}
|
||||
}
|
||||
}
|
||||
let final_url = response
|
||||
.final_url
|
||||
let response_url = response.final_url.clone();
|
||||
let final_url = response_url
|
||||
.clone()
|
||||
.unwrap_or_else(|| job.request.url.clone());
|
||||
// The inner network fetch already applies ORB. Synthesized bodies belong
|
||||
@@ -650,6 +651,7 @@ impl ServiceWorkerRuntimeService {
|
||||
if let Some(completion_tx) = job.direct_completion_tx.take() {
|
||||
let _ = completion_tx.send(ServiceWorkerDirectFetchResult::Response(
|
||||
ServiceWorkerDirectFetchResponse {
|
||||
response_url,
|
||||
response: Box::new(navigation_response),
|
||||
response_filter,
|
||||
from_network_fallback: false,
|
||||
|
||||
@@ -513,6 +513,7 @@ fn retained_imported_font_resources_keep_each_import_parser_base_and_response_sl
|
||||
);
|
||||
let responses = vec![
|
||||
crate::live_stylesheet::LiveStylesheetImportResponse {
|
||||
import_options: None,
|
||||
request_url: url::Url::parse("https://example.test/theme/first/imported.css").unwrap(),
|
||||
response_url: url::Url::parse("https://example.test/theme/first/imported.css").unwrap(),
|
||||
css_text: concat!(
|
||||
@@ -524,6 +525,7 @@ fn retained_imported_font_resources_keep_each_import_parser_base_and_response_sl
|
||||
origin_clean: true,
|
||||
},
|
||||
crate::live_stylesheet::LiveStylesheetImportResponse {
|
||||
import_options: None,
|
||||
request_url: url::Url::parse("https://example.test/theme/second/imported.css").unwrap(),
|
||||
response_url: url::Url::parse("https://example.test/theme/second/imported.css")
|
||||
.unwrap(),
|
||||
@@ -536,6 +538,7 @@ fn retained_imported_font_resources_keep_each_import_parser_base_and_response_sl
|
||||
origin_clean: true,
|
||||
},
|
||||
crate::live_stylesheet::LiveStylesheetImportResponse {
|
||||
import_options: None,
|
||||
request_url: url::Url::parse("https://example.test/redirect/entry.css").unwrap(),
|
||||
response_url: url::Url::parse("https://cdn.example.test/final/entry.css").unwrap(),
|
||||
css_text: "@import './nested.css';".to_owned(),
|
||||
@@ -543,6 +546,7 @@ fn retained_imported_font_resources_keep_each_import_parser_base_and_response_sl
|
||||
origin_clean: true,
|
||||
},
|
||||
crate::live_stylesheet::LiveStylesheetImportResponse {
|
||||
import_options: None,
|
||||
request_url: url::Url::parse("https://cdn.example.test/final/nested.css").unwrap(),
|
||||
response_url: url::Url::parse("https://assets.example.test/styles/nested.css").unwrap(),
|
||||
css_text: concat!(
|
||||
|
||||
@@ -244,6 +244,7 @@ impl StylesheetFetcher for RendererStylesheetFetcher {
|
||||
self.clone(),
|
||||
document_url,
|
||||
urls,
|
||||
self.prepare_stylesheet_fetch_options(StylesheetFetchOptions::default()),
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -305,7 +306,8 @@ pub(crate) async fn fetch_stylesheet_readiness_with_service_worker(
|
||||
&options,
|
||||
*response.response,
|
||||
response_provenance,
|
||||
);
|
||||
)
|
||||
.with_service_worker_response_url(response.response_url);
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(error) => {
|
||||
@@ -346,6 +348,7 @@ fn stylesheet_terminal_from_preload(
|
||||
preload.response,
|
||||
provenance,
|
||||
)
|
||||
.with_service_worker_response_url(preload.service_worker_response_url)
|
||||
}
|
||||
Err(error) => StylesheetFetchTerminal::consumed_preload_error(error),
|
||||
}
|
||||
@@ -358,9 +361,14 @@ pub(crate) fn stylesheet_request_mode_and_credentials(
|
||||
}
|
||||
|
||||
pub(crate) fn apply_stylesheet_request_parameters(
|
||||
request: moli_fetch::Request,
|
||||
mut request: moli_fetch::Request,
|
||||
options: &StylesheetFetchOptions,
|
||||
) -> moli_fetch::Request {
|
||||
if let Some(referrer_url) = options.referrer_url() {
|
||||
request = request
|
||||
.with_fetch_referrer(referrer_url.as_str())
|
||||
.expect("a parsed stylesheet referrer URL remains valid");
|
||||
}
|
||||
let (request_mode, credentials_mode) = stylesheet_request_mode_and_credentials(options);
|
||||
request
|
||||
.with_request_mode(request_mode)
|
||||
@@ -573,6 +581,7 @@ fn stylesheet_preload_response(
|
||||
// MIME interpretation and HTTP success belong to the stylesheet consumer.
|
||||
// Preserve the fetch response even when it cannot be installed as CSS.
|
||||
Ok(crate::network::preloads::DocumentPreloadResponse {
|
||||
service_worker_response_url: terminal.service_worker_response_url().cloned(),
|
||||
response,
|
||||
from_service_worker: terminal.from_service_worker(),
|
||||
response_filter: terminal.response_filter().cloned(),
|
||||
@@ -668,6 +677,98 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn imported_stylesheets_keep_document_origin_and_cookie_initiator() {
|
||||
let document = Url::parse("https://page.test/document").unwrap();
|
||||
let origin = moli_url::WebOrigin::from_url(&document);
|
||||
let parent = Url::parse("https://user:password@cdn.test/parent.css#fragment").unwrap();
|
||||
let target = Url::parse("https://cdn.test/child.css").unwrap();
|
||||
for (policy, expected) in [
|
||||
(None, Some("https://cdn.test/parent.css")),
|
||||
(Some("no-referrer"), None),
|
||||
(Some("origin"), Some("https://cdn.test/")),
|
||||
] {
|
||||
let options =
|
||||
StylesheetFetchOptions::for_import(parent.clone(), policy.map(str::to_owned))
|
||||
.with_document_referrer_policy(Some("no-referrer".to_owned()));
|
||||
let request = stylesheet_readiness_request(
|
||||
&document,
|
||||
&origin,
|
||||
&target,
|
||||
&options,
|
||||
moli_fetch::RequestResourceType::CssStyleSheet,
|
||||
false,
|
||||
None,
|
||||
);
|
||||
assert_eq!(request.referrer_header_value(&target).as_deref(), expected);
|
||||
assert_eq!(request.browser_origin().unwrap(), &origin);
|
||||
assert_eq!(
|
||||
request.cookie_context.initiator_url.as_ref(),
|
||||
Some(&document)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stylesheet_import_policy_distinguishes_network_and_synthetic_responses() {
|
||||
let url = Url::parse("https://example.test/parent.css").unwrap();
|
||||
let origin = moli_url::WebOrigin::from_url(&url);
|
||||
let document_options = StylesheetFetchOptions::default()
|
||||
.with_document_referrer_policy(Some("no-referrer".to_owned()));
|
||||
for (provenance, response_url, expected) in [
|
||||
(
|
||||
StylesheetResponseProvenance::Network,
|
||||
None,
|
||||
moli_fetch::DEFAULT_REFERRER_POLICY,
|
||||
),
|
||||
(
|
||||
StylesheetResponseProvenance::ServiceWorker { filter: None },
|
||||
None,
|
||||
"no-referrer",
|
||||
),
|
||||
(
|
||||
StylesheetResponseProvenance::ServiceWorker {
|
||||
filter: Some(moli_fetch::FetchResponseFilter::Basic),
|
||||
},
|
||||
None,
|
||||
"no-referrer",
|
||||
),
|
||||
(
|
||||
StylesheetResponseProvenance::ServiceWorker {
|
||||
filter: Some(moli_fetch::FetchResponseFilter::Basic),
|
||||
},
|
||||
Some(url.clone()),
|
||||
moli_fetch::DEFAULT_REFERRER_POLICY,
|
||||
),
|
||||
] {
|
||||
let response = stylesheet_response(&url, Some("text/css"), "");
|
||||
let terminal = stylesheet_terminal_from_response(
|
||||
&origin,
|
||||
&url,
|
||||
&document_options,
|
||||
response,
|
||||
provenance,
|
||||
);
|
||||
let terminal = terminal.with_service_worker_response_url(response_url);
|
||||
let options =
|
||||
crate::document_runtime::stylesheet_import_options(&terminal, &document_options)
|
||||
.unwrap();
|
||||
assert_eq!(options.referrer_policy(), Some(expected));
|
||||
assert_eq!(options.referrer_url(), Some(&url));
|
||||
let preload = stylesheet_preload_response(&terminal, &document_options).unwrap();
|
||||
let consumed =
|
||||
stylesheet_terminal_from_preload(&origin, &url, &document_options, Ok(preload));
|
||||
let consumed_options =
|
||||
crate::document_runtime::stylesheet_import_options(&consumed, &document_options)
|
||||
.unwrap();
|
||||
assert_eq!(consumed_options.referrer_policy(), Some(expected));
|
||||
assert_eq!(
|
||||
consumed.service_worker_response_url(),
|
||||
terminal.service_worker_response_url()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stylesheet_integrity_checks_raw_bytes_and_strongest_hash() {
|
||||
let document_url = Url::parse("https://example.test/page").unwrap();
|
||||
|
||||
@@ -447,6 +447,7 @@ impl DocumentRuntime {
|
||||
let origin_clean = result.terminal.origin_clean().unwrap_or(false);
|
||||
let physical_result = result.terminal.physical().as_result();
|
||||
ConnectedLoadNetworkResult {
|
||||
import_options: None,
|
||||
consumed_preload_error: result.terminal.is_consumed_preload_error(),
|
||||
document_owner,
|
||||
stylesheet_fetch: result.fetch,
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::module_runtime::{
|
||||
use crate::planning::{ScriptFetchMetadata, module_script_credentials_mode};
|
||||
use crate::service_worker_runtime::ServiceWorkerRequestDestination;
|
||||
use crate::stylesheet_blocking::{
|
||||
DocumentOwnedBlockingStylesheetDiscoveryInput, StylesheetFetchOptions,
|
||||
DocumentOwnedBlockingStylesheetDiscoveryInput, StylesheetFetchOptions, StylesheetFetcher,
|
||||
connected_preload_like_link_url, document_owned_blocking_stylesheet_candidate_for_node,
|
||||
link_rel_includes_token, preload_like_link_loads_stylesheet, stylesheet_link_disposition,
|
||||
stylesheet_preload_link_request,
|
||||
@@ -24,6 +24,7 @@ struct ConnectedLinkReadinessFetchResponse {
|
||||
response_filter: Option<AsyncSubresourceFetchResponseFilter>,
|
||||
integrity_matches: bool,
|
||||
from_service_worker: bool,
|
||||
service_worker_response_url: Option<Url>,
|
||||
}
|
||||
|
||||
impl ConnectedLinkReadinessFetchResponse {
|
||||
@@ -33,6 +34,7 @@ impl ConnectedLinkReadinessFetchResponse {
|
||||
load_event_successful: bool,
|
||||
integrity: Option<&str>,
|
||||
from_service_worker: bool,
|
||||
service_worker_response_url: Option<Url>,
|
||||
) -> Self {
|
||||
// Hash the original body, including binary responses. A matching digest
|
||||
// must not make an opaque response eligible, including SW responses to
|
||||
@@ -55,6 +57,7 @@ impl ConnectedLinkReadinessFetchResponse {
|
||||
response_filter,
|
||||
integrity_matches,
|
||||
from_service_worker,
|
||||
service_worker_response_url,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +68,7 @@ impl ConnectedLinkReadinessFetchResponse {
|
||||
return Err("preload response failed its integrity check".to_owned());
|
||||
}
|
||||
Ok(crate::network::preloads::DocumentPreloadResponse {
|
||||
service_worker_response_url: self.service_worker_response_url.clone(),
|
||||
response: self.response.clone(),
|
||||
from_service_worker: self.from_service_worker,
|
||||
response_filter: self.response_filter.clone(),
|
||||
@@ -1324,6 +1328,7 @@ impl DocumentRuntime {
|
||||
.as_ref()
|
||||
.is_ok_and(|response| response.load_event_successful),
|
||||
network_results: vec![ConnectedLoadNetworkResult {
|
||||
import_options: None,
|
||||
consumed_preload_error: false,
|
||||
document_owner,
|
||||
stylesheet_fetch: None,
|
||||
@@ -1515,11 +1520,14 @@ impl DocumentRuntime {
|
||||
.document_url()
|
||||
.clone();
|
||||
let resource_loader = stylesheet_fetcher.resource_loader().clone();
|
||||
let options =
|
||||
stylesheet_fetcher.prepare_stylesheet_fetch_options(StylesheetFetchOptions::default());
|
||||
resource_loader.spawn_resource_task(async move {
|
||||
let (graph, network_results) = fetch_observed_stylesheet_import_graph(
|
||||
stylesheet_fetcher,
|
||||
document_url,
|
||||
urls,
|
||||
options,
|
||||
vec![handle],
|
||||
)
|
||||
.await;
|
||||
@@ -1555,16 +1563,16 @@ impl DocumentRuntime {
|
||||
root_is_external_resource: bool,
|
||||
host_ptr: *mut JsContextHost,
|
||||
) {
|
||||
let urls = stylesheet
|
||||
.pending_import_requests_in_graph()
|
||||
.into_iter()
|
||||
.map(|request| request.url)
|
||||
let pending_imports = stylesheet.pending_import_requests_in_graph();
|
||||
let urls = pending_imports
|
||||
.iter()
|
||||
.map(|request| request.url.clone())
|
||||
.collect::<Vec<_>>();
|
||||
if urls.is_empty() {
|
||||
return;
|
||||
}
|
||||
let root = ConnectedStyleImportRoot::new(owner, &stylesheet, root_is_external_resource);
|
||||
let urls = match connected_style_import_readiness(urls) {
|
||||
match connected_style_import_readiness(urls) {
|
||||
ConnectedStyleImportReadiness::Ready(_) => {
|
||||
if unsafe { &*host_ptr }
|
||||
.install_live_stylesheet_import_graph(root.clone(), &[])
|
||||
@@ -1575,8 +1583,8 @@ impl DocumentRuntime {
|
||||
}
|
||||
return;
|
||||
}
|
||||
ConnectedStyleImportReadiness::Pending(urls) => urls,
|
||||
};
|
||||
ConnectedStyleImportReadiness::Pending(_) => {}
|
||||
}
|
||||
let task_producer = self
|
||||
.stylesheet_lifecycle
|
||||
.task_producer
|
||||
@@ -1589,16 +1597,38 @@ impl DocumentRuntime {
|
||||
.document_url()
|
||||
.clone();
|
||||
let resource_loader = stylesheet_fetcher.resource_loader().clone();
|
||||
let document_options =
|
||||
stylesheet_fetcher.prepare_stylesheet_fetch_options(StylesheetFetchOptions::default());
|
||||
let mut groups = Vec::<(StylesheetFetchOptions, Vec<Url>)>::new();
|
||||
let mut group_indices = HashMap::new();
|
||||
for request in pending_imports {
|
||||
let options = request
|
||||
.import_options
|
||||
.unwrap_or_else(|| document_options.clone());
|
||||
let index = *group_indices.entry(options.clone()).or_insert_with(|| {
|
||||
groups.push((options, Vec::new()));
|
||||
groups.len() - 1
|
||||
});
|
||||
groups[index].1.push(request.url);
|
||||
}
|
||||
resource_loader.spawn_resource_task(async move {
|
||||
let (_, mut network_results) = fetch_observed_stylesheet_import_graph(
|
||||
stylesheet_fetcher,
|
||||
document_url,
|
||||
urls,
|
||||
vec![owner],
|
||||
)
|
||||
.await;
|
||||
for result in &mut network_results {
|
||||
result.import_roots.push(root.clone());
|
||||
let results =
|
||||
futures_util::future::join_all(groups.into_iter().map(|(options, urls)| {
|
||||
fetch_observed_stylesheet_import_graph(
|
||||
stylesheet_fetcher.clone(),
|
||||
document_url.clone(),
|
||||
urls,
|
||||
options,
|
||||
vec![owner],
|
||||
)
|
||||
}))
|
||||
.await;
|
||||
let mut network_results = Vec::new();
|
||||
for (_, results) in results {
|
||||
for mut result in results {
|
||||
result.import_roots.push(root.clone());
|
||||
network_results.push(result);
|
||||
}
|
||||
}
|
||||
let _ = task_producer.send_live_import_completion(LiveStylesheetImportLoadCompletion {
|
||||
network_results,
|
||||
@@ -2504,6 +2534,7 @@ async fn fetch_connected_link_readiness_with_service_worker(
|
||||
load_event_successful,
|
||||
integrity,
|
||||
!response.from_network_fallback,
|
||||
response.response_url,
|
||||
));
|
||||
}
|
||||
Ok(None) => {}
|
||||
@@ -2531,6 +2562,7 @@ async fn fetch_connected_link_readiness_with_service_worker(
|
||||
load_event_successful,
|
||||
integrity,
|
||||
false,
|
||||
None,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -26,71 +26,132 @@ pub(in crate::document_runtime) enum ConnectedStyleImportReadiness {
|
||||
const MAX_DATA_STYLESHEET_IMPORT_EXPANSIONS: usize = 16;
|
||||
const MAX_DATA_STYLESHEET_IMPORT_URL_BYTES: usize = 16 * 1024;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct PendingStylesheetImport {
|
||||
url: Url,
|
||||
options: StylesheetFetchOptions,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ConnectedNetworkStyleImportGraph {
|
||||
pending_urls: VecDeque<Url>,
|
||||
admitted_identities: HashSet<Url>,
|
||||
pending: VecDeque<PendingStylesheetImport>,
|
||||
admitted: HashSet<Url>,
|
||||
}
|
||||
|
||||
impl ConnectedNetworkStyleImportGraph {
|
||||
fn extend(&mut self, urls: impl IntoIterator<Item = Url>) {
|
||||
fn extend(&mut self, urls: impl IntoIterator<Item = Url>, options: &StylesheetFetchOptions) {
|
||||
for url in urls {
|
||||
let identity = import_url_identity(&url);
|
||||
if self.admitted_identities.contains(&identity) {
|
||||
if !self.admitted.insert(identity) {
|
||||
continue;
|
||||
}
|
||||
self.admitted_identities.insert(identity);
|
||||
self.pending_urls.push_back(url);
|
||||
self.pending.push_back(PendingStylesheetImport {
|
||||
url,
|
||||
options: options.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.pending_urls.is_empty()
|
||||
self.pending.is_empty()
|
||||
}
|
||||
|
||||
fn take_pending(&mut self) -> Vec<Url> {
|
||||
self.pending_urls.drain(..).collect()
|
||||
fn take_pending(&mut self) -> Vec<PendingStylesheetImport> {
|
||||
self.pending.drain(..).collect()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn stylesheet_import_options(
|
||||
terminal: &crate::stylesheet_blocking::StylesheetFetchTerminal,
|
||||
document_options: &StylesheetFetchOptions,
|
||||
) -> Option<StylesheetFetchOptions> {
|
||||
let response = terminal.ready_response()?;
|
||||
let inherits_document_policy = matches!(response.final_url.scheme(), "data" | "blob")
|
||||
|| (terminal.from_service_worker() && terminal.service_worker_response_url().is_none());
|
||||
let policy =
|
||||
moli_fetch::response_referrer_policy_from_headers(&response.headers).or_else(|| {
|
||||
inherits_document_policy.then(|| {
|
||||
moli_fetch::effective_referrer_policy(
|
||||
None,
|
||||
document_options.document_referrer_policy(),
|
||||
)
|
||||
.to_owned()
|
||||
})
|
||||
});
|
||||
Some(
|
||||
StylesheetFetchOptions::for_import(response.final_url.clone(), policy)
|
||||
.with_document_referrer_policy(
|
||||
document_options
|
||||
.document_referrer_policy()
|
||||
.map(str::to_owned),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) async fn fetch_complete_stylesheet_import_graph(
|
||||
stylesheet_fetcher: crate::stylesheet_blocking::RendererStylesheetFetcher,
|
||||
document_url: Url,
|
||||
urls: Vec<Url>,
|
||||
options: StylesheetFetchOptions,
|
||||
) -> crate::stylesheet_blocking::StylesheetImportGraphFetchResult {
|
||||
let (mut successful, urls) = match connected_style_import_readiness(urls) {
|
||||
let urls = match connected_style_import_readiness(urls) {
|
||||
ConnectedStyleImportReadiness::Ready(successful) => {
|
||||
return crate::stylesheet_blocking::StylesheetImportGraphFetchResult::new(
|
||||
successful,
|
||||
Vec::new(),
|
||||
);
|
||||
}
|
||||
ConnectedStyleImportReadiness::Pending(urls) => (true, urls),
|
||||
ConnectedStyleImportReadiness::Pending(urls) => urls,
|
||||
};
|
||||
let document_options =
|
||||
stylesheet_fetcher.prepare_stylesheet_fetch_options(StylesheetFetchOptions::default());
|
||||
let mut successful = true;
|
||||
let mut network_results = Vec::new();
|
||||
let mut import_graph = ConnectedNetworkStyleImportGraph::default();
|
||||
import_graph.extend(urls);
|
||||
if let Some(parent) = options.referrer_url() {
|
||||
import_graph.admitted.insert(import_url_identity(parent));
|
||||
}
|
||||
import_graph.extend(urls, &options);
|
||||
while !import_graph.is_empty() {
|
||||
let current_urls = import_graph.take_pending();
|
||||
let pending_fetches = join_all(current_urls.into_iter().map(|url| {
|
||||
let mut network_requests = Vec::new();
|
||||
for request in import_graph.take_pending() {
|
||||
if request.url.scheme() != "data" {
|
||||
network_requests.push(request);
|
||||
continue;
|
||||
}
|
||||
match data_stylesheet_import_readiness(&request.url) {
|
||||
DataStylesheetImportReadiness::NoImports => {}
|
||||
DataStylesheetImportReadiness::Failed => successful = false,
|
||||
DataStylesheetImportReadiness::Imports(urls) => {
|
||||
let options = StylesheetFetchOptions::for_import(
|
||||
request.url,
|
||||
document_options
|
||||
.document_referrer_policy()
|
||||
.map(str::to_owned),
|
||||
);
|
||||
import_graph.extend(urls, &options);
|
||||
}
|
||||
}
|
||||
}
|
||||
let pending_fetches = join_all(network_requests.into_iter().map(|request| {
|
||||
let stylesheet_fetcher = stylesheet_fetcher.clone();
|
||||
let fetch_document_url = document_url.clone();
|
||||
let request_url = url.clone();
|
||||
let start_unix_millis = moli_time::unix_epoch_millis();
|
||||
async move {
|
||||
let terminal = stylesheet_fetcher
|
||||
.fetch_stylesheet_resource(
|
||||
fetch_document_url,
|
||||
request_url,
|
||||
StylesheetFetchOptions::default(),
|
||||
request.url.clone(),
|
||||
request.options,
|
||||
)
|
||||
.await;
|
||||
(url, start_unix_millis, terminal)
|
||||
(request.url, start_unix_millis, terminal)
|
||||
}
|
||||
}))
|
||||
.await;
|
||||
for (url, start_unix_millis, terminal) in pending_fetches {
|
||||
successful &= terminal.is_ready();
|
||||
let import_options = stylesheet_import_options(&terminal, &document_options);
|
||||
if let Some(response) = terminal.ready_response() {
|
||||
let nested_urls = crate::style_engine::stylesheet_top_level_import_urls(
|
||||
response.body_text(),
|
||||
@@ -102,8 +163,13 @@ pub(crate) async fn fetch_complete_stylesheet_import_graph(
|
||||
ConnectedStyleImportReadiness::Ready(nested_successful) => {
|
||||
successful &= nested_successful;
|
||||
}
|
||||
ConnectedStyleImportReadiness::Pending(urls) => {
|
||||
import_graph.extend(urls);
|
||||
ConnectedStyleImportReadiness::Pending(nested_urls) => {
|
||||
import_graph.extend(
|
||||
nested_urls,
|
||||
import_options
|
||||
.as_ref()
|
||||
.expect("ready stylesheet retains import referrer state"),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,7 +178,8 @@ pub(crate) async fn fetch_complete_stylesheet_import_graph(
|
||||
url,
|
||||
start_unix_millis,
|
||||
terminal,
|
||||
),
|
||||
)
|
||||
.with_import_options(import_options),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -149,10 +216,12 @@ pub(in crate::document_runtime) fn data_stylesheet_import_readiness(
|
||||
}
|
||||
|
||||
pub(in crate::document_runtime) fn connected_style_import_readiness(
|
||||
urls: Vec<Url>,
|
||||
mut urls: Vec<Url>,
|
||||
) -> ConnectedStyleImportReadiness {
|
||||
let mut pending = Vec::new();
|
||||
let mut stack = VecDeque::from(urls);
|
||||
let mut roots = HashSet::new();
|
||||
urls.retain(|url| roots.insert(import_url_identity(url)));
|
||||
let mut has_network_imports = false;
|
||||
let mut stack = urls.iter().cloned().collect::<VecDeque<_>>();
|
||||
let mut seen = HashSet::new();
|
||||
let mut data_expansions = 0;
|
||||
while let Some(url) = stack.pop_front() {
|
||||
@@ -160,7 +229,7 @@ pub(in crate::document_runtime) fn connected_style_import_readiness(
|
||||
continue;
|
||||
}
|
||||
if url.scheme() != "data" {
|
||||
pending.push(url);
|
||||
has_network_imports = true;
|
||||
continue;
|
||||
}
|
||||
if url.as_str().len() > MAX_DATA_STYLESHEET_IMPORT_URL_BYTES {
|
||||
@@ -182,10 +251,12 @@ pub(in crate::document_runtime) fn connected_style_import_readiness(
|
||||
}
|
||||
}
|
||||
}
|
||||
if pending.is_empty() {
|
||||
ConnectedStyleImportReadiness::Ready(true)
|
||||
if has_network_imports {
|
||||
// Preserve local parents so fetching can retain each sheet's referrer
|
||||
// state instead of treating its descendants as direct imports.
|
||||
ConnectedStyleImportReadiness::Pending(urls)
|
||||
} else {
|
||||
ConnectedStyleImportReadiness::Pending(pending)
|
||||
ConnectedStyleImportReadiness::Ready(true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,6 +305,20 @@ mod tests {
|
||||
assert_eq!(pending, vec![first]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connected_style_import_readiness_preserves_local_parent_sheets() {
|
||||
let local =
|
||||
Url::parse("data:text/css,@import 'https://example.test/imported.css';").unwrap();
|
||||
let direct = Url::parse("https://example.test/direct.css").unwrap();
|
||||
let ConnectedStyleImportReadiness::Pending(pending) =
|
||||
connected_style_import_readiness(vec![local.clone(), direct.clone()])
|
||||
else {
|
||||
panic!("local sheets with external imports must remain pending");
|
||||
};
|
||||
|
||||
assert_eq!(pending, vec![local, direct]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn network_style_import_graph_deduplicates_fragments_before_fetch() {
|
||||
let first = Url::parse("https://example.test/shared.css#first").unwrap();
|
||||
@@ -241,8 +326,18 @@ mod tests {
|
||||
let second = Url::parse("https://example.test/second.css").unwrap();
|
||||
let mut graph = ConnectedNetworkStyleImportGraph::default();
|
||||
|
||||
graph.extend([first.clone(), duplicate, second.clone()]);
|
||||
assert_eq!(graph.take_pending(), vec![first, second]);
|
||||
graph.extend(
|
||||
[first.clone(), duplicate, second.clone()],
|
||||
&StylesheetFetchOptions::default(),
|
||||
);
|
||||
assert_eq!(
|
||||
graph
|
||||
.take_pending()
|
||||
.into_iter()
|
||||
.map(|request| request.url)
|
||||
.collect::<Vec<_>>(),
|
||||
vec![first, second]
|
||||
);
|
||||
assert!(graph.is_empty());
|
||||
}
|
||||
|
||||
@@ -252,7 +347,7 @@ mod tests {
|
||||
.map(|index| Url::parse(&format!("https://example.test/import-{index}.css")).unwrap());
|
||||
let mut graph = ConnectedNetworkStyleImportGraph::default();
|
||||
|
||||
graph.extend(urls);
|
||||
graph.extend(urls, &StylesheetFetchOptions::default());
|
||||
|
||||
assert_eq!(graph.take_pending().len(), 1_100);
|
||||
assert!(graph.is_empty());
|
||||
|
||||
@@ -10,6 +10,7 @@ pub(in crate::document_runtime) async fn fetch_observed_stylesheet_import_graph(
|
||||
stylesheet_fetcher: crate::stylesheet_blocking::RendererStylesheetFetcher,
|
||||
document_url: Url,
|
||||
urls: Vec<Url>,
|
||||
options: crate::stylesheet_blocking::StylesheetFetchOptions,
|
||||
source_owners: Vec<DomHandle>,
|
||||
) -> (
|
||||
Arc<crate::stylesheet_blocking::StylesheetImportGraphFetchResult>,
|
||||
@@ -20,6 +21,7 @@ pub(in crate::document_runtime) async fn fetch_observed_stylesheet_import_graph(
|
||||
stylesheet_fetcher,
|
||||
document_url.clone(),
|
||||
urls,
|
||||
options,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
@@ -31,8 +33,10 @@ pub(in crate::document_runtime) async fn fetch_observed_stylesheet_import_graph(
|
||||
let start_unix_millis = result.start_unix_millis();
|
||||
let terminal = result.terminal();
|
||||
let origin_clean = terminal.origin_clean().unwrap_or(false);
|
||||
let import_options = result.import_options().cloned();
|
||||
let result = terminal.physical().as_result();
|
||||
ConnectedLoadNetworkResult {
|
||||
import_options,
|
||||
consumed_preload_error: terminal.is_consumed_preload_error(),
|
||||
document_owner: None,
|
||||
stylesheet_fetch: None,
|
||||
@@ -62,6 +66,7 @@ pub(crate) fn live_stylesheet_import_responses(
|
||||
let terminal = result.terminal();
|
||||
let ready_response = terminal.ready_response();
|
||||
crate::live_stylesheet::LiveStylesheetImportResponse {
|
||||
import_options: result.import_options().cloned(),
|
||||
request_url: result.request_url().clone(),
|
||||
response_url: match terminal.physical() {
|
||||
crate::stylesheet_blocking::StylesheetPhysicalOutcome::Response(response) => {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use super::*;
|
||||
use crate::stylesheet_blocking::StylesheetFetcher;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct LinkedStylesheetImportGraphCompletion {
|
||||
@@ -345,7 +346,7 @@ impl DocumentRuntime {
|
||||
) {
|
||||
let fetch = load.fetch().clone();
|
||||
let roots = self.linked_stylesheet_import_roots(&fetch);
|
||||
let urls = match super::import_graph::connected_style_import_readiness(urls.clone()) {
|
||||
let urls = match super::import_graph::connected_style_import_readiness(urls) {
|
||||
super::import_graph::ConnectedStyleImportReadiness::Ready(graph_successful) => {
|
||||
if !host_ptr.is_null() {
|
||||
for root in &roots {
|
||||
@@ -401,12 +402,22 @@ impl DocumentRuntime {
|
||||
.expect("linked stylesheet import requires a bound Page task producer");
|
||||
let resource_loader = stylesheet_fetcher.resource_loader().clone();
|
||||
let document_url = resource_loader.fetch_context().document_url().clone();
|
||||
let document_options = stylesheet_fetcher.prepare_stylesheet_fetch_options(
|
||||
crate::stylesheet_blocking::StylesheetFetchOptions::default(),
|
||||
);
|
||||
let options = fetch
|
||||
.terminal()
|
||||
.and_then(|terminal| {
|
||||
super::import_graph::stylesheet_import_options(&terminal, &document_options)
|
||||
})
|
||||
.expect("a linked import graph requires a usable root response");
|
||||
resource_loader.spawn_resource_task(async move {
|
||||
let (graph, network_results) =
|
||||
super::import_graph_projection::fetch_observed_stylesheet_import_graph(
|
||||
stylesheet_fetcher,
|
||||
document_url,
|
||||
urls,
|
||||
options,
|
||||
Vec::new(),
|
||||
)
|
||||
.await;
|
||||
|
||||
@@ -34,8 +34,8 @@ pub(crate) use completion::{
|
||||
StylesheetImportCompletionAuthority,
|
||||
};
|
||||
pub(crate) use connected::{ConnectedStyleLoadPrimeResult, PreparedConnectedStyleLoad};
|
||||
pub(crate) use import_graph::fetch_complete_stylesheet_import_graph;
|
||||
pub(super) use import_graph::{ConnectedStyleImportReadiness, connected_style_import_readiness};
|
||||
pub(crate) use import_graph::{fetch_complete_stylesheet_import_graph, stylesheet_import_options};
|
||||
pub(super) use import_graph_projection::fetch_observed_stylesheet_import_graph;
|
||||
pub(crate) use import_graph_projection::live_stylesheet_import_responses;
|
||||
pub(super) use link_state::{LinkStyleSettlement, LinkStyleState, StylesheetCompletionState};
|
||||
|
||||
@@ -205,6 +205,7 @@ impl DocumentRuntime {
|
||||
request_url.clone(),
|
||||
prepared.clone(),
|
||||
));
|
||||
host.retain_linked_stylesheet_import_options(load.owner(), terminal);
|
||||
}
|
||||
let import_root = prepared.as_ref().and_then(|_| {
|
||||
host.linked_live_stylesheet(load.owner()).map(|stylesheet| {
|
||||
|
||||
@@ -24,6 +24,7 @@ struct StylesheetFetchOptionsData {
|
||||
cross_origin: Option<String>,
|
||||
referrer_policy: Option<String>,
|
||||
document_referrer_policy: Option<String>,
|
||||
referrer_url: Option<Url>,
|
||||
integrity: Option<String>,
|
||||
nonce: Option<String>,
|
||||
charset: Option<String>,
|
||||
@@ -50,6 +51,7 @@ impl StylesheetFetchOptions {
|
||||
cross_origin: normalize_cross_origin(cross_origin),
|
||||
referrer_policy: normalize_token(referrer_policy),
|
||||
document_referrer_policy: None,
|
||||
referrer_url: None,
|
||||
integrity: normalize_preserved_value(integrity),
|
||||
nonce: normalize_preserved_value(nonce),
|
||||
charset: normalize_token(charset),
|
||||
@@ -80,6 +82,25 @@ impl StylesheetFetchOptions {
|
||||
self.0.document_referrer_policy.as_deref()
|
||||
}
|
||||
|
||||
/// Requests initiated by an external sheet use that sheet's response URL
|
||||
/// and policy, independently of the owning Document's fetch authority.
|
||||
pub fn for_import(mut referrer_url: Url, referrer_policy: Option<String>) -> Self {
|
||||
referrer_url.set_fragment(None);
|
||||
Self(Arc::new(StylesheetFetchOptionsData {
|
||||
referrer_url: Some(referrer_url),
|
||||
referrer_policy: Some(
|
||||
referrer_policy
|
||||
.filter(|policy| !policy.is_empty())
|
||||
.unwrap_or_else(|| moli_fetch::DEFAULT_REFERRER_POLICY.to_owned()),
|
||||
),
|
||||
..Default::default()
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn referrer_url(&self) -> Option<&Url> {
|
||||
self.0.referrer_url.as_ref()
|
||||
}
|
||||
|
||||
pub fn integrity(&self) -> Option<&str> {
|
||||
self.0.integrity.as_deref()
|
||||
}
|
||||
@@ -143,6 +164,7 @@ pub struct StylesheetResourceKey {
|
||||
credentials_mode: RequestCredentialsMode,
|
||||
referrer_policy: Option<String>,
|
||||
document_referrer_policy: Option<String>,
|
||||
referrer_url: Option<Url>,
|
||||
integrity: Option<String>,
|
||||
charset: Option<String>,
|
||||
quirks_mode_mime_compatibility: bool,
|
||||
@@ -158,6 +180,7 @@ impl StylesheetResourceKey {
|
||||
credentials_mode,
|
||||
referrer_policy: options.referrer_policy().map(str::to_owned),
|
||||
document_referrer_policy: options.document_referrer_policy().map(str::to_owned),
|
||||
referrer_url: options.referrer_url().cloned(),
|
||||
integrity: options.integrity().map(str::to_owned),
|
||||
charset: options.charset().map(str::to_owned),
|
||||
quirks_mode_mime_compatibility: options.quirks_mode_mime_compatibility(),
|
||||
@@ -207,6 +230,7 @@ impl StylesheetUsability {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StylesheetFetchTerminal {
|
||||
service_worker_response_url: Option<Url>,
|
||||
physical: StylesheetPhysicalOutcome,
|
||||
usability: StylesheetUsability,
|
||||
origin_clean: Option<bool>,
|
||||
@@ -226,6 +250,7 @@ impl StylesheetFetchTerminal {
|
||||
usability,
|
||||
origin_clean: Some(origin_clean),
|
||||
from_service_worker: false,
|
||||
service_worker_response_url: None,
|
||||
response_filter: None,
|
||||
consumed_preload_error: false,
|
||||
}
|
||||
@@ -256,6 +281,7 @@ impl StylesheetFetchTerminal {
|
||||
usability: StylesheetUsability::Failed { reason },
|
||||
origin_clean: None,
|
||||
from_service_worker: false,
|
||||
service_worker_response_url: None,
|
||||
response_filter: None,
|
||||
consumed_preload_error: false,
|
||||
}
|
||||
@@ -285,6 +311,15 @@ impl StylesheetFetchTerminal {
|
||||
self.from_service_worker
|
||||
}
|
||||
|
||||
pub fn with_service_worker_response_url(mut self, url: Option<Url>) -> Self {
|
||||
self.service_worker_response_url = url;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn service_worker_response_url(&self) -> Option<&Url> {
|
||||
self.service_worker_response_url.as_ref()
|
||||
}
|
||||
|
||||
pub fn response_filter(&self) -> Option<&moli_fetch::FetchResponseFilter> {
|
||||
self.response_filter.as_ref()
|
||||
}
|
||||
|
||||
@@ -148,6 +148,7 @@ pub struct StylesheetImportNetworkResult {
|
||||
pub(crate) request_url: Url,
|
||||
pub(crate) start_unix_millis: f64,
|
||||
pub(crate) terminal: StylesheetFetchTerminal,
|
||||
import_options: Option<crate::StylesheetFetchOptions>,
|
||||
}
|
||||
|
||||
impl StylesheetImportNetworkResult {
|
||||
@@ -160,6 +161,7 @@ impl StylesheetImportNetworkResult {
|
||||
request_url,
|
||||
start_unix_millis,
|
||||
terminal,
|
||||
import_options: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,6 +177,15 @@ impl StylesheetImportNetworkResult {
|
||||
&self.terminal
|
||||
}
|
||||
|
||||
pub fn with_import_options(mut self, options: Option<crate::StylesheetFetchOptions>) -> Self {
|
||||
self.import_options = options;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn import_options(&self) -> Option<&crate::StylesheetFetchOptions> {
|
||||
self.import_options.as_ref()
|
||||
}
|
||||
|
||||
pub fn into_parts(self) -> (Url, f64, StylesheetFetchTerminal) {
|
||||
(self.request_url, self.start_unix_millis, self.terminal)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user