diff --git a/Userland/Libraries/LibWeb/Crypto/Crypto.cpp b/Userland/Libraries/LibWeb/Crypto/Crypto.cpp index 0779adbdce..e5bef6a2f3 100644 --- a/Userland/Libraries/LibWeb/Crypto/Crypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/Crypto.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace Web::Crypto { @@ -42,24 +43,23 @@ JS::NonnullGCPtr Crypto::subtle() const } // https://w3c.github.io/webcrypto/#dfn-Crypto-method-getRandomValues -WebIDL::ExceptionOr Crypto::get_random_values(JS::Value array) const +WebIDL::ExceptionOr> Crypto::get_random_values(JS::Handle array) const { // 1. If array is not an Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array, then throw a TypeMismatchError and terminate the algorithm. - if (!array.is_object() || !(is(array.as_object()) || is(array.as_object()) || is(array.as_object()) || is(array.as_object()) || is(array.as_object()) || is(array.as_object()) || is(array.as_object()) || is(array.as_object()) || is(array.as_object()))) + if (!array->is_typed_array_base()) return WebIDL::TypeMismatchError::create(realm(), "array must be one of Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array"_fly_string); - auto& typed_array = static_cast(array.as_object()); // 2. If the byteLength of array is greater than 65536, throw a QuotaExceededError and terminate the algorithm. - if (typed_array.byte_length() > 65536) + if (array->viewed_array_buffer()->byte_length() > 65536) return WebIDL::QuotaExceededError::create(realm(), "array's byteLength may not be greater than 65536"_fly_string); // IMPLEMENTATION DEFINED: If the viewed array buffer is detached, throw a InvalidStateError and terminate the algorithm. - if (typed_array.viewed_array_buffer()->is_detached()) + if (array->viewed_array_buffer()->is_detached()) return WebIDL::InvalidStateError::create(realm(), "array is detached"_fly_string); // FIXME: Handle SharedArrayBuffers // 3. Overwrite all elements of array with cryptographically strong random values of the appropriate type. - fill_with_random(typed_array.viewed_array_buffer()->buffer()); + fill_with_random(array->viewed_array_buffer()->buffer()); // 4. Return array. return array; diff --git a/Userland/Libraries/LibWeb/Crypto/Crypto.h b/Userland/Libraries/LibWeb/Crypto/Crypto.h index 76b6eaad8a..95e34d95b5 100644 --- a/Userland/Libraries/LibWeb/Crypto/Crypto.h +++ b/Userland/Libraries/LibWeb/Crypto/Crypto.h @@ -23,7 +23,7 @@ public: JS::NonnullGCPtr subtle() const; - WebIDL::ExceptionOr get_random_values(JS::Value array) const; + WebIDL::ExceptionOr> get_random_values(JS::Handle) const; WebIDL::ExceptionOr random_uuid() const; protected: diff --git a/Userland/Libraries/LibWeb/Crypto/Crypto.idl b/Userland/Libraries/LibWeb/Crypto/Crypto.idl index 1924b6fce0..4553a22ddf 100644 --- a/Userland/Libraries/LibWeb/Crypto/Crypto.idl +++ b/Userland/Libraries/LibWeb/Crypto/Crypto.idl @@ -5,8 +5,7 @@ interface Crypto { [SecureContext] readonly attribute SubtleCrypto subtle; - // FIXME: the argument and the return value should be of type ArrayBufferView - any getRandomValues(any array); + ArrayBufferView getRandomValues(ArrayBufferView array); [SecureContext] DOMString randomUUID(); };