Skip to content

Commit 0b047b6

Browse files
authored
fix(iOS): Resolve crash when using WebKit CookieManager.get with missing URL protocol (#13)
* Resolve crash for invalid URLs * Update error message
1 parent de556c1 commit 0b047b6

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

ios/RNCookieManagerIOS/RNCookieManagerIOS.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#endif
1414

1515
static NSString * const NOT_AVAILABLE_ERROR_MESSAGE = @"WebKit/WebKit-Components are only available with iOS11 and higher!";
16+
static NSString * const INVALID_URL_MISSING_HTTP = @"Invalid URL: It may be missing a protocol (ex. http:// or https://).";
1617

1718
@implementation RNCookieManagerIOS
1819

@@ -109,6 +110,11 @@ -(NSString *)getDomainName:(NSURL *) url
109110
NSInteger maxLength = 2;
110111

111112
NSURLComponents *components = [[NSURLComponents alloc]initWithURL:url resolvingAgainstBaseURL:FALSE];
113+
114+
if ([components.host isEqual: @""] || components.host == nil) {
115+
return nil;
116+
}
117+
112118
NSArray<NSString *> *separatedHost = [components.host componentsSeparatedByString:separator];
113119
NSInteger count = [separatedHost count];
114120
NSInteger endPosition = count;
@@ -133,6 +139,11 @@ -(NSString *)getDomainName:(NSURL *) url
133139
dispatch_async(dispatch_get_main_queue(), ^(){
134140
NSString *topLevelDomain = [self getDomainName:url];
135141

142+
if (topLevelDomain == nil) {
143+
reject(@"", INVALID_URL_MISSING_HTTP, nil);
144+
return;
145+
}
146+
136147
WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore];
137148
[cookieStore getAllCookies:^(NSArray<NSHTTPCookie *> *allCookies) {
138149
NSMutableDictionary *cookies = [NSMutableDictionary dictionary];

0 commit comments

Comments
 (0)