Skip to content
Open
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
29 changes: 20 additions & 9 deletions Mastonaut/Extensions/NSURL+Sanitization.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ + (NSRegularExpression *)urlComponentsRegex
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

regex = [NSRegularExpression regularExpressionWithPattern:@"(?<protocol>\\w+)(?<slashes>://)((?<user>\\w+)"
"(:(?<password>\\w+))?@)?(?<host>[^:/]+)((?<colon>:)"
"(?<port>\\d+))?(?<path>/[^#?]*)?(?<query>\\?"
regex = [NSRegularExpression regularExpressionWithPattern:@"(?<protocol>\\w+)(?<slashes>://)((?<user>\\w+)?"
"((?<userColon>:)(?<password>\\w+))?(?<at>@))?"
"(?<host>[^:/]+)((?<portColon>:)(?<port>\\d+))?"
"(?<path>/[^#?]*)?(?<query>\\?"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the @ symbol is on its own (which is allowed by this regex), it is included in the host:
image

Is this intended?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ricardoboss ricardoboss Oct 18, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (?<host>[^:\/]+) implicitly allows any characters. Maybe this should be constrained to alphanumerics (i.e. (?<host>[a-zA-Z0-9-.]+))?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this doesn’t matter, because the host is later on (in the loop) restricted to [NSCharacterSet URLHostAllowedCharacterSet].

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link to test

I've made the user group optional in the regex, and if not set, 8e32b64 will now remove the @ altogether. Added another test for this.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

you may want to make group 3 a non-capturing group

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. this one
image

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used this one in one of my projects, in case you want to look at another approach: https://regex101.com/r/bsN8uj/5

"[^#]+)?((?<octothorpe>#)(?<fragment>.+))?"
options:0
error:nil];
Expand Down Expand Up @@ -90,12 +91,16 @@ + (nullable NSURL *)urlBySanitizingAddress:(nonnull NSString *)address

if (match == nil) { return nil; }

NSArray *groups = @[@"protocol", @"slashes", @"user",
@"password", @"host", @"colon",
@"port", @"path", @"query",
NSArray *groups = @[@"protocol", @"slashes",
@"user", @"userColon", @"password", @"at",
@"host", @"portColon", @"port",
@"path",
@"query",
@"octothorpe", @"fragment"];

NSMutableString *sanitizedAddress = [[NSMutableString alloc] initWithCapacity:[cleanString length]];

bool hasUser = false;

for (NSString *captureGroup in groups)
{
Expand All @@ -105,16 +110,22 @@ + (nullable NSURL *)urlBySanitizingAddress:(nonnull NSString *)address
NSString *substring = [cleanString substringWithRange:captureGroupRange];

NSCharacterSet *allowedCharset = [[self captureGroupCharsetMap] objectForKey:captureGroup];

if (allowedCharset == nil)
{
[sanitizedAddress appendString:substring];
if ([captureGroup isEqualToString: @"at"] && !hasUser)
continue;

[sanitizedAddress appendString:substring];
}
else
{
NSString *escapedString = [substring stringByAddingPercentEncodingWithAllowedCharacters:allowedCharset];
[sanitizedAddress appendString:escapedString];
}

if ([captureGroup isEqualToString: @"user"] && escapedString.length > 0)
hasUser = true;
}
}

return [NSURL URLWithString:sanitizedAddress];
Expand Down
8 changes: 6 additions & 2 deletions MastonautTests/URLSanitizerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ class URLSanitizerTests: XCTestCase {
("https://www.dw.com/en/germany-settle-for-a-draw-despite-second-half-leroy-sané-show/a-47993172?maca=en-rss-en-all-1573-rdf", "https://www.dw.com/en/germany-settle-for-a-draw-despite-second-half-leroy-san%C3%A9-show/a-47993172?maca=en-rss-en-all-1573-rdf"),
("https://www.apple.com:443", nil),
("https://web.archive.org/web/20190213201804/http://shirky.com/writings/situated_software.html", nil),
("https://mjtsai.com/blog/2023/06/06/xcode-15-announced/#xcode-15-announced-update-2023-08-23", nil)
("https://mjtsai.com/blog/2023/06/06/xcode-15-announced/#xcode-15-announced-update-2023-08-23", nil),
("https://example.com/wiki/2.0.8+73", nil),
("http://myUser@example.com/hello", nil),
("http://myUser:securePassword@example.com/hello", nil),
("http://@example.com/hello", "http://example.com/hello"),
]

func testInputDoesNotGetMangled() {
data.forEach {
input, expectedOutput in

// if expectedOutput is set, we expect a changed output (e.g. URL-encoding);
// otherwise, treat it as same as input
// if `nil`, treat it as same as input

XCTAssertEqual(expectedOutput ?? input, NSURL(bySanitizingAddress: input)?.absoluteString)
}
Expand Down