Skip to content

Truly independent web browser

LibWeb: Implement CredentialsContainer in Rust

Shannon Booth9fa20ac69578
diff --git a/Libraries/LibWeb/Bindings/Rust/src/credential_management/credential.rs b/Libraries/LibWeb/Bindings/Rust/src/credential_management/credential.rs
new file mode 100644
index 00000000000..d9be1331890
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/credential_management/credential.rs
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2025, Altomani Gianluca <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! Credential, https://w3c.github.io/webappsec-credential-management/#credential
+
+use crate::bindings::conversions::UsvString;
+
+/// https://w3c.github.io/webappsec-credential-management/#dictdef-credentialdata
+#[expect(dead_code, reason = "create() does not read the options yet")]
+pub(crate) struct CredentialData {
+ pub(crate) id: UsvString,
+}
+
+/// https://w3c.github.io/webappsec-credential-management/#enumdef-credentialmediationrequirement
+#[derive(Clone, Copy)]
+pub(crate) enum CredentialMediationRequirement {
+ Silent,
+ Optional,
+ Conditional,
+ Required,
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/credential_management/credentials_container.rs b/Libraries/LibWeb/Bindings/Rust/src/credential_management/credentials_container.rs
new file mode 100644
index 00000000000..cc2713e4154
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/credential_management/credentials_container.rs
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2025, Altomani Gianluca <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! CredentialsContainer, https://w3c.github.io/webappsec-credential-management/#credentialscontainer
+
+use core::ffi::c_void;
+
+use libjs_rust::api::{ErrorKind, ErrorType, Trace, Vm};
+
+use super::credential::CredentialMediationRequirement;
+use super::federated_credential::{FederatedCredentialInit, FederatedCredentialRequestOptions};
+use super::password_credential::PasswordCredentialInit;
+use crate::bindings::conversions::CppInterface;
+use crate::bindings::interface::{Wrappable, allocate, cell_into_abi};
+use crate::bindings::promise::{WebIdlPromise, reject_promise_with_exception};
+use crate::bindings::{CallContext, Exception, WebIdlResult};
+
+/// https://w3c.github.io/webappsec-credential-management/#dictdef-credentialrequestoptions
+#[expect(dead_code, reason = "get() does not read the options yet")]
+pub(crate) struct CredentialRequestOptions {
+ pub(crate) federated: Option<FederatedCredentialRequestOptions>,
+ pub(crate) mediation: CredentialMediationRequirement,
+ pub(crate) password: bool,
+ pub(crate) signal: Option<CppInterface>,
+}
+
+/// https://w3c.github.io/webappsec-credential-management/#dictdef-credentialcreationoptions
+#[expect(dead_code, reason = "create() does not read the options yet")]
+pub(crate) struct CredentialCreationOptions {
+ pub(crate) federated: Option<FederatedCredentialInit>,
+ pub(crate) mediation: CredentialMediationRequirement,
+ pub(crate) password: Option<PasswordCredentialInit>,
+ pub(crate) signal: Option<CppInterface>,
+}
+
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct CredentialsContainer;
+
+impl Wrappable for CredentialsContainer {}
+
+fn reject_not_implemented_promise(vm: &Vm, promise: WebIdlPromise, operation: &str) {
+ let throw = vm
+ .throw_completion::<()>(ErrorKind::InternalError, ErrorType::NotImplemented, &[&operation])
+ .expect_err("throwing a completion throws");
+ reject_promise_with_exception(vm, promise, Exception::from(throw));
+}
+
+impl CredentialsContainer {
+ // https://w3c.github.io/webappsec-credential-management/#dom-credentialscontainer-get
+ pub(crate) fn get(
+ &self,
+ cx: &mut CallContext,
+ promise: WebIdlPromise,
+ _: CredentialRequestOptions,
+ ) -> WebIdlResult<()> {
+ eprintln!("FIXME: Unimplemented CredentialsContainer::get()");
+ reject_not_implemented_promise(cx.vm(), promise, "get");
+ Ok(())
+ }
+
+ // https://w3c.github.io/webappsec-credential-management/#dom-credentialscontainer-store
+ pub(crate) fn store(&self, cx: &mut CallContext, promise: WebIdlPromise, _: CppInterface) -> WebIdlResult<()> {
+ eprintln!("FIXME: Unimplemented CredentialsContainer::store()");
+ reject_not_implemented_promise(cx.vm(), promise, "store");
+ Ok(())
+ }
+
+ // https://w3c.github.io/webappsec-credential-management/#dom-credentialscontainer-create
+ pub(crate) fn create(
+ &self,
+ cx: &mut CallContext,
+ promise: WebIdlPromise,
+ _: CredentialCreationOptions,
+ ) -> WebIdlResult<()> {
+ eprintln!("FIXME: Unimplemented CredentialsContainer::create()");
+ reject_not_implemented_promise(cx.vm(), promise, "create");
+ Ok(())
+ }
+
+ // https://w3c.github.io/webappsec-credential-management/#dom-credentialscontainer-preventsilentaccess
+ pub(crate) fn prevent_silent_access(&self, cx: &mut CallContext, promise: WebIdlPromise) -> WebIdlResult<()> {
+ eprintln!("FIXME: Unimplemented CredentialsContainer::prevent_silent_access()");
+ reject_not_implemented_promise(cx.vm(), promise, "prevent silent access");
+ Ok(())
+ }
+}
+
+/// # Safety
+///
+/// The VM must be live.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_rust_credentials_container_create(vm: *const c_void) -> *mut c_void {
+ // SAFETY: The caller passes a live VM.
+ let vm = unsafe { &*vm.cast::<Vm>() };
+ cell_into_abi(allocate(vm, |_| CredentialsContainer))
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/credential_management/federated_credential.rs b/Libraries/LibWeb/Bindings/Rust/src/credential_management/federated_credential.rs
new file mode 100644
index 00000000000..84e9563d5c9
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/credential_management/federated_credential.rs
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2025, Altomani Gianluca <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! FederatedCredential, https://w3c.github.io/webappsec-credential-management/#federatedcredential
+
+use ak::Utf16String;
+
+use super::credential::CredentialData;
+use crate::bindings::conversions::UsvString;
+
+/// https://w3c.github.io/webappsec-credential-management/#dictdef-federatedcredentialrequestoptions
+#[expect(dead_code, reason = "get() does not read the options yet")]
+pub(crate) struct FederatedCredentialRequestOptions {
+ pub(crate) protocols: Option<Vec<Utf16String>>,
+ pub(crate) providers: Option<Vec<UsvString>>,
+}
+
+/// https://w3c.github.io/webappsec-credential-management/#dictdef-federatedcredentialinit
+#[expect(dead_code, reason = "create() does not read the options yet")]
+pub(crate) struct FederatedCredentialInit {
+ pub(crate) parent: CredentialData,
+ pub(crate) icon_url: Option<UsvString>,
+ pub(crate) name: Option<UsvString>,
+ pub(crate) protocol: Option<Utf16String>,
+ pub(crate) provider: UsvString,
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/credential_management/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/credential_management/mod.rs
new file mode 100644
index 00000000000..63864b143f2
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/credential_management/mod.rs
@@ -0,0 +1,12 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! Credential Management, https://w3c.github.io/webappsec-credential-management/
+
+pub(crate) mod credential;
+pub(crate) mod credentials_container;
+pub(crate) mod federated_credential;
+pub(crate) mod password_credential;
diff --git a/Libraries/LibWeb/Bindings/Rust/src/credential_management/password_credential.rs b/Libraries/LibWeb/Bindings/Rust/src/credential_management/password_credential.rs
new file mode 100644
index 00000000000..de0d2d50b94
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/credential_management/password_credential.rs
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2025, Altomani Gianluca <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! PasswordCredential, https://w3c.github.io/webappsec-credential-management/#passwordcredential
+
+use super::credential::CredentialData;
+use crate::bindings::conversions::{CppInterface, UsvString};
+
+/// https://w3c.github.io/webappsec-credential-management/#dictdef-passwordcredentialdata
+#[expect(dead_code, reason = "create() does not read the options yet")]
+pub(crate) struct PasswordCredentialData {
+ pub(crate) parent: CredentialData,
+ pub(crate) icon_url: Option<UsvString>,
+ pub(crate) name: Option<UsvString>,
+ pub(crate) password: UsvString,
+}
+
+/// https://w3c.github.io/webappsec-credential-management/#typedefdef-passwordcredentialinit
+#[expect(dead_code, reason = "create() does not read the options yet")]
+pub(crate) enum PasswordCredentialInit {
+ PasswordCredentialData(PasswordCredentialData),
+ HTMLFormElement(CppInterface),
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/lib.rs b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
index a9f80b528ef..9806b94b697 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/lib.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
@@ -66,6 +66,16 @@ pub(crate) mod clipboard;
)]
pub(crate) mod compression;
#[cfg(not(test))]
+#[cfg_attr(
+ not(web_idl_bindings),
+ allow(dead_code, reason = "only the generated bindings use the interfaces")
+)]
+#[allow(
+ clippy::unnecessary_wraps,
+ reason = "the bindings call every attribute and operation as one that can throw"
+)]
+pub(crate) mod credential_management;
+#[cfg(not(test))]
pub(crate) mod crypto;
#[cfg(not(test))]
#[cfg_attr(
diff --git a/Libraries/LibWeb/CredentialManagement/CredentialsContainer.cpp b/Libraries/LibWeb/CredentialManagement/CredentialsContainer.cpp
index 8487abcf396..2c253dc75c3 100644
--- a/Libraries/LibWeb/CredentialManagement/CredentialsContainer.cpp
+++ b/Libraries/LibWeb/CredentialManagement/CredentialsContainer.cpp
@@ -4,60 +4,19 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <LibGC/Heap.h>
-#include <LibJS/Runtime/Error.h>
+#include <LibJS/HostClassBuilder.h>
+#include <LibJS/Runtime/VM.h>
#include <LibWeb/CredentialManagement/CredentialsContainer.h>
-#include <LibWeb/WebIDL/Promise.h>
namespace Web::CredentialManagement {
-GC_DEFINE_ALLOCATOR(CredentialsContainer);
-
-GC::Ref<CredentialsContainer> CredentialsContainer::create()
-{
- return GC::Heap::the().allocate<CredentialsContainer>();
-}
-
-CredentialsContainer::~CredentialsContainer() { }
-
-static void reject_not_implemented_promise(GC::Ref<WebIDL::Promise> promise, StringView operation)
-{
- auto& realm = WebIDL::promise_realm(promise);
- auto& vm = realm.vm();
- auto exception = vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, operation);
- WebIDL::reject_promise_with_exception(promise, exception);
-}
-
-// https://www.w3.org/TR/credential-management-1/#dom-credentialscontainer-get
-void CredentialsContainer::get(CredentialRequestOptions const&, GC::Ref<WebIDL::Promise> promise)
-{
- dbgln("FIXME: Unimplemented CredentialsContainer::get()");
- reject_not_implemented_promise(promise, "get"sv);
+extern "C" {
+CredentialsContainer* web_rust_credentials_container_create(JSVM*);
}
-// https://www.w3.org/TR/credential-management-1/#dom-credentialscontainer-store
-void CredentialsContainer::store(Credential const&, GC::Ref<WebIDL::Promise> promise)
-{
- dbgln("FIXME: Unimplemented CredentialsContainer::store()");
- reject_not_implemented_promise(promise, "store"sv);
-}
-
-// https://www.w3.org/TR/credential-management-1/#dom-credentialscontainer-create
-void CredentialsContainer::create(CredentialCreationOptions const&, GC::Ref<WebIDL::Promise> promise)
-{
- dbgln("FIXME: Unimplemented CredentialsContainer::create()");
- reject_not_implemented_promise(promise, "create"sv);
-}
-
-// https://www.w3.org/TR/credential-management-1/#dom-credentialscontainer-preventsilentaccess
-void CredentialsContainer::prevent_silent_access(GC::Ref<WebIDL::Promise> promise)
-{
- dbgln("FIXME: Unimplemented CredentialsContainer::prevent_silent_access()");
- reject_not_implemented_promise(promise, "prevent silent access"sv);
-}
-
-CredentialsContainer::CredentialsContainer()
+GC::Ref<CredentialsContainer> CredentialsContainer::create()
{
+ return *web_rust_credentials_container_create(JS::HostABI::vm_to_abi(JS::VM::the()));
}
}
diff --git a/Libraries/LibWeb/CredentialManagement/CredentialsContainer.h b/Libraries/LibWeb/CredentialManagement/CredentialsContainer.h
index defaf0d6589..e9b8084d9e7 100644
--- a/Libraries/LibWeb/CredentialManagement/CredentialsContainer.h
+++ b/Libraries/LibWeb/CredentialManagement/CredentialsContainer.h
@@ -7,31 +7,15 @@
#pragma once
#include <LibWeb/Bindings/CredentialsContainer.h>
-#include <LibWeb/Bindings/Wrappable.h>
-#include <LibWeb/CredentialManagement/Credential.h>
-#include <LibWeb/WebIDL/Promise.h>
+#include <LibWeb/Bindings/RustInterface.h>
namespace Web::CredentialManagement {
-using CredentialRequestOptions = Bindings::CredentialRequestOptions;
-using CredentialCreationOptions = Bindings::CredentialCreationOptions;
-
-class CredentialsContainer final : public Bindings::GCAllocatedWrappable {
- WEB_WRAPPABLE(CredentialsContainer, Bindings::GCAllocatedWrappable);
- GC_DECLARE_ALLOCATOR(CredentialsContainer);
+class CredentialsContainer : public Bindings::RustInterfaceCell {
+ WEB_RUST_INTERFACE(CredentialsContainer, credentials_container)
public:
[[nodiscard]] static GC::Ref<CredentialsContainer> create();
-
- virtual ~CredentialsContainer() override;
-
- void get(CredentialRequestOptions const&, GC::Ref<WebIDL::Promise>);
- void store(Credential const& credential, GC::Ref<WebIDL::Promise>);
- void create(CredentialCreationOptions const&, GC::Ref<WebIDL::Promise>);
- void prevent_silent_access(GC::Ref<WebIDL::Promise>);
-
-private:
- CredentialsContainer();
};
}
diff --git a/Libraries/LibWeb/CredentialManagement/CredentialsContainer.idl b/Libraries/LibWeb/CredentialManagement/CredentialsContainer.idl
index 48df8d0dfe5..1d4bd344316 100644
--- a/Libraries/LibWeb/CredentialManagement/CredentialsContainer.idl
+++ b/Libraries/LibWeb/CredentialManagement/CredentialsContainer.idl
@@ -1,4 +1,4 @@
-[Exposed=Window, SecureContext]
+[Exposed=Window, SecureContext, ImplementedIn=Rust]
interface CredentialsContainer {
[CreatesPromise] Promise<Credential?> get(optional CredentialRequestOptions options = {});
[CreatesPromise] Promise<undefined> store(Credential credential);
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 152c4185a44..f5eb2402230 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -1336,6 +1336,7 @@ Text/input/SiteIsolation/iframe/mixed-content-of-an-isolated-iframes-navigation-
; Upstream serves it from a page that is not potentially trustworthy, where its data: frame's blob: worker is not mixed content.
Text/input/wpt-import/webmessaging/broadcastchannel/opaque-origin.html
Text/input/wpt-import/speech-api/SpeechRecognition-non-secure-context.http.html
+Text/input/wpt-import/credential-management/require_securecontext.html
[Skipped]
; Need position-visibility: no-overflow to hide a target that overflows after scrolling
diff --git a/Tests/LibWeb/Text/expected/wpt-import/credential-management/require_securecontext.txt b/Tests/LibWeb/Text/expected/wpt-import/credential-management/require_securecontext.txt
new file mode 100644
index 00000000000..22b2e68ca1e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/credential-management/require_securecontext.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Credential Management must not be accessible in insecure contexts
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/credential-management/require_securecontext.html b/Tests/LibWeb/Text/input/wpt-import/credential-management/require_securecontext.html
new file mode 100644
index 00000000000..8103ba78c56
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/credential-management/require_securecontext.html
@@ -0,0 +1,13 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>Test that Credential Management requires secure contexts</title>
+<link rel="help" href="https://w3c.github.io/webappsec-credential-management/#idl-index">
+<script src=../resources/testharness.js></script>
+<script src=../resources/testharnessreport.js></script>
+<script>
+"use strict";
+ test(() => {
+ assert_false(isSecureContext);
+ assert_false('credentials' in navigator);
+ }, "Credential Management must not be accessible in insecure contexts");
+</script>