Skip to content

Commit db483bb

Browse files
jasnelladuh95
authored andcommitted
src: improve error handling in node_http2
PR-URL: #57764 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent b027770 commit db483bb

File tree

1 file changed

+63
-21
lines changed

1 file changed

+63
-21
lines changed

src/node_http2.cc

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,13 @@ Http2Priority::Http2Priority(Environment* env,
426426
Local<Value> weight,
427427
Local<Value> exclusive) {
428428
Local<Context> context = env->context();
429-
int32_t parent_ = parent->Int32Value(context).ToChecked();
430-
int32_t weight_ = weight->Int32Value(context).ToChecked();
429+
int32_t parent_;
430+
int32_t weight_;
431+
if (!parent->Int32Value(context).To(&parent_) ||
432+
!weight->Int32Value(context).To(&weight_)) {
433+
nghttp2_priority_spec_init(this, 0, 0, 0);
434+
return;
435+
}
431436
bool exclusive_ = exclusive->IsTrue();
432437
Debug(env, DebugCategory::HTTP2STREAM,
433438
"Http2Priority: parent: %d, weight: %d, exclusive: %s\n",
@@ -2719,11 +2724,12 @@ void Http2Stream::DecrementAvailableOutboundLength(size_t amount) {
27192724
// back to JS land
27202725
void HttpErrorString(const FunctionCallbackInfo<Value>& args) {
27212726
Environment* env = Environment::GetCurrent(args);
2722-
uint32_t val = args[0]->Uint32Value(env->context()).ToChecked();
2723-
args.GetReturnValue().Set(
2724-
OneByteString(
2725-
env->isolate(),
2726-
reinterpret_cast<const uint8_t*>(nghttp2_strerror(val))));
2727+
uint32_t val;
2728+
if (args[0]->Uint32Value(env->context()).To(&val)) {
2729+
args.GetReturnValue().Set(
2730+
OneByteString(env->isolate(),
2731+
reinterpret_cast<const uint8_t*>(nghttp2_strerror(val))));
2732+
}
27272733
}
27282734

27292735

@@ -2748,7 +2754,10 @@ void Http2Session::SetNextStreamID(const FunctionCallbackInfo<Value>& args) {
27482754
Environment* env = Environment::GetCurrent(args);
27492755
Http2Session* session;
27502756
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
2751-
int32_t id = args[0]->Int32Value(env->context()).ToChecked();
2757+
int32_t id;
2758+
if (!args[0]->Int32Value(env->context()).To(&id)) {
2759+
return;
2760+
}
27522761
if (nghttp2_session_set_next_stream_id(session->session(), id) < 0) {
27532762
Debug(session, "failed to set next stream id to %d", id);
27542763
return args.GetReturnValue().Set(false);
@@ -2766,7 +2775,10 @@ void Http2Session::SetLocalWindowSize(
27662775
Http2Session* session;
27672776
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
27682777

2769-
int32_t window_size = args[0]->Int32Value(env->context()).ToChecked();
2778+
int32_t window_size;
2779+
if (!args[0]->Int32Value(env->context()).To(&window_size)) {
2780+
return;
2781+
}
27702782

27712783
int result = nghttp2_session_set_local_window_size(
27722784
session->session(), NGHTTP2_FLAG_NONE, 0, window_size);
@@ -2826,8 +2838,11 @@ void Http2Session::New(const FunctionCallbackInfo<Value>& args) {
28262838
Http2State* state = realm->GetBindingData<Http2State>();
28272839

28282840
CHECK(args.IsConstructCall());
2829-
SessionType type = static_cast<SessionType>(
2830-
args[0]->Int32Value(realm->context()).ToChecked());
2841+
int32_t val;
2842+
if (!args[0]->Int32Value(realm->context()).To(&val)) {
2843+
return;
2844+
}
2845+
SessionType type = static_cast<SessionType>(val);
28312846
Http2Session* session = new Http2Session(state, args.This(), type);
28322847
Debug(session, "session created");
28332848
}
@@ -2849,7 +2864,10 @@ void Http2Session::Destroy(const FunctionCallbackInfo<Value>& args) {
28492864
Environment* env = Environment::GetCurrent(args);
28502865
Local<Context> context = env->context();
28512866

2852-
uint32_t code = args[0]->Uint32Value(context).ToChecked();
2867+
uint32_t code;
2868+
if (!args[0]->Uint32Value(context).To(&code)) {
2869+
return;
2870+
}
28532871
session->Close(code, args[1]->IsTrue());
28542872
}
28552873

@@ -2861,7 +2879,10 @@ void Http2Session::Request(const FunctionCallbackInfo<Value>& args) {
28612879
Environment* env = session->env();
28622880

28632881
Local<Array> headers = args[0].As<Array>();
2864-
int32_t options = args[1]->Int32Value(env->context()).ToChecked();
2882+
int32_t options;
2883+
if (!args[1]->Int32Value(env->context()).To(&options)) {
2884+
return;
2885+
}
28652886

28662887
Debug(session, "request submitted");
28672888

@@ -2910,8 +2931,14 @@ void Http2Session::Goaway(const FunctionCallbackInfo<Value>& args) {
29102931
Http2Session* session;
29112932
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
29122933

2913-
uint32_t code = args[0]->Uint32Value(context).ToChecked();
2914-
int32_t lastStreamID = args[1]->Int32Value(context).ToChecked();
2934+
uint32_t code;
2935+
if (!args[0]->Uint32Value(context).To(&code)) {
2936+
return;
2937+
}
2938+
int32_t lastStreamID;
2939+
if (!args[1]->Int32Value(context).To(&lastStreamID)) {
2940+
return;
2941+
}
29152942
ArrayBufferViewContents<uint8_t> opaque_data;
29162943

29172944
if (args[2]->IsArrayBufferView()) {
@@ -2947,7 +2974,10 @@ void Http2Stream::RstStream(const FunctionCallbackInfo<Value>& args) {
29472974
Local<Context> context = env->context();
29482975
Http2Stream* stream;
29492976
ASSIGN_OR_RETURN_UNWRAP(&stream, args.This());
2950-
uint32_t code = args[0]->Uint32Value(context).ToChecked();
2977+
uint32_t code;
2978+
if (!args[0]->Uint32Value(context).To(&code)) {
2979+
return;
2980+
}
29512981
Debug(stream, "sending rst_stream with code %d", code);
29522982
stream->SubmitRstStream(code);
29532983
}
@@ -2960,7 +2990,10 @@ void Http2Stream::Respond(const FunctionCallbackInfo<Value>& args) {
29602990
ASSIGN_OR_RETURN_UNWRAP(&stream, args.This());
29612991

29622992
Local<Array> headers = args[0].As<Array>();
2963-
int32_t options = args[1]->Int32Value(env->context()).ToChecked();
2993+
int32_t options;
2994+
if (!args[1]->Int32Value(env->context()).To(&options)) {
2995+
return;
2996+
}
29642997

29652998
args.GetReturnValue().Set(
29662999
stream->SubmitResponse(
@@ -3015,7 +3048,10 @@ void Http2Stream::PushPromise(const FunctionCallbackInfo<Value>& args) {
30153048
ASSIGN_OR_RETURN_UNWRAP(&parent, args.This());
30163049

30173050
Local<Array> headers = args[0].As<Array>();
3018-
int32_t options = args[1]->Int32Value(env->context()).ToChecked();
3051+
int32_t options;
3052+
if (!args[1]->Int32Value(env->context()).To(&options)) {
3053+
return;
3054+
}
30193055

30203056
Debug(parent, "creating push promise");
30213057

@@ -3110,7 +3146,10 @@ void Http2Session::AltSvc(const FunctionCallbackInfo<Value>& args) {
31103146
Http2Session* session;
31113147
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
31123148

3113-
int32_t id = args[0]->Int32Value(env->context()).ToChecked();
3149+
int32_t id;
3150+
if (!args[0]->Int32Value(env->context()).To(&id)) {
3151+
return;
3152+
}
31143153

31153154
// origin and value are both required to be ASCII, handle them as such.
31163155
Local<String> origin_str = args[1]->ToString(env->context()).ToLocalChecked();
@@ -3142,9 +3181,12 @@ void Http2Session::Origin(const FunctionCallbackInfo<Value>& args) {
31423181
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
31433182

31443183
Local<String> origin_string = args[0].As<String>();
3145-
size_t count = args[1]->Int32Value(context).ToChecked();
3184+
int32_t count;
3185+
if (!args[1]->Int32Value(context).To(&count)) {
3186+
return;
3187+
}
31463188

3147-
session->Origin(Origins(env, origin_string, count));
3189+
session->Origin(Origins(env, origin_string, static_cast<size_t>(count)));
31483190
}
31493191

31503192
// Submits a PING frame to be sent to the connected peer.

0 commit comments

Comments
 (0)