diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index de3129b..e496b24 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -67,7 +67,7 @@ jobs: id: manifest env: UPDATE_BASE_URL: ${{ vars.DASHCAM_OFFLOADER_UPDATE_BASE_URL || 'https://dashcam-offloader-updates.vortexradar.workers.dev' }} - VERSION: 0.1.44 + VERSION: 0.1.45 ZIP_NAME: ${{ steps.package.outputs.zip_name }} ZIP_SHA256: ${{ steps.package.outputs.sha256 }} SHORT_SHA: ${{ steps.package.outputs.short_sha }} diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index e656f4d..26bd58c 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -54,7 +54,10 @@ jobs: run: node --check workers/feedback/worker.js - name: Test feedback worker sanitizer - run: node --test workers/feedback/worker.test.mjs + run: node --test workers/feedback/*.test.mjs + + - name: Test updates worker + run: node --check workers/updates/worker.js && node --test workers/updates/*.test.mjs - name: Test scan storage evidence extraction run: python3 -m unittest discover -s scripts -p 'test_ingest_submitted_scans.py' && python3 -m unittest discover -s scripts -p 'test_scan_storage_rates.py' diff --git a/DESIGN.md b/DESIGN.md index 3433edb..5420811 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -487,8 +487,9 @@ must pass strict integrity verification and the app's bundle identifier, version, and build commit must exactly match the signed manifest. Eject actions are limited to local removable volumes, never network shares or a manually chosen subdirectory. Feedback is size-limited while streaming, rate-limited by -a Durable Object, opt-in for ordinary feedback, and excludes user-controlled -filenames, paths, and volume names. Copying computes SHA-256 for each regular +a Durable Object, opt-in for ordinary feedback, and excludes paths and volume +names. Ordinary feedback excludes filenames; the separately reviewed, opt-in +private evidence keeps bounded media basenames (see workers/feedback/README.md). Copying computes SHA-256 for each regular file after copy and before skipping an existing destination file; a same-size mismatch is left untouched and reported as a conflict. diff --git a/Sources/DashcamOffloaderApp/CardHealthDetails.swift b/Sources/DashcamOffloaderApp/CardHealthDetails.swift index 1e3e5a0..dae4dfe 100644 --- a/Sources/DashcamOffloaderApp/CardHealthDetails.swift +++ b/Sources/DashcamOffloaderApp/CardHealthDetails.swift @@ -163,7 +163,7 @@ struct CardHealthDetailsView: View { private func fileRow(_ file: CardHealthFileEvidence) -> some View { HStack(alignment: .top) { - Text("\(file.channel): \(file.relativePath)").font(.system(.caption, design: .monospaced)) + Text(verbatim: "\(file.channel): \(file.relativePath)").font(.system(.caption, design: .monospaced)) .frame(maxWidth: .infinity, alignment: .leading) Button("Reveal") { guard let url = report.revealableURL(for: file) else { diff --git a/Sources/DashcamOffloaderApp/CardLearningStructure.swift b/Sources/DashcamOffloaderApp/CardLearningStructure.swift index f249391..c93607f 100644 --- a/Sources/DashcamOffloaderApp/CardLearningStructure.swift +++ b/Sources/DashcamOffloaderApp/CardLearningStructure.swift @@ -68,7 +68,7 @@ enum CardLearningStructure { name.utf8.count <= 180 && !name.hasPrefix(".") && !name.contains("/") && !name.contains("\\") && !name.contains("..") && name.range(of: #".+\.(?:mp4|mov|m4v|avi|ts|mts|m2ts|jpg|jpeg|png)$"#, options: [.regularExpression, .caseInsensitive]) != nil && - name.range(of: #"(?:wifi|password|credential|gps|serial)|[^\s@]+@[^\s@]+\.[^\s@]+|\d{1,3}\.\d{3,}"#, options: [.regularExpression, .caseInsensitive]) == nil && + name.range(of: #"(?:wifi|password|credential|gps|serial)|[^\s@]+@[^\s@]+\.[^\s@]+|\d{1,3}\.\d{3,}|(? String? { - guard let values = try? url.resourceValues(forKeys: [.fileSizeKey]), - let fileSize = values.fileSize, - fileSize <= 1_000_000, - let data = try? Data(contentsOf: url) else { + // The path's size is not the size of what a symbolic link points at. + guard let data = SafeFileRead.data(at: url, maxBytes: 1_000_000) else { return nil } @@ -2391,7 +2389,7 @@ struct CardScanner { return nil } - private func parseTimestamp(groups: [String], format: TimestampFormat) -> Date? { + func parseTimestamp(groups: [String], format: TimestampFormat) -> Date? { switch format { case .yyyymmddHhmmss: // Some patterns have a mode-prefix group before the date @@ -2408,8 +2406,20 @@ struct CardScanner { guard groups.count >= 3 else { return nil } return parseDate(groups[0] + groups[1] + groups[2], format: "yyyyMMddHHmmss") case .yyyymmddDashHhmmss: - guard groups.count >= 6 else { return nil } - return parseDate(groups[1] + groups[2] + groups[3] + groups[4] + groups[5] + groups[6], format: "yyyyMMddHHmmss") + // 70mai profiles capture the date/time either as "YYYYMMDD" + "HHMMSS" or as six + // separate fields. Join consecutive all-digit groups that add up to exactly 14 digits. + // (Previously read groups[1...6] behind a count >= 6 guard: never matched the 8+6 + // form and skipped the year in the six-field form.) + for start in groups.indices { + var digits = "" + for group in groups[start...] { + guard !group.isEmpty, group.allSatisfy({ $0.isASCII && $0.isNumber }) else { break } + digits += group + if digits.count >= 14 { break } + } + if digits.count == 14 { return parseDate(digits, format: "yyyyMMddHHmmss") } + } + return nil case .unknown: return parseTimestampHeuristically(groups: groups) } diff --git a/Sources/DashcamOffloaderApp/CardScannerParkingInference.swift b/Sources/DashcamOffloaderApp/CardScannerParkingInference.swift index 2de1a70..9ff42ab 100644 --- a/Sources/DashcamOffloaderApp/CardScannerParkingInference.swift +++ b/Sources/DashcamOffloaderApp/CardScannerParkingInference.swift @@ -139,11 +139,8 @@ extension CardScanner { } let configURL = sourceURL.appendingPathComponent("BlackVue/Config/config.ini") - guard let values = try? configURL.resourceValues(forKeys: [.fileSizeKey]), - let fileSize = values.fileSize, - fileSize > 0, - fileSize <= 512 * 1024, - let data = try? Data(contentsOf: configURL), + guard let data = SafeFileRead.data(at: configURL, maxBytes: 512 * 1024), + !data.isEmpty, let text = String(data: data, encoding: .utf8) else { return nil } @@ -433,7 +430,7 @@ extension CardScanner { for item in sortedClips { if var current = moments.last, item.timestamp.timeIntervalSince(current.timestamp) <= 3 { - current.totalBytes += item.clip.size + current.totalBytes = current.totalBytes.addingSaturating(item.clip.size) current.relativePaths.append(item.clip.relativePath) current.timestamp = min(current.timestamp, item.timestamp) moments[moments.count - 1] = current @@ -648,7 +645,7 @@ extension CardScanner { } return grouped.compactMap { key, clips -> (key: Int, timestamp: Date, totalBytes: Int64)? in guard let timestamp = clips.compactMap(\.timestamp).min() else { return nil } - return (key, timestamp, clips.reduce(Int64(0)) { $0 + $1.size }) + return (key, timestamp, clips.reduce(Int64(0)) { $0.addingSaturating($1.size) }) } .sorted { $0.timestamp < $1.timestamp } } diff --git a/Sources/DashcamOffloaderApp/CardScannerSourceDiscovery.swift b/Sources/DashcamOffloaderApp/CardScannerSourceDiscovery.swift index b910aaa..7ea06e8 100644 --- a/Sources/DashcamOffloaderApp/CardScannerSourceDiscovery.swift +++ b/Sources/DashcamOffloaderApp/CardScannerSourceDiscovery.swift @@ -46,7 +46,11 @@ extension CardScanner { } - func enumerateFiles(sourceURL: URL) throws -> [URL] { + /// Matches the Electron engine's cap. A real dashcam card holds a few thousand + /// entries; a hostile or mistaken source with millions must not exhaust memory. + static let maxEnumeratedEntries = 500_000 + + func enumerateFiles(sourceURL: URL, entryLimit: Int = CardScanner.maxEnumeratedEntries) throws -> [URL] { guard let enumerator = fileManager.enumerator( at: sourceURL, includingPropertiesForKeys: [.isRegularFileKey, .fileSizeKey], @@ -57,7 +61,10 @@ extension CardScanner { } var files: [URL] = [] + var visited = 0 for case let fileURL as URL in enumerator { + visited += 1 + if visited > entryLimit { break } let relativePath = fileURL.relativePath(from: sourceURL) if shouldSkipTraversal(relativePath: relativePath) { enumerator.skipDescendants() diff --git a/Sources/DashcamOffloaderApp/CopyExecutor.swift b/Sources/DashcamOffloaderApp/CopyExecutor.swift index 4c4272c..50e765a 100644 --- a/Sources/DashcamOffloaderApp/CopyExecutor.swift +++ b/Sources/DashcamOffloaderApp/CopyExecutor.swift @@ -1,6 +1,7 @@ @preconcurrency import AVFoundation import Foundation import CryptoKit +import Darwin struct CopyExecutor: Sendable { private static let copyBufferSize = 8 * 1024 * 1024 @@ -59,8 +60,8 @@ struct CopyExecutor: Sendable { pending = nil await finish(previous, progress: &progress, results: &results, delivery: delivery) } - let exported = try await concatenateVideoItem(item, destinationURL: item.destinationURL) { copiedChunk in - progress.copiedBytes += copiedChunk + let exported = try await concatenateVideoItem(item, destinationURL: item.destinationURL, destinationRoot: plan.destinationRoot) { copiedChunk in + progress.copiedBytes = progress.copiedBytes.addingSaturating(copiedChunk) await update(progress, delivery: delivery) } progress.completedFiles += 1 @@ -70,7 +71,7 @@ struct CopyExecutor: Sendable { } else { result.status = .skipped result.message = "Already in destination" - progress.copiedBytes += item.totalSize + progress.copiedBytes = progress.copiedBytes.addingSaturating(item.totalSize) } } else { // A repeated destination must not be treated as an existing verified file. @@ -81,6 +82,7 @@ struct CopyExecutor: Sendable { let verification = try await copyOne( sourceURL: item.clip.sourceURL, destinationURL: item.destinationURL, + destinationRoot: plan.destinationRoot, expectedSize: item.clip.size, modificationDate: reliableRecordingDate(for: item.clip), beforeCompletion: { @@ -90,7 +92,7 @@ struct CopyExecutor: Sendable { } } ) { copiedChunk, waitForUI in - progress.copiedBytes += copiedChunk + progress.copiedBytes = progress.copiedBytes.addingSaturating(copiedChunk) await update(progress, delivery: delivery, waitForUI: waitForUI) } if let verification { @@ -103,7 +105,7 @@ struct CopyExecutor: Sendable { progress.completedFiles += 1 result.status = .skipped result.message = "Already in destination" - progress.copiedBytes += item.clip.size + progress.copiedBytes = progress.copiedBytes.addingSaturating(item.clip.size) } } } catch is CancellationError { @@ -144,11 +146,12 @@ struct CopyExecutor: Sendable { let verification = try await copyOne( sourceURL: item.sourceURL, destinationURL: item.destinationURL, + destinationRoot: plan.destinationRoot, expectedSize: item.size, modificationDate: nil, beforeCompletion: {} ) { copiedChunk, waitForUI in - progress.copiedBytes += copiedChunk + progress.copiedBytes = progress.copiedBytes.addingSaturating(copiedChunk) await update(progress, delivery: delivery, waitForUI: waitForUI) } if let verification { @@ -165,7 +168,7 @@ struct CopyExecutor: Sendable { } else { result.status = .skipped result.message = "Already in destination" - progress.copiedBytes += item.size + progress.copiedBytes = progress.copiedBytes.addingSaturating(item.size) } } catch is CancellationError { result.status = .cancelled @@ -192,8 +195,8 @@ struct CopyExecutor: Sendable { let copiedCount = results.filter { $0.status == .copied }.count + supportResults.filter { $0.status == .copied }.count let skippedCount = results.filter { $0.status == .skipped }.count + supportResults.filter { $0.status == .skipped }.count let failedCount = results.filter { $0.status == .failed }.count + supportResults.filter { $0.status == .failed }.count - let copiedBytes = results.filter { $0.status == .copied }.reduce(Int64(0)) { $0 + $1.totalSize } + - supportResults.filter { $0.status == .copied }.reduce(Int64(0)) { $0 + $1.size } + let copiedBytes = results.filter { $0.status == .copied }.reduce(Int64(0)) { $0.addingSaturating($1.totalSize) } + .addingSaturating(supportResults.filter { $0.status == .copied }.reduce(Int64(0)) { $0.addingSaturating($1.size) }) if copiedCount == 0 && failedCount == 0 && skippedCount > 0 { progress.summary = "All \(skippedCount) files were already in the destination" } else { @@ -252,6 +255,7 @@ struct CopyExecutor: Sendable { private func copyOne( sourceURL: URL, destinationURL: URL, + destinationRoot: URL, expectedSize: Int64, modificationDate: Date?, beforeCompletion: () async -> Void, @@ -261,24 +265,28 @@ struct CopyExecutor: Sendable { let destination = destinationURL let destinationDirectory = destination.deletingLastPathComponent() try fileManager.createDirectory(at: destinationDirectory, withIntermediateDirectories: true) + try Self.requireContainedDestination(directory: destinationDirectory, destinationRoot: destinationRoot) - if fileManager.fileExists(atPath: destination.path) { + // lstat semantics: a dangling symbolic link is an existing entry, not a free name. + if Self.entryExists(atPath: destination.path) { guard try filesHaveMatchingSHA256(sourceURL, destination) else { throw CopyError.destinationConflict } return nil } - let input = try FileHandle(forReadingFrom: sourceURL) + // Never follow a link or open a pipe/device/directory that replaced the scanned file. + let input = try Self.openRegularFileForReading(sourceURL) defer { try? input.close() } - fileManager.createFile(atPath: destination.path, contents: nil) + // O_EXCL: this call owns the file it removes on failure and can never + // truncate, replace or delete an entry that appeared after the check above. + let output = try Self.createExclusiveFileForWriting(destination) var didFinishWriting = false defer { if !didFinishWriting { try? fileManager.removeItem(at: destination) } } - let output = try FileHandle(forWritingTo: destination) var outputIsClosed = false defer { if !outputIsClosed { @@ -295,10 +303,13 @@ struct CopyExecutor: Sendable { throw CancellationError() } try beforeChunkHook?(writtenBytes) - let data = input.readData(ofLength: Self.copyBufferSize) + // Throwing variants: the legacy readData/write calls raise Objective-C + // exceptions on I/O errors (card removed, disk full), which Swift cannot + // catch; the process would abort and leave a partial file behind. + let data = try input.read(upToCount: Self.copyBufferSize) ?? Data() guard !data.isEmpty else { break } sourceHasher.update(data: data) - output.write(data) + try output.write(contentsOf: data) unreportedBytes += Int64(data.count) writtenBytes += Int64(data.count) let now = progressClock() @@ -314,6 +325,8 @@ struct CopyExecutor: Sendable { if unreportedBytes > 0 { await progress(unreportedBytes, true) } await beforeCompletion() if Task.isCancelled { throw CancellationError() } + // Push the bytes to the device before the copy is verified and reported as done. + try output.synchronize() try output.close() outputIsClosed = true @@ -356,41 +369,111 @@ struct CopyExecutor: Sendable { return clip.timestamp } - private func filesHaveMatchingSHA256(_ firstURL: URL, _ secondURL: URL) throws -> Bool { - try Array(Self.sha256Digest(for: firstURL)) == Array(Self.sha256Digest(for: secondURL)) + private func filesHaveMatchingSHA256(_ sourceURL: URL, _ destinationURL: URL) throws -> Bool { + try Array(Self.sha256Digest(for: sourceURL, isSource: true)) == Array(Self.sha256Digest(for: destinationURL)) } - private static func sha256Digest(for url: URL) throws -> SHA256.Digest { - let input = try FileHandle(forReadingFrom: url) + private static func sha256Digest(for url: URL, isSource: Bool = false) throws -> SHA256.Digest { + let input = try isSource ? openRegularFileForReading(url) : FileHandle(forReadingFrom: url) defer { try? input.close() } + if !isSource { + // Best effort: read the copy back from the device rather than the page cache. + _ = fcntl(input.fileDescriptor, F_NOCACHE, 1) + } var hasher = SHA256() while true { if Task.isCancelled { throw CancellationError() } - let data = input.readData(ofLength: Self.copyBufferSize) + let data = try input.read(upToCount: Self.copyBufferSize) ?? Data() guard !data.isEmpty else { break } hasher.update(data: data) } return hasher.finalize() } + /// Opens a source without following a final symbolic link and refuses anything + /// that is not a regular file. O_NONBLOCK keeps a named pipe from blocking open. + static func openRegularFileForReading(_ url: URL) throws -> FileHandle { + let descriptor = open(url.path, O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC) + guard descriptor >= 0 else { + let code = errno + if code == ELOOP { throw CopyError.sourceNotRegularFile } + throw NSError(domain: NSPOSIXErrorDomain, code: Int(code)) + } + var info = stat() + guard fstat(descriptor, &info) == 0, (info.st_mode & S_IFMT) == S_IFREG else { + close(descriptor) + throw CopyError.sourceNotRegularFile + } + return FileHandle(fileDescriptor: descriptor, closeOnDealloc: true) + } + + /// Creates the destination only if no entry of any kind has that name. + /// Give a finished temporary file its final name. link(2) fails with EEXIST instead of + /// replacing whatever appeared at the final name meanwhile. + static func publishWithoutOverwriting(_ temporaryURL: URL, as finalURL: URL) throws { + guard link(temporaryURL.path, finalURL.path) == 0 else { + let code = errno + if code == EEXIST { throw CopyError.destinationConflict } + throw NSError(domain: NSPOSIXErrorDomain, code: Int(code)) + } + unlink(temporaryURL.path) + } + + static func createExclusiveFileForWriting(_ url: URL) throws -> FileHandle { + let descriptor = open(url.path, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0o644) + guard descriptor >= 0 else { + let code = errno + if code == EEXIST { throw CopyError.destinationConflict } + throw NSError(domain: NSPOSIXErrorDomain, code: Int(code)) + } + return FileHandle(fileDescriptor: descriptor, closeOnDealloc: true) + } + + static func entryExists(atPath path: String) -> Bool { + var info = stat() + return lstat(path, &info) == 0 + } + + static func isPath(_ path: String, within root: String) -> Bool { + path == root || path.hasPrefix(root.hasSuffix("/") ? root : root + "/") + } + + /// Symbolic links already present below the destination must not redirect a + /// copy outside the folder the user chose (or back onto a source card). + static func requireContainedDestination(directory: URL, destinationRoot: URL) throws { + let resolvedDirectory = directory.resolvingSymlinksInPath().standardizedFileURL.path + let resolvedRoot = destinationRoot.resolvingSymlinksInPath().standardizedFileURL.path + guard isPath(resolvedDirectory, within: resolvedRoot) else { + throw CopyError.destinationOutsideChosenFolder + } + } + + /// True when copying would write onto the source itself. + static func isDestination(_ destinationRoot: URL, insideSource sourceRoot: URL) -> Bool { + isPath( + destinationRoot.resolvingSymlinksInPath().standardizedFileURL.path, + within: sourceRoot.resolvingSymlinksInPath().standardizedFileURL.path + ) + } + private func concatenateVideoItem( _ item: CopyPlanItem, destinationURL: URL, + destinationRoot: URL, progress: (Int64) async -> Void ) async throws -> Bool { let fileManager = FileManager.default let destinationDirectory = destinationURL.deletingLastPathComponent() try fileManager.createDirectory(at: destinationDirectory, withIntermediateDirectories: true) + try Self.requireContainedDestination(directory: destinationDirectory, destinationRoot: destinationRoot) - if fileManager.fileExists(atPath: destinationURL.path) { + if Self.entryExists(atPath: destinationURL.path) { return false } - var didFinishExport = false - defer { - if !didFinishExport { - try? fileManager.removeItem(at: destinationURL) - } - } + // Export to a hidden temporary name and publish it only when complete, so an + // interrupted export can never sit under the final name looking like a finished file. + let partialURL = destinationDirectory.appendingPathComponent(".partial-\(UUID().uuidString).mp4") + defer { try? fileManager.removeItem(at: partialURL) } let composition = AVMutableComposition() guard let videoTrack = composition.addMutableTrack( @@ -445,7 +528,7 @@ struct CopyExecutor: Sendable { ) else { throw CopyError.videoConcatenationFailed("Could not create export session") } - exportSession.outputURL = destinationURL + exportSession.outputURL = partialURL exportSession.outputFileType = .mp4 exportSession.shouldOptimizeForNetworkUse = false @@ -458,9 +541,9 @@ struct CopyExecutor: Sendable { switch exportSession.status { case .completed: if let modificationDate = reliableRecordingDate(for: item.clip) { - try fileManager.setAttributes([.modificationDate: modificationDate], ofItemAtPath: destinationURL.path) + try fileManager.setAttributes([.modificationDate: modificationDate], ofItemAtPath: partialURL.path) } - didFinishExport = true + try Self.publishWithoutOverwriting(partialURL, as: destinationURL) await progress(item.totalSize) return true case .cancelled: @@ -524,6 +607,8 @@ enum CopyError: LocalizedError { case sizeVerificationFailed case checksumVerificationFailed case destinationConflict + case sourceNotRegularFile + case destinationOutsideChosenFolder case videoConcatenationFailed(String) var errorDescription: String? { @@ -534,6 +619,10 @@ enum CopyError: LocalizedError { return "Copied file contents did not match source" case .destinationConflict: return "A different file with this name already exists in the destination" + case .sourceNotRegularFile: + return "The source is no longer a regular file and was not copied" + case .destinationOutsideChosenFolder: + return "The destination folder leads outside the chosen download folder; nothing was written" case .videoConcatenationFailed(let message): return "Could not combine video clips: \(message)" } diff --git a/Sources/DashcamOffloaderApp/CopyExecutorVerification.swift b/Sources/DashcamOffloaderApp/CopyExecutorVerification.swift index 598ebe4..9c4eeb2 100644 --- a/Sources/DashcamOffloaderApp/CopyExecutorVerification.swift +++ b/Sources/DashcamOffloaderApp/CopyExecutorVerification.swift @@ -55,6 +55,10 @@ enum CopyExecutorVerification { return false } + guard await HostileCardVerification.run() else { + return false + } + return await CopyThroughputVerification.run() } diff --git a/Sources/DashcamOffloaderApp/CopyPlanner.swift b/Sources/DashcamOffloaderApp/CopyPlanner.swift index a329ff2..e02b57a 100644 --- a/Sources/DashcamOffloaderApp/CopyPlanner.swift +++ b/Sources/DashcamOffloaderApp/CopyPlanner.swift @@ -176,7 +176,7 @@ struct CopyPlanner { let lastSequence = goProSequenceNumber(for: last.filename) ?? firstSequence let outputName = String(format: "%@%04d-%04d.%@", groupKey, firstSequence, lastSequence, ext.isEmpty ? "MP4" : ext) first.filename = outputName - first.size = clips.reduce(Int64(0)) { $0 + $1.size } + first.size = clips.reduce(Int64(0)) { $0.addingSaturating($1.size) } first.sourceURL = clips[0].sourceURL first.relativePath = clips[0].relativePath return first @@ -290,7 +290,13 @@ struct CopyPlanner { continue } - let size = (try? fileURL.resourceValues(forKeys: [.fileSizeKey]).fileSize).map(Int64.init) ?? 0 + // Only regular files: a symbolic link, folder, pipe or device named like a + // settings file must never be opened or copied. + guard let values = try? fileURL.resourceValues(forKeys: [.isRegularFileKey, .isSymbolicLinkKey, .fileSizeKey]), + values.isRegularFile == true, values.isSymbolicLink != true else { + continue + } + let size = Int64(values.fileSize ?? 0) let destinationURL = destinationRoot .appendingPathComponent("Camera Settings", isDirectory: true) .appendingPathComponent(volumeFolder.isEmpty ? "source" : volumeFolder, isDirectory: true) diff --git a/Sources/DashcamOffloaderApp/FeedbackService.swift b/Sources/DashcamOffloaderApp/FeedbackService.swift index b712940..4d6e055 100644 --- a/Sources/DashcamOffloaderApp/FeedbackService.swift +++ b/Sources/DashcamOffloaderApp/FeedbackService.swift @@ -2,7 +2,15 @@ import Foundation struct FeedbackService { var endpointURL: URL - var session: URLSession = .shared + var session: URLSession = FeedbackService.makeSession() + + static func makeSession() -> URLSession { + let configuration = URLSessionConfiguration.ephemeral + configuration.httpCookieStorage = nil + configuration.httpShouldSetCookies = false + configuration.urlCache = nil + return URLSession(configuration: configuration, delegate: RefuseRedirects(), delegateQueue: nil) + } static let production = FeedbackService( endpointURL: URL(string: "https://dashcam-offloader-feedback.vortexradar.workers.dev/feedback")! @@ -33,6 +41,13 @@ struct FeedbackService { } } +final class RefuseRedirects: NSObject, URLSessionTaskDelegate { + func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, + newRequest request: URLRequest) async -> URLRequest? { + nil + } +} + private struct FeedbackResponse: Decodable { var id: String } diff --git a/Sources/DashcamOffloaderApp/HostileCardVerification.swift b/Sources/DashcamOffloaderApp/HostileCardVerification.swift new file mode 100644 index 0000000..538ecfb --- /dev/null +++ b/Sources/DashcamOffloaderApp/HostileCardVerification.swift @@ -0,0 +1,262 @@ +import Foundation +import Darwin + +/// Hostile-source and destination-containment regressions for the copy path. +/// Written on a host without a Swift toolchain: run with `swift run DashcamOffloader --verify`. +enum HostileCardVerification { + static func run() async -> Bool { + do { + return try await runCases() + } catch { + print("VERIFY FAIL: hostile card verification error: \(error.localizedDescription)") + return false + } + } + + private static func runCases() async throws -> Bool { + let fileManager = FileManager.default + let root = fileManager.temporaryDirectory + .appendingPathComponent("dashcam-offloader-hostile-verify-\(UUID().uuidString)", isDirectory: true) + defer { try? fileManager.removeItem(at: root) } + + let sourceRoot = root.appendingPathComponent("card", isDirectory: true) + let destinationRoot = root.appendingPathComponent("destination", isDirectory: true) + let outsideRoot = root.appendingPathComponent("outside", isDirectory: true) + for directory in [sourceRoot, destinationRoot, outsideRoot] { + try fileManager.createDirectory(at: directory, withIntermediateDirectories: true) + } + let secret = outsideRoot.appendingPathComponent("secret.MP4") + try Data(repeating: 9, count: 1024).write(to: secret) + + // 1. A clip replaced by a symbolic link after the scan is refused, not followed. + let swapped = sourceRoot.appendingPathComponent("swapped.MP4") + try fileManager.createSymbolicLink(at: swapped, withDestinationURL: secret) + let swappedDestination = destinationRoot.appendingPathComponent("swapped.MP4") + var result = await runCopy(sourceRoot: sourceRoot, destinationRoot: destinationRoot, items: [ + item(source: swapped, destination: swappedDestination, expectedSize: 1024) + ]) + guard result.mediaItems.first?.status == .failed, + result.mediaItems.first?.message == CopyError.sourceNotRegularFile.localizedDescription, + !CopyExecutor.entryExists(atPath: swappedDestination.path) else { + print("VERIFY FAIL: copy followed a symbolic link that replaced a scanned clip") + return false + } + + // 2. A named pipe or a directory at the source path fails promptly instead of hanging or crashing. + let pipe = sourceRoot.appendingPathComponent("pipe.MP4") + guard mkfifo(pipe.path, 0o644) == 0 else { + print("VERIFY FAIL: could not create the named-pipe fixture") + return false + } + let folder = sourceRoot.appendingPathComponent("folder.MP4", isDirectory: true) + try fileManager.createDirectory(at: folder, withIntermediateDirectories: true) + result = await runCopy(sourceRoot: sourceRoot, destinationRoot: destinationRoot, items: [ + item(source: pipe, destination: destinationRoot.appendingPathComponent("pipe.MP4"), expectedSize: 0), + item(source: folder, destination: destinationRoot.appendingPathComponent("folder.MP4"), expectedSize: 0) + ]) + guard result.mediaItems.count == 2, + result.mediaItems.allSatisfy({ $0.status == .failed }), + !CopyExecutor.entryExists(atPath: destinationRoot.appendingPathComponent("pipe.MP4").path), + !CopyExecutor.entryExists(atPath: destinationRoot.appendingPathComponent("folder.MP4").path) else { + print("VERIFY FAIL: copy did not refuse a pipe or directory at a source path") + return false + } + + // 3. A dangling symbolic link at the destination is a conflict: nothing is + // written through it and the link itself is not removed. + let regular = sourceRoot.appendingPathComponent("regular.MP4") + try Data(repeating: 5, count: 2048).write(to: regular) + let danglingTarget = outsideRoot.appendingPathComponent("created-through-link.MP4") + let danglingLink = destinationRoot.appendingPathComponent("dangling.MP4") + try fileManager.createSymbolicLink(at: danglingLink, withDestinationURL: danglingTarget) + result = await runCopy(sourceRoot: sourceRoot, destinationRoot: destinationRoot, items: [ + item(source: regular, destination: danglingLink, expectedSize: 2048) + ]) + guard result.mediaItems.first?.status == .failed, + !fileManager.fileExists(atPath: danglingTarget.path), + (try? fileManager.destinationOfSymbolicLink(atPath: danglingLink.path)) != nil else { + print("VERIFY FAIL: copy wrote through or removed a symbolic link at the destination") + return false + } + + // 4. A symbolic-link folder inside the destination cannot redirect a copy elsewhere. + let linkedFolder = destinationRoot.appendingPathComponent("Driving", isDirectory: true) + try fileManager.createSymbolicLink(at: linkedFolder, withDestinationURL: outsideRoot) + result = await runCopy(sourceRoot: sourceRoot, destinationRoot: destinationRoot, items: [ + item(source: regular, destination: linkedFolder.appendingPathComponent("regular.MP4"), expectedSize: 2048) + ]) + guard result.mediaItems.first?.status == .failed, + !fileManager.fileExists(atPath: outsideRoot.appendingPathComponent("regular.MP4").path) else { + print("VERIFY FAIL: copy wrote outside the chosen destination through a symbolic-link folder") + return false + } + + // 5. The ordinary path still copies and verifies. + let plainDestination = destinationRoot.appendingPathComponent("regular.MP4") + result = await runCopy(sourceRoot: sourceRoot, destinationRoot: destinationRoot, items: [ + item(source: regular, destination: plainDestination, expectedSize: 2048) + ]) + guard result.mediaItems.first?.status == .copied, + try Data(contentsOf: plainDestination) == Data(repeating: 5, count: 2048) else { + print("VERIFY FAIL: hardened copy path did not copy a regular clip") + return false + } + + // 6. Downloading into the source is recognised, including through a symbolic link. + let insideSource = sourceRoot.appendingPathComponent("Downloads", isDirectory: true) + try fileManager.createDirectory(at: insideSource, withIntermediateDirectories: true) + let aliasIntoSource = root.appendingPathComponent("alias-into-card", isDirectory: true) + try fileManager.createSymbolicLink(at: aliasIntoSource, withDestinationURL: insideSource) + let sibling = root.appendingPathComponent("card-backup", isDirectory: true) + guard CopyExecutor.isDestination(insideSource, insideSource: sourceRoot), + CopyExecutor.isDestination(sourceRoot, insideSource: sourceRoot), + CopyExecutor.isDestination(aliasIntoSource, insideSource: sourceRoot), + !CopyExecutor.isDestination(destinationRoot, insideSource: sourceRoot), + !CopyExecutor.isDestination(sibling, insideSource: sourceRoot) else { + print("VERIFY FAIL: destination-inside-source detection is wrong") + return false + } + + // 7. Camera-settings preservation plans regular files only. + let config = sourceRoot.appendingPathComponent("config", isDirectory: true) + try fileManager.createDirectory(at: config.appendingPathComponent("nested", isDirectory: true), withIntermediateDirectories: true) + try Data("resolution=4K\n".utf8).write(to: config.appendingPathComponent("menu.ini")) + try fileManager.createSymbolicLink(at: config.appendingPathComponent("linked.ini"), withDestinationURL: secret) + guard mkfifo(config.appendingPathComponent("pipe.ini").path, 0o644) == 0 else { + print("VERIFY FAIL: could not create the settings named-pipe fixture") + return false + } + var filters = FilterState() + filters.includeCameraSettings = true + let plan = CopyPlanner().makePlan( + sourceRoot: sourceRoot, + destinationRoot: destinationRoot, + profile: .genericNewDashcam, + clips: [], + filters: filters + ) + guard plan.supportItems.map(\.relativePath) == ["config/menu.ini"] else { + print("VERIFY FAIL: settings preservation planned a non-regular entry: \(plan.supportItems.map(\.relativePath))") + return false + } + + // 8. Fixed-name metadata reads do not follow links and stay bounded. + let scanner = CardScanner() + let linkedEvidence = sourceRoot.appendingPathComponent("version.txt") + try fileManager.createSymbolicLink(at: linkedEvidence, withDestinationURL: URL(fileURLWithPath: "/dev/zero")) + let largeEvidence = sourceRoot.appendingPathComponent("large.txt") + try Data(repeating: 0x41, count: 1_000_001).write(to: largeEvidence) + let smallEvidence = sourceRoot.appendingPathComponent("small.txt") + try Data("model: TEST1\n".utf8).write(to: smallEvidence) + // A link to an ordinary small text file outside the card passes every type and + // size check once followed, so only O_NOFOLLOW keeps its contents out. + let outsideText = outsideRoot.appendingPathComponent("host-notes.txt") + try Data("model: HOSTFILE\n".utf8).write(to: outsideText) + let linkedText = sourceRoot.appendingPathComponent("config.txt") + try fileManager.createSymbolicLink(at: linkedText, withDestinationURL: outsideText) + guard scanner.evidenceText(at: linkedText) == nil, + SafeFileRead.data(at: linkedText, maxBytes: 1024) == nil else { + print("VERIFY FAIL: metadata read followed a link to a regular file outside the card") + return false + } + guard scanner.evidenceText(at: linkedEvidence) == nil, + scanner.evidenceText(at: largeEvidence) == nil, + scanner.evidenceText(at: pipe) == nil, + scanner.evidenceText(at: smallEvidence) == "model: TEST1\n" else { + print("VERIFY FAIL: metadata read followed a link, blocked on a pipe, or ignored the size limit") + return false + } + + // 8b. The destination open itself (not just the earlier existence check) refuses a + // link that appears at the destination name, and never truncates an existing file. + let racedTarget = outsideRoot.appendingPathComponent("raced-target.MP4") + let racedLink = destinationRoot.appendingPathComponent("raced.MP4") + try fileManager.createSymbolicLink(at: racedLink, withDestinationURL: racedTarget) + let existing = destinationRoot.appendingPathComponent("existing.MP4") + try Data(repeating: 9, count: 64).write(to: existing) + let racedOpen = try? CopyExecutor.createExclusiveFileForWriting(racedLink) + let existingOpen = try? CopyExecutor.createExclusiveFileForWriting(existing) + guard racedOpen == nil, existingOpen == nil, + !fileManager.fileExists(atPath: racedTarget.path), + (try? Data(contentsOf: existing))?.count == 64 else { + print("VERIFY FAIL: destination open followed a link or truncated an existing file") + return false + } + + // 8c. Finished merged exports are published under the final name without + // replacing a file that appeared there meanwhile. + let partial = destinationRoot.appendingPathComponent(".partial-test.mp4") + try Data(repeating: 3, count: 32).write(to: partial) + let published = destinationRoot.appendingPathComponent("merged.MP4") + try CopyExecutor.publishWithoutOverwriting(partial, as: published) + let partial2 = destinationRoot.appendingPathComponent(".partial-test2.mp4") + try Data(repeating: 4, count: 16).write(to: partial2) + let secondPublish: Void? = try? CopyExecutor.publishWithoutOverwriting(partial2, as: published) + guard !fileManager.fileExists(atPath: partial.path), + secondPublish == nil, + (try? Data(contentsOf: published)) == Data(repeating: 3, count: 32) else { + print("VERIFY FAIL: merged export publish replaced an existing file or left the temporary name") + return false + } + + // 8d. Enumeration stops at the entry cap instead of growing without bound. + let wide = root.appendingPathComponent("wide", isDirectory: true) + try fileManager.createDirectory(at: wide, withIntermediateDirectories: true) + for index in 0..<30 { try Data([0]).write(to: wide.appendingPathComponent("f\(index).MP4")) } + guard let capped = try? scanner.enumerateFiles(sourceURL: wide, entryLimit: 10), capped.count == 10, + let uncapped = try? scanner.enumerateFiles(sourceURL: wide), uncapped.count == 30 else { + print("VERIFY FAIL: scan enumeration ignored the entry cap") + return false + } + + // 9. Extension keys sent with feedback are short technical tokens only. + guard FeedbackExtensionKey.key(for: URL(fileURLWithPath: "/card/clip.MP4")) == "mp4", + FeedbackExtensionKey.key(for: URL(fileURLWithPath: "/card/README")) == "[none]", + FeedbackExtensionKey.key(for: URL(fileURLWithPath: "/card/passport.JaneExample1984")) == "[other]", + FeedbackExtensionKey.key(for: URL(fileURLWithPath: "/card/notes.caf\u{e9}")) == "[other]" else { + print("VERIFY FAIL: feedback extension keys can carry free-form name suffixes") + return false + } + + return true + } + + private static func runCopy(sourceRoot: URL, destinationRoot: URL, items: [CopyPlanItem]) async -> CopyRunResult { + let executor = CopyExecutor { _ in } + return await executor.copy(plan: CopyPlan( + sourceRoot: sourceRoot, + destinationRoot: destinationRoot, + profile: .genericNewDashcam, + clips: items.flatMap(\.orderedSourceClips), + items: items + )) + } + + private static func item(source: URL, destination: URL, expectedSize: Int64) -> CopyPlanItem { + CopyPlanItem( + clip: ClipItem( + sourceURL: source, + relativePath: source.lastPathComponent, + filename: source.lastPathComponent, + mode: "driving", + channel: "front", + timestamp: nil, + size: expectedSize, + extensionLowercased: source.pathExtension.lowercased(), + excludedReason: nil + ), + destinationURL: destination, + status: .planned + ) + } +} + +/// Extension labels for feedback aggregates. A file "extension" is the tail of a +/// user-controlled name, so anything that is not a short ASCII token is bucketed. +enum FeedbackExtensionKey { + static func key(for url: URL) -> String { + let ext = url.pathExtension.lowercased() + if ext.isEmpty { return "[none]" } + return ext.range(of: #"^[a-z0-9]{1,10}$"#, options: .regularExpression) != nil ? ext : "[other]" + } +} diff --git a/Sources/DashcamOffloaderApp/Models.swift b/Sources/DashcamOffloaderApp/Models.swift index 352b28b..89f46e9 100644 --- a/Sources/DashcamOffloaderApp/Models.swift +++ b/Sources/DashcamOffloaderApp/Models.swift @@ -517,7 +517,7 @@ struct CopyPlan: Hashable, Sendable { var supportItems: [SupportFileItem] = [] var selectedBytes: Int64 { - items.reduce(0) { $0 + $1.totalSize } + supportItems.reduce(0) { $0 + $1.size } + items.reduce(Int64(0)) { $0.addingSaturating($1.totalSize) }.addingSaturating(supportItems.reduce(Int64(0)) { $0.addingSaturating($1.size) }) } var selectedFileCount: Int { @@ -561,7 +561,7 @@ struct CopyPlanItem: Identifiable, Hashable, Sendable { } var totalSize: Int64 { - orderedSourceClips.reduce(Int64(0)) { $0 + $1.size } + orderedSourceClips.reduce(Int64(0)) { $0.addingSaturating($1.size) } } var displayFilename: String { @@ -1114,3 +1114,11 @@ extension Collection { indices.contains(index) ? self[index] : nil } } + +extension Int64 { + /// File sizes come from the card. A forged size near Int64.max must not trap when summed. + func addingSaturating(_ other: Int64) -> Int64 { + let (sum, overflow) = addingReportingOverflow(other) + return overflow ? (other < 0 ? .min : .max) : sum + } +} diff --git a/Sources/DashcamOffloaderApp/MulticamExporter.swift b/Sources/DashcamOffloaderApp/MulticamExporter.swift index 27c50ac..7f6b3b7 100644 --- a/Sources/DashcamOffloaderApp/MulticamExporter.swift +++ b/Sources/DashcamOffloaderApp/MulticamExporter.swift @@ -211,11 +211,11 @@ enum MulticamExporter { let description = try await audio.load(.formatDescriptions).first, let format = CMAudioFormatDescriptionGetStreamBasicDescription(description) { audioChannels = Int(format.pointee.mChannelsPerFrame) - audioRate = Int(format.pointee.mSampleRate) + audioRate = clampedInt(Double(format.pointee.mSampleRate)) } try Task.checkCancellation() - return Video(input: input, duration: duration, width: Int(abs(oriented.width)), - height: Int(abs(oriented.height)), frameDuration: frameDuration, + return Video(input: input, duration: duration, width: clampedInt(Double(abs(oriented.width))), + height: clampedInt(Double(abs(oriented.height))), frameDuration: frameDuration, audioChannels: audioChannels, audioRate: audioRate) } @@ -228,7 +228,22 @@ enum MulticamExporter { } static func time(frames: Int64, frameDuration: CMTime) -> String { - "\(frames * frameDuration.value)/\(frameDuration.timescale)s" + let (value, overflow) = frames.multipliedReportingOverflow(by: frameDuration.value) + return "\(overflow ? Int64.max : value)/\(frameDuration.timescale)s" + } + + /// Media durations, frame durations and capture dates come from untrusted files. + /// Non-finite or absurd values clamp instead of trapping in Int64(_:). 10^12 frames is + /// decades of video at any real frame rate, and leaves room to add offsets safely. + static func frameCount(_ seconds: Double, _ frameDuration: CMTime) -> Int64 { + let frames = (seconds / frameDuration.seconds).rounded() + guard frames.isFinite else { return 0 } + return Int64(max(-1e12, min(1e12, frames))) + } + + static func clampedInt(_ value: Double) -> Int { + guard value.isFinite else { return 0 } + return Int(max(0, min(1e9, value))) } struct Segment { @@ -241,7 +256,7 @@ enum MulticamExporter { // Latest-starting clip wins an overlap, but the underlying clip resumes afterwards. static func segments(_ videos: [Video], starts: [String: Int64], fd: CMTime) throws -> [Segment] { let offsets = videos.map { starts[$0.input.identity.stamp]! } - let ends = zip(videos, offsets).map { $0.1 + Int64(($0.0.duration / fd.seconds).rounded()) } + let ends = zip(videos, offsets).map { $0.1 + frameCount($0.0.duration, fd) } guard Set(offsets).count == offsets.count else { throw Failure.message("Camera timestamps are too close to align at this frame rate.") } @@ -293,8 +308,8 @@ enum MulticamExporter { var starts: [String: Int64] = [:] var total: Int64 = 0 for group in groups { - let start = Int64((group.value[0].input.identity.date.timeIntervalSince(base) / fd.seconds).rounded()) - let durations = group.value.map { Int64(($0.duration / fd.seconds).rounded()) } + let start = frameCount(group.value[0].input.identity.date.timeIntervalSince(base), fd) + let durations = group.value.map { frameCount($0.duration, fd) } guard let shortest = durations.min(), let longest = durations.max(), shortest > 0 else { throw Failure.message("Invalid camera duration at \(group.key). Check the recording files.") } @@ -317,7 +332,7 @@ enum MulticamExporter { sectionStarts.insert(video.input.url) } previousNote = note - previousEnd = start + Int64((video.duration / fd.seconds).rounded()) + previousEnd = start + frameCount(video.duration, fd) } } let sectionMarkers = marker("Folder: \(folderLabel)", start: 0) + groups.map { group in @@ -352,7 +367,7 @@ enum MulticamExporter { assetIDs[video.input.url] = (sourceFormat, asset) resources.append("") let audio = video.audioChannels > 0 ? " hasAudio=\"1\" audioSources=\"1\" audioChannels=\"\(video.audioChannels)\" audioRate=\"\(video.audioRate)\"" : "" - resources.append("") + resources.append("") } let note = video.input.identity.annotation let annotation = segment.sourceStart == 0 && sectionStarts.contains(video.input.url) ? marker(note, start: 0) : "" diff --git a/Sources/DashcamOffloaderApp/SafeFileRead.swift b/Sources/DashcamOffloaderApp/SafeFileRead.swift new file mode 100644 index 0000000..614dddc --- /dev/null +++ b/Sources/DashcamOffloaderApp/SafeFileRead.swift @@ -0,0 +1,28 @@ +import Foundation +import Darwin + +/// Bounded reads of small card files whose names the scanner looks up directly. +/// A card can be formatted with a filesystem that holds symbolic links, pipes and +/// devices, so the object is opened without following a final link and its type +/// and size are checked on the open descriptor, not on the path. +enum SafeFileRead { + static func data(at url: URL, maxBytes: Int) -> Data? { + let descriptor = open(url.path, O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC) + guard descriptor >= 0 else { return nil } + let handle = FileHandle(fileDescriptor: descriptor, closeOnDealloc: true) + var info = stat() + guard fstat(descriptor, &info) == 0, + (info.st_mode & S_IFMT) == S_IFREG, + info.st_size >= 0, + info.st_size <= Int64(maxBytes) else { + return nil + } + do { + // One byte over the limit detects a file that grew after fstat. + let data = try handle.read(upToCount: maxBytes + 1) ?? Data() + return data.count <= maxBytes ? data : nil + } catch { + return nil + } + } +} diff --git a/Sources/DashcamOffloaderApp/SeventyMaiTimestampVerification.swift b/Sources/DashcamOffloaderApp/SeventyMaiTimestampVerification.swift new file mode 100644 index 0000000..a732018 --- /dev/null +++ b/Sources/DashcamOffloaderApp/SeventyMaiTimestampVerification.swift @@ -0,0 +1,58 @@ +import CoreMedia +import Foundation + +/// 70mai filenames use YYYYMMDD-HHMMSS, captured either as two groups (8 + 6 digits) +/// or as six fields. Both must yield the filename time; anything else yields nil. +enum SeventyMaiTimestampVerification { + static func run() -> Bool { + let scanner = CardScanner() + let format = DateFormatter() + format.locale = Locale(identifier: "en_US_POSIX") + format.timeZone = .current + format.dateFormat = "yyyyMMddHHmmss" + let expected = format.date(from: "20300102123456") + let good: [[String]] = [ + ["NO", "20300102", "123456", "000001", "F"], // X800 / T800 + ["2030", "01", "02", "12", "34", "56", "000001"], // M310 six-field form + ] + for groups in good where scanner.parseTimestamp(groups: groups, format: .yyyymmddDashHhmmss) != expected { + print("70MAI TIMESTAMP VERIFY FAIL: \(groups)") + return false + } + let bad: [[String]] = [ + ["NO", "2030010", "123456", "1", "F"], // 13 digits + ["NO", "20301302", "123456", "1", "F"], // month 13 + ["EV", "abcdefgh", "123456", "1", "R"], + [], + ] + for groups in bad where scanner.parseTimestamp(groups: groups, format: .yyyymmddDashHhmmss) != nil { + print("70MAI TIMESTAMP VERIFY FAIL: accepted \(groups)") + return false + } + print("70MAI TIMESTAMP VERIFY PASS: 8+6 and six-field forms parse; short, invalid and non-digit groups rejected") + return true + } +} + +enum SizeSumVerification { + static func run() -> Bool { + guard Int64.max.addingSaturating(1) == .max, + (Int64.max - 5).addingSaturating(Int64.max) == .max, + Int64(40).addingSaturating(2) == 42, + [Int64.max, Int64.max, 7].reduce(Int64(0)) { $0.addingSaturating($1) } == .max else { + print("SIZE SUM VERIFY FAIL"); return false + } + let fd = CMTime(value: 1, timescale: 30) + guard MulticamExporter.frameCount(.infinity, fd) == 0, + MulticamExporter.frameCount(.nan, fd) == 0, + MulticamExporter.frameCount(1e300, fd) == 1_000_000_000_000, + MulticamExporter.frameCount(2, fd) == 60, + MulticamExporter.frameCount(2, CMTime(value: 0, timescale: 30)) == 0, + MulticamExporter.time(frames: .max, frameDuration: CMTime(value: 1001, timescale: 30000)) == "\(Int64.max)/30000s", + MulticamExporter.clampedInt(.nan) == 0 else { + print("MULTICAM CLAMP VERIFY FAIL"); return false + } + print("SIZE SUM VERIFY PASS: forged card sizes saturate and forged media durations clamp instead of trapping") + return true + } +} diff --git a/Sources/DashcamOffloaderApp/TransferViewModel.swift b/Sources/DashcamOffloaderApp/TransferViewModel.swift index 1d4d4f8..00725f0 100644 --- a/Sources/DashcamOffloaderApp/TransferViewModel.swift +++ b/Sources/DashcamOffloaderApp/TransferViewModel.swift @@ -686,7 +686,7 @@ final class TransferViewModel: ObservableObject { // Use the context-clicked row, not the selected card or download destination. // Keep this read-only action available while scanning or copying. - func showSourceInFinder(_ source: MountedSource, open: (URL) -> Bool = { NSWorkspace.shared.open($0) }) { + func showSourceInFinder(_ source: MountedSource, open: (URL) -> Bool = { NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: $0.path) }) { let url = source.url var isDirectory: ObjCBool = false guard url.isFileURL, url.path.hasPrefix("/"), url.path != "/", @@ -1181,6 +1181,11 @@ final class TransferViewModel: ObservableObject { return } guard !copyProgress.isRunning && !isCreatingMulticam else { return } + // The source stays read-only: never download into the card or folder being read. + guard !CopyExecutor.isDestination(runPlan.destinationRoot, insideSource: runPlan.sourceRoot) else { + statusMessage = "Choose a download folder outside the source card or folder" + return + } if retryingItemIDs != nil { statusMessage = "Retrying \(runPlan.items.count) failed files..." @@ -1293,7 +1298,7 @@ final class TransferViewModel: ObservableObject { var lastRunSummaryLine: String { let copied = copyResults.filter { $0.status == .copied } let copiedSupport = supportFileResults.filter { $0.status == .copied } - let copiedBytes = copied.reduce(Int64(0)) { $0 + $1.totalSize } + copiedSupport.reduce(Int64(0)) { $0 + $1.size } + let copiedBytes = copied.reduce(Int64(0)) { $0.addingSaturating($1.totalSize) }.addingSaturating(copiedSupport.reduce(Int64(0)) { $0.addingSaturating($1.size) }) let skippedCount = copyResults.filter { $0.status == .skipped }.count + supportFileResults.filter { $0.status == .skipped }.count var parts = ["Copied \(copied.count + copiedSupport.count) (\(copiedBytes.formattedBytes))"] @@ -1490,20 +1495,17 @@ final class TransferViewModel: ObservableObject { let safeFiles = lastScannedFiles.filter { isSafeTrainingSample($0, sourceRoot: sourceRoot) } let extensionCounts = Dictionary(grouping: safeFiles) { url in - let ext = url.pathExtension.lowercased() - return ext.isEmpty ? "[none]" : ext + FeedbackExtensionKey.key(for: url) } .mapValues(\.count) let mediaExtensionCounts = Dictionary(grouping: safeFiles.filter(isMediaLikeFile)) { url in - let ext = url.pathExtension.lowercased() - return ext.isEmpty ? "[none]" : ext + FeedbackExtensionKey.key(for: url) } .mapValues(\.count) let unrecognizedExtensionCounts = Dictionary(grouping: safeFiles.filter { fileURL in !isMediaLikeFile(fileURL) && !isPotentialSettingsFile(fileURL, sourceRoot: sourceRoot) }) { url in - let ext = url.pathExtension.lowercased() - return ext.isEmpty ? "[none]" : ext + FeedbackExtensionKey.key(for: url) } .mapValues(\.count) let timestampSourceCounts = Dictionary(grouping: eligibleClips) { clip in @@ -1816,8 +1818,7 @@ final class TransferViewModel: ObservableObject { let mediaFiles = folderFiles.filter(isMediaLikeFile) let sizes = folderFiles.compactMap(fileSizeBytes) let extensionCounts = Dictionary(grouping: folderFiles) { fileURL in - let ext = fileURL.pathExtension.lowercased() - return ext.isEmpty ? "[none]" : ext + FeedbackExtensionKey.key(for: fileURL) } .mapValues(\.count) @@ -1881,8 +1882,7 @@ final class TransferViewModel: ObservableObject { let mediaFiles = files.filter(isMediaLikeFile) let sizes = files.compactMap(fileSizeBytes) let extensionCounts = Dictionary(grouping: files) { fileURL in - let ext = fileURL.pathExtension.lowercased() - return ext.isEmpty ? "[none]" : ext + FeedbackExtensionKey.key(for: fileURL) } .mapValues(\.count) let sampleFilenames = files diff --git a/Sources/DashcamOffloaderApp/UpdateService.swift b/Sources/DashcamOffloaderApp/UpdateService.swift index 0fe1ad3..6b2e86c 100644 --- a/Sources/DashcamOffloaderApp/UpdateService.swift +++ b/Sources/DashcamOffloaderApp/UpdateService.swift @@ -340,8 +340,14 @@ struct UpdateService: @unchecked Sendable { } private func sha256Hex(for fileURL: URL) throws -> String { - let data = try Data(contentsOf: fileURL) - let digest = SHA256.hash(data: data) + // Stream the archive in 1 MiB chunks instead of loading it into memory. + let handle = try FileHandle(forReadingFrom: fileURL) + defer { try? handle.close() } + var hasher = SHA256() + while let chunk = try handle.read(upToCount: 1 << 20), !chunk.isEmpty { + hasher.update(data: chunk) + } + let digest = hasher.finalize() return digest.map { String(format: "%02x", $0) }.joined() } diff --git a/Sources/DashcamOffloaderApp/VerificationTest.swift b/Sources/DashcamOffloaderApp/VerificationTest.swift index 1b01c2a..a7f72c4 100644 --- a/Sources/DashcamOffloaderApp/VerificationTest.swift +++ b/Sources/DashcamOffloaderApp/VerificationTest.swift @@ -576,6 +576,8 @@ enum VerificationTest { guard ObservedChannelVerification.run(profiles: profiles), DDPAIZ100Verification.run(profiles: profiles), DDPAIMini3XVerification.run(profiles: profiles), + SeventyMaiTimestampVerification.run(), + SizeSumVerification.run(), Z100StampVerification.run(profiles: profiles), VantrueNamingVerification.run(vantruePickerModels: vantruePickerModels), VantrueIdentityVerification.run(profiles: profiles) else { return false } diff --git a/desktop/package-lock.json b/desktop/package-lock.json index c48565c..1f70639 100644 --- a/desktop/package-lock.json +++ b/desktop/package-lock.json @@ -11,7 +11,7 @@ "electron-updater": "6.8.9" }, "devDependencies": { - "electron": "43.7.8", + "electron": "43.8.12", "electron-builder": "26.15.3" } }, @@ -163,9 +163,9 @@ } }, "node_modules/@electron/get/node_modules/undici": { - "version": "7.30.0", - "resolved": "https://registry.npmjs.org/undici/-/undici-7.30.0.tgz", - "integrity": "sha512-dkrQXeHSaoamnItlYbmzG0wFYrM0ZwDxCIg0A7aKjTyyhh9svRzCNFEzV+Vm05/yehjCzjDZ31KXfGEjYSztDQ==", + "version": "7.30.2", + "resolved": "https://registry.npmjs.org/undici/-/undici-7.30.2.tgz", + "integrity": "sha512-mLyPQnHwUxaM/rideoK/+KL8DaAJLRl4aBCfmX444aCSLnIaomqPLtNRe2jdCYcsk5wZzXrDIQC8ZeYNRAYyIA==", "dev": true, "license": "MIT", "optional": true, @@ -1707,9 +1707,9 @@ } }, "node_modules/electron": { - "version": "43.7.8", - "resolved": "https://registry.npmjs.org/electron/-/electron-43.7.8.tgz", - "integrity": "sha512-h9qnttBn9b+pmnJjagrpjvK97Wb9WbsyIB03BV6NnhnPqSq1Jw7500hNkqHPkosHCE0zL/806v4zf9d3VrII0w==", + "version": "43.8.12", + "resolved": "https://registry.npmjs.org/electron/-/electron-43.8.12.tgz", + "integrity": "sha512-j/yQT+d31UqY0MwEy1AZDBCd7DNyPyAK1P4z2y5L4yDJUktvauL0KskC786Vsil/20zTmqVhpWm7taPK6pL0Ww==", "dev": true, "license": "MIT", "dependencies": { @@ -2089,9 +2089,9 @@ "license": "MIT" }, "node_modules/fast-uri": { - "version": "3.1.8", - "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.8.tgz", - "integrity": "sha512-GZMtZUTNRpOVIECoXwLNZS5xUGE+mVNbTB8h/7Rwh2TFWcBQiPzTgyZi05BF9UMZKkLJv8XBRJTlU7zg8+ZfMg==", + "version": "3.1.9", + "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.9.tgz", + "integrity": "sha512-hS/0Wt3Ya043oKXs0sIxrMpKQITXhMLIrVAKKXi4gozGC9v7YRWVnXIZzjNPTO02M9BdxLCWqK1yRXgNziVyJQ==", "dev": true, "funding": [ { @@ -3789,9 +3789,9 @@ } }, "node_modules/undici": { - "version": "6.29.0", - "resolved": "https://registry.npmjs.org/undici/-/undici-6.29.0.tgz", - "integrity": "sha512-R+RODBqp6i2pPflGdq+xIOUkl+RNfGgHwoinecKu/JCuf2uO06cOKoDbI2P7Dn6KcswdKwrczbU6IYJ6K8X+wg==", + "version": "6.29.1", + "resolved": "https://registry.npmjs.org/undici/-/undici-6.29.1.tgz", + "integrity": "sha512-EvRlUt/QqAEvXdQeRMYUfwOQsfzpDztykrG2owGEE/rBOBoYv9uF39ZzsNDh4lTFHhTr2XOZjKkYgueD12ZmGA==", "dev": true, "license": "MIT", "engines": { diff --git a/desktop/package.json b/desktop/package.json index 968f141..32bafdb 100644 --- a/desktop/package.json +++ b/desktop/package.json @@ -45,7 +45,7 @@ "electron-updater": "6.8.9" }, "devDependencies": { - "electron": "43.7.8", + "electron": "43.8.12", "electron-builder": "26.15.3" }, "allowScripts": { diff --git a/desktop/src/offload-engine.js b/desktop/src/offload-engine.js index 61e7802..fdcb014 100644 --- a/desktop/src/offload-engine.js +++ b/desktop/src/offload-engine.js @@ -6,19 +6,28 @@ const path = require("node:path"); const MEDIA_EXTENSIONS = new Set([".3gp", ".avi", ".jpeg", ".jpg", ".m4v", ".mkv", ".mov", ".mp4", ".png", ".webm"]); const EXCLUDED_DIRECTORIES = new Set([".spotlight-v100", ".trashes", "system volume information"]); -function isPathWithin(childPath, parentPath) { - const relative = path.relative(path.resolve(parentPath), path.resolve(childPath)); - return relative === "" || (!relative.startsWith(`..${path.sep}`) && relative !== ".."); +// pathModule is injectable so Windows drive semantics are testable on any host. +function isPathWithin(childPath, parentPath, pathModule = path) { + const relative = pathModule.relative(pathModule.resolve(parentPath), pathModule.resolve(childPath)); + // path.relative returns an absolute path when the two are on different Windows drives. + return relative === "" || + (!relative.startsWith(`..${pathModule.sep}`) && relative !== ".." && !pathModule.isAbsolute(relative)); } +// Windows treats these stems as devices in any folder ("CON.mp4" opens the console). +const WINDOWS_RESERVED = /^(con|prn|aux|nul|com[1-9\u00b9\u00b2\u00b3]|lpt[1-9\u00b9\u00b2\u00b3])$/i; + function safeFilename(sourcePath) { - const filename = path.basename(sourcePath).replace(/[<>:"/\\|?*\u0000-\u001f]/g, "_").trim(); + // Windows also silently drops trailing dots and spaces, which can merge two names. + let filename = path.basename(sourcePath).replace(/[<>:"/\\|?*\u0000-\u001f]/g, "_").trim().replace(/[. ]+$/, ""); if (!filename || filename === "." || filename === "..") throw new Error("A source filename cannot be used safely."); + if (WINDOWS_RESERVED.test(filename.split(".")[0].trim())) filename = `_${filename}`; return filename; } function safeRelativePath(relativePath) { - const components = relativePath.split(path.sep); + // Windows accepts both separators, so "../x" must count as traversal there too. + const components = relativePath.split(path.sep === "\\" ? /[\\/]/ : path.sep); if (!components.length || components.some((component) => component === "" || component === "." || component === "..")) { throw new Error("A source path cannot be used safely."); } @@ -35,6 +44,101 @@ async function sha256(filePath) { }); } +class UnsafeCopyItemError extends Error {} + +// O_NOFOLLOW refuses a final-component symlink; O_NONBLOCK keeps a named pipe or +// device from blocking the open. Both are 0 where the platform lacks them. +const SOURCE_OPEN_FLAGS = fsSync.constants.O_RDONLY | + (fsSync.constants.O_NOFOLLOW || 0) | (fsSync.constants.O_NONBLOCK || 0); + +// The scan result is only a snapshot. Re-check, at copy time, that the reviewed +// path is still a regular file inside the approved source, and keep the open +// descriptor so the bytes copied are the bytes checked. +async function openVerifiedSource(item) { + let handle; + try { + const linkStat = await fs.lstat(item.sourcePath); + if (!linkStat.isFile()) throw new UnsafeCopyItemError("The source is no longer a regular file and was not copied."); + handle = await fs.open(item.sourcePath, SOURCE_OPEN_FLAGS); + const stat = await handle.stat(); + if (!stat.isFile()) throw new UnsafeCopyItemError("The source is no longer a regular file and was not copied."); + if (item.sourceRoot) { + const [realSource, realRoot] = await Promise.all([fs.realpath(item.sourcePath), fs.realpath(item.sourceRoot)]); + if (!isPathWithin(realSource, realRoot)) { + throw new UnsafeCopyItemError("The source now resolves outside the approved source folder and was not copied."); + } + // Compare identity so a swap between the open and the path check cannot pass. + const realStat = await fs.stat(realSource); + if (realStat.dev !== stat.dev || realStat.ino !== stat.ino) { + throw new UnsafeCopyItemError("The source changed during the copy and was not copied."); + } + } + return handle; + } catch (error) { + await handle?.close().catch(() => {}); + if (error instanceof UnsafeCopyItemError) throw error; + if (["ENOENT", "ENOTDIR", "ELOOP", "EMLINK", "ENXIO"].includes(error.code)) { + throw new UnsafeCopyItemError("The source changed since the scan and was not copied."); + } + throw error; + } +} + +// Create the destination folder, then confirm that symbolic links already in the +// destination did not redirect it outside the chosen folder or back onto the source. +async function prepareDestinationDirectory(item) { + const directory = path.dirname(item.destinationPath); + await fs.mkdir(directory, { recursive: true }); + if (!item.destinationRoot) return; + const [realDirectory, realRoot] = await Promise.all([fs.realpath(directory), fs.realpath(item.destinationRoot)]); + if (!isPathWithin(realDirectory, realRoot)) { + throw new UnsafeCopyItemError("The destination folder resolves outside the chosen download folder; nothing was written."); + } + if (item.sourceRoot) { + let realSourceRoot = null; + try { realSourceRoot = await fs.realpath(item.sourceRoot); } catch { /* source vanished; handled when it is opened */ } + if (realSourceRoot && isPathWithin(realDirectory, realSourceRoot)) { + throw new UnsafeCopyItemError("The destination folder resolves inside the source; nothing was written."); + } + } +} + +function hashStream(stream) { + return new Promise((resolve, reject) => { + const hash = crypto.createHash("sha256"); + stream.on("error", reject); + stream.on("data", (chunk) => hash.update(chunk)); + stream.on("end", () => resolve(hash.digest("hex"))); + }); +} + +// Copy from the verified descriptor into a file this call created exclusively. +// Returns the SHA-256 of the bytes that were read from the source. +async function copyFromHandle(sourceHandle, destinationPath) { + const destinationHandle = await fs.open(destinationPath, "wx"); + try { + const hash = crypto.createHash("sha256"); + const buffer = Buffer.allocUnsafe(1024 * 1024); + let position = 0; + while (true) { + const { bytesRead } = await sourceHandle.read(buffer, 0, buffer.length, position); + if (bytesRead === 0) break; + const chunk = buffer.subarray(0, bytesRead); + hash.update(chunk); + await destinationHandle.writeFile(chunk); + position += bytesRead; + } + await destinationHandle.sync(); + return hash.digest("hex"); + } catch (error) { + await destinationHandle.close().catch(() => {}); + await fs.unlink(destinationPath).catch(() => {}); + throw error; + } finally { + await destinationHandle.close().catch(() => {}); + } +} + async function scanSource(sourcePath) { const root = path.resolve(sourcePath); const rootStat = await fs.stat(root); @@ -75,7 +179,11 @@ function planCopy(sourcePath, destinationPath, media) { if (isPathWithin(destination, source)) throw new Error("Choose a download folder outside the source card or folder."); return media.map((item) => { if (!isPathWithin(item.sourcePath, source)) throw new Error("A planned source file is outside the approved source folder."); - return { ...item, destinationPath: path.join(destination, safeRelativePath(item.relativePath)) }; + const destinationPath = path.join(destination, safeRelativePath(item.relativePath)); + if (!isPathWithin(destinationPath, destination) || destinationPath === destination) { + throw new Error("A planned destination is outside the download folder."); + } + return { ...item, sourceRoot: source, destinationRoot: destination, destinationPath }; }); } @@ -85,25 +193,34 @@ async function executeCopy(plan, onProgress = () => {}) { const conflicts = []; for (let index = 0; index < plan.length; index += 1) { const item = plan[index]; - await fs.mkdir(path.dirname(item.destinationPath), { recursive: true }); + let sourceHandle; try { - const destinationStat = await fs.stat(item.destinationPath); - if (destinationStat.size === item.bytes && await sha256(item.sourcePath) === await sha256(item.destinationPath)) { - skipped += 1; - onProgress({ index: index + 1, total: plan.length, item, status: "Skipped" }); + await prepareDestinationDirectory(item); + sourceHandle = await openVerifiedSource(item); + + let destinationExists = true; + try { + // lstat: a symbolic link at the destination is a conflict, never a write target. + const destinationStat = await fs.lstat(item.destinationPath); + if (destinationStat.isFile() && destinationStat.size === item.bytes && + await hashStream(sourceHandle.createReadStream({ autoClose: false, start: 0 })) === await sha256(item.destinationPath)) { + skipped += 1; + onProgress({ index: index + 1, total: plan.length, item, status: "Skipped" }); + continue; + } + } catch (error) { + if (error.code !== "ENOENT") throw error; + destinationExists = false; + } + if (destinationExists) { + conflicts.push({ ...item, reason: "An existing destination file differs and was left untouched." }); + onProgress({ index: index + 1, total: plan.length, item, status: "Conflict" }); continue; } - conflicts.push({ ...item, reason: "An existing destination file differs and was left untouched." }); - onProgress({ index: index + 1, total: plan.length, item, status: "Conflict" }); - continue; - } catch (error) { - if (error.code !== "ENOENT") throw error; - } - try { - await fs.copyFile(item.sourcePath, item.destinationPath, fs.constants.COPYFILE_EXCL); + const sourceDigest = await copyFromHandle(sourceHandle, item.destinationPath); const destinationStat = await fs.stat(item.destinationPath); - if (destinationStat.size !== item.bytes || await sha256(item.sourcePath) !== await sha256(item.destinationPath)) { + if (destinationStat.size !== item.bytes || sourceDigest !== await sha256(item.destinationPath)) { await fs.unlink(item.destinationPath); throw new Error("Copied file did not verify and was removed."); } @@ -112,12 +229,20 @@ async function executeCopy(plan, onProgress = () => {}) { } catch (error) { if (error.code === "EEXIST") { conflicts.push({ ...item, reason: "A destination file appeared during copy and was left untouched." }); + onProgress({ index: index + 1, total: plan.length, item, status: "Conflict" }); + continue; + } + if (error instanceof UnsafeCopyItemError) { + conflicts.push({ ...item, reason: error.message }); + onProgress({ index: index + 1, total: plan.length, item, status: "Not copied" }); continue; } throw error; + } finally { + await sourceHandle?.close().catch(() => {}); } } return { copied, skipped, conflicts }; } -module.exports = { executeCopy, isPathWithin, planCopy, scanSource, sha256 }; +module.exports = { executeCopy, isPathWithin, planCopy, safeFilename, scanSource, sha256 }; diff --git a/desktop/test/hostile-card.test.js b/desktop/test/hostile-card.test.js new file mode 100644 index 0000000..e3b8230 --- /dev/null +++ b/desktop/test/hostile-card.test.js @@ -0,0 +1,192 @@ +const assert = require("node:assert/strict"); +const fs = require("node:fs/promises"); +const os = require("node:os"); +const path = require("node:path"); +const test = require("node:test"); +const { executeCopy, isPathWithin, planCopy, scanSource } = require("../src/offload-engine"); + +async function fixture() { + const root = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), "dashcam-offloader-hostile-"))); + const source = path.join(root, "card"); + const destination = path.join(root, "destination"); + const outside = path.join(root, "outside"); + await fs.mkdir(path.join(source, "Record"), { recursive: true }); + await fs.mkdir(destination); + await fs.mkdir(outside); + await fs.writeFile(path.join(source, "Record", "front.mp4"), "front-video"); + await fs.writeFile(path.join(outside, "secret.mp4"), "HOST-SECRET"); + return { root, source, destination, outside }; +} + +async function listTree(directory) { + const names = []; + for (const entry of await fs.readdir(directory, { withFileTypes: true, recursive: true })) { + names.push(path.relative(directory, path.join(entry.parentPath ?? entry.path, entry.name))); + } + return names.sort(); +} + +const symlinkTest = process.platform === "win32" ? test.skip : test; + +symlinkTest("scan ignores symlinked files and directories planted on a card", async (t) => { + const { root, source, outside } = await fixture(); + t.after(() => fs.rm(root, { recursive: true, force: true })); + await fs.symlink(path.join(outside, "secret.mp4"), path.join(source, "Record", "link.mp4")); + await fs.symlink(outside, path.join(source, "LinkedFolder")); + const media = await scanSource(source); + assert.deepEqual(media.map((item) => item.relativePath), [path.join("Record", "front.mp4")]); +}); + +symlinkTest("does not copy a source file that became a symlink after the scan", async (t) => { + const { root, source, destination, outside } = await fixture(); + t.after(() => fs.rm(root, { recursive: true, force: true })); + const media = await scanSource(source); + const plan = planCopy(source, destination, media); + + // Swap the reviewed clip for a link to a file outside the approved source. + const clip = path.join(source, "Record", "front.mp4"); + await fs.rm(clip); + await fs.writeFile(path.join(outside, "secret.mp4"), "HOST-SECRET".padEnd(11)); + await fs.symlink(path.join(outside, "secret.mp4"), clip); + + const result = await executeCopy(plan); + assert.equal(result.copied, 0); + assert.equal(result.conflicts.length, 1); + assert.deepEqual(await listTree(destination), ["Record"]); +}); + +symlinkTest("does not copy through a source folder that became a symlink after the scan", async (t) => { + const { root, source, destination, outside } = await fixture(); + t.after(() => fs.rm(root, { recursive: true, force: true })); + const media = await scanSource(source); + const plan = planCopy(source, destination, media); + + await fs.rm(path.join(source, "Record"), { recursive: true }); + await fs.writeFile(path.join(outside, "front.mp4"), "front-video"); + await fs.symlink(outside, path.join(source, "Record")); + + const result = await executeCopy(plan); + assert.equal(result.copied, 0); + assert.equal(result.conflicts.length, 1); + assert.deepEqual(await listTree(destination), ["Record"]); +}); + +symlinkTest("does not write through a symlinked folder that leaves the chosen destination", async (t) => { + const { root, source, destination, outside } = await fixture(); + t.after(() => fs.rm(root, { recursive: true, force: true })); + const media = await scanSource(source); + const plan = planCopy(source, destination, media); + + await fs.symlink(outside, path.join(destination, "Record")); + const before = await listTree(outside); + + const result = await executeCopy(plan); + assert.equal(result.copied, 0); + assert.equal(result.conflicts.length, 1); + assert.deepEqual(await listTree(outside), before); +}); + +symlinkTest("does not write back onto the source through a destination symlink", async (t) => { + const { root, source, destination } = await fixture(); + t.after(() => fs.rm(root, { recursive: true, force: true })); + await fs.mkdir(path.join(source, "Event")); + const media = await scanSource(source); + const plan = planCopy(source, destination, media); + + await fs.symlink(path.join(source, "Event"), path.join(destination, "Record")); + const result = await executeCopy(plan); + assert.equal(result.copied, 0); + assert.deepEqual(await fs.readdir(path.join(source, "Event")), []); +}); + +symlinkTest("does not hang on a clip that was replaced by a named pipe after the scan", async (t) => { + const { root, source, destination } = await fixture(); + t.after(() => fs.rm(root, { recursive: true, force: true })); + const media = await scanSource(source); + const plan = planCopy(source, destination, media); + const clip = path.join(source, "Record", "front.mp4"); + await fs.rm(clip); + require("node:child_process").execFileSync("mkfifo", [clip]); + + const result = await Promise.race([ + executeCopy(plan), + new Promise((resolve) => setTimeout(() => resolve("hung"), 1500).unref()) + ]); + assert.notEqual(result, "hung"); + assert.equal(result.copied, 0); + assert.equal(result.conflicts.length, 1); +}); + +test("a source file that changed after the scan is not reported as verified", async (t) => { + const { root, source, destination } = await fixture(); + t.after(() => fs.rm(root, { recursive: true, force: true })); + const media = await scanSource(source); + const plan = planCopy(source, destination, media); + await fs.writeFile(path.join(source, "Record", "front.mp4"), "front-video-with-more-bytes"); + + await assert.rejects(() => executeCopy(plan), /did not verify/); + assert.deepEqual(await listTree(destination), ["Record"]); +}); + +test("one unsafe item does not stop the remaining verified copies", async (t) => { + const { root, source, destination } = await fixture(); + t.after(() => fs.rm(root, { recursive: true, force: true })); + await fs.mkdir(path.join(source, "Event")); + await fs.writeFile(path.join(source, "Event", "event.mp4"), "event-video"); + const media = await scanSource(source); + const plan = planCopy(source, destination, media); + await fs.rm(path.join(source, "Event", "event.mp4")); + + const result = await executeCopy(plan); + assert.equal(result.copied, 1); + assert.equal(result.conflicts.length, 1); + assert.equal(await fs.readFile(path.join(destination, "Record", "front.mp4"), "utf8"), "front-video"); +}); + +test("hostile names cannot leave the destination", async (t) => { + const { root, source, destination } = await fixture(); + t.after(() => fs.rm(root, { recursive: true, force: true })); + const hostile = [ + "..\\..\\escape.mp4", + "a:b|c?.mp4", + " lead and trail .mp4", + `${"x".repeat(200)}.mp4`, + "new\nline\u0007.mp4", + "-rf.mp4", + "$(touch pwned).mp4" + ].filter((name) => process.platform !== "win32" || !/[<>:"|?*\u0000-\u001f\\]/.test(name)); // Windows can't create these names + for (const name of hostile) await fs.writeFile(path.join(source, "Record", name), name); + const media = await scanSource(source); + const plan = planCopy(source, destination, media); + for (const item of plan) assert.equal(isPathWithin(item.destinationPath, destination), true); + const result = await executeCopy(plan); + // front.mp4 from the fixture plus every hostile name except dot-prefixed ones, which are hidden and never planned. + assert.equal(media.length, 1 + hostile.filter((name) => !name.startsWith(".")).length); + assert.equal(result.copied + result.conflicts.length, media.length); + for (const name of await listTree(root)) { + assert.equal(name.startsWith("card") || name.startsWith("destination") || name.startsWith("outside"), true, name); + } + assert.deepEqual((await fs.readdir(source)).sort(), ["Record"]); +}); + +test("planned media cannot be redirected with traversal segments", () => { + const source = path.resolve("/card"); + const destination = path.resolve("/downloads"); + for (const relativePath of ["../escape.mp4", path.join("Record", "..", "..", "escape.mp4"), "", "."]) { + assert.throws(() => planCopy(source, destination, [{ + sourcePath: path.join(source, "Record", "clip.mp4"), + relativePath, + filename: "clip.mp4", + bytes: 1, + kind: "Video" + }]), /cannot be used safely/); + } +}); + +test("containment treats another Windows drive as outside", () => { + assert.equal(isPathWithin("C:\\Users\\me\\Videos", "E:\\", path.win32), false); + assert.equal(isPathWithin("E:\\DCIM\\clip.mp4", "E:\\", path.win32), true); + assert.equal(isPathWithin("E:\\..data\\clip.mp4", "E:\\", path.win32), true); + assert.equal(isPathWithin("/card/../other", "/card", path.posix), false); + assert.equal(isPathWithin("/card/..hidden/clip.mp4", "/card", path.posix), true); +}); diff --git a/desktop/test/windows-names.test.js b/desktop/test/windows-names.test.js new file mode 100644 index 0000000..2824576 --- /dev/null +++ b/desktop/test/windows-names.test.js @@ -0,0 +1,13 @@ +const test = require("node:test"); +const assert = require("node:assert/strict"); +const { safeFilename } = require("../src/offload-engine"); + +test("Windows device names and trailing dots/spaces never reach the destination as-is", () => { + for (const [input, expected] of [ + ["CON.mp4", "_CON.mp4"], ["con.MP4", "_con.MP4"], ["NUL", "_NUL"], ["com1.mov", "_com1.mov"], + ["LPT9.tar.mp4", "_LPT9.tar.mp4"], ["aux .mp4", "_aux .mp4"], + ["clip.mp4.", "clip.mp4"], ["clip.mp4 . ", "clip.mp4"], + ["CONSOLE.mp4", "CONSOLE.mp4"], ["COM10.mp4", "COM10.mp4"], ["20300101_120000_NF.mp4", "20300101_120000_NF.mp4"], + ]) assert.equal(safeFilename(`/card/DCIM/${input}`), expected, input); + for (const bad of ["...", " . "]) assert.throws(() => safeFilename(`/card/${bad}`)); +}); diff --git a/docs/releases/latest.md b/docs/releases/latest.md index 35aab16..df610c7 100644 --- a/docs/releases/latest.md +++ b/docs/releases/latest.md @@ -1,8 +1,14 @@ -# Dashcam Offloader 0.1.44 +# Dashcam Offloader 0.1.45 -- Show automatically recognized MINI3X and other supported exact identities as detected, while preserving explicit manual selections as manual—even for validated profiles. -- Preserve Z100 main-unit identity caveats and keep unsupported, generic, and unscanned cards clearly distinguished from verified detection. Recognition does not establish a retail revision or recording event. -- Correct executable filename patterns for ROVE R2-4K PRO and VANTRUE E1 Pro/E1 Pro S profiles so generated catalogs preserve their intended matching behavior. -- Add production-property regression checks for detection and selection wording to both source verification and release checks. +Security and reliability hardening from an independent review: -Existing signed in-app updates and private submission behavior are unchanged. This macOS build is ad-hoc signed, not notarized; a first installation may require normal macOS Open Anyway approval. +- A card that fails or is removed mid-copy no longer crashes the app or leaves a truncated clip under its final name. +- Copies never follow links planted on a card or in the download folder, never write onto the source card, and refuse a download folder inside the card. +- "Preserve camera settings" copies regular files only; small camera metadata files are read with size limits and without following links. +- Merged GoPro exports appear under their final name only when complete. +- Copies are flushed to disk before they are verified. +- Very large or malformed cards can no longer exhaust memory or crash the app. +- 70mai filename timestamps are now recognized. +- Feedback uploads use no cookies and refuse redirects; private evidence no longer keeps coordinate-like file names. + +Existing signed in-app updates are unchanged. This macOS build is ad-hoc signed, not notarized; a first installation may require normal macOS Open Anyway approval. diff --git a/docs/security-audit-2026-08-13.md b/docs/security-audit-2026-08-13.md index e177151..ddb238e 100644 --- a/docs/security-audit-2026-08-13.md +++ b/docs/security-audit-2026-08-13.md @@ -90,3 +90,8 @@ A replacement desktop release must not be published until all of these pass: production code signing and update-chain verification are complete for that platform. The native macOS updater uses the separately documented signed manifest and strict ad-hoc bundle-integrity model. + +> **Update, 2026-11-19:** the "filenames" removal above applies to ordinary +> feedback. Opt-in private evidence (added later) keeps bounded media basenames +> after structural checks, disclosed in the review step. See +> `workers/feedback/README.md` and the 2026-11-19 security review fixes. diff --git a/scripts/build-macos-app.sh b/scripts/build-macos-app.sh index 74b68d7..9165f1f 100755 --- a/scripts/build-macos-app.sh +++ b/scripts/build-macos-app.sh @@ -53,9 +53,9 @@ cat > "$CONTENTS_DIR/Info.plist" <CFBundlePackageType APPL CFBundleShortVersionString - 0.1.44 + 0.1.45 CFBundleVersion - 44 + 45 DashcamOffloaderBuildCommit $BUILD_COMMIT LSMinimumSystemVersion diff --git a/scripts/ingest-submitted-scans.py b/scripts/ingest-submitted-scans.py index 0af8caa..e6227ab 100644 --- a/scripts/ingest-submitted-scans.py +++ b/scripts/ingest-submitted-scans.py @@ -35,7 +35,7 @@ def safe_media(value): and not any(unicodedata.category(char) in {"Cc", "Cf", "Cs", "Zl", "Zp"} for char in value) and len(value.encode("utf-8")) <= 180 and SAFE_MEDIA.fullmatch(value) and "/" not in value and "\\" not in value and ".." not in value - and not re.search(r"(?:wifi|password|credential|gps|serial)|[^\s@]+@[^\s@]+\.[^\s@]+|\d{1,3}\.\d{3,}", value, re.I)) + and not re.search(r"(?:wifi|password|credential|gps|serial)|[^\s@]+@[^\s@]+\.[^\s@]+|\d{1,3}\.\d{3,}|(? address); + +const HOST_PATH = "/Users/example/Dashcam Card"; +const COORDINATES = "37.774929,-122.419416"; + +function environment() { + const stored = new Map(); + const limiters = new Map(); + return { + stored, + limiters, + RATE_LIMIT_SALT: "s".repeat(32), + FEEDBACK_KV: { async put(key, value) { stored.set(key, value); } }, + FEEDBACK_RATE_LIMITER: { + idFromName: (name) => name, + get(name) { + if (!limiters.has(name)) { + const data = new Map(); + const storage = { get: async (key) => data.get(key), put: async (key, value) => { data.set(key, value); } }; + limiters.set(name, new FeedbackRateLimiter({ storage: { transaction: (run) => run(storage) } })); + } + return limiters.get(name); + }, + }, + }; +} + +function post(body, ip = "203.0.113.7") { + return new Request("https://feedback.example/feedback", { + method: "POST", + headers: { "Content-Type": "application/json", "CF-Connecting-IP": ip }, + body: typeof body === "string" ? body : JSON.stringify(body), + }); +} + +test("free-form scan fields cannot carry host paths or coordinates into storage", () => { + const scan = sanitizeScan({ + identifiedCamera: { manufacturer: `${HOST_PATH}/maker`, model: `Model at ${COORDINATES}`, isSupported: true }, + candidates: [{ profileID: "viofo-a229-pro", profileName: "VIOFO A229 Pro", score: 90, confidence: `high ${HOST_PATH}` }], + videoSpecSummaries: [{ extensionLowercased: `${HOST_PATH}/clip.mp4`, fileCount: 1 }], + extensionCounts: { mp4: 3, "passport.jane-example-1984": 1, janeexample1984: 1, "[none]": 2 }, + unrecognizedExtensionCounts: { janeexample1984: 1, thm: 2 }, + directorySummaries: [{ path: "DCIM", extensionCounts: { mp4: 1, janeexample1984: 1 } }], + folderSummaries: [{ path: "DCIM", extensionCounts: { mp4: 1, janeexample1984: 1 } }], + }); + const stored = JSON.stringify(scan); + assert.equal(stored.includes(HOST_PATH.slice(0, 7)), false, stored); + assert.equal(stored.includes("37.774929"), false, stored); + assert.equal(stored.includes("janeexample1984"), false, stored); + assert.equal(scan.identifiedCamera, null); + assert.equal(scan.candidates[0].confidence, ""); + assert.equal(scan.videoSpecSummaries[0].extensionLowercased, ""); + assert.deepEqual(scan.extensionCounts, { mp4: 3 }); + assert.deepEqual(scan.unrecognizedExtensionCounts, { thm: 2 }); + assert.deepEqual(scan.directorySummaries[0].extensionCounts, { mp4: 1 }); + assert.deepEqual(scan.folderSummaries[0].extensionCounts, { mp4: 1 }); +}); + +test("ordinary camera identity, confidence and extension values are still retained", () => { + const scan = sanitizeScan({ + identifiedCamera: { manufacturer: "VIOFO", model: "A229 Pro (3CH)", isSupported: true }, + candidates: [{ profileID: "viofo-a229-pro", profileName: "VIOFO A229 Pro", score: 90, confidence: "high" }], + videoSpecSummaries: [{ extensionLowercased: "MP4", fileCount: 1 }], + extensionCounts: { mp4: 3, JPG: 1, m2ts: 1 }, + }); + assert.deepEqual(scan.identifiedCamera, { manufacturer: "VIOFO", model: "A229 Pro (3CH)", evidence: [], isSupported: true }); + assert.equal(scan.candidates[0].confidence, "high"); + assert.equal(scan.videoSpecSummaries[0].extensionLowercased, "mp4"); + assert.deepEqual(scan.extensionCounts, { mp4: 3, jpg: 1, m2ts: 1 }); +}); + +test("app version and timestamp are bounded technical values", () => { + const record = sanitizeFeedback({ + kind: "bug", + message: "It crashed", + appVersion: `0.1.44 ${HOST_PATH} ${"x".repeat(5000)}`, + createdAt: { toString: () => HOST_PATH }, + }); + assert.equal(record.appVersion, ""); + assert.equal(record.createdAt, ""); + const ordinary = sanitizeFeedback({ kind: "bug", message: "ok", appVersion: "0.1.44 213 76f37b9", createdAt: "2026-11-19T10:11:12Z" }); + assert.equal(ordinary.appVersion, "0.1.44 213 76f37b9"); + assert.equal(ordinary.createdAt, "2026-11-19T10:11:12Z"); +}); + +test("malformed scan arrays are rejected or ignored instead of crashing the Worker", async () => { + for (const scan of [ + { candidates: [null] }, + { candidates: ["text", 7] }, + { scanDiagnostics: [null] }, + { scanDiagnostics: [[]] }, + { mediaFileSamples: [null], videoSpecSummaries: [null], settingSnapshots: [null], directorySummaries: [null], folderSummaries: [null] }, + ]) { + assert.doesNotThrow(() => validateFeedback({ kind: "bug", message: "x", scan }), JSON.stringify(scan)); + const env = environment(); + const response = await worker.fetch(post({ kind: "bug", message: "x", scan }), env); + assert.equal(response.status, 202, JSON.stringify(scan)); + const record = JSON.parse([...env.stored.values()][0]); + assert.deepEqual(record.scan.candidates, []); + assert.deepEqual(record.scan.scanDiagnostics, []); + } +}); + +test("non-object training and scalar scan payloads never throw", async () => { + for (const body of [ + { kind: "bug", message: "x", training: "text" }, + { kind: "bug", message: "x", training: 5 }, + { kind: "bug", message: "x", scan: 5 }, + { kind: "bug", message: "x", scan: "text" }, + { kind: "bug", message: "x", contact: 12345 }, + ]) { + const response = await worker.fetch(post(body), environment()); + assert.ok([202, 400].includes(response.status), `${JSON.stringify(body)} -> ${response.status}`); + } +}); + +test("rotating addresses inside one IPv6 /64 shares a single rate-limit bucket", async () => { + assert.equal(rateLimitClientKey("2001:db8:1:2:aaaa:bbbb:cccc:dddd"), rateLimitClientKey("2001:db8:1:2::1")); + assert.equal(rateLimitClientKey("2001:DB8:1:2::1"), rateLimitClientKey("2001:db8:0001:0002:ffff::")); + assert.notEqual(rateLimitClientKey("2001:db8:1:2::1"), rateLimitClientKey("2001:db8:1:3::1")); + assert.notEqual(rateLimitClientKey("2001:db8::1"), rateLimitClientKey("2001:db8:1::1")); + assert.equal(rateLimitClientKey("203.0.113.7"), "203.0.113.7"); + assert.notEqual(rateLimitClientKey("203.0.113.7"), rateLimitClientKey("203.0.113.8")); + assert.equal(rateLimitClientKey("::ffff:203.0.113.7"), "203.0.113.7"); + assert.equal(rateLimitClientKey("not an address"), "not an address"); + + const env = environment(); + let accepted = 0; + for (let index = 0; index < 60; index += 1) { + const response = await worker.fetch(post({ kind: "bug", message: "x" }, `2001:db8:1:2::${(index + 1).toString(16)}`), env); + if (response.status === 202) accepted += 1; + } + assert.equal(accepted, 20); + assert.equal(env.stored.size, 20); +}); + +test("browser-originated posts are refused unless the origin is allowlisted", async () => { + const req = (origin) => new Request("https://feedback.example/feedback", { + method: "POST", headers: { "Content-Type": "application/json", ...(origin ? { Origin: origin } : {}) }, body: "{}", + }); + assert.equal(feedback.originPermitted(req(), {}), true, "native app (no Origin)"); + assert.equal(feedback.originPermitted(req("https://evil.example"), {}), false); + assert.equal(feedback.originPermitted(req("null"), {}), false); + assert.equal(feedback.originPermitted(req("https://ok.example"), { ALLOWED_ORIGINS: "https://ok.example" }), true); + const response = await worker.fetch(req("https://evil.example"), {}); + assert.equal(response.status, 403); + assert.equal((await response.json()).error, "forbidden_origin"); +}); diff --git a/workers/feedback/media-basename-cases.json b/workers/feedback/media-basename-cases.json index 662d2ab..ef076ec 100644 --- a/workers/feedback/media-basename-cases.json +++ b/workers/feedback/media-basename-cases.json @@ -9,7 +9,11 @@ "café.MOV", "🎥2030.mp4", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.mp4", - "照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照aa.mp4" + "照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照照aa.mp4", + "20300101_120000_NF.mp4", + "REC_2030_0101_1200_N.MP4", + "v2.1-front.MP4", + "FILE2030.1N.mp4" ], "rejected": [ "../clip.mp4", @@ -32,6 +36,10 @@ "gps2030.mp4", "wifi-password.mp4", "person\u0040example.com.mp4", - "47.61234_-122.12345.mp4" + "47.61234_-122.12345.mp4", + "40.71N_74.00W.mp4", + "N40.71_W74.00.MP4", + "clip 47.6 N 122.3 W.mov", + "12.5°N.jpg" ] } diff --git a/workers/feedback/worker.js b/workers/feedback/worker.js index 758640d..971cd7b 100644 --- a/workers/feedback/worker.js +++ b/workers/feedback/worker.js @@ -54,6 +54,12 @@ export default { return json({ error: "method_not_allowed" }, 405, request, env); } + // The Mac app sends no Origin. A browser always does, so refuse browser-originated + // posts (cross-site spam from visitors' browsers) unless the origin is allowlisted. + if (!originPermitted(request, env)) { + return json({ error: "forbidden_origin" }, 403, request, env); + } + const rateLimit = await checkRateLimit(request, env); if (!rateLimit.ok) { return json( @@ -164,7 +170,25 @@ async function readJSONBody(request) { async function hashedClientID(request, env) { const rawClient = request.headers.get("CF-Connecting-IP") || "unknown"; - return clientFingerprint(rawClient, String(env.RATE_LIMIT_SALT || "")); + return clientFingerprint(rateLimitClientKey(rawClient), String(env.RATE_LIMIT_SALT || "")); +} + +// One subscriber usually controls a whole IPv6 /64, so per-address counters let a +// single client rotate through addresses without limit. Count IPv6 by /64 prefix. +function rateLimitClientKey(rawClient) { + const address = String(rawClient).trim().toLowerCase(); + if (!address.includes(":") || !/^[0-9a-f:.]+$/.test(address)) return address; + const mapped = /^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/.exec(address); + if (mapped) return mapped[1]; + const halves = address.split("::"); + if (halves.length > 2) return address; + const head = halves[0] ? halves[0].split(":") : []; + const tail = halves.length === 2 && halves[1] ? halves[1].split(":") : []; + if (halves.length === 1 && head.length !== 8) return address; + const missing = 8 - head.length - tail.length; + if (missing < 0 || [...head, ...tail].some((group) => !/^[0-9a-f]{1,4}$/.test(group))) return address; + const groups = [...head, ...Array(missing).fill("0"), ...tail]; + return `${groups.slice(0, 4).map((group) => group.replace(/^0+(?=.)/, "")).join(":")}::/64`; } async function clientFingerprint(rawClient, salt) { @@ -245,19 +269,54 @@ function validateFeedback(body) { } function sanitizeFeedback(body) { - const scan = body.scan ? sanitizeScan(body.scan) : null; + const scan = body.scan && typeof body.scan === "object" && !Array.isArray(body.scan) ? sanitizeScan(body.scan) : null; return { kind: body.kind, message: body.message.trim(), contact: String(body.contact || "").trim(), - appVersion: String(body.appVersion || ""), - createdAt: String(body.createdAt || ""), - training: body.training ? sanitizeTraining(body.training) : null, + appVersion: appVersionValue(body.appVersion), + createdAt: timestampValue(body.createdAt), + training: body.training && typeof body.training === "object" && !Array.isArray(body.training) + ? sanitizeTraining(body.training) : null, scan, }; } +// Client-supplied but not user-typed: keep only the shapes the app produces. +function appVersionValue(value) { + return typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9 ._+()-]{0,63}$/.test(value) ? value : ""; +} + +function timestampValue(value) { + return typeof value === "string" && + /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:\d{2})$/.test(value) ? value : ""; +} + +// A file extension is the tail of a user-controlled name: keep short ASCII tokens only. +function extensionKey(value) { + const key = stringValue(value).trim().toLowerCase(); + return /^[a-z0-9]{1,10}$/.test(key) ? key : ""; +} + +function extensionCountMap(value) { + if (!value || typeof value !== "object" || Array.isArray(value)) return {}; + const result = {}; + for (const [key, count] of Object.entries(value)) { + const safeKey = extensionKey(key); + if (safeKey) result[safeKey] = (result[safeKey] || 0) + numberValue(count); + } + return result; +} + +// Camera make/model labels: catalog-style names, never paths, addresses or coordinates. +function cameraLabel(value) { + const label = stringValue(value).trim(); + const word = "[\\p{L}\\p{N}][\\p{L}\\p{N} ._+()\\[\\]&'-]*"; + return label.length <= 80 && new RegExp(`^${word}(?: / ${word})*$`, "u").test(label) && + !isSensitiveSettingPair(label, "") ? label : ""; +} + function sanitizeTraining(training) { return { manufacturer: stringValue(training.manufacturer).trim(), @@ -272,7 +331,7 @@ function safeMediaBasename(name) { return typeof name === "string" && new TextEncoder().encode(name).length <= 180 && !name.startsWith(".") && !name.includes("..") && !/[/\\\p{Cc}\p{Cf}\p{Cs}\p{Zl}\p{Zp}]/u.test(name) && - !/(?:wifi|password|credential|gps|serial)|[^\s@]+@[^\s@]+\.[^\s@]+|\d{1,3}\.\d{3,}/i.test(name) && + !/(?:wifi|password|credential|gps|serial)|[^\s@]+@[^\s@]+\.[^\s@]+|\d{1,3}\.\d{3,}|(? value.trim()).filter(Boolean); +} + +function originPermitted(request, env) { + const origin = request?.headers?.get("Origin"); + return origin === null || origin === undefined || configuredOrigins(env).includes(origin); +} + function allowedOrigin(request, env) { const origin = request?.headers?.get("Origin") || ""; const configured = String(env.ALLOWED_ORIGINS || "") @@ -932,6 +1002,7 @@ function allowedOrigin(request, env) { } export { + originPermitted, validateFeedback, sanitizeFeedback, sanitizeScan, @@ -940,4 +1011,5 @@ export { isSensitiveSettingPair, isSafeSettingPair, clientFingerprint, + rateLimitClientKey, }; diff --git a/workers/updates/hardening.test.mjs b/workers/updates/hardening.test.mjs new file mode 100644 index 0000000..803cf03 --- /dev/null +++ b/workers/updates/hardening.test.mjs @@ -0,0 +1,30 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import worker from "./worker.js"; + +const manifest = { assetName: "Dashcam-Offloader-current.zip", assetKey: "dashcam-offloader/releases/Dashcam-Offloader-current.zip" }; +const env = { + UPDATES_BUCKET: { + async get(key) { + return key === "dashcam-offloader/latest.json" + ? { body: JSON.stringify(manifest), httpEtag: '"m"', json: async () => manifest, writeHttpMetadata() {} } + : { body: "archive bytes", httpEtag: '"a"', writeHttpMetadata() {} }; + }, + }, +}; + +test("malformed percent-encoding in a download name is a 404, not an unhandled exception", async () => { + for (const suffix of ["%", "%E0%A4%A", "%zz.zip", "Dashcam%2"]) { + const response = await worker.fetch(new Request(`https://updates.example/dashcam-offloader/download/${suffix}`), env); + assert.equal(response.status, 404, suffix); + } +}); + +test("only the current archive name is served and encoded separators are refused", async () => { + const ok = await worker.fetch(new Request("https://updates.example/dashcam-offloader/download/Dashcam-Offloader-current.zip"), env); + assert.equal(ok.status, 200); + for (const suffix of ["..%2Flatest.json", "%2e%2e%2flatest.json", "old.zip", "a%5Cb.zip"]) { + const response = await worker.fetch(new Request(`https://updates.example/dashcam-offloader/download/${suffix}`), env); + assert.equal(response.status, 404, suffix); + } +}); diff --git a/workers/updates/worker.js b/workers/updates/worker.js index 237744a..91312da 100644 --- a/workers/updates/worker.js +++ b/workers/updates/worker.js @@ -197,7 +197,12 @@ export default { const downloadPrefix = "/dashcam-offloader/download/"; if (path.startsWith(downloadPrefix)) { - const assetName = decodeURIComponent(path.slice(downloadPrefix.length)); + let assetName; + try { + assetName = decodeURIComponent(path.slice(downloadPrefix.length)); + } catch { + return notFound(); + } if (!assetName || assetName.includes("/") || assetName.includes("\\")) { return notFound(); }