From 215937729650e7b6fe97cbbf732c2466279bbc44 Mon Sep 17 00:00:00 2001 From: Jamie Mansfield Date: Wed, 29 May 2024 19:27:27 +0100 Subject: [PATCH] LibWeb: Upgrade mixed requests to potentially trustworthy URLs (if appropriate) --- .../LibWeb/Fetch/Fetching/Fetching.cpp | 4 ++- .../MixedContent/AbstractOperations.cpp | 30 +++++++++++++++++++ .../LibWeb/MixedContent/AbstractOperations.h | 2 ++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp index 02e6e67bf11..222db7178c2 100644 --- a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp @@ -237,7 +237,9 @@ WebIDL::ExceptionOr> main_fetch(JS::Realm& realm, Inf // FIXME: 4. Run report Content Security Policy violations for request. // FIXME: 5. Upgrade request to a potentially trustworthy URL, if appropriate. - // FIXME: 6. Upgrade a mixed content request to a potentially trustworthy URL, if appropriate. + + // 6. Upgrade a mixed content request to a potentially trustworthy URL, if appropriate. + MixedContent::upgrade_a_mixed_content_request_to_a_potentially_trustworthy_url_if_appropriate(request); // 7. If should request be blocked due to a bad port, should fetching request be blocked as mixed content, or // should request be blocked by Content Security Policy returns blocked, then set response to a network error. diff --git a/Userland/Libraries/LibWeb/MixedContent/AbstractOperations.cpp b/Userland/Libraries/LibWeb/MixedContent/AbstractOperations.cpp index 552d99ffb15..73acf022d72 100644 --- a/Userland/Libraries/LibWeb/MixedContent/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/MixedContent/AbstractOperations.cpp @@ -10,6 +10,36 @@ namespace Web::MixedContent { +// https://w3c.github.io/webappsec-mixed-content/#upgrade-algorithm +void upgrade_a_mixed_content_request_to_a_potentially_trustworthy_url_if_appropriate(Fetch::Infrastructure::Request& request) +{ + // 1. If one or more of the following conditions is met, return without modifying request: + if ( + // 1. request’s URL is a potentially trustworthy URL. + SecureContexts::is_url_potentially_trustworthy(request.url()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy + + // 2. request’s URL’s host is an IP address. + || request.url().host().has() || request.url().host().has() + + // 3. § 4.3 Does settings prohibit mixed security contexts? returns "Does Not Restrict Mixed Security Contents" when applied to request’s client. + || does_settings_prohibit_mixed_security_contexts(request.client()) == ProhibitsMixedSecurityContexts::DoesNotRestrictMixedSecurityContexts + + // 4. request’s destination is not "image", "audio", or "video". + || (request.destination() != Fetch::Infrastructure::Request::Destination::Image + && request.destination() != Fetch::Infrastructure::Request::Destination::Audio + && request.destination() != Fetch::Infrastructure::Request::Destination::Video) + + // 5. request’s destination is "image" and request’s initiator is "imageset". + || (request.destination() == Fetch::Infrastructure::Request::Destination::Image + && request.initiator() == Fetch::Infrastructure::Request::Initiator::ImageSet)) { + return; + } + + // 2. If request’s URL’s scheme is http, set request’s URL’s scheme to https, and return. + if (request.url().scheme() == "http") + request.url().set_scheme("https"_string); +} + // https://w3c.github.io/webappsec-mixed-content/#categorize-settings-object ProhibitsMixedSecurityContexts does_settings_prohibit_mixed_security_contexts(JS::GCPtr settings) { diff --git a/Userland/Libraries/LibWeb/MixedContent/AbstractOperations.h b/Userland/Libraries/LibWeb/MixedContent/AbstractOperations.h index 8234bd44992..c46121a16ec 100644 --- a/Userland/Libraries/LibWeb/MixedContent/AbstractOperations.h +++ b/Userland/Libraries/LibWeb/MixedContent/AbstractOperations.h @@ -12,6 +12,8 @@ namespace Web::MixedContent { +void upgrade_a_mixed_content_request_to_a_potentially_trustworthy_url_if_appropriate(Fetch::Infrastructure::Request&); + enum class ProhibitsMixedSecurityContexts { ProhibitsMixedSecurityContexts, DoesNotRestrictMixedSecurityContexts,