From d95b63895ea1323e727d74be2fcf717244a10421 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 31 Jul 2020 15:50:14 +1000 Subject: [PATCH] Update file size limit for onion requests --- SignalServiceKit/src/Loki/API/DotNetAPI.swift | 2 +- SignalServiceKit/src/Loki/API/FileServerAPI.swift | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/DotNetAPI.swift b/SignalServiceKit/src/Loki/API/DotNetAPI.swift index a75923c3c..4671a7b05 100644 --- a/SignalServiceKit/src/Loki/API/DotNetAPI.swift +++ b/SignalServiceKit/src/Loki/API/DotNetAPI.swift @@ -135,7 +135,7 @@ public class DotNetAPI : NSObject { } // Check the file size if needed print("[Loki] File size: \(data.count)") - if data.count > FileServerAPI.maxFileSize { + if Double(data.count) > Double(FileServerAPI.maxFileSize) / FileServerAPI.fileSizeORMultiplier { return seal.reject(DotNetAPIError.maxFileSizeExceeded) } // Create the request diff --git a/SignalServiceKit/src/Loki/API/FileServerAPI.swift b/SignalServiceKit/src/Loki/API/FileServerAPI.swift index 288ee5712..73a378acd 100644 --- a/SignalServiceKit/src/Loki/API/FileServerAPI.swift +++ b/SignalServiceKit/src/Loki/API/FileServerAPI.swift @@ -7,11 +7,17 @@ public final class FileServerAPI : DotNetAPI { private static let attachmentType = "net.app.core.oembed" private static let deviceLinkType = "network.loki.messenger.devicemapping" - internal static let fileServerPublicKey = "62509D59BDEEC404DD0D489C1E15BA8F94FD3D619B01C1BF48A9922BFCB7311C" + internal static let fileServerPublicKey = "2662315C4E728FDBDEC61F69ECA2316BFF267AA8931197907A1C944C7C4E667A" public static let maxFileSize = 10_000_000 // 10 MB + /// The file server has a file size limit of `maxFileSize`, which the Service Nodes try to enforce as well. However, the limit applied by the Service Nodes + /// is on the **HTTP request** and not the file size. Because of onion request encryption, a file that's about 4 MB will result in a request that's about 15 MB. + /// On average the multiplier appears to be about 3.4, so when checking whether the file will exceed the file size limit when uploading a file we just divide + /// the size of the file by this number. The alternative would be to actually check the size of the HTTP request but that's only possible after proof of work + /// has been calculated and the onion request encryption has happened, which takes several seconds. + public static let fileSizeORMultiplier = 3.4 - @objc public static let server = "https://file.getsession.org" + @objc public static let server = "https://file-dev.getsession.org" // MARK: Storage override internal class var authTokenCollection: String { return "LokiStorageAuthTokenCollection" } @@ -153,7 +159,7 @@ public final class FileServerAPI : DotNetAPI { } public static func uploadProfilePicture(_ profilePicture: Data) -> Promise { - guard profilePicture.count < maxFileSize else { return Promise(error: DotNetAPIError.maxFileSizeExceeded) } + guard Double(profilePicture.count) < Double(maxFileSize) / fileSizeORMultiplier else { return Promise(error: DotNetAPIError.maxFileSizeExceeded) } let url = "\(server)/files" let parameters: JSON = [ "type" : attachmentType, "Content-Type" : "application/binary" ] var error: NSError?