Skip to content

Truly independent web browser

LibWeb/CSP: Let host-sources match IP address hosts

Andreas Klingfb7c6c09e1c4
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp
index 13e18ba7dc1..ee5e29d1f9f 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp
@@ -352,15 +352,17 @@ static MatchResult host_part_matches(Utf16View pattern, Optional<URL::Host> cons
auto const& host = maybe_host.value();
- if (!host.is_domain())
+ // INTEROP: All major engines match IP addresses as well, comparing the pattern against the serialized host as a
+ // plain string, including "*." suffix matching. IPv6 addresses serialize with brackets, which a host-part
+ // cannot contain, so only "*" matches them.
+ if (host.has<URL::OpaqueHost>() || host.is_empty_host())
return MatchResult::DoesNotMatch;
// 2. If pattern is "*", return "Matches".
if (equals(pattern, "*"sv))
return MatchResult::Matches;
- VERIFY(host.has<String>());
- auto host_string = host.get<String>();
+ auto host_string = host.serialize();
// 3. If pattern starts with "*.":
if (starts_with(pattern, "*."sv)) {
diff --git a/Tests/LibWeb/Text/expected/ContentSecurityPolicy/script-src-host-source-matches-ip-address.txt b/Tests/LibWeb/Text/expected/ContentSecurityPolicy/script-src-host-source-matches-ip-address.txt
index 6d8aee122cf..a910ae960cb 100644
--- a/Tests/LibWeb/Text/expected/ContentSecurityPolicy/script-src-host-source-matches-ip-address.txt
+++ b/Tests/LibWeb/Text/expected/ContentSecurityPolicy/script-src-host-source-matches-ip-address.txt
@@ -1,2 +1,2 @@
-127.0.0.1: failed, CSP violation: true
+127.0.0.1: loaded, CSP violation: false
127.0.0.2: failed, CSP violation: true