Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Tuist/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Tuist/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
6 changes: 5 additions & 1 deletion kDriveCore/DI/CoreTargetAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
109 changes: 92 additions & 17 deletions kDriveCore/Utils/DeeplinkParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import InfomaniakCore
import InfomaniakCoreCommonUI
import InfomaniakDI
import MatomoTracker
Expand All @@ -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?) {
Comment thread
PhilippeWeidmann marked this conversation as resolved.
Resolved
self.sharedContainerURL = sharedContainerURL
}

private func handleDeeplink(url: URL) async -> Bool {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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,
Comment thread
adrien-coye marked this conversation as resolved.
Resolved
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
}
}
120 changes: 120 additions & 0 deletions kDriveTests/kDriveCore/DeepLinks/UTFileSharingDeeplink.swift
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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)
}
}
Loading