Title: [259732] trunk/Source/WebCore
Revision
259732
Author
[email protected]
Date
2020-04-08 11:15:49 -0700 (Wed, 08 Apr 2020)

Log Message

[macOS] Make PlatformPasteboard robust against types that cannot be encoded with +defaultCStringEncoding
https://bugs.webkit.org/show_bug.cgi?id=210195
<rdar://problem/61084208>

Reviewed by Tim Horton.

When setting pasteboard data using the three PlatformPasteboard methods below, avoid calling into NSPasteboard
in the case where the pasteboard type fails to be encoded using +[NSString defaultCStringEncoding]. This is
because AppKit pasteboard logic will attempt to convert the given string into a C string using [NSString
defaultCStringEncoding], and then assume that the result is non-null, if the type is neither declared nor
dynamic.

* platform/mac/PlatformPasteboardMac.mm:
(WebCore::canWritePasteboardType):
(WebCore::PlatformPasteboard::setTypes):
(WebCore::PlatformPasteboard::setBufferForType):
(WebCore::PlatformPasteboard::setStringForType):

Add early returns if canWritePasteboardType returns false.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (259731 => 259732)


--- trunk/Source/WebCore/ChangeLog	2020-04-08 18:07:52 UTC (rev 259731)
+++ trunk/Source/WebCore/ChangeLog	2020-04-08 18:15:49 UTC (rev 259732)
@@ -1,3 +1,25 @@
+2020-04-08  Wenson Hsieh  <[email protected]>
+
+        [macOS] Make PlatformPasteboard robust against types that cannot be encoded with +defaultCStringEncoding
+        https://bugs.webkit.org/show_bug.cgi?id=210195
+        <rdar://problem/61084208>
+
+        Reviewed by Tim Horton.
+
+        When setting pasteboard data using the three PlatformPasteboard methods below, avoid calling into NSPasteboard
+        in the case where the pasteboard type fails to be encoded using +[NSString defaultCStringEncoding]. This is
+        because AppKit pasteboard logic will attempt to convert the given string into a C string using [NSString
+        defaultCStringEncoding], and then assume that the result is non-null, if the type is neither declared nor
+        dynamic.
+
+        * platform/mac/PlatformPasteboardMac.mm:
+        (WebCore::canWritePasteboardType):
+        (WebCore::PlatformPasteboard::setTypes):
+        (WebCore::PlatformPasteboard::setBufferForType):
+        (WebCore::PlatformPasteboard::setStringForType):
+
+        Add early returns if canWritePasteboardType returns false.
+
 2020-04-08  Truitt Savell  <[email protected]>
 
         Unreviewed, reverting r259708.

Modified: trunk/Source/WebCore/platform/mac/PlatformPasteboardMac.mm (259731 => 259732)


--- trunk/Source/WebCore/platform/mac/PlatformPasteboardMac.mm	2020-04-08 18:07:52 UTC (rev 259731)
+++ trunk/Source/WebCore/platform/mac/PlatformPasteboardMac.mm	2020-04-08 18:15:49 UTC (rev 259732)
@@ -40,6 +40,15 @@
 
 namespace WebCore {
 
+static bool canWritePasteboardType(const String& type)
+{
+    auto cfString = type.createCFString();
+    if (UTTypeIsDeclared(cfString.get()) || UTTypeIsDynamic(cfString.get()))
+        return true;
+
+    return [(__bridge NSString *)cfString.get() lengthOfBytesUsingEncoding:NSString.defaultCStringEncoding];
+}
+
 PlatformPasteboard::PlatformPasteboard(const String& pasteboardName)
     : m_pasteboard([NSPasteboard pasteboardWithName:pasteboardName])
 {
@@ -317,18 +326,22 @@
 
 int64_t PlatformPasteboard::setTypes(const Vector<String>& pasteboardTypes)
 {
-    if (pasteboardTypes.isEmpty())
-        return [m_pasteboard declareTypes:@[] owner:nil];
-
-    RetainPtr<NSMutableArray> types = adoptNS([[NSMutableArray alloc] init]);
-    for (size_t i = 0; i < pasteboardTypes.size(); ++i)
-        [types.get() addObject:pasteboardTypes[i]];
-
-    return [m_pasteboard.get() declareTypes:types.get() owner:nil];
+    auto types = adoptNS([[NSMutableArray alloc] init]);
+    for (auto& pasteboardType : pasteboardTypes) {
+        if (!canWritePasteboardType(pasteboardType)) {
+            [types removeAllObjects];
+            break;
+        }
+        [types addObject:pasteboardType];
+    }
+    return [m_pasteboard declareTypes:types.get() owner:nil];
 }
 
 int64_t PlatformPasteboard::setBufferForType(SharedBuffer* buffer, const String& pasteboardType)
 {
+    if (!canWritePasteboardType(pasteboardType))
+        return 0;
+
     BOOL didWriteData = [m_pasteboard setData:buffer ? buffer->createNSData().get() : nil forType:pasteboardType];
     if (!didWriteData)
         return 0;
@@ -359,6 +372,9 @@
 
 int64_t PlatformPasteboard::setStringForType(const String& string, const String& pasteboardType)
 {
+    if (!canWritePasteboardType(pasteboardType))
+        return 0;
+
     BOOL didWriteData;
 
     if (pasteboardType == String(legacyURLPasteboardType())) {
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to