Skip to content

Commit 08a7df8

Browse files
committed
src: improve error handling in node_http2
PR-URL: nodejs#57764 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent e06a2ad commit 08a7df8

File tree

1 file changed

+63
-21
lines changed

1 file changed

+63
-21
lines changed

src/node_http2.cc

+63-21
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",
@@ -2715,11 +2720,12 @@ void Http2Stream::DecrementAvailableOutboundLength(size_t amount) {
27152720
// back to JS land
27162721
void HttpErrorString(const FunctionCallbackInfo<Value>& args) {
27172722
Environment* env = Environment::GetCurrent(args);
2718-
uint32_t val = args[0]->Uint32Value(env->context()).ToChecked();
2719-
args.GetReturnValue().Set(
2720-
OneByteString(
2721-
env->isolate(),
2722-
reinterpret_cast<const uint8_t*>(nghttp2_strerror(val))));
2723+
uint32_t val;
2724+
if (args[0]->Uint32Value(env->context()).To(&val)) {
2725+
args.GetReturnValue().Set(
2726+
OneByteString(env->isolate(),
2727+
reinterpret_cast<const uint8_t*>(nghttp2_strerror(val))));
2728+
}
27232729
}
27242730

27252731

@@ -2744,7 +2750,10 @@ void Http2Session::SetNextStreamID(const FunctionCallbackInfo<Value>& args) {
27442750
Environment* env = Environment::GetCurrent(args);
27452751
Http2Session* session;
27462752
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
2747-
int32_t id = args[0]->Int32Value(env->context()).ToChecked();
2753+
int32_t id;
2754+
if (!args[0]->Int32Value(env->context()).To(&id)) {
2755+
return;
2756+
}
27482757
if (nghttp2_session_set_next_stream_id(session->session(), id) < 0) {
27492758
Debug(session, "failed to set next stream id to %d", id);
27502759
return args.GetReturnValue().Set(false);
@@ -2762,7 +2771,10 @@ void Http2Session::SetLocalWindowSize(
27622771
Http2Session* session;
27632772
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
27642773

2765-
int32_t window_size = args[0]->Int32Value(env->context()).ToChecked();
2774+
int32_t window_size;
2775+
if (!args[0]->Int32Value(env->context()).To(&window_size)) {
2776+
return;
2777+
}
27662778

27672779
int result = nghttp2_session_set_local_window_size(
27682780
session->session(), NGHTTP2_FLAG_NONE, 0, window_size);
@@ -2822,8 +2834,11 @@ void Http2Session::New(const FunctionCallbackInfo<Value>& args) {
28222834
Http2State* state = realm->GetBindingData<Http2State>();
28232835

28242836
CHECK(args.IsConstructCall());
2825-
SessionType type = static_cast<SessionType>(
2826-
args[0]->Int32Value(realm->context()).ToChecked());
2837+
int32_t val;
2838+
if (!args[0]->Int32Value(realm->context()).To(&val)) {
2839+
return;
2840+
}
2841+
SessionType type = static_cast<SessionType>(val);
28272842
Http2Session* session = new Http2Session(state, args.This(), type);
28282843
Debug(session, "session created");
28292844
}
@@ -2845,7 +2860,10 @@ void Http2Session::Destroy(const FunctionCallbackInfo<Value>& args) {
28452860
Environment* env = Environment::GetCurrent(args);
28462861
Local<Context> context = env->context();
28472862

2848-
uint32_t code = args[0]->Uint32Value(context).ToChecked();
2863+
uint32_t code;
2864+
if (!args[0]->Uint32Value(context).To(&code)) {
2865+
return;
2866+
}
28492867
session->Close(code, args[1]->IsTrue());
28502868
}
28512869

@@ -2857,7 +2875,10 @@ void Http2Session::Request(const FunctionCallbackInfo<Value>& args) {
28572875
Environment* env = session->env();
28582876

28592877
Local<Array> headers = args[0].As<Array>();
2860-
int32_t options = args[1]->Int32Value(env->context()).ToChecked();
2878+
int32_t options;
2879+
if (!args[1]->Int32Value(env->context()).To(&options)) {
2880+
return;
2881+
}
28612882

28622883
Debug(session, "request submitted");
28632884

@@ -2906,8 +2927,14 @@ void Http2Session::Goaway(const FunctionCallbackInfo<Value>& args) {
29062927
Http2Session* session;
29072928
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
29082929

2909-
uint32_t code = args[0]->Uint32Value(context).ToChecked();
2910-
int32_t lastStreamID = args[1]->Int32Value(context).ToChecked();
2930+
uint32_t code;
2931+
if (!args[0]->Uint32Value(context).To(&code)) {
2932+
return;
2933+
}
2934+
int32_t lastStreamID;
2935+
if (!args[1]->Int32Value(context).To(&lastStreamID)) {
2936+
return;
2937+
}
29112938
ArrayBufferViewContents<uint8_t> opaque_data;
29122939

29132940
if (args[2]->IsArrayBufferView()) {
@@ -2945,7 +2972,10 @@ void Http2Stream::RstStream(const FunctionCallbackInfo<Value>& args) {
29452972
Local<Context> context = env->context();
29462973
Http2Stream* stream;
29472974
ASSIGN_OR_RETURN_UNWRAP(&stream, args.This());
2948-
uint32_t code = args[0]->Uint32Value(context).ToChecked();
2975+
uint32_t code;
2976+
if (!args[0]->Uint32Value(context).To(&code)) {
2977+
return;
2978+
}
29492979
Debug(stream, "sending rst_stream with code %d", code);
29502980
stream->SubmitRstStream(code);
29512981
}
@@ -2958,7 +2988,10 @@ void Http2Stream::Respond(const FunctionCallbackInfo<Value>& args) {
29582988
ASSIGN_OR_RETURN_UNWRAP(&stream, args.This());
29592989

29602990
Local<Array> headers = args[0].As<Array>();
2961-
int32_t options = args[1]->Int32Value(env->context()).ToChecked();
2991+
int32_t options;
2992+
if (!args[1]->Int32Value(env->context()).To(&options)) {
2993+
return;
2994+
}
29622995

29632996
args.GetReturnValue().Set(
29642997
stream->SubmitResponse(
@@ -3013,7 +3046,10 @@ void Http2Stream::PushPromise(const FunctionCallbackInfo<Value>& args) {
30133046
ASSIGN_OR_RETURN_UNWRAP(&parent, args.This());
30143047

30153048
Local<Array> headers = args[0].As<Array>();
3016-
int32_t options = args[1]->Int32Value(env->context()).ToChecked();
3049+
int32_t options;
3050+
if (!args[1]->Int32Value(env->context()).To(&options)) {
3051+
return;
3052+
}
30173053

30183054
Debug(parent, "creating push promise");
30193055

@@ -3108,7 +3144,10 @@ void Http2Session::AltSvc(const FunctionCallbackInfo<Value>& args) {
31083144
Http2Session* session;
31093145
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
31103146

3111-
int32_t id = args[0]->Int32Value(env->context()).ToChecked();
3147+
int32_t id;
3148+
if (!args[0]->Int32Value(env->context()).To(&id)) {
3149+
return;
3150+
}
31123151

31133152
// origin and value are both required to be ASCII, handle them as such.
31143153
Local<String> origin_str;
@@ -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)