mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:27:35 +00:00
LibWeb: Use ArrayBufferView for Crypto::getRandomValues
Co-Authored-By: Matthew Olsson <mattco@serenityos.org>
This commit is contained in:
parent
04c094343f
commit
eab20129b9
3 changed files with 8 additions and 9 deletions
|
@ -12,6 +12,7 @@
|
||||||
#include <LibWeb/Bindings/Intrinsics.h>
|
#include <LibWeb/Bindings/Intrinsics.h>
|
||||||
#include <LibWeb/Crypto/Crypto.h>
|
#include <LibWeb/Crypto/Crypto.h>
|
||||||
#include <LibWeb/Crypto/SubtleCrypto.h>
|
#include <LibWeb/Crypto/SubtleCrypto.h>
|
||||||
|
#include <LibWeb/WebIDL/Buffers.h>
|
||||||
|
|
||||||
namespace Web::Crypto {
|
namespace Web::Crypto {
|
||||||
|
|
||||||
|
@ -42,24 +43,23 @@ JS::NonnullGCPtr<SubtleCrypto> Crypto::subtle() const
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://w3c.github.io/webcrypto/#dfn-Crypto-method-getRandomValues
|
// https://w3c.github.io/webcrypto/#dfn-Crypto-method-getRandomValues
|
||||||
WebIDL::ExceptionOr<JS::Value> Crypto::get_random_values(JS::Value array) const
|
WebIDL::ExceptionOr<JS::Handle<WebIDL::ArrayBufferView>> Crypto::get_random_values(JS::Handle<WebIDL::ArrayBufferView> 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.
|
// 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<JS::Int8Array>(array.as_object()) || is<JS::Uint8Array>(array.as_object()) || is<JS::Uint8ClampedArray>(array.as_object()) || is<JS::Int16Array>(array.as_object()) || is<JS::Uint16Array>(array.as_object()) || is<JS::Int32Array>(array.as_object()) || is<JS::Uint32Array>(array.as_object()) || is<JS::BigInt64Array>(array.as_object()) || is<JS::BigUint64Array>(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);
|
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<JS::TypedArrayBase&>(array.as_object());
|
|
||||||
|
|
||||||
// 2. If the byteLength of array is greater than 65536, throw a QuotaExceededError and terminate the algorithm.
|
// 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);
|
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.
|
// 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);
|
return WebIDL::InvalidStateError::create(realm(), "array is detached"_fly_string);
|
||||||
// FIXME: Handle SharedArrayBuffers
|
// FIXME: Handle SharedArrayBuffers
|
||||||
|
|
||||||
// 3. Overwrite all elements of array with cryptographically strong random values of the appropriate type.
|
// 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.
|
// 4. Return array.
|
||||||
return array;
|
return array;
|
||||||
|
|
|
@ -23,7 +23,7 @@ public:
|
||||||
|
|
||||||
JS::NonnullGCPtr<SubtleCrypto> subtle() const;
|
JS::NonnullGCPtr<SubtleCrypto> subtle() const;
|
||||||
|
|
||||||
WebIDL::ExceptionOr<JS::Value> get_random_values(JS::Value array) const;
|
WebIDL::ExceptionOr<JS::Handle<WebIDL::ArrayBufferView>> get_random_values(JS::Handle<WebIDL::ArrayBufferView>) const;
|
||||||
WebIDL::ExceptionOr<String> random_uuid() const;
|
WebIDL::ExceptionOr<String> random_uuid() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -5,8 +5,7 @@
|
||||||
interface Crypto {
|
interface Crypto {
|
||||||
[SecureContext] readonly attribute SubtleCrypto subtle;
|
[SecureContext] readonly attribute SubtleCrypto subtle;
|
||||||
|
|
||||||
// FIXME: the argument and the return value should be of type ArrayBufferView
|
ArrayBufferView getRandomValues(ArrayBufferView array);
|
||||||
any getRandomValues(any array);
|
|
||||||
|
|
||||||
[SecureContext] DOMString randomUUID();
|
[SecureContext] DOMString randomUUID();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue