mirror of
https://github.com/burakgon/OpenScreens.git
synced 2026-12-02 05:00:24 +08:00
engine: clean a downloaded item's name one code point at a time
A file copied from a Mac is saved under the name the Mac gives it, with slashes replaced first. They were replaced by searching the text for "/", and that search steps over a slash followed by a combining mark: the two are one character, which is not "/". The file system splits the name there all the same, so an item named "../" and an accent was written one folder above the one its download belongs in. The name is now gone through code point by code point, and what counts as "." or ".." is decided on its bytes.
This commit is contained in:
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
51
Vendor/rootshell-vnc/Tests/rootshellVNCTests/FileTransferNameTests.swift
vendored
Normal file
51
Vendor/rootshell-vnc/Tests/rootshellVNCTests/FileTransferNameTests.swift
vendored
Normal file
@@ -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 })")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user