fix(referrer): persist meta policy on each document

Apply meta referrer delivery on insertion and attribute changes, preserve it across removal and document.open(), and share the state with Fetch, XHR, and existing script consumers. Cover temporal ordering, namespaces, windowless documents, cloning, and main/child/popup network owners.
This commit is contained in:
ldm0
2026-11-20 22:47:44 +08:00
parent 7ba52e5dab
commit cf2d928040
6 changed files with 238 additions and 7 deletions
+6
View File
@@ -46,6 +46,9 @@ mod modulepreload_document_referrer;
#[path = "classic_document_referrer.rs"]
mod classic_document_referrer;
#[path = "meta_referrer_policy.rs"]
mod meta_referrer_policy;
struct IntegrityServers {
origin: String,
cross_origin: String,
@@ -202,6 +205,9 @@ fn fixture_response(
let (path, query) = request.path.split_once('?').unwrap_or((&request.path, ""));
let javascript = "Content-Type: text/javascript\r\n";
match path {
"/meta-policy.html" | "/meta-policy.js" | "/meta-policy-fetch" => {
meta_referrer_policy::fixture_response(path, request.referer.as_deref())
}
"/classic-policy.html" | "/classic-policy.js" => {
classic_document_referrer::fixture_response(path, query, origin, cross)
}
@@ -0,0 +1,213 @@
use super::*;
pub(super) fn fixture_response(
path: &str,
referrer: Option<&str>,
) -> (&'static str, String, String) {
let (content_type, body) = match path {
"/meta-policy.html" => (
"text/html",
r#"<!doctype html><head><meta name=referrer content=no-referrer id=first></head><body>Policy document
<script>
addEventListener('message', async event => {
if (!event.data.metaPolicyProbe) return;
try {
opener.postMessage({metaPolicyResult: await eval(event.data.metaPolicyProbe)}, '*');
} catch (error) {
opener.postMessage({metaPolicyError: String(error)}, '*');
}
});
</script></body>"#.to_owned(),
),
"/meta-policy.js" => ("text/javascript", "/* policy probe */".to_owned()),
_ => ("application/json", serde_json::to_string(&referrer).unwrap()),
};
(
"200 OK",
format!(
"Content-Type: {content_type}\r\nCache-Control: no-store\r\nReferrer-Policy: unsafe-url\r\nAccess-Control-Allow-Origin: *\r\n"
),
body,
)
}
async fn check_meta_policy(owner: &str) -> Result<()> {
let servers = IntegrityServers::spawn().await?;
let browser = Browser::new(AppConfig::default())?;
let document_url = format!("{}/meta-policy.html?owner={owner}", servers.origin);
let parent_url = format!(
"{}/document-referrer.html?response=no-referrer",
servers.origin
);
let mut page = browser
.fetch(if owner == "main" {
&document_url
} else {
&parent_url
})
.await?;
let probe = format!(
r#"(async () => {{
const observations = [];
let index = 0;
async function check(label, expected) {{
for (const origin of {origins}) {{
const id = ++index;
const resource = origin + '/meta-policy-fetch?' + id;
const actual = await (await fetch(resource)).json();
if (actual !== expected) throw new Error(label + ': fetch ' + actual + ' != ' + expected);
const explicit = await (await fetch(resource + '-explicit', {{referrerPolicy:'unsafe-url'}})).json();
if (explicit !== location.href) throw new Error(label + ': explicit policy ' + explicit);
const xhr = new XMLHttpRequest();
const value = await new Promise((resolve,reject) => {{
xhr.open('GET', resource + '-xhr');
xhr.onload = () => resolve(JSON.parse(xhr.responseText)); xhr.onerror = reject; xhr.send();
}});
if (value !== expected) throw new Error(label + ': XHR ' + value + ' != ' + expected);
const sync = new XMLHttpRequest();
sync.open('GET', resource + '-sync-xhr', false); sync.send();
const syncValue = JSON.parse(sync.responseText);
if (syncValue !== expected) throw new Error(label + ': sync XHR ' + syncValue + ' != ' + expected);
const observation = {{expected}};
if ({check_scripts}) {{
const url = origin + '/meta-policy.js?' + id;
await new Promise((resolve,reject) => {{
const script = document.createElement('script'); script.src=url;
script.onload=resolve; script.onerror=reject; document.body.append(script);
}});
observation.url = url;
}}
observations.push(observation);
}}
}}
const origin = location.origin + '/';
const first = document.getElementById('first');
await check('parser meta overrides response', null);
const second = document.createElement('meta'); second.name='referrer'; second.content='origin';
document.head.insertBefore(second,first);
await check('most recent insertion before older meta', origin);
first.content='no-referrer';
await check('same-value assignment delivers again', null);
first.remove();
await check('removal does not undo delivery', null);
first.content='unsafe-url';
await check('detached mutation has no effect', null);
document.body.append(first);
await check('body insertion delivers policy', location.href);
first.content='never';
await check('legacy never', null);
first.content='always';
await check('legacy always', location.href);
first.content='origin';
for (const value of ['', ' origin', 'origin, no-referrer', 'future-policy']) first.content=value;
await check('invalid values retain current policy', origin);
first.name='other'; first.content='no-referrer';
await check('other metadata ignored', origin);
first.name='REFERRER';
await check('name change delivers content', null);
const host=document.createElement('div');document.body.append(host);
const shadow=host.attachShadow({{mode:'open'}});
const hidden=document.createElement('meta'); hidden.name='referrer'; hidden.content='unsafe-url';
shadow.append(hidden); hidden.content='origin';
await check('shadow metadata ignored', null);
first.setAttributeNS('urn:other','content','unsafe-url');
await check('namespaced content ignored', null);
first.setAttributeNS(null,'content','origin');
await check('null namespace content applies', origin);
first.remove(); second.remove();
await check('all metadata removed', origin);
return JSON.stringify(observations);
}})()"#,
origins = serde_json::to_string(&[&servers.origin, &servers.cross_origin])?,
// Lightweight popups do not yet schedule dynamically inserted scripts.
// Keep their Fetch and synchronous/asynchronous XHR coverage independent.
check_scripts = owner != "popup",
);
let expression = match owner {
"child" => format!(
r#"(async () => {{
const frame=document.createElement('iframe');frame.src={url};
await new Promise(resolve => {{frame.onload=resolve;document.body.append(frame);}});
return frame.contentWindow.eval({probe});
}})()"#,
url = serde_json::to_string(&document_url)?,
probe = serde_json::to_string(&probe)?,
),
"popup" => format!(
r#"(async () => {{
const popup=window.open({url});
while (!popup.document || popup.document.readyState !== 'complete' || !popup.document.getElementById('first'))
await new Promise(resolve => setTimeout(resolve,1));
try {{
return await new Promise((resolve,reject) => {{
function onResult(event) {{
if ('metaPolicyResult' in event.data) {{
removeEventListener('message',onResult); resolve(event.data.metaPolicyResult);
}} else if ('metaPolicyError' in event.data) {{
removeEventListener('message',onResult); reject(new Error(event.data.metaPolicyError));
}}
}}
addEventListener('message',onResult);
popup.postMessage({{metaPolicyProbe:{probe}}}, '*');
}});
}} finally {{ popup.close(); }}
}})()"#,
url = serde_json::to_string(&document_url)?,
probe = serde_json::to_string(&probe)?,
),
_ => probe,
};
let result = tokio::time::timeout(
Duration::from_secs(15),
page.evaluate_runtime_expression_with_await_async(&expression, true),
)
.await??;
let observations: Vec<serde_json::Value> = serde_json::from_str(
result["value"]
.as_str()
.unwrap_or_else(|| panic!("{owner}: {result}")),
)?;
assert_eq!(observations.len(), 30, "{owner}: {result}");
assert_eq!(
observations
.iter()
.filter(|entry| entry["url"].is_string())
.count(),
if owner == "popup" { 0 } else { 30 },
"{owner}: script observations"
);
let requests = servers.requests.lock();
for observation in &observations {
let Some(script_url) = observation["url"].as_str() else {
continue;
};
let url = url::Url::parse(script_url)?;
let path = format!("{}?{}", url.path(), url.query().unwrap());
let found = requests
.iter()
.filter(|request| request.path == path)
.collect::<Vec<_>>();
assert_eq!(found.len(), 1, "{owner}, {path}: {requests:?}");
assert_eq!(
found[0].referer.as_deref(),
observation["expected"].as_str(),
"{owner}, {path}"
);
}
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn main_document_meta_referrer_policy_tracks_deliveries() -> Result<()> {
check_meta_policy("main").await
}
#[tokio::test(flavor = "multi_thread")]
async fn child_document_meta_referrer_policy_tracks_deliveries() -> Result<()> {
check_meta_policy("child").await
}
#[tokio::test(flavor = "multi_thread")]
async fn popup_document_meta_referrer_policy_tracks_deliveries() -> Result<()> {
check_meta_policy("popup").await
}
+4
View File
@@ -91,6 +91,7 @@ impl NativeDom {
};
let base_document =
self.base_element_attribute_owner_document(node_id, None, &normalized_name);
let referrer_attribute = self.is_meta_referrer_attribute(node_id, None, &normalized_name);
let changed = self
.node_mut(node_id)
.and_then(|node| node.data_mut().as_element_mut())
@@ -106,6 +107,9 @@ impl NativeDom {
if changed && let Some(document) = base_document {
self.process_base_element(document, false);
}
if referrer_attribute {
self.process_meta_referrer(node_id);
}
changed
}
+7
View File
@@ -90,6 +90,9 @@ struct DocumentMetadata {
#[derive(Debug, Clone, Default)]
struct DocumentArenaState {
source_response_status: Option<u16>,
// Meta delivery updates policy in time order; removing the element does
// not undo the policy it delivered to this Document.
meta_referrer_policy: Option<&'static str>,
// Sticky for this Document; aborting a parser disables document.open/write.
active_parser_was_aborted: bool,
is_inert_template_document: bool,
@@ -237,6 +240,10 @@ impl Document {
self.arena_state.active_parser_was_aborted = true;
}
pub fn meta_referrer_policy(&self) -> Option<&str> {
self.arena_state.meta_referrer_policy
}
pub fn visibility_hidden(&self) -> bool {
self.metadata.visibility_hidden
}
+2 -2
View File
@@ -110,8 +110,8 @@ mod navigation_serialize;
pub(crate) use navigation_entry::navigation_entry_public_token;
pub(crate) use navigation_serialize::current_document_referrer_policy;
pub(crate) use navigation_serialize::{
document_referrer_policy_for_native_document, document_referrer_policy_in_subtree,
history_document_id_for_holder, publish_top_level_navigation_history,
document_referrer_policy_for_native_document, history_document_id_for_holder,
publish_top_level_navigation_history,
};
mod navigation_surface;
mod navigation_transition_worlds;
@@ -1010,11 +1010,12 @@ impl DocumentRuntime {
}
pub(crate) fn current_document_referrer_policy(&self) -> Option<String> {
crate::context_bootstrap::document_referrer_policy_in_subtree(
&self.dom_host,
self.dom_host.document_handle(),
)
.or_else(|| self.response_referrer_policy().map(ToOwned::to_owned))
self.dom_host
.node(self.dom_host.document_handle())
.and_then(Node::as_document)
.and_then(|document| document.meta_referrer_policy())
.map(ToOwned::to_owned)
.or_else(|| self.response_referrer_policy().map(ToOwned::to_owned))
}
pub(crate) fn cross_origin_embedder_policy(