Skip to content

Truly independent web browser

LibWeb: Implement the cross-origin resource policy check

Shannon Boothbaed814f5887
diff --git a/Libraries/LibWeb/Fetch/Fetching/Checks.cpp b/Libraries/LibWeb/Fetch/Fetching/Checks.cpp
index e09248ee7a7..c5701bf74f6 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Checks.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/Checks.cpp
@@ -103,4 +103,107 @@ bool navigation_tao_check(Infrastructure::Response const& response, URL::Origin
return true;
}
+// https://fetch.spec.whatwg.org/#cross-origin-resource-policy-internal-check
+static Infrastructure::RequestOrResponseBlocking cross_origin_resource_policy_internal_check(URL::Origin const& origin, HTML::EmbedderPolicyValue embedder_policy_value, Infrastructure::Response const& response, ForNavigation for_navigation)
+{
+ // 1. If forNavigation is true and embedderPolicyValue is "unsafe-none", then return allowed.
+ if (for_navigation == ForNavigation::Yes && embedder_policy_value == HTML::EmbedderPolicyValue::UnsafeNone)
+ return Infrastructure::RequestOrResponseBlocking::Allowed;
+
+ // 2. Let policy be the result of getting `Cross-Origin-Resource-Policy` from response's header list.
+ // NOTE: This means that `Cross-Origin-Resource-Policy: same-site, same-origin` ends up as allowed below as it will
+ // never match anything, as long as embedderPolicyValue is "unsafe-none". Two or more
+ // `Cross-Origin-Resource-Policy` headers will have the same effect.
+ auto policy = response.header_list()->get("Cross-Origin-Resource-Policy"sv);
+
+ // 3. If policy is neither `same-origin`, `same-site`, nor `cross-origin`, then set policy to null.
+ if (policy.has_value() && !policy->is_one_of("same-origin"sv, "same-site"sv, "cross-origin"sv))
+ policy.clear();
+
+ // 4. If policy is null, then switch on embedderPolicyValue:
+ if (!policy.has_value()) {
+ switch (embedder_policy_value) {
+ // -> "unsafe-none"
+ case HTML::EmbedderPolicyValue::UnsafeNone:
+ // Do nothing.
+ break;
+ // -> "credentialless"
+ case HTML::EmbedderPolicyValue::Credentialless:
+ // Set policy to `same-origin` if:
+ // - response's request-includes-credentials is true, or
+ // - forNavigation is true.
+ if (response.request_includes_credentials() || for_navigation == ForNavigation::Yes)
+ policy = "same-origin"sv;
+ break;
+ // -> "require-corp"
+ case HTML::EmbedderPolicyValue::RequireCorp:
+ // Set policy to `same-origin`.
+ policy = "same-origin"sv;
+ break;
+ }
+ }
+
+ // 5. Switch on policy:
+ // -> null
+ // -> `cross-origin`
+ // Return allowed.
+ if (!policy.has_value() || *policy == "cross-origin"sv)
+ return Infrastructure::RequestOrResponseBlocking::Allowed;
+
+ auto const& response_url = response.url().value();
+
+ // -> `same-origin`
+ if (*policy == "same-origin"sv) {
+ // If origin is same origin with response's URL's origin, then return allowed.
+ // Otherwise, return blocked.
+ return origin.is_same_origin(response_url.origin()) ? Infrastructure::RequestOrResponseBlocking::Allowed : Infrastructure::RequestOrResponseBlocking::Blocked;
+ }
+
+ // -> `same-site`
+ // If all of the following are true
+ // - origin is schemelessly same site with response's URL's origin
+ // - origin's scheme is "https" or response's URL's scheme is not "https"
+ // then return allowed.
+ // Otherwise, return blocked.
+ // NOTE: `Cross-Origin-Resource-Policy: same-site` does not consider a response delivered via a secure transport to
+ // match a non-secure requesting origin, even if their hosts are otherwise same site. Securely-transported
+ // responses will only match a securely-transported initiator.
+ if (origin.is_schemelessly_same_site(response_url.origin())
+ && ((!origin.is_opaque() && origin.scheme() == "https"sv) || response_url.scheme() != "https"sv)) {
+ return Infrastructure::RequestOrResponseBlocking::Allowed;
+ }
+ return Infrastructure::RequestOrResponseBlocking::Blocked;
+}
+
+// https://fetch.spec.whatwg.org/#cross-origin-resource-policy-check
+// NB: settingsObject is given as its policy container's embedder policy, which is all these steps read of it while
+// violation reports are not queued. destination is only used for reports.
+Infrastructure::RequestOrResponseBlocking cross_origin_resource_policy_check(URL::Origin const& origin, HTML::EmbedderPolicy const& embedder_policy, Infrastructure::Response const& response, ForNavigation for_navigation)
+{
+ // 1. Set forNavigation to false if it is not given.
+ // 2. Let embedderPolicy be settingsObject's policy container's embedder policy.
+
+ // 3. If the cross-origin resource policy internal check with origin, "unsafe-none", response, and forNavigation
+ // returns blocked, then return blocked.
+ // NOTE: This step is needed because we don't want to report violations not related to Cross-Origin Embedder Policy
+ // below.
+ if (cross_origin_resource_policy_internal_check(origin, HTML::EmbedderPolicyValue::UnsafeNone, response, for_navigation) == Infrastructure::RequestOrResponseBlocking::Blocked)
+ return Infrastructure::RequestOrResponseBlocking::Blocked;
+
+ // FIXME: 4. If the cross-origin resource policy internal check with origin, embedderPolicy's report-only value,
+ // response, and forNavigation returns blocked, then queue a cross-origin embedder policy CORP violation
+ // report with response, settingsObject, destination, and true.
+
+ // 5. If the cross-origin resource policy internal check with origin, embedderPolicy's value, response, and
+ // forNavigation returns allowed, then return allowed.
+ if (cross_origin_resource_policy_internal_check(origin, embedder_policy.value, response, for_navigation) == Infrastructure::RequestOrResponseBlocking::Allowed)
+ return Infrastructure::RequestOrResponseBlocking::Allowed;
+
+ // FIXME: 6. Queue a cross-origin embedder policy CORP violation report with response, settingsObject, destination,
+ // and false.
+
+ // 7. Return blocked.
+ return Infrastructure::RequestOrResponseBlocking::Blocked;
+}
+
}
diff --git a/Libraries/LibWeb/Fetch/Fetching/Checks.h b/Libraries/LibWeb/Fetch/Fetching/Checks.h
index 65d0c78889b..7d8e757307c 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Checks.h
+++ b/Libraries/LibWeb/Fetch/Fetching/Checks.h
@@ -8,7 +8,9 @@
#include <AK/Forward.h>
#include <LibURL/Forward.h>
+#include <LibWeb/Fetch/Infrastructure/RequestOrResponseBlocking.h>
#include <LibWeb/Forward.h>
+#include <LibWebCommon/HTML/EmbedderPolicy.h>
namespace Web::Fetch::Fetching {
@@ -16,4 +18,11 @@ namespace Web::Fetch::Fetching {
[[nodiscard]] bool tao_check(Infrastructure::Request const&, Infrastructure::Response const&);
[[nodiscard]] bool navigation_tao_check(Infrastructure::Response const&, URL::Origin const& destination_origin);
+enum class ForNavigation : u8 {
+ No,
+ Yes,
+};
+
+[[nodiscard]] Infrastructure::RequestOrResponseBlocking cross_origin_resource_policy_check(URL::Origin const&, HTML::EmbedderPolicy const&, Infrastructure::Response const&, ForNavigation = ForNavigation::No);
+
}
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
index 6bb3a1ef5cb..f12eeb7a90f 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
@@ -1525,9 +1525,11 @@ GC::Ref<PendingResponse> http_fetch(JS::Realm& realm, Infrastructure::FetchParam
// NOTE: The cross-origin resource policy check runs for responses coming from the network and responses coming
// from the service worker. This is different from the CORS check, as request’s client and the service
// worker can have different embedder policies.
+ // AD-HOC: request's client can be null, which the check does not allow for. Such a request is checked against a
+ // new embedder policy.
+ auto client_embedder_policy = request->client() ? request->client()->policy_container()->embedder_policy : HTML::EmbedderPolicy {};
if ((request->response_tainting() == Infrastructure::Request::ResponseTainting::Opaque || response->type() == Infrastructure::Response::Type::Opaque)
- && false // FIXME: "and the cross-origin resource policy check with request’s origin, request’s client, request’s destination, and actualResponse returns blocked"
- ) {
+ && cross_origin_resource_policy_check(request->origin().get<URL::Origin>(), client_embedder_policy, *internal_response) == Infrastructure::RequestOrResponseBlocking::Blocked) {
returned_pending_response->resolve(Infrastructure::Response::network_error("Response was blocked by cross-origin resource policy check"_string));
return;
}
diff --git a/Tests/LibWeb/Text/expected/Fetch/cross-origin-resource-policy-credentialless.txt b/Tests/LibWeb/Text/expected/Fetch/cross-origin-resource-policy-credentialless.txt
new file mode 100644
index 00000000000..b5a91171726
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Fetch/cross-origin-resource-policy-credentialless.txt
@@ -0,0 +1,2 @@
+cross-origin-without-corp: loaded
+cross-origin-with-same-origin-corp: blocked
diff --git a/Tests/LibWeb/Text/expected/Fetch/cross-origin-resource-policy-require-corp.txt b/Tests/LibWeb/Text/expected/Fetch/cross-origin-resource-policy-require-corp.txt
new file mode 100644
index 00000000000..993d32b6897
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Fetch/cross-origin-resource-policy-require-corp.txt
@@ -0,0 +1,5 @@
+same-origin-without-corp: loaded
+cross-origin-without-corp: blocked
+cross-origin-with-cross-origin-corp: loaded
+cross-origin-with-same-origin-corp: blocked
+cross-origin-with-invalid-corp: blocked
diff --git a/Tests/LibWeb/Text/input/Fetch/cross-origin-resource-policy-credentialless.html b/Tests/LibWeb/Text/input/Fetch/cross-origin-resource-policy-credentialless.html
new file mode 100644
index 00000000000..0a53f7f5b03
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Fetch/cross-origin-resource-policy-credentialless.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const server = httpTestServer();
+ const load = async (name, { crossOrigin, headers = {}, credentials = "same-origin" }) => {
+ const url = new URL(await server.createEcho("GET", `/${name}`, {
+ status: 200,
+ headers: { "Content-Type": "text/plain", ...headers },
+ body: "",
+ }));
+ if (crossOrigin)
+ url.hostname = uniqueLocalhostHostname(name);
+ try {
+ await fetch(url, { mode: "no-cors", credentials });
+ println(`${name}: loaded`);
+ } catch {
+ println(`${name}: blocked`);
+ }
+ };
+
+ // Credentialless omits credentials from cross-origin no-cors requests, so a response without a
+ // Cross-Origin-Resource-Policy header loads.
+ await load("cross-origin-without-corp", { crossOrigin: true, credentials: "include" });
+ await load("cross-origin-with-same-origin-corp", { crossOrigin: true, credentials: "include", headers: { "Cross-Origin-Resource-Policy": "same-origin" } });
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/Fetch/cross-origin-resource-policy-credentialless.html.headers b/Tests/LibWeb/Text/input/Fetch/cross-origin-resource-policy-credentialless.html.headers
new file mode 100644
index 00000000000..32523a69788
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Fetch/cross-origin-resource-policy-credentialless.html.headers
@@ -0,0 +1 @@
+Cross-Origin-Embedder-Policy: credentialless
diff --git a/Tests/LibWeb/Text/input/Fetch/cross-origin-resource-policy-require-corp.html b/Tests/LibWeb/Text/input/Fetch/cross-origin-resource-policy-require-corp.html
new file mode 100644
index 00000000000..f7b5dbefd69
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Fetch/cross-origin-resource-policy-require-corp.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const server = httpTestServer();
+ const load = async (name, { crossOrigin, headers = {}, credentials = "same-origin" }) => {
+ const url = new URL(await server.createEcho("GET", `/${name}`, {
+ status: 200,
+ headers: { "Content-Type": "text/plain", ...headers },
+ body: "",
+ }));
+ if (crossOrigin)
+ url.hostname = uniqueLocalhostHostname(name);
+ try {
+ await fetch(url, { mode: "no-cors", credentials });
+ println(`${name}: loaded`);
+ } catch {
+ println(`${name}: blocked`);
+ }
+ };
+
+ // Without a Cross-Origin-Resource-Policy header, require-corp treats a no-cors response as "same-origin".
+ await load("same-origin-without-corp", { crossOrigin: false });
+ await load("cross-origin-without-corp", { crossOrigin: true });
+ await load("cross-origin-with-cross-origin-corp", { crossOrigin: true, headers: { "Cross-Origin-Resource-Policy": "cross-origin" } });
+ await load("cross-origin-with-same-origin-corp", { crossOrigin: true, headers: { "Cross-Origin-Resource-Policy": "same-origin" } });
+ await load("cross-origin-with-invalid-corp", { crossOrigin: true, headers: { "Cross-Origin-Resource-Policy": "same-site, cross-origin" } });
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/Fetch/cross-origin-resource-policy-require-corp.html.headers b/Tests/LibWeb/Text/input/Fetch/cross-origin-resource-policy-require-corp.html.headers
new file mode 100644
index 00000000000..6604450991a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Fetch/cross-origin-resource-policy-require-corp.html.headers
@@ -0,0 +1 @@
+Cross-Origin-Embedder-Policy: require-corp