diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm index b9a9bce520..f218350960 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm @@ -900,6 +900,15 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { return _textInputClient != 0; } +- (BOOL)canPerformAction:(SEL)action withSender:(nullable id)sender { + if (action == @selector(paste:)) { + // Forbid pasting images, memojis, or other non-string content. + return [UIPasteboard generalPasteboard].string != nil; + } + + return [super canPerformAction:action withSender:sender]; +} + #pragma mark - UIResponderStandardEditActions Overrides - (void)cut:(id)sender { @@ -912,7 +921,10 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { } - (void)paste:(id)sender { - [self insertText:[UIPasteboard generalPasteboard].string]; + NSString* pasteboardString = [UIPasteboard generalPasteboard].string; + if (pasteboardString != nil) { + [self insertText:pasteboardString]; + } } - (void)delete:(id)sender { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm index cd803f7fb7..bf223cdae6 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm @@ -86,6 +86,9 @@ FLUTTER_ASSERT_ARC textInputPlugin.textInputDelegate = engine; viewController = [FlutterViewController new]; textInputPlugin.viewController = viewController; + + // Clear pasteboard between tests. + UIPasteboard.generalPasteboard.items = @[]; } - (void)tearDown { @@ -268,6 +271,7 @@ FLUTTER_ASSERT_ARC [inputView selectAll:nil]; [inputView cut:nil]; [inputView insertText:@"bbbb"]; + XCTAssertTrue([inputView canPerformAction:@selector(paste:) withSender:nil]); [inputView paste:nil]; [inputView selectAll:nil]; [inputView copy:nil]; @@ -282,6 +286,20 @@ FLUTTER_ASSERT_ARC XCTAssertEqualObjects(substring, @"bbbbaaaabbbbaaaa"); } +- (void)testPastingNonTextDisallowed { + NSDictionary* config = self.mutableTemplateCopy; + [self setClientId:123 configuration:config]; + NSArray* inputFields = self.installedInputViews; + FlutterTextInputView* inputView = inputFields[0]; + + UIPasteboard.generalPasteboard.color = UIColor.redColor; + XCTAssertNil(UIPasteboard.generalPasteboard.string); + XCTAssertFalse([inputView canPerformAction:@selector(paste:) withSender:nil]); + [inputView paste:nil]; + + XCTAssertEqualObjects(inputView.text, @""); +} + - (void)testNoZombies { // Regression test for https://github.com/flutter/flutter/issues/62501. FlutterSecureTextInputView* passwordView = [[FlutterSecureTextInputView alloc] init];