Bug 215388 - Add a "use stored credentials" setting to WKWebView
Summary: Add a "use stored credentials" setting to WKWebView
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit API (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Brady Eidson
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2020-08-11 12:50 PDT by Brady Eidson
Modified: 2022-05-04 13:45 PDT (History)
6 users (show)

See Also:


Attachments
Patch (18.12 KB, patch)
2020-08-11 13:02 PDT, Brady Eidson
no flags Details | Formatted Diff | Diff
Patch (18.06 KB, patch)
2020-08-11 13:08 PDT, Brady Eidson
no flags Details | Formatted Diff | Diff
Patch (18.04 KB, patch)
2020-08-11 14:40 PDT, Brady Eidson
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Brady Eidson 2020-08-11 12:50:26 PDT
Add a "use stored credentials" setting to WKWebView

<rdar://problem/63308019>
Comment 1 Brady Eidson 2020-08-11 13:02:20 PDT
Created attachment 406407 [details]
Patch
Comment 2 Brady Eidson 2020-08-11 13:08:18 PDT
Created attachment 406408 [details]
Patch
Comment 3 Alex Christensen 2020-08-11 13:28:48 PDT
Comment on attachment 406408 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=406408&action=review

> Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h:355
> +@property (nonatomic, setter=_setCanUseCredentialStorage:) BOOL _canUseCredentialStorage WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));

If this is intended to be constant, could we put it on WKWebViewConfiguration?  Otherwise, could we put it on WKPreferences?
Comment 4 Geoffrey Garen 2020-08-11 13:30:58 PDT
Comment on attachment 406408 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=406408&action=review

r=me

> Tools/TestWebKitAPI/Tests/WebKitCocoa/Preconnect.mm:102
> +    // The preconnect to the server will use the default setting of "use the credential store",
> +    // and therefore use the credential-store-blessed NSURLSession.
> +    [webView _preconnectToServer:server.request().URL];
> +    Util::run(&anyConnections);
> +    Util::spinRunLoop(10);
> +    EXPECT_FALSE(requested);
> +
> +    // Then this request will *not* use the credential store, therefore using a different NSURLSession
> +    // that doesn't know about the above preconnect, triggering a second connection to the server.
> +    webView.get()._canUseCredentialStorage = NO;
> +    [webView loadRequest:server.request()];
> +    Util::run(&requested);

This test is a little upside-down because it verifies that the *unwanted* behavior is still possible, rather than verifying that the *wanted* behavior works as expected. Can you add a test that sets _canUseCredentialStorage up front and verifies that only one connection is made if you reconnect and then connect?
Comment 5 Geoffrey Garen 2020-08-11 13:33:14 PDT
> > Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h:355
> > +@property (nonatomic, setter=_setCanUseCredentialStorage:) BOOL _canUseCredentialStorage WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
> 
> If this is intended to be constant, could we put it on
> WKWebViewConfiguration?  Otherwise, could we put it on WKPreferences?

In Safari's current behavior, this setting ends up being toggled. So, it would need to be WKPreferences. (Or maybe _WKWebsitePolicies? _WKWebsitePolicies is actually kind of a good fit -- better than the toggle because you'd get an internally consistent behavior throughout a page load automatically. But perhaps that's too much of a risk of a change in behavior.)
Comment 6 Brady Eidson 2020-08-11 14:00:01 PDT
(In reply to Geoffrey Garen from comment #4)
> Comment on attachment 406408 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=406408&action=review
> 
> r=me
> 
> > Tools/TestWebKitAPI/Tests/WebKitCocoa/Preconnect.mm:102
> > +    // The preconnect to the server will use the default setting of "use the credential store",
> > +    // and therefore use the credential-store-blessed NSURLSession.
> > +    [webView _preconnectToServer:server.request().URL];
> > +    Util::run(&anyConnections);
> > +    Util::spinRunLoop(10);
> > +    EXPECT_FALSE(requested);
> > +
> > +    // Then this request will *not* use the credential store, therefore using a different NSURLSession
> > +    // that doesn't know about the above preconnect, triggering a second connection to the server.
> > +    webView.get()._canUseCredentialStorage = NO;
> > +    [webView loadRequest:server.request()];
> > +    Util::run(&requested);
> 
> This test is a little upside-down because it verifies that the *unwanted*
> behavior is still possible, rather than verifying that the *wanted* behavior
> works as expected. Can you add a test that sets _canUseCredentialStorage up
> front and verifies that only one connection is made if you reconnect and
> then connect?

That's actually what the test right above it does (with my augmentation), covering the default behavior.

Testing this is a struggle because of how Safari gets into the mess.

I think verifying both the wanted *and* unwanted behavior is the best we can do from a WebKit perspective.
Comment 7 Brady Eidson 2020-08-11 14:00:51 PDT
(In reply to Alex Christensen from comment #3)
> Comment on attachment 406408 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=406408&action=review
> 
> > Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h:355
> > +@property (nonatomic, setter=_setCanUseCredentialStorage:) BOOL _canUseCredentialStorage WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
> 
> If this is intended to be constant, could we put it on
> WKWebViewConfiguration?  Otherwise, could we put it on WKPreferences?

It's definitely not constant.

And it's definitely meant to be per-WebView, so the possibility of sharing a prefs objective makes that a no-go.
Comment 8 EWS 2020-08-11 14:18:38 PDT
commit-queue failed to commit attachment 406408 [details] to WebKit repository.
Comment 9 Brady Eidson 2020-08-11 14:40:05 PDT
Created attachment 406419 [details]
Patch
Comment 10 EWS 2020-08-11 15:20:22 PDT
Committed r265525: <https://trac.webkit.org/changeset/265525>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 406419 [details].
Comment 11 Chris Thoburn 2022-05-04 13:45:54 PDT
A lot of folks bump into issues with password autofill/saving (see for instance https://developer.apple.com/forums/thread/654338), especially when building an app using cordova or capacitor.

I was going to open a new ticket; however, this ticket suggests a setting exists that can make wkWebView work "as expected" in these scenarios. Reading the attached commit I haven't been able to figure out how to configure the xcodeproject; is this something capacitor (for instance) would need to do directly on the webview?