Skip to content

Commit 21d11bd

Browse files
committed
wasm: reject non-Uint8Array streaming chunks
Restrict WebAssembly streaming response chunks to Uint8Array, as required by Fetch body consumption. Signed-off-by: Archkon <180910180+Archkon@users.noreply.github.com>
1 parent 594d0c5 commit 21d11bd

1 file changed

Lines changed: 8 additions & 20 deletions

File tree

src/node_wasm_web_api.cc

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
namespace node {
88
namespace wasm_web_api {
99

10-
using v8::ArrayBuffer;
11-
using v8::ArrayBufferView;
1210
using v8::Context;
1311
using v8::Function;
1412
using v8::FunctionCallbackInfo;
@@ -17,6 +15,7 @@ using v8::Isolate;
1715
using v8::Local;
1816
using v8::MaybeLocal;
1917
using v8::Object;
18+
using v8::Uint8Array;
2019
using v8::Value;
2120
using v8::WasmStreaming;
2221

@@ -98,28 +97,17 @@ void WasmStreamingObject::Push(const FunctionCallbackInfo<Value>& args) {
9897
CHECK_EQ(args.Length(), 1);
9998
Local<Value> chunk = args[0];
10099

101-
// The start of the memory section backing the ArrayBuffer(View), the offset
102-
// of the ArrayBuffer(View) within the memory section, and its size in bytes.
103-
const void* bytes;
104-
size_t offset;
105-
size_t size;
106-
107-
if (chunk->IsArrayBufferView()) [[likely]] {
108-
Local<ArrayBufferView> view = chunk.As<ArrayBufferView>();
109-
bytes = view->Buffer()->Data();
110-
offset = view->ByteOffset();
111-
size = view->ByteLength();
112-
} else if (chunk->IsArrayBuffer()) [[likely]] {
113-
Local<ArrayBuffer> buffer = chunk.As<ArrayBuffer>();
114-
bytes = buffer->Data();
115-
offset = 0;
116-
size = buffer->ByteLength();
117-
} else {
100+
if (!chunk->IsUint8Array()) [[unlikely]] {
118101
return node::THROW_ERR_INVALID_ARG_TYPE(
119102
Environment::GetCurrent(args),
120-
"chunk must be an ArrayBufferView or an ArrayBuffer");
103+
"chunk must be a Uint8Array");
121104
}
122105

106+
Local<Uint8Array> view = chunk.As<Uint8Array>();
107+
const void* bytes = view->Buffer()->Data();
108+
size_t offset = view->ByteOffset();
109+
size_t size = view->ByteLength();
110+
123111
// Forward the data to V8. Internally, V8 will make a copy.
124112
obj->streaming_->OnBytesReceived(static_cast<const uint8_t*>(bytes) + offset,
125113
size);

0 commit comments

Comments
 (0)