From 0d7b8f988c9e22e4f785c556449fba52d76e5cb2 Mon Sep 17 00:00:00 2001 From: wutschel Date: Thu, 12 Oct 2023 15:33:56 +0200 Subject: [PATCH] Check for expected JSON object after deserialization Improves robustness against mal-formatted JSON API responses. Introduces new error type and localized error string. --- .../dbowen-Demiurgic-JSON-RPC-168ecc9/DSJSONRPC.m | 14 ++++++++++++++ .../DSJSONRPCError.h | 3 ++- XBMC Remote/en.lproj/Localizable.strings | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/XBMC Remote/dbowen-Demiurgic-JSON-RPC-168ecc9/DSJSONRPC.m b/XBMC Remote/dbowen-Demiurgic-JSON-RPC-168ecc9/DSJSONRPC.m index de6a7ebd4..9f804a488 100755 --- a/XBMC Remote/dbowen-Demiurgic-JSON-RPC-168ecc9/DSJSONRPC.m +++ b/XBMC Remote/dbowen-Demiurgic-JSON-RPC-168ecc9/DSJSONRPC.m @@ -184,6 +184,20 @@ - (void)URLSession:(NSURLSession*)session task:(NSURLSessionTask*)task didComple completionHandler(methodName, aID, nil, nil, aError); } } + // Deserialized object is not a dictionary + else if (![jsonResult isKindOfClass:[NSDictionary class]]) { + // Pass the error to completion handler + if (completionHandler) { + NSError *aError = [NSError errorWithDomain:RPC_DOMAIN code:DSJSONRPCParseError userInfo:@{NSLocalizedDescriptionKey: LOCALIZED_STR(@"Received unexpected JSON object.")}]; + NSDictionary *jsonErrorDict = @{ + @"code": @(JSONRPCInvalidObject), + @"message": LOCALIZED_STR(@"Received unexpected JSON object."), + @"data": [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding], + }; + DSJSONRPCError *jsonRPCError = [DSJSONRPCError errorWithData:jsonErrorDict]; + completionHandler(methodName, aID, nil, jsonRPCError, aError); + } + } // The JSON server passed back an error for the response else if (!jsonError && jsonResult[@"error"] != nil && [jsonResult[@"error"] isKindOfClass:[NSDictionary class]]) { // Pass the error to completion handler diff --git a/XBMC Remote/dbowen-Demiurgic-JSON-RPC-168ecc9/DSJSONRPCError.h b/XBMC Remote/dbowen-Demiurgic-JSON-RPC-168ecc9/DSJSONRPCError.h index a36ac7949..f583f2cc4 100755 --- a/XBMC Remote/dbowen-Demiurgic-JSON-RPC-168ecc9/DSJSONRPCError.h +++ b/XBMC Remote/dbowen-Demiurgic-JSON-RPC-168ecc9/DSJSONRPCError.h @@ -43,7 +43,8 @@ typedef enum { JSONRPCInvalidRequest = -32600, JSONRPCMethodNotFound = -32601, JSONRPCInvalidParams = -32602, - JSONRPCInternalError = -32603 + JSONRPCInternalError = -32603, + JSONRPCInvalidObject = -32604, } JSONRPCErrorType; diff --git a/XBMC Remote/en.lproj/Localizable.strings b/XBMC Remote/en.lproj/Localizable.strings index c6384ea0e..4ad4b5bde 100644 --- a/XBMC Remote/en.lproj/Localizable.strings +++ b/XBMC Remote/en.lproj/Localizable.strings @@ -362,3 +362,5 @@ "Metering mode" = "Metering mode"; "Focal length" = "Focal length"; "Camera model" = "Camera model"; + +"Received unexpected JSON object." = "Received unexpected JSON object.";