mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 16:03:10 +00:00
fix(csp): retain inherited self for script element admission
Check enforced and report-only script URLs against the policy's retained self URL while preserving the protected Document URL in violation reports. This lets initial about:blank popup script elements load same-origin modules without widening cross-origin policy. Strengthen the real auxiliary-target regression with inherited script-src/connect-src self and an unchanged about:blank URL. Cover positive and negative script policies plus reporting for blank, srcdoc and data Documents. Validation: cargo fmt --all; cargo clippy --workspace --all-targets --all-features -- -D warnings; cargo nextest run --no-fail-fast (23552 passed, 18 existing skips; 32 threads, no retries).
This commit is contained in:
+9
-2
@@ -42,10 +42,15 @@ async fn auxiliary_module_completions_follow_the_committed_document_without_a_fr
|
||||
[("content-type", mime), ("access-control-allow-origin", "*")],
|
||||
body,
|
||||
).into_response();
|
||||
if uri.path() == "/popup-blocked" {
|
||||
let policy = match uri.path() {
|
||||
"/parent" => Some("script-src 'self' 'unsafe-eval'; connect-src 'self'"),
|
||||
"/popup-blocked" => Some("script-src 'unsafe-eval'; connect-src 'self'"),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(policy) = policy {
|
||||
response.headers_mut().insert(
|
||||
"content-security-policy",
|
||||
axum::http::HeaderValue::from_static("script-src 'unsafe-eval'; connect-src 'self'"),
|
||||
axum::http::HeaderValue::from_static(policy),
|
||||
);
|
||||
}
|
||||
response
|
||||
@@ -69,6 +74,8 @@ async fn auxiliary_module_completions_follow_the_committed_document_without_a_fr
|
||||
|
||||
// Never discover or attach a frontend to the auxiliary target. Its own
|
||||
// networking tasks must finish while only the opener is being observed.
|
||||
// The blank Document inherits 'self' from its opener without changing
|
||||
// its about:blank URL to the opener's URL.
|
||||
for mode in ["blank", "navigate", "blocked"] {
|
||||
let source = format!(
|
||||
"{}\nstartAuxiliaryModuleProbe({}, {});true",
|
||||
|
||||
@@ -15,6 +15,9 @@ function startAuxiliaryModuleProbe(base, mode) {
|
||||
if (!value) throw Error(name);
|
||||
checks.push(name);
|
||||
};
|
||||
if (mode === 'blank' && document.URL !== 'about:blank') {
|
||||
throw Error('initial Document URL must remain about:blank');
|
||||
}
|
||||
if (mode === 'blocked') {
|
||||
const events = [];
|
||||
addEventListener('securitypolicyviolation', event => events.push(event));
|
||||
|
||||
@@ -1214,19 +1214,29 @@ impl DocumentRuntime {
|
||||
request: ContentSecurityPolicyScriptElementRequest<'_>,
|
||||
) -> Option<DocumentContentSecurityPolicyViolation> {
|
||||
let document_url = self.document_url();
|
||||
let policy_url = self
|
||||
.policy_container
|
||||
.content_security_policy_self_url
|
||||
.as_ref()
|
||||
.unwrap_or(document_url);
|
||||
let policies = self.document_content_security_policies_for_optional_document(
|
||||
Some(self.document_handle()),
|
||||
&self.policy_container.response_content_security_policies,
|
||||
&self.policy_container.content_security_reporting_endpoints,
|
||||
);
|
||||
document_script_element_url_policy_violation_from_document_policies(
|
||||
let violation = document_script_element_url_policy_violation_from_document_policies(
|
||||
policies,
|
||||
document_url,
|
||||
policy_url,
|
||||
request_url,
|
||||
redirect_status,
|
||||
ContentSecurityPolicyDisposition::Enforce,
|
||||
request,
|
||||
)
|
||||
)?;
|
||||
Some(DocumentContentSecurityPolicyViolation {
|
||||
document_uri: crate::content_security_policy::csp_url_for_report(document_url),
|
||||
source_file: crate::content_security_policy::csp_url_for_report(document_url),
|
||||
..violation
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -1268,17 +1278,27 @@ impl DocumentRuntime {
|
||||
request: ContentSecurityPolicyScriptElementRequest<'_>,
|
||||
) -> Option<DocumentContentSecurityPolicyViolation> {
|
||||
let document_url = self.document_url();
|
||||
document_script_element_url_policy_violation(
|
||||
let policy_url = self
|
||||
.policy_container
|
||||
.content_security_policy_self_url
|
||||
.as_ref()
|
||||
.unwrap_or(document_url);
|
||||
let violation = document_script_element_url_policy_violation(
|
||||
&self
|
||||
.policy_container
|
||||
.response_content_security_report_only_policies,
|
||||
&self.policy_container.content_security_reporting_endpoints,
|
||||
document_url,
|
||||
policy_url,
|
||||
request_url,
|
||||
redirect_status,
|
||||
ContentSecurityPolicyDisposition::Report,
|
||||
request,
|
||||
)
|
||||
)?;
|
||||
Some(DocumentContentSecurityPolicyViolation {
|
||||
document_uri: crate::content_security_policy::csp_url_for_report(document_url),
|
||||
source_file: crate::content_security_policy::csp_url_for_report(document_url),
|
||||
..violation
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn document_connect_csp_check_with_redirect_status(
|
||||
@@ -4268,6 +4288,66 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn script_requests_keep_inherited_self_separate_from_the_protected_document() {
|
||||
let policy_url = Url::parse("https://example.test/parent").unwrap();
|
||||
let policy_text = b"script-src 'self'; report-uri /report";
|
||||
for document_url in [
|
||||
"about:blank",
|
||||
"about:srcdoc",
|
||||
"data:text/html,local-document",
|
||||
] {
|
||||
let document_url = Url::parse(document_url).unwrap();
|
||||
let document = HtmlParser::SCRIPTING_ENABLED
|
||||
.parse(document_url.clone(), "<!doctype html>".to_owned());
|
||||
let mut runtime = DocumentRuntime::from_document(document);
|
||||
runtime.set_initial_document_policy_container(
|
||||
DocumentPolicyContainer::from_navigation_response_headers(
|
||||
&[
|
||||
("Content-Security-Policy".to_owned(), policy_text.to_vec()),
|
||||
(
|
||||
"Content-Security-Policy-Report-Only".to_owned(),
|
||||
policy_text.to_vec(),
|
||||
),
|
||||
],
|
||||
&policy_url,
|
||||
),
|
||||
);
|
||||
for (request_url, blocked) in [
|
||||
("https://example.test/script.js", false),
|
||||
("https://other.test/script.js", true),
|
||||
] {
|
||||
let request_url = Url::parse(request_url).unwrap();
|
||||
let enforced = runtime.script_element_request_csp_violation(&request_url);
|
||||
let reported =
|
||||
runtime.script_element_request_csp_report_only_violation(&request_url);
|
||||
assert_eq!(enforced.is_some(), blocked);
|
||||
assert_eq!(reported.is_some(), blocked);
|
||||
for (violation, disposition) in enforced
|
||||
.iter()
|
||||
.map(|v| (v, ContentSecurityPolicyDisposition::Enforce))
|
||||
.chain(
|
||||
reported
|
||||
.iter()
|
||||
.map(|v| (v, ContentSecurityPolicyDisposition::Report)),
|
||||
)
|
||||
{
|
||||
assert_eq!(violation.disposition, disposition);
|
||||
assert_eq!(
|
||||
violation.document_uri,
|
||||
crate::content_security_policy::csp_url_for_report(&document_url)
|
||||
);
|
||||
assert_eq!(violation.source_file, violation.document_uri);
|
||||
assert_eq!(
|
||||
violation.report_uri_endpoints,
|
||||
["https://example.test/report"]
|
||||
);
|
||||
}
|
||||
}
|
||||
assert_eq!(runtime.document_url(), &document_url);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csp_host_source_path_allows_directory_descendants_only() {
|
||||
let runtime = runtime_for_html(
|
||||
|
||||
Reference in New Issue
Block a user