Skip to content

Commit

Permalink
Merge pull request #188 from jszumski/nssecurecoding
Browse files Browse the repository at this point in the history
Adopt `NSSecureCoding` during deserialization
  • Loading branch information
tcamin authored Sep 27, 2023
2 parents 7fb89e1 + 8ec9ec2 commit 4262ed2
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 74 deletions.
4 changes: 2 additions & 2 deletions Example/SBTUITestTunnel_Tests/MiscellaneousTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ class MiscellaneousTests: XCTestCase {

let monitoredRequests = app.monitoredRequestsFlushAll()
XCTAssertEqual(monitoredRequests.count, 1)
let responsetHeaders = monitoredRequests.first?.response?.allHeaderFields
let responseHeaders = monitoredRequests.first?.response?.allHeaderFields

XCTAssertEqual(networkString, "{\"hello\":\"there\"}\n")
XCTAssertEqual(responsetHeaders!["Content-Type"] as? String, "application/json")
XCTAssertEqual(responseHeaders?["Content-Type"] as? String, "application/json")
}

func testShutdown() {
Expand Down
46 changes: 38 additions & 8 deletions Sources/SBTUITestTunnelClient/SBTUITestTunnelClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,11 @@ - (BOOL)stubRequestsRemoveAll
NSString *objectBase64 = [self sendSynchronousRequestWithPath:SBTUITunneledApplicationCommandStubRequestsAll params:nil];
if (objectBase64) {
NSData *objectData = [[NSData alloc] initWithBase64EncodedString:objectBase64 options:0];

NSArray *result = [NSKeyedUnarchiver unarchiveTopLevelObjectWithData:objectData error:nil];

NSError *unarchiveError;
NSSet *classes = [NSSet setWithObjects:[NSArray class], [SBTActiveStub class], nil];
NSArray *result = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes fromData:objectData error:&unarchiveError];
NSAssert(unarchiveError == nil, @"Error unarchiving NSArray of SBTActiveStub");

return result ?: @[];
}
Expand Down Expand Up @@ -396,7 +399,12 @@ - (NSString *)monitorRequestsMatching:(SBTRequestMatch *)match
if (objectBase64) {
NSData *objectData = [[NSData alloc] initWithBase64EncodedString:objectBase64 options:0];

return [NSKeyedUnarchiver unarchiveObjectWithData:objectData] ?: @[];
NSError *unarchiveError;
NSSet *classes = [NSSet setWithObjects:[NSArray class], [SBTMonitoredNetworkRequest class], nil];
NSArray *result = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes fromData:objectData error:&unarchiveError];
NSAssert(unarchiveError == nil, @"Error unarchiving NSArray of SBTMonitoredNetworkRequest");

return result ?: @[];
}

return @[];
Expand All @@ -408,7 +416,12 @@ - (NSString *)monitorRequestsMatching:(SBTRequestMatch *)match
if (objectBase64) {
NSData *objectData = [[NSData alloc] initWithBase64EncodedString:objectBase64 options:0];

return [NSKeyedUnarchiver unarchiveObjectWithData:objectData] ?: @[];
NSError *unarchiveError;
NSSet *classes = [NSSet setWithObjects:[NSArray class], [SBTMonitoredNetworkRequest class], nil];
NSArray *result = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes fromData:objectData error:&unarchiveError];
NSAssert(unarchiveError == nil, @"Error unarchiving NSArray of SBTMonitoredNetworkRequest");

return result ?: @[];
}

return @[];
Expand Down Expand Up @@ -585,8 +598,12 @@ - (id)userDefaultsObjectForKey:(NSString *)key suiteName:(NSString *)suiteName;

if (objectBase64) {
NSData *objectData = [[NSData alloc] initWithBase64EncodedString:objectBase64 options:0];


// this can't switch to the non-deprecated NSSecureCoding method because the types aren't known ahead of time
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return [NSKeyedUnarchiver unarchiveObjectWithData:objectData];
#pragma clang diagnostic pop
}

return nil;
Expand All @@ -608,7 +625,11 @@ - (BOOL)userDefaultsResetSuiteName:(NSString *)suiteName;
if (objectBase64) {
NSData *objectData = [[NSData alloc] initWithBase64EncodedString:objectBase64 options:0];

// this can't switch to the non-deprecated NSSecureCoding method because the types aren't known ahead of time
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return [NSKeyedUnarchiver unarchiveObjectWithData:objectData];
#pragma clang diagnostic pop
}

return nil;
Expand Down Expand Up @@ -642,8 +663,13 @@ - (BOOL)uploadItemAtPath:(NSString *)srcPath toPath:(NSString *)destPath relativ

