Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(dom): enforce document.domain registrable boundaries

ldm01e2b0bb46e38
diff --git a/Cargo.lock b/Cargo.lock
index 79dcf5c65..afe2035d6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3126,6 +3126,7 @@ dependencies = [
"moli-selector",
"moli-session-history",
"moli-shared-worker",
+ "moli-site",
"moli-storage-key",
"moli-storage-service",
"moli-streams",
diff --git a/moli-renderer-v8/Cargo.toml b/moli-renderer-v8/Cargo.toml
index 7282e0e0c..acb282e69 100644
--- a/moli-renderer-v8/Cargo.toml
+++ b/moli-renderer-v8/Cargo.toml
@@ -68,6 +68,7 @@ moli-module-syntax = { path = "../moli-module-syntax" }
moli-owner-queue = { path = "../moli-owner-queue" }
moli-paint = { path = "../moli-paint" }
moli-script = { path = "../moli-script" }
+moli-site = { path = "../moli-site" }
moli-session-history = { path = "../moli-session-history" }
moli-history = { path = "../moli-history" }
moli-stylesheet-blocking = { path = "../moli-stylesheet-blocking" }
diff --git a/moli-renderer-v8/src/native_bridge/context_host/document_domain.rs b/moli-renderer-v8/src/native_bridge/context_host/document_domain.rs
index 07d856aa1..2b411d9aa 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/document_domain.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/document_domain.rs
@@ -240,16 +240,16 @@ fn normalize_document_domain_value(value: &str) -> Option<String> {
}
fn document_domain_is_allowed_for_host(current_host: &str, domain: &str) -> bool {
- if host_is_ip_literal(current_host) {
- return domain == current_host;
+ if domain == current_host {
+ return true;
}
- if !host_matches_document_domain(current_host, domain) {
+ if host_is_ip_literal(current_host) || !host_matches_document_domain(current_host, domain) {
return false;
}
- if domain != current_host && moli_cookie_jar::host_is_public_suffix(domain) {
- return false;
- }
- domain == current_host || domain == "localhost" || domain.contains('.')
+ // A parent must stay within the original host's registrable domain. Checking
+ // only the candidate's PSL entry misses implicit single-label suffixes and
+ // parents above a public suffix selected by a wildcard rule.
+ host_matches_document_domain(domain, moli_site::registrable_site_host(current_host))
}
fn host_is_ip_literal(host: &str) -> bool {
@@ -270,6 +270,45 @@ pub(crate) fn host_matches_document_domain(host: &str, domain: &str) -> bool {
mod tests {
use super::*;
+ #[test]
+ fn document_domain_validation_respects_the_original_hosts_public_suffix() {
+ for (host, domain, allowed) in [
+ ("www1.localhost", "localhost", false),
+ ("localhost", "localhost", true),
+ (
+ "www1.web-platform.localhost",
+ "web-platform.localhost",
+ true,
+ ),
+ (
+ "example.compute.amazonaws.com",
+ "compute.amazonaws.com",
+ false,
+ ),
+ (
+ "www.example.compute.amazonaws.com",
+ "example.compute.amazonaws.com",
+ false,
+ ),
+ ("www.example.compute.amazonaws.com", "amazonaws.com", false),
+ ("test.amazonaws.com", "amazonaws.com", true),
+ (
+ "example.compute.amazonaws.com",
+ "example.compute.amazonaws.com",
+ true,
+ ),
+ ("www.city.kawasaki.jp", "city.kawasaki.jp", true),
+ ("www.foo.kawasaki.jp", "kawasaki.jp", false),
+ ] {
+ assert_eq!(
+ document_domain_is_allowed_for_host(host, domain),
+ allowed,
+ "{host} must {}relax to {domain}",
+ if allowed { "" } else { "not " }
+ );
+ }
+ }
+
#[test]
fn document_domain_validation_allows_current_or_parent_domain() {
assert_eq!(
@@ -288,7 +327,7 @@ mod tests {
"example.test",
"example.test"
));
- assert!(document_domain_is_allowed_for_host(
+ assert!(!document_domain_is_allowed_for_host(
"www1.localhost",
"localhost"
));
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage/postmessage_selection_and_errors.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage/postmessage_selection_and_errors.rs
index 297274bd4..e8a8bd2ed 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage/postmessage_selection_and_errors.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage/postmessage_selection_and_errors.rs
@@ -1902,3 +1902,54 @@ fn selection_range_membership_uses_native_document_relationships() {
""
);
}
+
+#[test]
+fn document_domain_setter_respects_the_original_hosts_public_suffix() {
+ for (host, domain, allowed) in [
+ ("www1.localhost", "localhost", false),
+ ("localhost", "localhost", true),
+ (
+ "www1.web-platform.localhost",
+ "web-platform.localhost",
+ true,
+ ),
+ ("www.example.compute.amazonaws.com", "amazonaws.com", false),
+ (
+ "www.example.compute.amazonaws.com",
+ "example.compute.amazonaws.com",
+ false,
+ ),
+ ("test.amazonaws.com", "amazonaws.com", true),
+ ("www.city.kawasaki.jp", "city.kawasaki.jp", true),
+ ] {
+ let mut vm = new_storage_test_vm(&format!("https://{host}/path"));
+ let result = vm
+ .eval(&format!(
+ r#"((value) => {{
+ const before = document.domain;
+ let outcome;
+ try {{ document.domain = value; outcome = "accepted"; }}
+ catch (error) {{
+ outcome = `${{error.name}}:${{error instanceof DOMException}}:${{error.code}}`;
+ }}
+ return JSON.stringify([before, outcome, document.domain]);
+ }})({})"#,
+ serde_json::to_string(domain).unwrap(),
+ ))
+ .expect("document.domain boundary probe should evaluate");
+ assert_eq!(
+ result,
+ serde_json::json!([
+ host,
+ if allowed {
+ "accepted"
+ } else {
+ "SecurityError:true:18"
+ },
+ if allowed { domain } else { host },
+ ])
+ .to_string(),
+ "{host} -> {domain}"
+ );
+ }
+}