Skip to content

Commit f7dea6e

Browse files
authored
Fix a race condition where a download size could exceed maxSize (#10412)
1 parent 7de1b3e commit f7dea6e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

FirebaseStorage/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 10.2.0
2+
- [fixed] Fix a race condition where a download size could exceed the value of the `maxSize` parameter. (#10358)
3+
14
# 10.1.0
25
- [fixed] Fixed a 10.0.0 regression where metadata passed to `putFile` was not properly initialized. (#10353)
36
- [fixed] Fixed a 10.0.0 regression handling an empty JSON metadata field from the emulator. (#10370)

FirebaseStorage/Sources/StorageReference.swift

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,12 @@ import Foundation
275275
let callbackQueue = fetcherService.callbackQueue ?? DispatchQueue.main
276276

277277
task.observe(.success) { snapshot in
278+
let error = self.checkSizeOverflow(task: snapshot.task, maxSize: maxSize)
278279
callbackQueue.async {
279280
if !completed {
280281
completed = true
281-
completion(task.downloadData, nil)
282+
let data = error == nil ? task.downloadData : nil
283+
completion(data, error)
282284
}
283285
}
284286
}
@@ -291,14 +293,8 @@ import Foundation
291293
}
292294
}
293295
task.observe(.progress) { snapshot in
294-
let task = snapshot.task
295-
if task.progress.totalUnitCount > maxSize || task.progress.completedUnitCount > maxSize {
296-
let error = StorageErrorCode.error(withCode: .downloadSizeExceeded,
297-
infoDictionary: [
298-
"totalSize": task.progress.totalUnitCount,
299-
"maxAllowedSize": maxSize,
300-
])
301-
(task as? StorageDownloadTask)?.cancel(withError: error)
296+
if let error = self.checkSizeOverflow(task: snapshot.task, maxSize: maxSize) {
297+
task.cancel(withError: error)
302298
}
303299
}
304300
task.enqueue()
@@ -695,4 +691,18 @@ import Foundation
695691
self.storage = storage
696692
self.path = path
697693
}
694+
695+
/**
696+
* For maxSize API, return an error if the size is exceeded.
697+
*/
698+
private func checkSizeOverflow(task: StorageTask, maxSize: Int64) -> NSError? {
699+
if task.progress.totalUnitCount > maxSize || task.progress.completedUnitCount > maxSize {
700+
return StorageErrorCode.error(withCode: .downloadSizeExceeded,
701+
infoDictionary: [
702+
"totalSize": task.progress.totalUnitCount,
703+
"maxAllowedSize": maxSize,
704+
])
705+
}
706+
return nil
707+
}
698708
}

0 commit comments

Comments
 (0)