Skip to content
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

src: harden JSStream callbacks #18028

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions src/js_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::TryCatch;
using v8::Value;


Expand Down Expand Up @@ -87,24 +87,41 @@ bool JSStream::IsAlive() {
bool JSStream::IsClosing() {
HandleScope scope(env()->isolate());
Context::Scope context_scope(env()->context());
return MakeCallback(env()->isclosing_string(), 0, nullptr)
.ToLocalChecked()->IsTrue();
TryCatch try_catch(env()->isolate());
Local<Value> value;
if (!MakeCallback(env()->isclosing_string(), 0, nullptr).ToLocal(&value)) {
FatalException(env()->isolate(), try_catch);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FatalException() can return so this should probably be followed by an ABORT() or something. The unguarded value->IsTrue() below is not safe, at any rate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bnoordhuis Yea, thanks. 👍 I’ve added an extra return true; here and UV_EPROTO as the default return value for the other places, that’s what tls_wrap.cc uses as a generic failure code (but feel free to let me know of a better choice) – it shouldn’t make any difference in practice, I guess.

return true;
}
return value->IsTrue();
}


int JSStream::ReadStart() {
HandleScope scope(env()->isolate());
Context::Scope context_scope(env()->context());
return MakeCallback(env()->onreadstart_string(), 0, nullptr)
.ToLocalChecked()->Int32Value();
TryCatch try_catch(env()->isolate());
Local<Value> value;
int value_int = UV_EPROTO;
if (!MakeCallback(env()->onreadstart_string(), 0, nullptr).ToLocal(&value) ||
!value->Int32Value(env()->context()).To(&value_int)) {
FatalException(env()->isolate(), try_catch);
}
return value_int;
}


int JSStream::ReadStop() {
HandleScope scope(env()->isolate());
Context::Scope context_scope(env()->context());
return MakeCallback(env()->onreadstop_string(), 0, nullptr)
.ToLocalChecked()->Int32Value();
TryCatch try_catch(env()->isolate());
Local<Value> value;
int value_int = UV_EPROTO;
if (!MakeCallback(env()->onreadstop_string(), 0, nullptr).ToLocal(&value) ||
!value->Int32Value(env()->context()).To(&value_int)) {
FatalException(env()->isolate(), try_catch);
}
return value_int;
}


Expand All @@ -117,10 +134,17 @@ int JSStream::DoShutdown(ShutdownWrap* req_wrap) {
};

req_wrap->Dispatched();
MaybeLocal<Value> res =
MakeCallback(env()->onshutdown_string(), arraysize(argv), argv);

return res.ToLocalChecked()->Int32Value();
TryCatch try_catch(env()->isolate());
Local<Value> value;
int value_int = UV_EPROTO;
if (!MakeCallback(env()->onshutdown_string(),
arraysize(argv),
argv).ToLocal(&value) ||
!value->Int32Value(env()->context()).To(&value_int)) {
FatalException(env()->isolate(), try_catch);
}
return value_int;
}


Expand All @@ -146,10 +170,17 @@ int JSStream::DoWrite(WriteWrap* w,
};

w->Dispatched();
MaybeLocal<Value> res =
MakeCallback(env()->onwrite_string(), arraysize(argv), argv);

return res.ToLocalChecked()->Int32Value();
TryCatch try_catch(env()->isolate());
Local<Value> value;
int value_int = UV_EPROTO;
if (!MakeCallback(env()->onwrite_string(),
arraysize(argv),
argv).ToLocal(&value) ||
!value->Int32Value(env()->context()).To(&value_int)) {
FatalException(env()->isolate(), try_catch);
}
return value_int;
}


Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-wrap-js-stream-exceptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const JSStreamWrap = require('internal/wrap_js_stream');
const { Duplex } = require('stream');

process.once('uncaughtException', common.mustCall((err) => {
assert.strictEqual(err.message, 'exception!');
}));

const socket = new JSStreamWrap(new Duplex({
read: common.mustCall(),
write: common.mustCall((buffer, data, cb) => {
throw new Error('exception!');
})
}));

assert.throws(() => socket.end('foo'), /Error: write EPROTO/);