Skip to content

Commit

Permalink
modified OoniRunViewController#validateLink to use updated code tha…
Browse files Browse the repository at this point in the history
…t allows a better validation
  • Loading branch information
aanorbel committed Jul 28, 2023
1 parent 8e59ca2 commit 5a0e413
Showing 1 changed file with 35 additions and 10 deletions.
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) {
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;
}
// If all checks pass, the URL is valid
return YES;
}

#pragma mark - Table view data source
Expand Down

0 comments on commit 5a0e413

Please sign in to comment.