if (itemsBase64) {
NSData *itemsData = [[NSData alloc] initWithBase64EncodedString:itemsBase64 options:0];

return [NSKeyedUnarchiver unarchiveObjectWithData:itemsData];

NSError *unarchiveError;
NSSet *classes = [NSSet setWithObjects:[NSArray class], [NSData class], nil];
NSArray *result = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes fromData:itemsData error:&unarchiveError];
NSAssert(unarchiveError == nil, @"Error unarchiving NSArray of NSData");

return result;
}

return nil;
Expand All @@ -660,8 +686,12 @@ - (id)performCustomCommandNamed:(NSString *)commandName object:(id)object

if (objectBase64) {
NSData *objectData = [[NSData alloc] initWithBase64EncodedString:objectBase64 options:0];


// this can't switch to the non-deprecated NSSecureCoding method because the types aren't known ahead of time
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return [NSKeyedUnarchiver unarchiveObjectWithData:objectData];
#pragma clang diagnostic pop
}

return nil;
Expand Down
8 changes: 6 additions & 2 deletions Sources/SBTUITestTunnelCommon/SBTActiveStub.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

@implementation SBTActiveStub : NSObject

+ (BOOL)supportsSecureCoding {
return YES;
}

- (instancetype)initWithMatch:(SBTRequestMatch *)match response:(SBTStubResponse *)response
{
if (self = [super init]) {
Expand All @@ -33,8 +37,8 @@ - (instancetype)initWithMatch:(SBTRequestMatch *)match response:(SBTStubResponse
- (instancetype)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
self.match = [decoder decodeObjectForKey:NSStringFromSelector(@selector(match))];
self.response = [decoder decodeObjectForKey:NSStringFromSelector(@selector(response))];
self.match = [decoder decodeObjectOfClass:[SBTRequestMatch class] forKey:NSStringFromSelector(@selector(match))];
self.response = [decoder decodeObjectOfClass:[SBTStubResponse class] forKey:NSStringFromSelector(@selector(response))];
}

return self;
Expand Down
12 changes: 8 additions & 4 deletions Sources/SBTUITestTunnelCommon/SBTMonitoredNetworkRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@

@implementation SBTMonitoredNetworkRequest : NSObject

+ (BOOL)supportsSecureCoding {
return YES;
}

- (instancetype)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
self.timestamp = [decoder decodeDoubleForKey:NSStringFromSelector(@selector(timestamp))];
self.requestTime = [decoder decodeDoubleForKey:NSStringFromSelector(@selector(requestTime))];
self.request = [decoder decodeObjectForKey:NSStringFromSelector(@selector(request))];
self.originalRequest = [decoder decodeObjectForKey:NSStringFromSelector(@selector(originalRequest))];
self.response = [decoder decodeObjectForKey:NSStringFromSelector(@selector(response))];
self.responseData = [decoder decodeObjectForKey:NSStringFromSelector(@selector(responseData))];
self.request = [decoder decodeObjectOfClass:[NSURLRequest class] forKey:NSStringFromSelector(@selector(request))];
self.originalRequest = [decoder decodeObjectOfClass:[NSURLRequest class] forKey:NSStringFromSelector(@selector(originalRequest))];
self.response = [decoder decodeObjectOfClasses:[NSSet setWithObjects:[NSHTTPURLResponse class], [NSString class], [NSURLResponse class], nil] forKey:NSStringFromSelector(@selector(response))];
self.responseData = [decoder decodeObjectOfClass:[NSData class] forKey:NSStringFromSelector(@selector(responseData))];
self.isStubbed = [decoder decodeBoolForKey:NSStringFromSelector(@selector(isStubbed))];
self.isRewritten = [decoder decodeBoolForKey:NSStringFromSelector(@selector(isRewritten))];
}
Expand Down
18 changes: 12 additions & 6 deletions Sources/SBTUITestTunnelCommon/SBTRequestMatch.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ - (BOOL)matchExpectedHeaders:(NSDictionary<NSString *, NSString *> *)expectedHea

@implementation SBTRequestMatch : NSObject

+ (BOOL)supportsSecureCoding {
return YES;
}

- (instancetype)initWithURL:(NSString *)url query:(NSArray<NSString *> *)query method:(NSString *)method body:(NSString *)body requestHeaders:(NSDictionary<NSString *,NSString *> *)requestHeaders responseHeaders:(NSDictionary<NSString *,NSString *> *)responseHeaders
{
if (self = [super init]) {
Expand All @@ -61,12 +65,14 @@ - (instancetype)initWithURL:(NSString *)url query:(NSArray<NSString *> *)query m
- (instancetype)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
self.url = [decoder decodeObjectForKey:NSStringFromSelector(@selector(url))];
self.query = [decoder decodeObjectForKey:NSStringFromSelector(@selector(query))];
self.method = [decoder decodeObjectForKey:NSStringFromSelector(@selector(method))];
self.body = [decoder decodeObjectForKey:NSStringFromSelector(@selector(body))];
self.requestHeaders = [decoder decodeObjectForKey:NSStringFromSelector(@selector(requestHeaders))];
self.responseHeaders = [decoder decodeObjectForKey:NSStringFromSelector(@selector(responseHeaders))];
self.url = [decoder decodeObjectOfClass:[NSString class] forKey:NSStringFromSelector(@selector(url))];
self.query = [decoder decodeObjectOfClasses:[NSSet setWithObjects:[NSArray class], [NSString class], nil] forKey:NSStringFromSelector(@selector(query))];
self.method = [decoder decodeObjectOfClass:[NSString class] forKey:NSStringFromSelector(@selector(method))];
self.body = [decoder decodeObjectOfClass:[NSString class] forKey:NSStringFromSelector(@selector(body))];

NSSet *dictClasses = [NSSet setWithObjects:[NSDictionary class], [NSString class], nil];
self.requestHeaders = [decoder decodeObjectOfClasses:dictClasses forKey:NSStringFromSelector(@selector(requestHeaders))];
self.responseHeaders = [decoder decodeObjectOfClasses:dictClasses forKey:NSStringFromSelector(@selector(responseHeaders))];
}

return self;
Expand Down
18 changes: 13 additions & 5 deletions Sources/SBTUITestTunnelCommon/SBTRewrite.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

@implementation SBTRewrite : NSObject

+ (BOOL)supportsSecureCoding {
return YES;
}

- (instancetype)initWithUrlReplacement:(NSArray<SBTRewriteReplacement *> *)urlReplacement
requestReplacement:(NSArray<SBTRewriteReplacement *> *)requestReplacement
responseReplacement:(NSArray<SBTRewriteReplacement *> *)responseReplacement
Expand All @@ -43,11 +47,15 @@ - (instancetype)initWithUrlReplacement:(NSArray<SBTRewriteReplacement *> *)urlRe
- (instancetype)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
self.urlReplacement = [decoder decodeObjectForKey:NSStringFromSelector(@selector(urlReplacement))];
self.requestReplacement = [decoder decodeObjectForKey:NSStringFromSelector(@selector(requestReplacement))];
self.responseReplacement = [decoder decodeObjectForKey:NSStringFromSelector(@selector(responseReplacement))];
self.requestHeadersReplacement = [decoder decodeObjectForKey:NSStringFromSelector(@selector(requestHeadersReplacement))];
self.responseHeadersReplacement = [decoder decodeObjectForKey:NSStringFromSelector(@selector(responseHeadersReplacement))];
NSSet *replacementClasses = [NSSet setWithObjects:[NSArray class], [SBTRewriteReplacement class], nil];
self.urlReplacement = [decoder decodeObjectOfClasses:replacementClasses forKey:NSStringFromSelector(@selector(urlReplacement))];
self.requestReplacement = [decoder decodeObjectOfClasses:replacementClasses forKey:NSStringFromSelector(@selector(requestReplacement))];
self.responseReplacement = [decoder decodeObjectOfClasses:replacementClasses forKey:NSStringFromSelector(@selector(responseReplacement))];

NSSet *dictClasses = [NSSet setWithObjects:[NSDictionary class], [NSString class], nil];
self.requestHeadersReplacement = [decoder decodeObjectOfClasses:dictClasses forKey:NSStringFromSelector(@selector(requestHeadersReplacement))];
self.responseHeadersReplacement = [decoder decodeObjectOfClasses:dictClasses forKey:NSStringFromSelector(@selector(responseHeadersReplacement))];

self.responseStatusCode = [decoder decodeIntForKey:NSStringFromSelector(@selector(responseStatusCode))];
self.activeIterations = [decoder decodeIntForKey:NSStringFromSelector(@selector(activeIterations))];
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/SBTUITestTunnelCommon/SBTRewriteReplacement.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ @interface SBTRewriteReplacement ()

@implementation SBTRewriteReplacement : NSObject

+ (BOOL)supportsSecureCoding {
return YES;
}

- (instancetype)initWithFind:(NSString *)find replace:(NSString *)replace
{
if (self = [super init]) {
Expand All @@ -38,8 +42,8 @@ - (instancetype)initWithFind:(NSString *)find replace:(NSString *)replace
- (instancetype)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
self.findData = [decoder decodeObjectForKey:NSStringFromSelector(@selector(findData))];
self.replaceData = [decoder decodeObjectForKey:NSStringFromSelector(@selector(replaceData))];
self.findData = [decoder decodeObjectOfClass:[NSData class] forKey:NSStringFromSelector(@selector(findData))];
self.replaceData = [decoder decodeObjectOfClass:[NSData class] forKey:NSStringFromSelector(@selector(replaceData))];
}

return self;
Expand Down
4 changes: 4 additions & 0 deletions Sources/SBTUITestTunnelCommon/SBTStubFailureResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

@implementation SBTStubFailureResponse

+ (BOOL)supportsSecureCoding {
return YES;
}

- (nonnull instancetype)initWithFailureCode:(NSInteger)failureCode
responseTime:(NSTimeInterval)responseTime
activeIterations:(NSInteger)activeIterations;
Expand Down
13 changes: 10 additions & 3 deletions Sources/SBTUITestTunnelCommon/SBTStubResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ @implementation SBTStubResponse : NSObject

static SBTResponseDefaults *_defaults;

+ (BOOL)supportsSecureCoding {
return YES;
}

- (instancetype)initWithResponse:(NSObject *)response
headers:(NSDictionary<NSString *, NSString *> *)headers
contentType:(NSString *)contentType
Expand Down Expand Up @@ -166,9 +170,12 @@ - (instancetype)initWithFileNamed:(NSString *)fileNamed
- (instancetype)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
self.data = [decoder decodeObjectForKey:NSStringFromSelector(@selector(data))];
self.contentType = [decoder decodeObjectForKey:NSStringFromSelector(@selector(contentType))];
self.headers = [decoder decodeObjectForKey:NSStringFromSelector(@selector(headers))];
self.data = [decoder decodeObjectOfClass:[NSData class] forKey:NSStringFromSelector(@selector(data))];
self.contentType = [decoder decodeObjectOfClass:[NSString class] forKey:NSStringFromSelector(@selector(contentType))];

NSSet *dictClasses = [NSSet setWithObjects:[NSDictionary class], [NSString class], nil];
self.headers = [decoder decodeObjectOfClasses:dictClasses forKey:NSStringFromSelector(@selector(headers))];

self.returnCode = [decoder decodeIntegerForKey:NSStringFromSelector(@selector(returnCode))];
self.responseTime = [decoder decodeDoubleForKey:NSStringFromSelector(@selector(responseTime))];
self.activeIterations = [decoder decodeIntegerForKey:NSStringFromSelector(@selector(activeIterations))];
Expand Down
2 changes: 1 addition & 1 deletion Sources/SBTUITestTunnelCommon/include/SBTActiveStub.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@class SBTRequestMatch, SBTStubResponse;

@interface SBTActiveStub: NSObject<NSCoding, NSCopying>
@interface SBTActiveStub: NSObject<NSSecureCoding, NSCopying>

/// The request match
@property (nonnull, nonatomic, strong) SBTRequestMatch *match;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@class SBTRequestMatch;

@interface SBTMonitoredNetworkRequest : NSObject<NSCoding>
@interface SBTMonitoredNetworkRequest : NSObject<NSSecureCoding>

- (nullable NSString *)responseString;
- (nullable id)responseJSON;
Expand Down
2 changes: 1 addition & 1 deletion Sources/SBTUITestTunnelCommon/include/SBTRequestMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@import Foundation;

@interface SBTRequestMatch: NSObject<NSCoding, NSCopying>
@interface SBTRequestMatch: NSObject<NSSecureCoding, NSCopying>

/// A regex that is matched against the request url
@property (nullable, nonatomic, strong) NSString *url;
Expand Down
2 changes: 1 addition & 1 deletion Sources/SBTUITestTunnelCommon/include/SBTRewrite.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@class SBTRewriteReplacement;

@interface SBTRewrite: NSObject<NSCoding>
@interface SBTRewrite: NSObject<NSSecureCoding>

@property (nonnull, nonatomic, strong) NSArray<SBTRewriteReplacement *> *urlReplacement;
@property (nonnull, nonatomic, strong) NSArray<SBTRewriteReplacement *> *requestReplacement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@import Foundation;

@interface SBTRewriteReplacement: NSObject<NSCoding, NSCopying>
@interface SBTRewriteReplacement: NSObject<NSSecureCoding, NSCopying>

/**
* Initializer
Expand Down
2 changes: 1 addition & 1 deletion Sources/SBTUITestTunnelCommon/include/SBTStubResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@import Foundation;

@interface SBTStubResponse: NSObject<NSCoding, NSCopying>
@interface SBTStubResponse: NSObject<NSSecureCoding, NSCopying>

/// Set the default response time for all SBTStubResponses when not specified in intializer. If positive, the amount of time used to send the entire response. If negative, the rate in KB/s at which to send the response data. Use SBTUITunnelStubsDownloadSpeed* constants
@property (class, nonatomic, assign) NSTimeInterval defaultResponseTime;
Expand Down
Loading

0 comments on commit 4262ed2

Please sign in to comment.