Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save whsieh/38bf9b2fbf3ab22dfca7401333da1677 to your computer and use it in GitHub Desktop.
Save whsieh/38bf9b2fbf3ab22dfca7401333da1677 to your computer and use it in GitHub Desktop.
From 6ac48d12ab1e3089186f68343ba9149aa7c5e804 Mon Sep 17 00:00:00 2001
From: Eryn Wells <eryn_wells@apple.com>
Date: Fri, 8 Jul 2022 16:51:00 -0700
Subject: [PATCH] Should be able to copy editable strong password on iOS
https://bugs.webkit.org/show_bug.cgi?id=242544 rdar://96259620
Reviewed by NOBODY (OOPS!).
Add a flag to EditorState to track when selection is in an autofilled and
viewable field. Consult this flag in WKContentView when we're deciding what
editing actions to allow.
Test: WebKit.CopyInAutoFilledAndViewablePasswordField
* Source/WebKit/Shared/EditorState.cpp:
(WebKit::EditorState::encode const):
(WebKit::EditorState::decode):
Implement transmitting the new flag over the IPC interface
* Source/WebKit/Shared/EditorState.h:
Add selectionIsInAutoFilledAndViewableField to EditorState
* Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView canPerformActionForWebView:withSender:]):
Check the new flag when deciding whether to add the Copy item to the list of
available actions
* Source/WebKit/WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::editorState const):
Set the new flag based on whether the selection is in an AutoFilled and
Viewable field
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentViewEditingActions.mm:
Add a new API test to exercise this fix.
---
Source/WebKit/Shared/EditorState.cpp | 4 ++++
Source/WebKit/Shared/EditorState.h | 1 +
.../UIProcess/ios/WKContentViewInteraction.mm | 2 +-
Source/WebKit/WebProcess/WebPage/WebPage.cpp | 1 +
.../WKContentViewEditingActions.mm | 19 +++++++++++++++++++
5 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/Source/WebKit/Shared/EditorState.cpp b/Source/WebKit/Shared/EditorState.cpp
index 3c2ae4eeb53c..2c9df1717056 100644
--- a/Source/WebKit/Shared/EditorState.cpp
+++ b/Source/WebKit/Shared/EditorState.cpp
@@ -40,6 +40,7 @@ void EditorState::encode(IPC::Encoder& encoder) const
encoder << selectionIsNone;
encoder << selectionIsRange;
encoder << selectionIsRangeInsideImageOverlay;
+ encoder << selectionIsRangeInAutoFilledAndViewableField;
encoder << isContentEditable;
encoder << isContentRichlyEditable;
encoder << isInPasswordField;
@@ -74,6 +75,9 @@ bool EditorState::decode(IPC::Decoder& decoder, EditorState& result)
if (!decoder.decode(result.selectionIsRangeInsideImageOverlay))
return false;
+ if (!decoder.decode(result.selectionIsRangeInAutoFilledAndViewableField))
+ return false;
+
if (!decoder.decode(result.isContentEditable))
return false;
diff --git a/Source/WebKit/Shared/EditorState.h b/Source/WebKit/Shared/EditorState.h
index 131f9e81207b..30d3e1bcd3b8 100644
--- a/Source/WebKit/Shared/EditorState.h
+++ b/Source/WebKit/Shared/EditorState.h
@@ -77,6 +77,7 @@ struct EditorState {
bool selectionIsNone { true }; // This will be false when there is a caret selection.
bool selectionIsRange { false };
bool selectionIsRangeInsideImageOverlay { false };
+ bool selectionIsRangeInAutoFilledAndViewableField { false };
bool isContentEditable { false };
bool isContentRichlyEditable { false };
bool isInPasswordField { false };
diff --git a/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm b/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm
index 70dc4831cf11..a882d716f60a 100644
--- a/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm
+++ b/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm
@@ -3978,7 +3978,7 @@ - (BOOL)canPerformActionForWebView:(SEL)action withSender:(id)sender
}
if (action == @selector(copy:)) {
- if (editorState.isInPasswordField)
+ if (editorState.isInPasswordField && !editorState.selectionIsRangeInAutoFilledAndViewableField)
return NO;
return editorState.selectionIsRange;
}
diff --git a/Source/WebKit/WebProcess/WebPage/WebPage.cpp b/Source/WebKit/WebProcess/WebPage/WebPage.cpp
index 6b8f89fffa2e..d30f3dcb36fa 100644
--- a/Source/WebKit/WebProcess/WebPage/WebPage.cpp
+++ b/Source/WebKit/WebProcess/WebPage/WebPage.cpp
@@ -1251,6 +1251,7 @@ EditorState WebPage::editorState(ShouldPerformLayout shouldPerformLayout) const
if (result.selectionIsRange) {
auto selectionRange = selection.range();
result.selectionIsRangeInsideImageOverlay = selectionRange && ImageOverlay::isInsideOverlay(*selectionRange);
+ result.selectionIsRangeInAutoFilledAndViewableField = selection.isInAutoFilledAndViewableField();
}
m_lastEditorStateWasContentEditable = result.isContentEditable ? EditorStateIsContentEditable::Yes : EditorStateIsContentEditable::No;
diff --git a/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentViewEditingActions.mm b/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentViewEditingActions.mm
index 7f84805fe1c6..c7d2237e763b 100644
--- a/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentViewEditingActions.mm
+++ b/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentViewEditingActions.mm
@@ -77,6 +77,25 @@ TEST(WebKit, InvokeShareWithoutSelection)
[webView waitForNextPresentationUpdate];
}
+TEST(WebKit, CopyInAutoFilledAndViewablePasswordField)
+{
+ RetainPtr configuration = [WKWebViewConfiguration _test_configurationWithTestPlugInClassName:@"WebProcessPlugInWithInternals" configureJSCForTesting:YES];
+
+ auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) configuration:configuration.get()]);
+ auto *contentView = [webView textInputContentView];
+ [webView synchronouslyLoadHTMLString:@"<input type='password' value='hunter2' autofocus /><input type='password' value='hunter2' id='autofill' />"];
+ [webView selectAll:nil];
+ [webView waitForNextPresentationUpdate];
+ EXPECT_FALSE([contentView canPerformAction:@selector(copy:) withSender:nil]);
+
+ [webView objectByEvaluatingJavaScript:@(R"script(
+ let field = document.getElementById('autofill');
+ internals.setAutoFilledAndViewable(field, true);
+ field.select())script")];
+ [webView waitForNextPresentationUpdate];
+ EXPECT_TRUE([contentView canPerformAction:@selector(copy:) withSender:nil]);
+}
+
#if ENABLE(IMAGE_ANALYSIS)
#if ENABLE(APP_HIGHLIGHTS)
--
2.32.0 (Apple Git-132)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment