Add safe init with json for Data to avoid crash when json is invalid #6020

pull/6021/head
Krypto Pank 2 years ago
parent 5aecd54bb5
commit bbabf4e2c3
  1. 4
      AlphaWallet.xcodeproj/project.pbxproj
  2. 25
      AlphaWalletTests/Factories/DataTests.swift
  3. 2
      modules/AlphaWalletFoundation/AlphaWalletFoundation/EtherClient/Requests/BlockByNumberRequest.swift
  4. 4
      modules/AlphaWalletFoundation/AlphaWalletFoundation/EtherClient/Requests/DASLookupRequest.swift
  5. 2
      modules/AlphaWalletFoundation/AlphaWalletFoundation/EtherClient/Requests/TransactionReceiptRequest.swift
  6. 9
      modules/AlphaWalletFoundation/AlphaWalletFoundation/Extensions/Data.swift
  7. 2
      modules/AlphaWalletFoundation/AlphaWalletFoundation/Extensions/Dictionary.swift

@ -755,6 +755,7 @@
87D847452744EFA300EDBA80 /* JSONRPC_2_0.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87D847442744EFA300EDBA80 /* JSONRPC_2_0.swift */; };
87D84747274521FD00EDBA80 /* TransactionConfirmationRPCServerInfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87D84746274521FD00EDBA80 /* TransactionConfirmationRPCServerInfoView.swift */; };
87D84749274533F600EDBA80 /* UIImageView+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87D84748274533F500EDBA80 /* UIImageView+Extension.swift */; };
87DC8A48295CA0C300CFBEDA /* DataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87DC8A47295CA0C300CFBEDA /* DataTests.swift */; };
87DCCB51266F655D003E8EA0 /* WalletSummaryViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87DCCB50266F655D003E8EA0 /* WalletSummaryViewModel.swift */; };
87DD0C42279979F900460260 /* EnterKeystorePasswordViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87DD0C41279979F900460260 /* EnterKeystorePasswordViewModel.swift */; };
87DD0C44279980E500460260 /* ValidatorsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87DD0C43279980E500460260 /* ValidatorsTests.swift */; };
@ -1622,6 +1623,7 @@
87D847442744EFA300EDBA80 /* JSONRPC_2_0.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONRPC_2_0.swift; sourceTree = "<group>"; };
87D84746274521FD00EDBA80 /* TransactionConfirmationRPCServerInfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionConfirmationRPCServerInfoView.swift; sourceTree = "<group>"; };
87D84748274533F500EDBA80 /* UIImageView+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImageView+Extension.swift"; sourceTree = "<group>"; };
87DC8A47295CA0C300CFBEDA /* DataTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataTests.swift; sourceTree = "<group>"; };
87DCCB50266F655D003E8EA0 /* WalletSummaryViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletSummaryViewModel.swift; sourceTree = "<group>"; };
87DD0C41279979F900460260 /* EnterKeystorePasswordViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnterKeystorePasswordViewModel.swift; sourceTree = "<group>"; };
87DD0C43279980E500460260 /* ValidatorsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValidatorsTests.swift; sourceTree = "<group>"; };
@ -2632,6 +2634,7 @@
87D1F91328900E4600056EBE /* FakeUniversalLinkCoordinator.swift */,
87D1F91528900EDD00056EBE /* FakeNotificationService.swift */,
87BCB98529362A8100C6BA67 /* FakeTokenScriptOverridesFileManager.swift */,
87DC8A47295CA0C300CFBEDA /* DataTests.swift */,
);
path = Factories;
sourceTree = "<group>";
@ -5629,6 +5632,7 @@
02EFD0FC27DF0815008B5808 /* RepeatTests.swift in Sources */,
295B61D61FE7FC8300642E60 /* FakeTokensDataStore.swift in Sources */,
291E8FBF1F7DEA85003F0ECF /* EtherKeystoreTests.swift in Sources */,
87DC8A48295CA0C300CFBEDA /* DataTests.swift in Sources */,
732E0F542022786400B39C1F /* LockCreatePasscodeCoordinatorTest.swift in Sources */,
298543001FBEAE2F00CB5081 /* ChainState.swift in Sources */,
29AD8A0E1F93FF28008E10E7 /* WalletSession.swift in Sources */,

@ -0,0 +1,25 @@
//
// DataTests.swift
// AlphaWalletTests
//
// Created by Vladyslav Shepitko on 28.12.2022.
//
import XCTest
@testable import AlphaWallet
import AlphaWalletFoundation
final class DataTests: XCTestCase {
func testInit() throws {
XCTAssertThrowsError(try Data(json: NSNull.init()))
let json: [String: Any] = ["key": NSNull()]
XCTAssertNoThrow(try Data(json: json))
guard let string = "string".data(using: .utf8) else { return }
XCTAssertThrowsError(try Data(json: string))
}
}

@ -25,7 +25,7 @@ struct BlockByNumberRequest: JSONRPCKit.Request {
func response(from resultObject: Any) throws -> Response {
do {
let data = try JSONSerialization.data(withJSONObject: resultObject)
let data = try Data(json: resultObject)
return try JSONDecoder().decode(Block.self, from: data)
} catch {
throw CastError(actualValue: resultObject, expectedType: Response.self)

@ -22,7 +22,7 @@ struct DASLookupRequest: JSONRPCKit.Request {
}
func response(from resultObject: Any) throws -> Response {
guard let data = try? JSONSerialization.data(withJSONObject: resultObject, options: []) else {
guard let data = try? Data(json: resultObject) else {
throw CastError(actualValue: resultObject, expectedType: Response.self)
}
if let data = try? JSONDecoder().decode(DASLookupResponse.self, from: data) {
@ -86,4 +86,4 @@ struct DASLookupResponse: Decodable {
let value: String
let ttl: String
}
}
}

@ -25,7 +25,7 @@ struct TransactionReceiptRequest: JSONRPCKit.Request {
func response(from resultObject: Any) throws -> Response {
do {
let data = try JSONSerialization.data(withJSONObject: resultObject)
let data = try Data(json: resultObject)
return try JSONDecoder().decode(TransactionReceipt.self, from: data)
} catch {
throw CastError(actualValue: resultObject, expectedType: Response.self)

@ -2,8 +2,17 @@
import UIKit
import CoreImage
import AlphaWalletWeb3
extension Data {
public init(json: Any, options: JSONSerialization.WritingOptions = []) throws {
guard JSONSerialization.isValidJSONObject(json) else {
throw DecodeError.initFailure
}
self = try JSONSerialization.data(withJSONObject: json, options: options)
}
public struct HexEncodingOptions: OptionSet {
public let rawValue: Int
public static let upperCase = HexEncodingOptions(rawValue: 1 << 0)

@ -6,7 +6,7 @@ extension Dictionary where Key: ExpressibleByStringLiteral, Value: Any {
public var jsonString: String? {
if let dict = (self as AnyObject) as? [String: AnyObject] {
do {
let data = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions(rawValue: 0))
let data = try Data(json: dict, options: JSONSerialization.WritingOptions(rawValue: 0))
if let string = String(data: data, encoding: String.Encoding.utf8) {
return string
}

Loading…
Cancel
Save