Skip to content

Commit

Permalink
Add validity checks to all input fields that has to be a correct Json.
Browse files Browse the repository at this point in the history
  • Loading branch information
KGronek-Pubnub committed Nov 4, 2024
1 parent 93dc0e4 commit 9077cda
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,40 @@ FString UPubnubJsonUtilities::JsonObjectToString(TSharedPtr<FJsonObject> JsonObj
bool UPubnubJsonUtilities::StringToJsonObject(FString JsonString, TSharedPtr<FJsonObject>& JsonObject)
{
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::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<FJsonObject> 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<FString>& Channels)
Expand Down
57 changes: 57 additions & 0 deletions Source/PubnubLibrary/Private/PubnubSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FJsonObject> &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")
Expand Down

0 comments on commit 9077cda

Please sign in to comment.