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: invalid handling of OONI Run v1 link with partial URL #528

Merged
merged 3 commits into from
Jul 31, 2023
Merged
Changes from 1 commit
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
45 changes: 35 additions & 10 deletions ooniprobe/View/OONIRun/OoniRunViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ - (void)showTestScreen{
-(void)validateAndAddURLs{
for (NSString *url in [testArguments objectForKey:@"urls"]){
@try {
if ([url length] < 2083 && [self validateLink:url]){
if ([self validateLink:url]){
[Url checkExistingUrl:url];
[urls addObject:url];
}
Expand All @@ -190,16 +190,41 @@ -(void)validateAndAddURLs{
}
}

/*
This regex tests whether the URL starts with http:// or https://,
then checks for at least 1 character, then checks for a dot, and then again checks for at least 1 character.
No spaces allowed.
/**
* Checks if the input string is a valid URL with a maximum length of 2083 characters and enforces that the scheme is only "http" or "https".
*
* @param urlString The input string to check if it is a valid URL.
* @return YES if the input string is a valid URL; otherwise, NO.
*/
- (BOOL)validateLink:(NSString *)link
{
NSString *regex = @"(?i)(http|https)(:\\/\\/)([^ .]+)(\\.)([^ \n]+)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [predicate evaluateWithObject:link];
- (BOOL)validateLink:(NSString *)urlString {
// Check if the length of the input string is greater than 2083 characters; if so, the URL is not valid
if (urlString.length > 2083) {
aanorbel marked this conversation as resolved.
Show resolved Hide resolved
return NO;
}
// Create an NSURLComponents object from the input string
NSURLComponents *components = [NSURLComponents componentsWithString:urlString];
// Check if the NSURLComponents object is nil, indicating that the string is not a valid URL
if (components == nil) {
return NO;
}
// Get the scheme from the NSURLComponents object and convert it to lowercase
NSString *scheme = components.scheme.lowercaseString;
// Check if the scheme is "http" or "https"; if not, the URL is not valid
if (![scheme isEqualToString:@"http"] && ![scheme isEqualToString:@"https"]) {
return NO;
}
// Check if the host property of the NSURLComponents object is nil; if so, the URL is not valid
if (components.host == nil) {
return NO;
}
// Convert the NSURLComponents object to an NSURL object
NSURL *URL = components.URL;
// Check if the NSURL object is nil, indicating that the URL is not valid
if (URL == nil) {
return NO;
}
aanorbel marked this conversation as resolved.
Show resolved Hide resolved
// If all checks pass, the URL is valid
return YES;
}

#pragma mark - Table view data source
Expand Down