diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp
index 551c83b4f4..ff5ee24a8a 100644
--- a/Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp
+++ b/Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp
@@ -13,7 +13,9 @@
#include
#include
#include
+#include
#include
+#include
namespace Web::HTML {
@@ -391,4 +393,34 @@ JS::Object& relevant_global_object(JS::Object const& object)
return relevant_realm(object).global_object();
}
+// https://html.spec.whatwg.org/multipage/webappapis.html#secure-context
+bool is_secure_context(Environment const& environment)
+{
+ // 1. If environment is an environment settings object, then:
+ if (is(environment)) {
+ // 1. Let global be environment's global object.
+ // FIXME: Add a const global_object() getter to ESO
+ auto& global = static_cast(const_cast(environment)).global_object();
+
+ // 2. If global is a WorkerGlobalScope, then:
+ if (is(global)) {
+ // FIXME: 1. If global's owner set[0]'s relevant settings object is a secure context, then return true.
+ // NOTE: We only need to check the 0th item since they will necessarily all be consistent.
+
+ // 2. Return false.
+ return false;
+ }
+
+ // FIXME: 3. If global is a WorkletGlobalScope, then return true.
+ // NOTE: Worklets can only be created in secure contexts.
+ }
+
+ // 2. If the result of Is url potentially trustworthy? given environment's top-level creation URL is "Potentially Trustworthy", then return true.
+ if (SecureContexts::is_url_potentially_trustworthy(environment.top_level_creation_url) == SecureContexts::Trustworthiness::PotentiallyTrustworthy)
+ return true;
+
+ // 3. Return false.
+ return false;
+}
+
}
diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/Environments.h b/Userland/Libraries/LibWeb/HTML/Scripting/Environments.h
index 1915c24022..c2690d91b5 100644
--- a/Userland/Libraries/LibWeb/HTML/Scripting/Environments.h
+++ b/Userland/Libraries/LibWeb/HTML/Scripting/Environments.h
@@ -143,5 +143,6 @@ JS::Realm& relevant_realm(JS::Object const&);
EnvironmentSettingsObject& relevant_settings_object(JS::Object const&);
EnvironmentSettingsObject& relevant_settings_object(DOM::Node const&);
JS::Object& relevant_global_object(JS::Object const&);
+[[nodiscard]] bool is_secure_context(Environment const&);
}