Lightpanda: the headless browser designed for AI and automation
protect crypto and websocket from getting an iteratable
Karl Seguin5f70b40f6d15
diff --git a/src/browser/js/Caller.zig b/src/browser/js/Caller.zigindex 4e0527996..c19d21053 100644--- a/src/browser/js/Caller.zig+++ b/src/browser/js/Caller.zig@@ -1117,9 +1117,10 @@ fn getArgs(comptime F: type, comptime offset: usize, local: *const Local, info:// type instantiation of jsValueToZig may not include such errors// in its inferred error set.@field(args, tupleFieldName(field_index)) = local.jsValueToZig(param_type.?, js_val) catch |err| {- if (err == error.JsException) {+ if (err == error.JsException or err == error.ExecutionTerminated) {// an exception thrown by user code (e.g. a toString- // getter) is pending; propagate it untouched+ // getter), or the termination, is pending; propagate it+ // untouchedreturn err;}const DOMException = @import("../webapi/DOMException.zig");diff --git a/src/browser/js/Local.zig b/src/browser/js/Local.zigindex 480b4a020..ec2efc07a 100644--- a/src/browser/js/Local.zig+++ b/src/browser/js/Local.zig@@ -741,11 +741,12 @@ fn iterableToSlice(self: *const Local, comptime T: type, js_val: js.Value) ![]T// Value.iterator rethrows the page's exception (TryCatchRethrow): it's// pending, so report it as one; a malformed iterator is InvalidArgument.+// Anything else (ExecutionTerminated, OutOfMemory) passes through.fn iterationError(err: anyerror) anyerror {return switch (err) {- error.TryCatchRethrow, error.JsException => error.JsException,- error.OutOfMemory => err,- else => error.InvalidArgument,+ error.TryCatchRethrow => error.JsException,+ error.TypeError => error.InvalidArgument,+ else => err,};}diff --git a/src/browser/tests/crypto.html b/src/browser/tests/crypto.htmlindex 93abac9a4..d9e396613 100644--- a/src/browser/tests/crypto.html+++ b/src/browser/tests/crypto.html@@ -49,6 +49,13 @@testing.expectEqual(ti64a, crypto.getRandomValues(ti64a))isRandom(ti64a)}++ {+ // Only an ArrayBufferView, not an Array or another iterable+ testing.expectError('TypeError', () => crypto.getRandomValues([1, 2]));+ testing.expectError('TypeError', () => crypto.getRandomValues(new Set([1, 2])));+ testing.expectError('TypeError', () => crypto.getRandomValues((function* () { yield 1; })()));+ }</script><script id="randomUUID">diff --git a/src/browser/tests/net/websocket.html b/src/browser/tests/net/websocket.htmlindex b97449328..e5bfb64d7 100644--- a/src/browser/tests/net/websocket.html+++ b/src/browser/tests/net/websocket.html@@ -845,3 +845,33 @@});}</script>++<script id=send_non_buffer_object type=module>+{+ const state = await testing.async();+ let received = [];++ let ws = new WebSocket('ws://127.0.0.1:9584/');++ ws.addEventListener('open', () => {+ // Anything that isn't a buffer is sent as its string, iterable or not+ ws.send(new Set([1]));+ ws.send([1, 2]);+ });++ ws.addEventListener('message', (e) => {+ received.push(e.data);+ if (received.length === 2) {+ ws.close();+ }+ });++ ws.addEventListener('close', () => {+ state.resolve();+ });++ await state.done(() => {+ testing.expectEqual(['echo-[object Set]', 'echo-1,2'], received);+ });+}+</script>diff --git a/src/browser/webapi/Crypto.zig b/src/browser/webapi/Crypto.zigindex 96aad8e9b..9eebd9613 100644--- a/src/browser/webapi/Crypto.zig+++ b/src/browser/webapi/Crypto.zig@@ -31,13 +31,16 @@ pub const init: Crypto = .{};// TypedArrayfn getRandomValues(_: *const Crypto, js_obj: js.Object, exec: *const js.Execution) !js.Object {const value = js_obj.toValue();- if (value.isFloat16Array() or value.isFloat32Array() or value.isFloat64Array() or (value.isArrayBufferView() and !value.isTypedArray())) {+ if (value.isArrayBufferView() == false) {+ // AllowSharedBufferSource would also take an ArrayBuffer+ return error.InvalidArgument;+ }+ if (value.isFloat16Array() or value.isFloat32Array() or value.isFloat64Array() or value.isTypedArray() == false) {// only integer TypedArrays are supportedreturn error.TypeMismatch;}- var into = try js_obj.toZig(RandomValues);- const buf = into.asBuffer();+ const buf = (try js_obj.toZig(js.AllowSharedBufferSource)).bytes;if (buf.len > 65_536) {return QuotaExceededError.throw(js_obj.local, exec);}@@ -55,30 +58,6 @@ fn getSubtle(self: *Crypto) *SubtleCrypto {return &self._subtle;}-const RandomValues = union(enum) {- int8: []i8,- uint8: []u8,- int16: []i16,- uint16: []u16,- int32: []i32,- uint32: []u32,- int64: []i64,- uint64: []u64,-- fn asBuffer(self: RandomValues) []u8 {- return switch (self) {- .int8 => |b| (@as([]u8, @ptrCast(b)))[0..b.len],- .uint8 => |b| (@as([]u8, @ptrCast(b)))[0..b.len],- .int16 => |b| (@as([]u8, @ptrCast(b)))[0 .. b.len * 2],- .uint16 => |b| (@as([]u8, @ptrCast(b)))[0 .. b.len * 2],- .int32 => |b| (@as([]u8, @ptrCast(b)))[0 .. b.len * 4],- .uint32 => |b| (@as([]u8, @ptrCast(b)))[0 .. b.len * 4],- .int64 => |b| (@as([]u8, @ptrCast(b)))[0 .. b.len * 8],- .uint64 => |b| (@as([]u8, @ptrCast(b)))[0 .. b.len * 8],- };- }-};-pub const JsApi = struct {pub const bridge = js.Bridge(Crypto);diff --git a/src/browser/webapi/net/WebSocket.zig b/src/browser/webapi/net/WebSocket.zigindex b2a02a656..9c7754bd7 100644--- a/src/browser/webapi/net/WebSocket.zig+++ b/src/browser/webapi/net/WebSocket.zig@@ -539,31 +539,6 @@ const SendData = union(enum) {js_val: js.Value,};-/// Union for extracting bytes from ArrayBuffer/TypedArray-const BinaryData = union(enum) {- int8: []i8,- uint8: []u8,- int16: []i16,- uint16: []u16,- int32: []i32,- uint32: []u32,- int64: []i64,- uint64: []u64,- float16: []f16,- float32: []f32,- float64: []f64,-- fn asBuffer(self: BinaryData) []u8 {- return switch (self) {- .int8 => |b| @as([*]u8, @ptrCast(b.ptr))[0..b.len],- .uint8 => |b| b,- inline .int16, .uint16, .float16 => |b| @as([*]u8, @ptrCast(b.ptr))[0 .. b.len * 2],- inline .int32, .uint32, .float32 => |b| @as([*]u8, @ptrCast(b.ptr))[0 .. b.len * 4],- inline .int64, .uint64, .float64 => |b| @as([*]u8, @ptrCast(b.ptr))[0 .. b.len * 8],- };- }-};-pub fn send(self: *WebSocket, data: SendData) !void {if (self._ready_state != .open) {return error.InvalidStateError;@@ -579,7 +554,9 @@ pub fn send(self: *WebSocket, data: SendData) !void {} });},.js_val => |js_val| {- if (js_val.isString()) |str| {+ if (js_val.isArrayBuffer() == false and js_val.isArrayBufferView() == false) {+ // Anything that isn't a buffer is sent as its string, as in Chrome+ const str = try js_val.toString();const arena = try self._exec.getArena(str.len(), "WebSocket.message");errdefer arena.release();try self.queueMessage(.{ .text = .{@@ -587,8 +564,7 @@ pub fn send(self: *WebSocket, data: SendData) !void {.data = try str.toSliceWithAlloc(arena.allocator()),} });} else {- const binary = try js_val.toZig(BinaryData);- const buffer = binary.asBuffer();+ const buffer = (try js_val.toZig(js.BufferSource)).bytes;const arena = try self._exec.getArena(buffer.len, "WebSocket.message");errdefer arena.release();