From 0fc9a3db2f8e2c70ab2ef9b3500d7d0c18c57a95 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Mon, 9 Nov 2020 14:53:41 +0100 Subject: [PATCH] Stricter arg length checks --- src/AppWrapper.h | 11 +++++++---- src/Utilities.h | 11 +++++++++++ src/WebSocketWrapper.h | 4 ++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/AppWrapper.h b/src/AppWrapper.h index 73d3154..4e2f4a0 100644 --- a/src/AppWrapper.h +++ b/src/AppWrapper.h @@ -24,6 +24,11 @@ using namespace v8; template void uWS_App_ws(const FunctionCallbackInfo &args) { + /* pattern, behavior */ + if (missingArguments(2, args)) { + return; + } + Isolate *isolate = args.GetIsolate(); PerContextData *perContextData = (PerContextData *) Local::Cast(args.Data())->Value(); @@ -288,9 +293,7 @@ void uWS_App_listen(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); /* Require at least two arguments */ - if (args.Length() < 2) { - /* Throw here */ - args.GetReturnValue().Set(isolate->ThrowException(String::NewFromUtf8(isolate, "App.listen requires port and callback", NewStringType::kNormal).ToLocalChecked())); + if (missingArguments(2, args)) { return; } @@ -332,7 +335,7 @@ void uWS_App_publish(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); /* topic, message [isBinary, compress] */ - if (args.Length() < 2) { + if (missingArguments(2, args)) { return; } diff --git a/src/Utilities.h b/src/Utilities.h index 7a6926b..1f0238a 100644 --- a/src/Utilities.h +++ b/src/Utilities.h @@ -55,6 +55,17 @@ static constexpr int getAppTypeIndex() { return std::is_same::value; } +static inline bool missingArguments(int length, const FunctionCallbackInfo &args) { + if (args.Length() < length) { + std::string message = "Function requires at least "; + message += std::to_string(length); + message += " arguments."; + args.GetReturnValue().Set(args.GetIsolate()->ThrowException(String::NewFromUtf8(args.GetIsolate(), message.c_str(), NewStringType::kNormal).ToLocalChecked())); + return true; + } + return false; +} + struct Callback { bool invalid = false; UniquePersistent f; diff --git a/src/WebSocketWrapper.h b/src/WebSocketWrapper.h index 562e236..f1b3221 100644 --- a/src/WebSocketWrapper.h +++ b/src/WebSocketWrapper.h @@ -74,6 +74,10 @@ struct WebSocketWrapper { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { + if (missingArguments(2, args)) { + return; + } + NativeString topic(isolate, args[0]); if (topic.isInvalid(args)) { return;