From 0d5d2a923b45c81f3830a60b8e8a45b64827e628 Mon Sep 17 00:00:00 2001 From: Jason Wray Date: Fri, 13 May 2016 21:46:13 -0400 Subject: [PATCH] [ios] Improve telemetry server URL validation (#5022) - Validate that test server URL actually has a reasonable value. - Refactor `baseURL` and `testServerURL` to use NSURL. --- platform/ios/src/MGLAPIClient.m | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/platform/ios/src/MGLAPIClient.m b/platform/ios/src/MGLAPIClient.m index 86f57e4e8..2a224c558 100644 --- a/platform/ios/src/MGLAPIClient.m +++ b/platform/ios/src/MGLAPIClient.m @@ -14,7 +14,7 @@ static NSString * const MGLAPIClientHTTPMethodPost = @"POST"; @interface MGLAPIClient () @property (nonatomic, copy) NSURLSession *session; -@property (nonatomic, copy) NSString *baseURL; +@property (nonatomic, copy) NSURL *baseURL; @property (nonatomic, copy) NSData *digicertCert; @property (nonatomic, copy) NSData *geoTrustCert; @property (nonatomic, copy) NSData *testServerCert; @@ -76,8 +76,9 @@ static NSString * const MGLAPIClientHTTPMethodPost = @"POST"; #pragma mark Utilities - (NSURLRequest *)requestForEvents:(NS_ARRAY_OF(MGLMapboxEventAttributes *) *)events { - NSString *url = [NSString stringWithFormat:@"%@/%@?access_token=%@", self.baseURL, MGLAPIClientEventsPath, [MGLAccountManager accessToken]]; - NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; + NSString *path = [NSString stringWithFormat:@"%@?access_token=%@", MGLAPIClientEventsPath, [MGLAccountManager accessToken]]; + NSURL *url = [NSURL URLWithString:path relativeToURL:self.baseURL]; + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setValue:self.userAgent forHTTPHeaderField:MGLAPIClientHeaderFieldUserAgentKey]; [request setValue:MGLAPIClientHeaderFieldContentTypeValue forHTTPHeaderField:MGLAPIClientHeaderFieldContentTypeKey]; [request setHTTPMethod:MGLAPIClientHTTPMethodPost]; @@ -87,12 +88,13 @@ static NSString * const MGLAPIClientHTTPMethodPost = @"POST"; } - (void)setupBaseURL { - NSString *testServerURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"MGLTelemetryTestServerURL"]; - if (testServerURL) { - _baseURL = testServerURL; - _usesTestServer = YES; + NSString *testServerURLString = [[NSUserDefaults standardUserDefaults] stringForKey:@"MGLTelemetryTestServerURL"]; + NSURL *testServerURL = [NSURL URLWithString:testServerURLString]; + if (testServerURL && [testServerURL.scheme isEqualToString:@"https"]) { + self.baseURL = testServerURL; + self.usesTestServer = YES; } else { - _baseURL = MGLAPIClientBaseURL; + self.baseURL = [NSURL URLWithString:MGLAPIClientBaseURL]; } }