From 25727539bbe8d2f75d9a632f43d6c80b4b472825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sharma?= <737941+loic-sharma@users.noreply.github.com> Date: Thu, 16 Mar 2023 16:52:27 -0700 Subject: [PATCH] [Windows] Fix crash if clipboard is set to null (flutter/engine#40238) Currently, the framework allows setting the clipboard to `null`. [Platforms handle this case inconsistently](https://github.com/flutter/flutter/issues/121976#issuecomment-1464721716), and we will update the framework to disallow setting the clipboard's data to `null`. In the meantime, this change makes the Windows embedder resilient to unexpected clipboard messages. If your clipboard data's text is unexpected, you will now get an error instead of crashing your app. Part of https://github.com/flutter/flutter/issues/121976 --- .../shell/platform/windows/platform_handler.cc | 4 ++++ .../windows/platform_handler_unittests.cc | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/engine/src/flutter/shell/platform/windows/platform_handler.cc b/engine/src/flutter/shell/platform/windows/platform_handler.cc index 605094bdf2..4c54b3d4cf 100644 --- a/engine/src/flutter/shell/platform/windows/platform_handler.cc +++ b/engine/src/flutter/shell/platform/windows/platform_handler.cc @@ -369,6 +369,10 @@ void PlatformHandler::HandleMethodCall( result->Error(kClipboardError, kUnknownClipboardFormatMessage); return; } + if (!itr->value.IsString()) { + result->Error(kClipboardError, kUnknownClipboardFormatMessage); + return; + } SetPlainText(itr->value.GetString(), std::move(result)); } else if (method.compare(kPlaySoundMethod) == 0) { // Only one string argument is expected. diff --git a/engine/src/flutter/shell/platform/windows/platform_handler_unittests.cc b/engine/src/flutter/shell/platform/windows/platform_handler_unittests.cc index 2ae025b464..71f383ae73 100644 --- a/engine/src/flutter/shell/platform/windows/platform_handler_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/platform_handler_unittests.cc @@ -37,6 +37,8 @@ static constexpr char kClipboardHasStringsFakeContentTypeMessage[] = "{\"method\":\"Clipboard.hasStrings\",\"args\":\"text/madeupcontenttype\"}"; static constexpr char kClipboardSetDataMessage[] = "{\"method\":\"Clipboard.setData\",\"args\":{\"text\":\"hello\"}}"; +static constexpr char kClipboardSetDataNullTextMessage[] = + "{\"method\":\"Clipboard.setData\",\"args\":{\"text\":null}}"; static constexpr char kClipboardSetDataUnknownTypeMessage[] = "{\"method\":\"Clipboard.setData\",\"args\":{\"madeuptype\":\"hello\"}}"; static constexpr char kSystemSoundTypeAlertMessage[] = @@ -368,6 +370,19 @@ TEST_F(PlatformHandlerTest, ClipboardSetData) { EXPECT_EQ(result, "[null]"); } +// Regression test for: https://github.com/flutter/flutter/issues/121976 +TEST_F(PlatformHandlerTest, ClipboardSetDataTextMustBeString) { + use_engine_with_view(); + + TestBinaryMessenger messenger; + PlatformHandler platform_handler(&messenger, engine()); + + std::string result = + SimulatePlatformMessage(&messenger, kClipboardSetDataNullTextMessage); + + EXPECT_EQ(result, "[\"Clipboard error\",\"Unknown clipboard format\",null]"); +} + TEST_F(PlatformHandlerTest, ClipboardSetDataUnknownType) { use_engine_with_view();