-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
buffer: add bounds validation to SlowCopy and FastCopy #60264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -578,9 +578,28 @@ void CopyImpl(Local<Value> source_obj, | |
| void SlowCopy(const FunctionCallbackInfo<Value>& args) { | ||
| Local<Value> source_obj = args[0]; | ||
| Local<Value> target_obj = args[1]; | ||
| Environment* env = Environment::GetCurrent(args); | ||
| Isolate* isolate = env->isolate(); | ||
|
|
||
| // Add validation before call CopyImpl blindly | ||
| if (!Buffer::HasInstance(source_obj) || !Buffer::HasInstance(target_obj)) { | ||
| isolate->ThrowException(v8::Exception::TypeError(String::NewFromUtf8Literal( | ||
| isolate, "Arguments must be Buffer instances"))); | ||
| return; | ||
| } | ||
| const uint32_t target_start = args[2].As<Uint32>()->Value(); | ||
| const uint32_t source_start = args[3].As<Uint32>()->Value(); | ||
| const uint32_t to_copy = args[4].As<Uint32>()->Value(); | ||
| size_t source_len = Buffer::Length(source_obj.As<Object>()); | ||
| size_t target_len = Buffer::Length(target_obj.As<Object>()); | ||
|
|
||
| if (source_start > source_len || target_start > target_len || | ||
| to_copy > source_len - source_start || | ||
| to_copy > target_len - target_start) { | ||
| isolate->ThrowException(v8::Exception::RangeError( | ||
| String::NewFromUtf8Literal(isolate, "Buffer copy out of range"))); | ||
| return; | ||
| } | ||
|
|
||
| CopyImpl(source_obj, target_obj, target_start, source_start, to_copy); | ||
|
|
||
|
|
@@ -597,7 +616,21 @@ uint32_t FastCopy(Local<Value> receiver, | |
| // NOLINTNEXTLINE(runtime/references) | ||
| FastApiCallbackOptions& options) { | ||
| HandleScope scope(options.isolate); | ||
| Isolate* isolate = options.isolate; | ||
|
|
||
| if (!node::Buffer::HasInstance(source_obj) || | ||
| !node::Buffer::HasInstance(target_obj)) { | ||
| return 0; | ||
| } | ||
| // Validate First before call CopyImpl blindly | ||
| size_t src_len = Buffer::Length(source_obj.As<v8::Object>()); | ||
| size_t dst_len = Buffer::Length(target_obj.As<v8::Object>()); | ||
|
|
||
| if (source_start > src_len || target_start > dst_len || | ||
| to_copy > src_len - source_start || to_copy > dst_len - target_start) { | ||
| // options.fallback = true; // Let JS slow path handle it (safe fallback) | ||
|
||
| return 0; | ||
| } | ||
| CopyImpl(source_obj, target_obj, target_start, source_start, to_copy); | ||
|
|
||
| return to_copy; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just fyi,
Buffer::HasInstance(obj)is an alias forobj->IsArrayBufferView()andBuffer::Length(obj)is an alias forobj.As<ArrayBufferView>()->ByteLength(). I'd suggest just using those here directlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the note you’re absolutely right. I’ve updated the code to use IsArrayBufferView() and ByteLength() directly instead of Buffer::HasInstance() and Buffer::Length() for consistency with the V8 API.