From 9077cdaaf8fc177878d414edc4143cdc8bc715aa Mon Sep 17 00:00:00 2001 From: Kamil Gronek Date: Mon, 4 Nov 2024 14:54:15 +0100 Subject: [PATCH] Add validity checks to all input fields that has to be a correct Json. --- .../FunctionLibraries/PubnubJsonUtilities.cpp | 36 +++++++++++- .../PubnubLibrary/Private/PubnubSubsystem.cpp | 57 +++++++++++++++++++ .../FunctionLibraries/PubnubJsonUtilities.h | 8 +++ 3 files changed, 99 insertions(+), 2 deletions(-) diff --git a/Source/PubnubLibrary/Private/FunctionLibraries/PubnubJsonUtilities.cpp b/Source/PubnubLibrary/Private/FunctionLibraries/PubnubJsonUtilities.cpp index 1c77fde..93a575f 100644 --- a/Source/PubnubLibrary/Private/FunctionLibraries/PubnubJsonUtilities.cpp +++ b/Source/PubnubLibrary/Private/FunctionLibraries/PubnubJsonUtilities.cpp @@ -20,8 +20,40 @@ FString UPubnubJsonUtilities::JsonObjectToString(TSharedPtr JsonObj bool UPubnubJsonUtilities::StringToJsonObject(FString JsonString, TSharedPtr& JsonObject) { TSharedRef> JsonReader = TJsonReaderFactory::Create(JsonString); - bool Result = FJsonSerializer::Deserialize(JsonReader, JsonObject); - return Result; + return FJsonSerializer::Deserialize(JsonReader, JsonObject); +} + +bool UPubnubJsonUtilities::IsCorrectJsonString(const FString InString, bool AllowSimpleTypes) +{ + //A String is correct Json if it's a valid Json Object or Json Array + TSharedPtr JsonObject = MakeShareable(new FJsonObject); + if(StringToJsonObject(InString, JsonObject)) + { + return true; + } + + if(!AllowSimpleTypes) + { + return false; + } + + //Or it's a text in quotes (string type) + if(InString.Left(1) == "\"" && InString.Right(1) == "\"") + { + return true; + } + //Or a numeric field + if(InString.IsNumeric()) + { + return true; + } + //Or a bool field + if(InString == "true" || InString == "false") + { + return true; + } + + return false; } void UPubnubJsonUtilities::ListChannelsFromGroupJsonToData(FString ResponseJson, bool& Error, int& Status, TArray& Channels) diff --git a/Source/PubnubLibrary/Private/PubnubSubsystem.cpp b/Source/PubnubLibrary/Private/PubnubSubsystem.cpp index 1081302..8e02968 100644 --- a/Source/PubnubLibrary/Private/PubnubSubsystem.cpp +++ b/Source/PubnubLibrary/Private/PubnubSubsystem.cpp @@ -1132,6 +1132,12 @@ void UPubnubSubsystem::PublishMessage_priv(FString ChannelName, FString Message, if(CheckIsFieldEmpty(ChannelName, "ChannelName", "PublishMessage") || CheckIsFieldEmpty(Message, "Message", "PublishMessage")) {return;} + + if(!UPubnubJsonUtilities::IsCorrectJsonString(Message)) + { + PubnubError("Can't Publish Message, Message has to be a correct Json", EPubnubErrorType::PET_Warning); + return; + } //Convert all UE PublishSettings to Pubnub PublishOptions @@ -1475,6 +1481,12 @@ void UPubnubSubsystem::SetState_priv(FString ChannelName, FString StateJson, FPu if(CheckIsFieldEmpty(ChannelName, "ChannelName", "SetState") || CheckIsFieldEmpty(StateJson, "StateJson", "SetState")) {return;} + + if(!UPubnubJsonUtilities::IsCorrectJsonString(StateJson, false)) + { + PubnubError("Can't Set State, StateJson has to be a correct Json Object", EPubnubErrorType::PET_Warning); + return; + } //Set all options from SetStateSettings @@ -1493,6 +1505,9 @@ void UPubnubSubsystem::SetState_priv(FString ChannelName, FString StateJson, FPu if (PNR_OK != PubnubResponse) { PubnubResponseError(PubnubResponse, "Failed to set state."); } + + //Clean up the responses + pubnub_get(ctx_pub); } void UPubnubSubsystem::GetState_priv(FString ChannelName, FString ChannelGroup, FString UserID, FOnPubnubResponse OnGetStateResponse) @@ -1781,6 +1796,12 @@ void UPubnubSubsystem::SetUUIDMetadata_priv(FString UUIDMetadataID, FString UUID if(CheckIsFieldEmpty(UUIDMetadataID, "UUIDMetadataID", "SetUUIDMetadata") || CheckIsFieldEmpty(UUIDMetadataObj, "UUIDMetadataObj", "SetUUIDMetadata")) {return;} + if(!UPubnubJsonUtilities::IsCorrectJsonString(UUIDMetadataObj, false)) + { + PubnubError("Can't Set UUID Metadata, UUIDMetadataObj has to be a correct Json Object", EPubnubErrorType::PET_Warning); + return; + } + pubnub_set_uuidmetadata(ctx_pub, TCHAR_TO_ANSI(*UUIDMetadataID), TCHAR_TO_ANSI(*Include), TCHAR_TO_ANSI(*UUIDMetadataObj)); pubnub_res PubnubResponse = pubnub_await(ctx_pub); @@ -1921,6 +1942,12 @@ void UPubnubSubsystem::SetChannelMetadata_priv(FString ChannelMetadataID, FStrin if(CheckIsFieldEmpty(ChannelMetadataID, "ChannelMetadataID", "SetChannelMetadata") || CheckIsFieldEmpty(ChannelMetadataObj, "ChannelMetadataObj", "SetChannelMetadata")) {return;} + if(!UPubnubJsonUtilities::IsCorrectJsonString(ChannelMetadataObj, false)) + { + PubnubError("Can't Set Channel Metadata, ChannelMetadataObj has to be a correct Json Object", EPubnubErrorType::PET_Warning); + return; + } + pubnub_set_channelmetadata(ctx_pub, TCHAR_TO_ANSI(*ChannelMetadataID), TCHAR_TO_ANSI(*Include), TCHAR_TO_ANSI(*ChannelMetadataObj)); pubnub_res PubnubResponse = pubnub_await(ctx_pub); @@ -2072,6 +2099,12 @@ void UPubnubSubsystem::SetMemberships_priv(FString UUIDMetadataID, FString SetOb if(CheckIsFieldEmpty(UUIDMetadataID, "UUIDMetadataID", "SetMemberships") || CheckIsFieldEmpty(SetObj, "SetObj", "SetMemberships")) {return;} + if(!UPubnubJsonUtilities::IsCorrectJsonString(SetObj, false)) + { + PubnubError("Can't Set Memberships, SetObj has to be a correct Json Object", EPubnubErrorType::PET_Warning); + return; + } + pubnub_set_memberships(ctx_pub, TCHAR_TO_ANSI(*UUIDMetadataID), TCHAR_TO_ANSI(*Include), TCHAR_TO_ANSI(*SetObj)); pubnub_res PubnubResponse = pubnub_await(ctx_pub); @@ -2089,6 +2122,12 @@ void UPubnubSubsystem::RemoveMemberships_priv(FString UUIDMetadataID, FString Re if(CheckIsFieldEmpty(UUIDMetadataID, "UUIDMetadataID", "RemoveMemberships") || CheckIsFieldEmpty(RemoveObj, "RemoveObj", "RemoveMemberships")) {return;} + if(!UPubnubJsonUtilities::IsCorrectJsonString(RemoveObj, false)) + { + PubnubError("Can't Remove Memberships, RemoveObj has to be a correct Json Object", EPubnubErrorType::PET_Warning); + return; + } + pubnub_remove_memberships(ctx_pub, TCHAR_TO_ANSI(*UUIDMetadataID), TCHAR_TO_ANSI(*Include), TCHAR_TO_ANSI(*RemoveObj)); pubnub_res PubnubResponse = pubnub_await(ctx_pub); @@ -2174,6 +2213,12 @@ void UPubnubSubsystem::AddChannelMembers_priv(FString ChannelMetadataID, FString if(CheckIsFieldEmpty(ChannelMetadataID, "ChannelMetadataID", "AddChannelMembers") || CheckIsFieldEmpty(AddObj, "AddObj", "AddChannelMembers")) {return;} + if(!UPubnubJsonUtilities::IsCorrectJsonString(AddObj, false)) + { + PubnubError("Can't Add Channel Members, AddObj has to be a correct Json Object", EPubnubErrorType::PET_Warning); + return; + } + pubnub_add_members(ctx_pub, TCHAR_TO_ANSI(*ChannelMetadataID), TCHAR_TO_ANSI(*Include), TCHAR_TO_ANSI(*AddObj)); pubnub_res PubnubResponse = pubnub_await(ctx_pub); @@ -2191,6 +2236,12 @@ void UPubnubSubsystem::SetChannelMembers_priv(FString ChannelMetadataID, FString if(CheckIsFieldEmpty(ChannelMetadataID, "ChannelMetadataID", "SetChannelMembers") || CheckIsFieldEmpty(SetObj, "SetObj", "SetChannelMembers")) {return;} + if(!UPubnubJsonUtilities::IsCorrectJsonString(SetObj, false)) + { + PubnubError("Can't Set Channel Members, SetObj has to be a correct Json Object", EPubnubErrorType::PET_Warning); + return; + } + pubnub_set_members(ctx_pub, TCHAR_TO_ANSI(*ChannelMetadataID), TCHAR_TO_ANSI(*Include), TCHAR_TO_ANSI(*SetObj)); pubnub_res PubnubResponse = pubnub_await(ctx_pub); @@ -2208,6 +2259,12 @@ void UPubnubSubsystem::RemoveChannelMembers_priv(FString ChannelMetadataID, FStr if(CheckIsFieldEmpty(ChannelMetadataID, "ChannelMetadataID", "RemoveChannelMembers") || CheckIsFieldEmpty(RemoveObj, "RemoveObj", "RemoveChannelMembers")) {return;} + if(!UPubnubJsonUtilities::IsCorrectJsonString(RemoveObj, false)) + { + PubnubError("Can't Remove Channel Members, RemoveObj has to be a correct Json Object", EPubnubErrorType::PET_Warning); + return; + } + pubnub_remove_members(ctx_pub, TCHAR_TO_ANSI(*ChannelMetadataID), TCHAR_TO_ANSI(*Include), TCHAR_TO_ANSI(*RemoveObj)); pubnub_res PubnubResponse = pubnub_await(ctx_pub); diff --git a/Source/PubnubLibrary/Public/FunctionLibraries/PubnubJsonUtilities.h b/Source/PubnubLibrary/Public/FunctionLibraries/PubnubJsonUtilities.h index 35d929c..070ef9e 100644 --- a/Source/PubnubLibrary/Public/FunctionLibraries/PubnubJsonUtilities.h +++ b/Source/PubnubLibrary/Public/FunctionLibraries/PubnubJsonUtilities.h @@ -22,6 +22,14 @@ class PUBNUBLIBRARY_API UPubnubJsonUtilities : public UBlueprintFunctionLibrary //Convert FString to JsonObject. Returns true if conversion was successful static bool StringToJsonObject(FString JsonString, TSharedPtr &JsonObject); + /** + * Checks if gives string can be converted to a json + * @param InString - String to check + * @param AllowSimpleTypes - If json root can be of type string, number or int + */ + UFUNCTION(BlueprintCallable, BlueprintPure, Category="Pubnub|Json Utilities") + static bool IsCorrectJsonString(const FString InString, bool AllowSimpleTypes = true); + /* JSON CONVERTERS*/ UFUNCTION(BlueprintCallable, BlueprintPure, Category="Pubnub|Json Utilities")