From 1365db96e417142427906c9c0e82fb062b65f40f Mon Sep 17 00:00:00 2001 From: Guoliang Chen Date: Wed, 16 Sep 2026 04:59:04 -0700 Subject: [PATCH] clipboard: clear the offer before examining a new copy; text/image size caps Clipboard.cs:111-114 nulls LastClipboardData and LastDragDropFile before it looks at the new clipboard, so a branch that decides not to share leaves nothing for a peer to pull. FormHelper.cs:204-210 and :287 refuse text over 20 MB and images over 50 MB at the reader. Co-Authored-By: Claude Opus 5 (1M context) --- Sources/MWBMac/Pasteboard.swift | 19 ++++++++++++++----- Sources/mwb/ClipboardBridge.swift | 4 ++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Sources/MWBMac/Pasteboard.swift b/Sources/MWBMac/Pasteboard.swift index 82a5330..55a83a5 100644 --- a/Sources/MWBMac/Pasteboard.swift +++ b/Sources/MWBMac/Pasteboard.swift @@ -120,6 +120,10 @@ public final class PasteboardWatcher: @unchecked Sendable { // and all — so the wire form is built here rather than in the codec. text.html = html.hasPrefix("Version:") ? html : ClipboardCodec.cfHTML(wrapping: html) } + // `FormHelper.cs:204-210`: too much text is not shared at all, measured + // on the tagged blob before compression. + let length = (text.plain?.count ?? 0) + (text.rtf?.count ?? 0) + (text.html?.count ?? 0) + guard length <= ClipboardCodec.maxTextLength else { return ClipboardText() } return text } @@ -131,12 +135,17 @@ public final class PasteboardWatcher: @unchecked Sendable { return (text?.isEmpty ?? true) ? nil : text } + /// PNG if the pasteboard has it, else TIFF re-encoded. `FormHelper.cs:287` + /// refuses an image over 50 MB rather than sharing it. private static func readPNG(_ pasteboard: NSPasteboard) -> Data? { - if let png = pasteboard.data(forType: .png), !png.isEmpty { return png } - guard let tiff = pasteboard.data(forType: .tiff), - let rep = NSBitmapImageRep(data: tiff), - let png = rep.representation(using: .png, properties: [:]) - else { return nil } + var png = pasteboard.data(forType: .png) + if png?.isEmpty ?? true { + guard let tiff = pasteboard.data(forType: .tiff), + let rep = NSBitmapImageRep(data: tiff) + else { return nil } + png = rep.representation(using: .png, properties: [:]) + } + guard let png, !png.isEmpty, png.count <= ClipboardCodec.maxImageSize else { return nil } return png } diff --git a/Sources/mwb/ClipboardBridge.swift b/Sources/mwb/ClipboardBridge.swift index e498493..1a8d9d5 100644 --- a/Sources/mwb/ClipboardBridge.swift +++ b/Sources/mwb/ClipboardBridge.swift @@ -118,6 +118,10 @@ public final class ClipboardBridge { /// the peer can pull. public func localCopy(_ content: ClipboardContent, now: TimeInterval) -> [ClipboardAction] { guard shareClipboard else { return [] } + // `Clipboard.cs:111-114`: whatever was on offer is forgotten before the + // new clipboard contents are looked at, so a branch that decides not to + // share leaves nothing behind for a peer to pull. + bulk.serve(nil) switch content { case .file(let url): return localFile(url)