diff options
author | Leo K <crowlkats@toaxl.com> | 2021-06-05 19:30:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-05 19:30:20 +0200 |
commit | 083f5c345445af4e434281c37817e12249d7553d (patch) | |
tree | 2db46d622283ab37182ca988ecb0712dc11af6e8 /extensions/crypto | |
parent | 706b75d7421a33a54f51ceafc0747be4a646c707 (diff) |
refactor(crypto): validate max random bytes in Rust (#10857)
Diffstat (limited to 'extensions/crypto')
-rw-r--r-- | extensions/crypto/01_crypto.js | 8 | ||||
-rw-r--r-- | extensions/crypto/Cargo.toml | 1 | ||||
-rw-r--r-- | extensions/crypto/lib.rs | 17 |
3 files changed, 13 insertions, 13 deletions
diff --git a/extensions/crypto/01_crypto.js b/extensions/crypto/01_crypto.js index 627035c28..f18b6d5aa 100644 --- a/extensions/crypto/01_crypto.js +++ b/extensions/crypto/01_crypto.js @@ -26,18 +26,12 @@ "TypeMismatchError", ); } - if (arrayBufferView.byteLength > 65536) { - throw new DOMException( - `The ArrayBufferView's byte length (${arrayBufferView.byteLength}) exceeds the number of bytes of entropy available via this API (65536)`, - "QuotaExceededError", - ); - } const ui8 = new Uint8Array( arrayBufferView.buffer, arrayBufferView.byteOffset, arrayBufferView.byteLength, ); - core.opSync("op_crypto_get_random_values", null, ui8); + core.opSync("op_crypto_get_random_values", ui8); return arrayBufferView; } diff --git a/extensions/crypto/Cargo.toml b/extensions/crypto/Cargo.toml index 8b919b611..b263b6014 100644 --- a/extensions/crypto/Cargo.toml +++ b/extensions/crypto/Cargo.toml @@ -15,5 +15,6 @@ path = "lib.rs" [dependencies] deno_core = { version = "0.88.1", path = "../../core" } +deno_web = { version = "0.38.1", path = "../web" } rand = "0.8.3" uuid = { version = "0.8.2", features = ["v4"] } diff --git a/extensions/crypto/lib.rs b/extensions/crypto/lib.rs index bf4174f75..0ec7f4717 100644 --- a/extensions/crypto/lib.rs +++ b/extensions/crypto/lib.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::include_js_files; use deno_core::op_sync; @@ -39,10 +38,16 @@ pub fn init(maybe_seed: Option<u64>) -> Extension { pub fn op_crypto_get_random_values( state: &mut OpState, - _args: (), - zero_copy: Option<ZeroCopyBuf>, + mut zero_copy: ZeroCopyBuf, + _: (), ) -> Result<(), AnyError> { - let mut zero_copy = zero_copy.ok_or_else(null_opbuf)?; + if zero_copy.len() > 65536 { + return Err( + deno_web::DomExceptionQuotaExceededError::new(&format!("The ArrayBufferView's byte length ({}) exceeds the number of bytes of entropy available via this API (65536)", zero_copy.len())) + .into(), + ); + } + let maybe_seeded_rng = state.try_borrow_mut::<StdRng>(); if let Some(seeded_rng) = maybe_seeded_rng { seeded_rng.fill(&mut *zero_copy); @@ -56,8 +61,8 @@ pub fn op_crypto_get_random_values( pub fn op_crypto_random_uuid( state: &mut OpState, - _args: (), - _zero_copy: (), + _: (), + _: (), ) -> Result<String, AnyError> { let maybe_seeded_rng = state.try_borrow_mut::<StdRng>(); let uuid = if let Some(seeded_rng) = maybe_seeded_rng { |