Truly independent web browser
LibJS: Throw when toBase64() or toHex() would overflow a string
sideshowbarker986504619b4f
diff --git a/Libraries/LibJS/Rust/src/runtime/uint8_array.rs b/Libraries/LibJS/Rust/src/runtime/uint8_array.rsindex da3be6f5b0f..93e46e27864 100644--- a/Libraries/LibJS/Rust/src/runtime/uint8_array.rs+++ b/Libraries/LibJS/Rust/src/runtime/uint8_array.rs@@ -10,7 +10,9 @@ use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;use crate::layout::object::Object;use crate::layout::value::Value;-use crate::runtime::abstract_operations::get_options_object;+use crate::runtime::abstract_operations::{+ checked_js_string_length_product, checked_js_string_length_sum, get_options_object,+};use crate::runtime::array_buffer::{ArrayBuffer, Order, Shared};use crate::runtime::completion::{Must, Throw, ThrowCompletionOr};use crate::runtime::error::ErrorKind;@@ -449,6 +451,22 @@ impl Uint8ArrayPrototypeHelpers {let length = typed_array_length(&typed_array_record) as usize;let byte_offset = typed_array.byte_offset() as usize;+ // NB: Every 3 bytes become 4 characters. A final 1 or 2 bytes become 4 characters with padding, or 2 or 3 without+ // it. Reject a result too long for a string before copying or encoding any bytes.+ let full_groups_length =+ checked_js_string_length_product(vm, length / 3, 4, ErrorType::StringSizeMustNotOverflow)?;+ let final_group_length = match length % 3 {+ 0 => 0,+ remaining_bytes if omit_padding => remaining_bytes + 1,+ _ => 4,+ };+ checked_js_string_length_sum(+ vm,+ full_groups_length,+ final_group_length,+ ErrorType::StringSizeMustNotOverflow,+ )?;+let to_encode = typed_array.viewed_array_buffer().copy_to_byte_buffer(byte_offset, length);@@ -481,8 +499,11 @@ impl Uint8ArrayPrototypeHelpers {let length = typed_array_length(&typed_array_record) as usize;let byte_offset = typed_array.byte_offset() as usize;+ // NB: Every byte becomes 2 characters. Reject a result too long for a string before copying or encoding any bytes.+ let out_length = checked_js_string_length_product(vm, length, 2, ErrorType::StringSizeMustNotOverflow)?;+// 4. Let out be the empty String.- let mut out = Utf16StringBuilder::with_capacity(length * 2);+ let mut out = Utf16StringBuilder::with_capacity(out_length);let to_encode = typed_array.viewed_array_buffer()diff --git a/Tests/LibJS/Runtime/builtins/TypedArray/Uint8Array.prototype.toBase64.js b/Tests/LibJS/Runtime/builtins/TypedArray/Uint8Array.prototype.toBase64.jsindex bc34647bf7a..e912ee8b069 100644--- a/Tests/LibJS/Runtime/builtins/TypedArray/Uint8Array.prototype.toBase64.js+++ b/Tests/LibJS/Runtime/builtins/TypedArray/Uint8Array.prototype.toBase64.js@@ -53,6 +53,16 @@ describe("errors", () => {"TypedArray contains a property which references a value at an index not contained within its buffer's bounds");});++ test("result too long for a string", () => {+ // 3,221,225,470 bytes encode to 4,294,967,296 characters, which is more than a string can hold.+ let arrayBuffer = new ArrayBuffer(0, { maxByteLength: 3221225470 });+ arrayBuffer.resize(3221225470);++ expect(() => {+ new Uint8Array(arrayBuffer).toBase64();+ }).toThrowWithMessage(RangeError, "string size must not overflow");+ });});describe("correct behavior", () => {diff --git a/Tests/LibJS/Runtime/builtins/TypedArray/Uint8Array.prototype.toHex.js b/Tests/LibJS/Runtime/builtins/TypedArray/Uint8Array.prototype.toHex.jsindex 6cb371182fd..ceeec15348f 100644--- a/Tests/LibJS/Runtime/builtins/TypedArray/Uint8Array.prototype.toHex.js+++ b/Tests/LibJS/Runtime/builtins/TypedArray/Uint8Array.prototype.toHex.js@@ -37,6 +37,16 @@ describe("errors", () => {"TypedArray contains a property which references a value at an index not contained within its buffer's bounds");});++ test("result too long for a string", () => {+ // 2,147,483,648 bytes encode to 4,294,967,296 characters, which is more than a string can hold.+ let arrayBuffer = new ArrayBuffer(0, { maxByteLength: 2147483648 });+ arrayBuffer.resize(2147483648);++ expect(() => {+ new Uint8Array(arrayBuffer).toHex();+ }).toThrowWithMessage(RangeError, "string size must not overflow");+ });});describe("correct behavior", () => {