diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index db800f3..6849b3b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,7 +27,7 @@ jobs: - name: Engine tests run: > swift test --package-path Vendor/rootshell-vnc - --filter "RFBProtocolTests|RFBTransportTests|VideoSourceFrameCounterTests" + --filter "RFBProtocolTests|RFBTransportTests|VideoSourceFrameCounterTests|FileTransferNameTests" - name: Build the Mac app run: > xcodebuild build diff --git a/Vendor/rootshell-vnc/Sources/rootshellVNC/API/VNCFileTransfers.swift b/Vendor/rootshell-vnc/Sources/rootshellVNC/API/VNCFileTransfers.swift index 8b2397d..e231596 100644 --- a/Vendor/rootshell-vnc/Sources/rootshellVNC/API/VNCFileTransfers.swift +++ b/Vendor/rootshell-vnc/Sources/rootshellVNC/API/VNCFileTransfers.swift @@ -721,10 +721,20 @@ public final class VNCFileTransferController { } } + /// One code point at a time. Searching the text for "/" steps over a + /// slash that has a combining mark or a joiner after it, since the two + /// make one character that is not "/", and the file system would still + /// split the name there: a Mac that named an item "../" and an accent + /// would have it saved one folder above where downloads go. nonisolated static func sanitizedName(_ name: String) -> String { - let cleaned = name.replacingOccurrences(of: "/", with: ":") - .replacingOccurrences(of: "\0", with: "") - return cleaned.isEmpty || cleaned == "." || cleaned == ".." ? "File" : cleaned + var scalars = String.UnicodeScalarView() + for scalar in name.unicodeScalars where scalar != "\0" { + scalars.append(scalar == "/" ? ":" : scalar) + } + let cleaned = String(scalars) + let bytes = cleaned.utf8 + return bytes.isEmpty || bytes.elementsEqual(".".utf8) || bytes.elementsEqual("..".utf8) + ? "File" : cleaned } /// Link-aware: a dangling symbolic link still counts as taken. diff --git a/Vendor/rootshell-vnc/Tests/rootshellVNCTests/FileTransferNameTests.swift b/Vendor/rootshell-vnc/Tests/rootshellVNCTests/FileTransferNameTests.swift new file mode 100644 index 0000000..a82790c --- /dev/null +++ b/Vendor/rootshell-vnc/Tests/rootshellVNCTests/FileTransferNameTests.swift @@ -0,0 +1,51 @@ +import XCTest +@testable import rootshellVNC + +/// Names of downloaded items come from the other computer and become file +/// names here. None may lead out of the folder a download is written to. +final class FileTransferNameTests: XCTestCase { + func testOrdinaryNamesAreKept() { + XCTAssertEqual(VNCFileTransferController.sanitizedName("Report.pdf"), "Report.pdf") + XCTAssertEqual(VNCFileTransferController.sanitizedName("Résumé – final.txt"), "Résumé – final.txt") + XCTAssertEqual(VNCFileTransferController.sanitizedName(".hidden"), ".hidden") + } + + func testSlashesAndNULsDoNotSurvive() { + XCTAssertEqual(VNCFileTransferController.sanitizedName("a/b"), "a:b") + XCTAssertEqual(VNCFileTransferController.sanitizedName("../../etc/passwd"), "..:..:etc:passwd") + XCTAssertEqual(VNCFileTransferController.sanitizedName("a\0b"), "ab") + } + + func testDotNamesAreReplaced() { + XCTAssertEqual(VNCFileTransferController.sanitizedName(""), "File") + XCTAssertEqual(VNCFileTransferController.sanitizedName("."), "File") + XCTAssertEqual(VNCFileTransferController.sanitizedName(".."), "File") + XCTAssertEqual(VNCFileTransferController.sanitizedName("\0"), "File") + } + + /// A slash followed by a combining mark or an invisible joiner is one + /// character that is not "/", and a search of the text for "/" does not + /// find it. The file system splits the name there all the same. + func testASlashHiddenInsideACharacterIsStillReplaced() { + for hidden in ["../\u{0301}escaped.txt", "../\u{200D}escaped", "x/\u{FE0F}y", "a/\u{0338}b"] { + let cleaned = VNCFileTransferController.sanitizedName(hidden) + XCTAssertFalse(cleaned.utf8.contains(UInt8(ascii: "/")), "\(hidden.unicodeScalars.map { $0.value })") + } + } + + /// Whatever the name, the place it leads to is directly inside the folder. + func testEveryCleanedNameStaysInsideTheFolder() { + let folder = URL(fileURLWithPath: "/tmp/openscreens-downloads/one", isDirectory: true) + let names = [ + "../\u{0301}up", "..", "../..", "/absolute", "a/../../b", "\u{0301}/..", + "..\u{200D}/x", "normal.txt", "..:..", "\0/\0", + ] + for name in names { + let url = VNCFileTransferController.uniqueURL( + in: folder, name: VNCFileTransferController.sanitizedName(name)) + XCTAssertEqual( + url.standardizedFileURL.deletingLastPathComponent().path, folder.path, + "\(name.unicodeScalars.map { $0.value })") + } + } +}