Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update CDVAllowList to support more valid schemes #1473

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ - (void)addAllowListEntry:(NSString*)origin
self.allowList = nil;
self.permittedSchemes = nil;
} else { // specific access
NSRegularExpression* parts = [NSRegularExpression regularExpressionWithPattern:@"^((\\*|[A-Za-z-]+):/?/?)?(((\\*\\.)?[^*/:]+)|\\*)?(:(\\d+))?(/.*)?" options:0 error:nil];
NSRegularExpression* parts = [NSRegularExpression regularExpressionWithPattern:@"^((\\*|([a-z][a-z0-9+\\-.]*)):/?/?)?(((\\*\\.)?[^*/:]+)|\\*)?(:(\\d+))?(/.*)?" options:0 error:nil];
NSTextCheckingResult* m = [parts firstMatchInString:origin options:NSMatchingAnchored range:NSMakeRange(0, [origin length])];
if (m != nil) {
NSRange r;
Expand All @@ -180,7 +180,7 @@ - (void)addAllowListEntry:(NSString*)origin
}

NSString* host = nil;
r = [m rangeAtIndex:3];
r = [m rangeAtIndex:4];
if (r.location != NSNotFound) {
host = [origin substringWithRange:r];
}
Expand All @@ -191,13 +191,13 @@ - (void)addAllowListEntry:(NSString*)origin
}

NSString* port = nil;
r = [m rangeAtIndex:7];
r = [m rangeAtIndex:8];
if (r.location != NSNotFound) {
port = [origin substringWithRange:r];
}

NSString* path = nil;
r = [m rangeAtIndex:8];
r = [m rangeAtIndex:9];
if (r.location != NSNotFound) {
path = [origin substringWithRange:r];
}
Expand Down
15 changes: 15 additions & 0 deletions tests/CordovaLibTests/CDVAllowListTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,21 @@ - (void)testAllowListRejectionString
XCTAssertTrue([expectedErrorString isEqualToString:errorString], @"Customized allowList rejection string has unexpected value.");
}

- (void)testUnusualSchemes
{
NSArray* allowedHosts = [NSArray arrayWithObjects:
@"com.myapp://*",
@"web+app://*",
@"a12345://*",
nil];

CDVAllowList* allowList = [[CDVAllowList alloc] initWithArray:allowedHosts];

XCTAssertTrue([allowList URLIsAllowed:[NSURL URLWithString:@"com.myapp://www.apache.org"]]);
XCTAssertTrue([allowList URLIsAllowed:[NSURL URLWithString:@"web+app://www.apache.org"]]);
XCTAssertTrue([allowList URLIsAllowed:[NSURL URLWithString:@"a12345://www.apache.org"]]);
}

- (void)testSpecificProtocol
{
NSArray* allowedHosts = [NSArray arrayWithObjects:
Expand Down