Skip to content

Commit e336955

Browse files
authored
Fix URL handling bug when path is a substring of host (#8880)
1 parent 66414aa commit e336955

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

FirebaseDatabase/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# v8.10.0
2+
- [fixed] Fixed URL handling bug when path is a substring of host. (#8874)
3+
14
# v8.7.0
25
- [fixed] Fixed Firebase App Check token periodic refresh. (#8544)
36

FirebaseDatabase/Sources/Utilities/FUtilities.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,13 @@ + (FParsedUrl *)parseUrl:(NSString *)url {
154154

155155
// Sanitize the database URL by removing the path component, which may
156156
// contain invalid URL characters.
157+
NSRange lastMatch = [url rangeOfString:originalPathString
158+
options:NSBackwardsSearch];
157159
NSString *sanitizedUrlWithoutPath =
158-
[url stringByReplacingOccurrencesOfString:originalPathString
159-
withString:@""];
160+
(lastMatch.location != NSNotFound)
161+
? [url substringToIndex:lastMatch.location]
162+
: url;
163+
160164
NSURLComponents *urlComponents =
161165
[NSURLComponents componentsWithString:sanitizedUrlWithoutPath];
162166
if (!urlComponents) {

FirebaseDatabase/Tests/Unit/FUtilitiesTest.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,30 @@ - (void)testUrlParsedWithSslDetection {
8484
XCTAssertTrue(parsedUrl.repoInfo.secure);
8585
}
8686

87+
- (void)testUrlParsedWithPathPartOfHost {
88+
FParsedUrl *parsedUrl = [FUtilities parseUrl:@"https://sample.firebaseio.com/a"];
89+
XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"sample.firebaseio.com");
90+
XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"sample");
91+
XCTAssertTrue(parsedUrl.repoInfo.secure);
92+
XCTAssertEqualObjects(parsedUrl.path, [FPath pathWithString:@"a"]);
93+
}
94+
95+
- (void)testUrlParsedWithPathPartOfHost2 {
96+
FParsedUrl *parsedUrl = [FUtilities parseUrl:@"https://sample.firebaseio.com/"];
97+
XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"sample.firebaseio.com");
98+
XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"sample");
99+
XCTAssertTrue(parsedUrl.repoInfo.secure);
100+
XCTAssertEqualObjects(parsedUrl.path, [FPath pathWithString:@""]);
101+
}
102+
103+
- (void)testUrlParsedWithPathPartOfHost3 {
104+
FParsedUrl *parsedUrl = [FUtilities parseUrl:@"https://sample.firebaseio.com"];
105+
XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"sample.firebaseio.com");
106+
XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"sample");
107+
XCTAssertTrue(parsedUrl.repoInfo.secure);
108+
XCTAssertEqualObjects(parsedUrl.path, [FPath pathWithString:@""]);
109+
}
110+
87111
- (void)testDefaultCacheSizeIs10MB {
88112
XCTAssertEqual([FTestHelpers defaultConfig].persistenceCacheSizeBytes,
89113
(NSUInteger)10 * 1024 * 1024);

0 commit comments

Comments
 (0)