diff --git a/Tuist/Package.resolved b/Tuist/Package.resolved index 4e71d8fb27..ce1f83d644 100644 --- a/Tuist/Package.resolved +++ b/Tuist/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "be2917d078b211555241082ce1e100ca53701c019733595f440fc3b514cf4a92", + "originHash" : "7048349745621ad4c418040ff7c14724e324bfc04f8177c50576b2b87d9b9b54", "pins" : [ { "identity" : "alamofire", @@ -69,8 +69,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/Infomaniak/ios-core", "state" : { - "revision" : "0430ec9ee85e55b6d5db63b7f2cbcb66a9d2b2ca", - "version" : "19.1.3" + "revision" : "e667ed34941d4e06f0107d9633e2cc9438685fef", + "version" : "19.1.4" } }, { diff --git a/Tuist/Package.swift b/Tuist/Package.swift index a18adfac35..5e7eae297b 100644 --- a/Tuist/Package.swift +++ b/Tuist/Package.swift @@ -61,7 +61,7 @@ let package = Package( .package(url: "https://github.com/flowbe/SwiftRegex", .upToNextMajor(from: "1.0.0")), .package(url: "https://github.com/getsentry/sentry-cocoa", .upToNextMajor(from: "9.8.0")), .package(url: "https://github.com/Infomaniak/ios-bug-tracker", .upToNextMajor(from: "19.0.0")), - .package(url: "https://github.com/Infomaniak/ios-core", .upToNextMajor(from: "19.0.0")), + .package(url: "https://github.com/Infomaniak/ios-core", .upToNextMajor(from: "19.1.4")), .package(url: "https://github.com/Infomaniak/ios-core-ui", .upToNextMajor(from: "26.0.0")), .package(url: "https://github.com/Infomaniak/ios-core-uikit", .upToNextMajor(from: "2.0.1")), .package(url: "https://github.com/Infomaniak/ios-dependency-injection", .upToNextMajor(from: "2.0.5")), diff --git a/kDriveCore/DI/CoreTargetAssembly.swift b/kDriveCore/DI/CoreTargetAssembly.swift index e43d691495..f6a30f0c51 100644 --- a/kDriveCore/DI/CoreTargetAssembly.swift +++ b/kDriveCore/DI/CoreTargetAssembly.swift @@ -108,7 +108,11 @@ open class CoreTargetAssembly: TargetAssembly { DeeplinkService() }, Factory(type: DeeplinkParsable.self) { _, _ in - DeeplinkParser() + DeeplinkParser( + sharedContainerURL: FileManager.default.containerURL( + forSecurityApplicationGroupIdentifier: KDriveFileSharingConstants.appGroupIdentifier + ) + ) }, Factory(type: PublicShareApiFetcher.self) { _, _ in PublicShareApiFetcher() diff --git a/kDriveCore/Utils/DeeplinkParser.swift b/kDriveCore/Utils/DeeplinkParser.swift index 311985c508..132879b564 100644 --- a/kDriveCore/Utils/DeeplinkParser.swift +++ b/kDriveCore/Utils/DeeplinkParser.swift @@ -16,6 +16,7 @@ along with this program. If not, see . */ +import InfomaniakCore import InfomaniakCoreCommonUI import InfomaniakDI import MatomoTracker @@ -39,8 +40,10 @@ public struct DeeplinkParser: DeeplinkParsable { @LazyInjectService var accountManager: AccountManageable @LazyInjectService var router: AppNavigable - public init() { - // META: keep SonarCloud happy + private let sharedContainerURL: URL? + + init(sharedContainerURL: URL?) { + self.sharedContainerURL = sharedContainerURL } private func handleDeeplink(url: URL) async -> Bool { @@ -93,8 +96,21 @@ public struct DeeplinkParser: DeeplinkParsable { } public func parse(url: URL) async -> Bool { - guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true), - let params = components.queryItems else { + guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else { + return await handleDeeplink(url: url) + } + + if components.scheme == KDriveFileSharingConstants.scheme, components.host == KDriveFileSharingConstants.host { + guard let files = filesForSharingDeeplink(url) else { + Log.sceneDelegate("Failed to import files: Invalid request", level: .error) + return false + } + await router.navigate(to: .saveFiles(files: files)) + matomo.track(eventWithCategory: .deeplink, name: DeeplinkPath.file.rawValue) + return true + } + + guard let params = components.queryItems else { return await handleDeeplink(url: url) } @@ -110,24 +126,83 @@ public struct DeeplinkParser: DeeplinkParsable { await router.navigate(to: .store(driveId: driveIdInt, userId: userIdInt)) matomo.track(eventWithCategory: .deeplink, name: DeeplinkPath.store.rawValue) return true + } + + Log.sceneDelegate("unable to parse deeplink URL: \(url)", level: .error) + return false + } - } else if components.host == DeeplinkPath.file.rawValue { - let files: [ImportedFile] = params.compactMap { param in - guard param.name == "url", let filePath = param.value else { return nil } - let fileUrl = URL(fileURLWithPath: filePath) + func filesForSharingDeeplink(_ url: URL) -> [ImportedFile]? { + guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false), + components.scheme == KDriveFileSharingConstants.scheme, + components.host == KDriveFileSharingConstants.host, + components.path.isEmpty, + components.user == nil, + components.password == nil, + components.port == nil, + components.fragment == nil, + let queryItems = components.queryItems, + !queryItems.isEmpty, + queryItems.count <= KDriveFileSharingConstants.maximumFileCount, + queryItems.allSatisfy({ + $0.name == KDriveFileSharingConstants.urlQueryItemName && !($0.value?.isEmpty ?? true) + }), + let sharedContainerURL else { + return nil + } - return ImportedFile(name: fileUrl.lastPathComponent, path: fileUrl, uti: fileUrl.uti ?? .data) + let handoffRootURL = KDriveFileSharingConstants.handoffDirectoryURL(in: sharedContainerURL).standardizedFileURL + let resolvedSharedContainerURL = sharedContainerURL.resolvingSymlinksInPath().standardizedFileURL + let resolvedHandoffRootURL = handoffRootURL.resolvingSymlinksInPath().standardizedFileURL + let expectedHandoffRootURL = KDriveFileSharingConstants + .handoffDirectoryURL(in: resolvedSharedContainerURL) + .standardizedFileURL + guard resolvedHandoffRootURL == expectedHandoffRootURL else { + return nil + } + + var files = [ImportedFile]() + for queryItem in queryItems { + guard let path = queryItem.value, + NSString(string: path).isAbsolutePath, + let fileName = path.safeLastPathComponent else { + return nil } - guard !files.isEmpty else { - Log.sceneDelegate("Failed to import files: No files found", level: .error) - return false + + let sourceURL = URL(fileURLWithPath: path).standardizedFileURL + guard sourceURL.isSharedFile(in: handoffRootURL) else { + return nil } - await router.navigate(to: .saveFiles(files: files)) - matomo.track(eventWithCategory: .deeplink, name: DeeplinkPath.file.rawValue) - return true + + let resolvedSourceURL = sourceURL.resolvingSymlinksInPath().standardizedFileURL + let expectedSourceURL = sourceURL.pathComponents + .suffix(2) + .reduce(resolvedHandoffRootURL) { $0.appendingPathComponent($1) } + .standardizedFileURL + guard resolvedSourceURL == expectedSourceURL, + let values = try? resolvedSourceURL.resourceValues(forKeys: [.isRegularFileKey]), + values.isRegularFile == true else { + return nil + } + + files.append(ImportedFile( + name: fileName, + path: resolvedSourceURL, + uti: resolvedSourceURL.uti ?? .data + )) } + return files + } +} - Log.sceneDelegate("unable to parse deeplink URL: \(url)", level: .error) - return false +private extension URL { + func isSharedFile(in rootURL: URL) -> Bool { + let rootComponents = rootURL.pathComponents + let sourceComponents = pathComponents + guard sourceComponents.count == rootComponents.count + 2, + Array(sourceComponents.prefix(rootComponents.count)) == rootComponents else { + return false + } + return UUID(uuidString: sourceComponents[rootComponents.count]) != nil } } diff --git a/kDriveTests/kDriveCore/DeepLinks/UTFileSharingDeeplink.swift b/kDriveTests/kDriveCore/DeepLinks/UTFileSharingDeeplink.swift new file mode 100644 index 0000000000..4eec1803ad --- /dev/null +++ b/kDriveTests/kDriveCore/DeepLinks/UTFileSharingDeeplink.swift @@ -0,0 +1,120 @@ +/* + Infomaniak kDrive - iOS App + Copyright (C) 2026 Infomaniak Network SA + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +import Foundation +import InfomaniakCore +@testable import kDriveCore +import XCTest + +final class UTFileSharingDeeplink: XCTestCase { + private let fileManager = FileManager.default + private var testRootURL: URL! + private var sharedContainerURL: URL! + private var handoffRootURL: URL! + + override func setUpWithError() throws { + try super.setUpWithError() + testRootURL = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString, isDirectory: true) + sharedContainerURL = testRootURL.appendingPathComponent("shared", isDirectory: true) + handoffRootURL = KDriveFileSharingConstants.handoffDirectoryURL(in: sharedContainerURL) + try fileManager.createDirectory(at: handoffRootURL, withIntermediateDirectories: true) + } + + override func tearDownWithError() throws { + try? fileManager.removeItem(at: testRootURL) + try super.tearDownWithError() + } + + func testAcceptsRegularFileFromHandoffDirectory() throws { + let sourceURL = try createSourceFile() + + let deeplinkURL = try makeURL(for: sourceURL) + let files = DeeplinkParser(sharedContainerURL: sharedContainerURL).filesForSharingDeeplink(deeplinkURL) + + XCTAssertEqual(files?.count, 1) + XCTAssertEqual(files?.first?.name, sourceURL.lastPathComponent) + XCTAssertEqual(files?.first?.path, sourceURL) + } + + func testRejectsWrongRoute() throws { + let sourceURL = try createSourceFile() + let parser = DeeplinkParser(sharedContainerURL: sharedContainerURL) + + let wrongSchemeURL = try makeURL(for: sourceURL, scheme: "kdrive") + let wrongHostURL = try makeURL(for: sourceURL, host: "other") + + XCTAssertNil(parser.filesForSharingDeeplink(wrongSchemeURL)) + XCTAssertNil(parser.filesForSharingDeeplink(wrongHostURL)) + } + + func testRejectsFileOutsideHandoffDirectory() throws { + let outsideURL = testRootURL.appendingPathComponent("outside.txt") + try Data().write(to: outsideURL) + + let deeplinkURL = try makeURL(for: outsideURL) + let files = DeeplinkParser(sharedContainerURL: sharedContainerURL).filesForSharingDeeplink(deeplinkURL) + + XCTAssertNil(files) + } + + func testRejectsSymlinkEscapingHandoffDirectory() throws { + let outsideURL = testRootURL.appendingPathComponent("outside.txt") + try Data().write(to: outsideURL) + let symlinkURL = try createHandoffDirectory().appendingPathComponent("link.txt") + try fileManager.createSymbolicLink(at: symlinkURL, withDestinationURL: outsideURL) + + let deeplinkURL = try makeURL(for: symlinkURL) + let files = DeeplinkParser(sharedContainerURL: sharedContainerURL).filesForSharingDeeplink(deeplinkURL) + + XCTAssertNil(files) + } + + func testRejectsDirectory() throws { + let directoryURL = try createHandoffDirectory().appendingPathComponent("folder", isDirectory: true) + try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: false) + + let deeplinkURL = try makeURL(for: directoryURL) + let files = DeeplinkParser(sharedContainerURL: sharedContainerURL).filesForSharingDeeplink(deeplinkURL) + + XCTAssertNil(files) + } + + private func createHandoffDirectory() throws -> URL { + let directoryURL = handoffRootURL.appendingPathComponent(UUID().uuidString, isDirectory: true) + try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: false) + return directoryURL + } + + private func createSourceFile() throws -> URL { + let sourceURL = try createHandoffDirectory().appendingPathComponent("file.txt") + try Data("content".utf8).write(to: sourceURL) + return sourceURL + } + + private func makeURL( + for sourceURL: URL, + scheme: String = KDriveFileSharingConstants.scheme, + host: String = KDriveFileSharingConstants.host + ) throws -> URL { + var components = URLComponents() + components.scheme = scheme + components.host = host + components.queryItems = [URLQueryItem(name: KDriveFileSharingConstants.urlQueryItemName, value: sourceURL.path)] + return try XCTUnwrap(components.url) + } +}