Skip to content

Commit 220cc23

Browse files
committed
Bug 1966443: Ignore NULs in clipboard paste and DND drop operations r=stransky,geckoview-reviewers,mac-reviewers,bradwerth,tcampbell
Windows and GTK null-terminate text flavors in clipboard and DND operations. For other platforms, it sanitizes text to ensure that there are no surprise NUL characters in it, since NSStrings and Java strings in general can. On a related note, GTK expects text to be obtained with gtk_selection_data_get_text, not gtk_selection_data_get_data (and it guarantees null-termination), so this patch makes that change as well. Differential Revision: https://phabricator.services.mozilla.com/D256877
1 parent 9e6fb6b commit 220cc23

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

widget/android/nsClipboard.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ nsClipboard::GetNativeClipboardData(const nsACString& aFlavor,
124124
if (!text) {
125125
return nsCOMPtr<nsISupports>{};
126126
}
127-
nsString buffer = text->ToString();
127+
nsAutoString buffer(text->ToString());
128+
buffer.StripChar(char16_t(0));
128129
if (buffer.IsEmpty()) {
129130
return nsCOMPtr<nsISupports>{};
130131
}

widget/cocoa/nsClipboard.mm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,11 @@
219219
bool isRTF = [pboardType
220220
isEqualToString:[UTIHelper stringFromPboardType:NSPasteboardTypeRTF]];
221221
if (isRTF) {
222-
stringData = [pString dataUsingEncoding:NSASCIIStringEncoding];
222+
stringData = [pString dataUsingEncoding:NSASCIIStringEncoding
223+
allowLossyConversion:YES];
223224
} else {
224-
stringData = [pString dataUsingEncoding:NSUnicodeStringEncoding];
225+
stringData = [pString dataUsingEncoding:NSUnicodeStringEncoding
226+
allowLossyConversion:YES];
225227
}
226228
unsigned int dataLength = [stringData length];
227229
void* clipboardDataPtr = malloc(dataLength);

widget/cocoa/nsCocoaUtils.mm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,10 @@ PanGestureInput panEvent(
16641664
NSString* availableType =
16651665
[aItem availableTypeFromArray:[NSArray arrayWithObjects:(id)aType, nil]];
16661666
if (availableType && IsValidPasteboardType(availableType, aAllowFileURL)) {
1667-
return [aItem stringForType:(id)availableType];
1667+
NSString* str = [aItem stringForType:(id)availableType];
1668+
// Sanitize (remove NULs, etc) to align with other platforms.
1669+
return [NSString stringWithCString:[str UTF8String]
1670+
encoding:NSUTF8StringEncoding];
16681671
}
16691672

16701673
return nil;

widget/gtk/nsDragService.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,7 @@ bool DragData::IsFileFlavor() const {
248248
}
249249

250250
bool DragData::IsTextFlavor() const {
251-
return mDataFlavor == nsDragSession::sTextMimeAtom ||
252-
mDataFlavor == nsDragSession::sTextPlainUTF8TypeAtom ||
253-
mDataFlavor == nsDragSession::sUTF8STRINGMimeAtom ||
254-
mDataFlavor == nsDragSession::sSTRINGMimeAtom;
251+
return nsDragSession::IsTextFlavor(mDataFlavor);
255252
}
256253

257254
bool DragData::IsURIFlavor() const {
@@ -1574,8 +1571,15 @@ void nsDragSession::TargetDataReceived(GtkWidget* aWidget,
15741571
LOGDRAGSERVICE(" TargetDataReceived(): URI data, MIME %s",
15751572
GUniquePtr<gchar>(gdk_atom_name(target)).get());
15761573
} else {
1577-
const guchar* data = gtk_selection_data_get_data(aSelectionData);
1578-
gint len = gtk_selection_data_get_length(aSelectionData);
1574+
const guchar* data = nullptr;
1575+
gint len = -1;
1576+
if (IsTextFlavor(target)) {
1577+
data = gtk_selection_data_get_text(aSelectionData);
1578+
len = data ? g_utf8_strlen(reinterpret_cast<const gchar*>(data), -1) : -1;
1579+
} else {
1580+
data = gtk_selection_data_get_data(aSelectionData);
1581+
len = gtk_selection_data_get_length(aSelectionData);
1582+
}
15791583
if (len < 0 && !data) {
15801584
LOGDRAGSERVICE(" TargetDataReceived() failed");
15811585
return;
@@ -2341,10 +2345,7 @@ void nsDragSession::SourceDataGet(GtkWidget* aWidget, GdkDragContext* aContext,
23412345
return;
23422346
}
23432347

2344-
if (requestedFlavor == sTextMimeAtom ||
2345-
requestedFlavor == sTextPlainUTF8TypeAtom ||
2346-
requestedFlavor == sUTF8STRINGMimeAtom ||
2347-
requestedFlavor == sSTRINGMimeAtom) {
2348+
if (IsTextFlavor(requestedFlavor)) {
23482349
if (!SourceDataGetText(item, nsDependentCString(kTextMime),
23492350
/* aNeedToDoConversionToPlainText */ true,
23502351
aSelectionData)) {
@@ -2989,4 +2990,12 @@ nsAutoCString nsDragSession::GetDebugTag() const {
29892990
return tag;
29902991
}
29912992

2993+
/* static */
2994+
bool nsDragSession::IsTextFlavor(GdkAtom aFlavor) {
2995+
return aFlavor == nsDragSession::sTextMimeAtom ||
2996+
aFlavor == nsDragSession::sTextPlainUTF8TypeAtom ||
2997+
aFlavor == nsDragSession::sUTF8STRINGMimeAtom ||
2998+
aFlavor == nsDragSession::sSTRINGMimeAtom;
2999+
}
3000+
29923001
#undef LOGDRAGSERVICE

widget/gtk/nsDragService.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ class nsDragSession : public nsBaseDragSession, public nsIObserver {
131131

132132
static int GetLoopDepth() { return sEventLoopDepth; };
133133

134+
static bool IsTextFlavor(GdkAtom aFlavor);
135+
134136
protected:
135137
// mScheduledTask indicates what signal has been received from GTK and
136138
// so what needs to be dispatched when the scheduled task is run. It is

0 commit comments

Comments
 (0)