-
Notifications
You must be signed in to change notification settings - Fork 1.8k
bound array access in StoragePath url parsers #16243
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,14 +84,39 @@ class StoragePathTests: XCTestCase { | |
| XCTAssertEqual(path.object, "#hashtag/no/token") | ||
| } | ||
|
|
||
| func testHTTPURLObjectMarkerWithoutObject() throws { | ||
| // "/o" object marker present but no object path; must not index past the | ||
| // path components. | ||
| let httpURL = "http://firebasestorage.googleapis.com/v0/b/bucket/o" | ||
| let path = try StoragePath.path(string: httpURL) | ||
| XCTAssertEqual(path.bucket, "bucket") | ||
| XCTAssertNil(path.object) | ||
| } | ||
|
|
||
| func testGSURIThrowsOnNoBucket() { | ||
| XCTAssertThrowsError(try StoragePath.path(string: "gs://")) | ||
| } | ||
|
|
||
| func testGSURIThrowsOnOnlySlashes() { | ||
| XCTAssertThrowsError(try StoragePath.path(string: "gs:///")) | ||
| } | ||
|
|
||
| func testHTTPURLThrowsOnNoBucket() { | ||
| XCTAssertThrowsError(try StoragePath.path(string: "http://firebasestorage.googleapis.com/")) | ||
| } | ||
|
|
||
| func testHTTPURLThrowsOnMissingObjectMarker() { | ||
| // A path segment in the "/o" marker position that isn't "o" is not a valid | ||
| // Storage URL and must throw rather than be parsed as an object. | ||
| let httpURL = "http://firebasestorage.googleapis.com/v0/b/bucket/x/path/to/object" | ||
| XCTAssertThrowsError(try StoragePath.path(string: httpURL)) | ||
|
|
||
| // Same invalid marker but with no object path (exactly five components) must | ||
| // also throw rather than be parsed as a bucket-only path. | ||
| let shortHTTPURL = "http://firebasestorage.googleapis.com/v0/b/bucket/x" | ||
| XCTAssertThrowsError(try StoragePath.path(string: shortHTTPURL)) | ||
| } | ||
|
Comment on lines
+108
to
+118
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To prevent regressions, it would be beneficial to also test the case where the URL has exactly 5 path components but the 5th component is not func testHTTPURLThrowsOnMissingObjectMarker() {
// A path segment in the "/o" marker position that isn't "o" is not a valid
// Storage URL and must throw rather than be parsed as an object.
let httpURL = "http://firebasestorage.googleapis.com/v0/b/bucket/x/path/to/object"
XCTAssertThrowsError(try StoragePath.path(string: httpURL))
let shortHttpURL = "http://firebasestorage.googleapis.com/v0/b/bucket/x"
XCTAssertThrowsError(try StoragePath.path(string: shortHttpURL))
}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the five-component case ( |
||
|
|
||
| func testThrowsOnInvalidScheme() { | ||
| let ftpURL = "ftp://firebasestorage.googleapis.com/v0/b/bucket/o/path/to/object" | ||
| XCTAssertThrowsError(try StoragePath.path(string: ftpURL)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of checking
splitStringArray.count == 2and accessing the indexsplitStringArray[1], you can usesplitStringArray.dropFirst().firstto safely and idiomatically retrieve the optional object path. This is cleaner and avoids hardcoded index checks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, switched to
splitStringArray.dropFirst().first. Drops thecount == 2check and the hardcoded[1]index.