From 001c8fe3d288802de9a8c29cfd2f46f9220d97c5 Mon Sep 17 00:00:00 2001 From: AetherWing Date: Thu, 23 Apr 2026 14:11:22 +0800 Subject: [PATCH] feat(webview2): add option to disable browser-level autofill on Windows (#14722) * feat(webview2): add option to disable browser-level autofill on Windows * docs(api): set disableAutofill api since to 2.11.0 * docs(disable_autofill ): unify documentation * Update .changes/.disable-autofill.md Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com> * refactor(runtime): rename disable_autofill to general_autofill_enabled * refactor(api): delete general autofill option in WindowOptions Co-authored-by: Copilot * Update crates/tauri-runtime-wry/src/lib.rs Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com> * fix: fix default value for general_autofill_enabled * fix(schema): fix default value for general_autofill_enabled * Clean up * Revert new line --------- Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com> Co-authored-by: Tony --- .changes/disable-autofill.md | 9 ++++ crates/tauri-cli/config.schema.json | 5 +++ crates/tauri-runtime-wry/src/lib.rs | 3 +- crates/tauri-runtime/src/webview.rs | 45 ++++++++++++++++++- .../schemas/config.schema.json | 5 +++ crates/tauri-utils/src/config.rs | 24 +++++++++- crates/tauri/src/webview/mod.rs | 23 ++++++++++ crates/tauri/src/webview/webview_window.rs | 23 ++++++++++ packages/api/src/webview.ts | 19 ++++++++ 9 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 .changes/disable-autofill.md diff --git a/.changes/disable-autofill.md b/.changes/disable-autofill.md new file mode 100644 index 000000000000..41dfd62dc042 --- /dev/null +++ b/.changes/disable-autofill.md @@ -0,0 +1,9 @@ +--- +'tauri-runtime': 'minor:feat' +'tauri-runtime-wry': 'minor:feat' +'tauri-utils': 'minor:feat' +'tauri': 'minor:feat' +'@tauri-apps/api': 'minor:feat' +--- + +Add a WebView option to control browser-level general autofill behavior. This option does not disable password or credit card autofill. On Windows (WebView2), setting it to true disables the general autofill "Suggestions" UI, which may appear even when `autocomplete="off"` is specified on input elements. On Linux, macOS, iOS, and Android, this option is currently unsupported and performs no operation. \ No newline at end of file diff --git a/crates/tauri-cli/config.schema.json b/crates/tauri-cli/config.schema.json index c8d038cb3c03..f4381d54c1f0 100644 --- a/crates/tauri-cli/config.schema.json +++ b/crates/tauri-cli/config.schema.json @@ -625,6 +625,11 @@ "string", "null" ] + }, + "generalAutofillEnabled": { + "description": "Controls the WebView's browser-level general autofill behavior.\n\n **This option does not disable password or credit card autofill.**\n\n When set to `false`, the WebView will not automatically populate\n general form fields using previously stored data such as addresses\n or contact information.\n\n If not specified, this is `true` by default.\n\n ## Platform-specific\n\n - **Windows**: Supported. WebView2's autofill feature (called\n \"Suggestions\") may not honor `autocomplete=\"off\"` on input\n elements in some cases.\n - **Linux / Android / iOS / macOS**: Unsupported and performs no\n operation.", + "default": true, + "type": "boolean" } }, "additionalProperties": false diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index cbb59a30b736..da4cb6a637c0 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -4760,7 +4760,8 @@ You may have it installed on another user account, but it is not available for t .with_accept_first_mouse(webview_attributes.accept_first_mouse) .with_incognito(webview_attributes.incognito) .with_clipboard(webview_attributes.clipboard) - .with_hotkeys_zoom(webview_attributes.zoom_hotkeys_enabled); + .with_hotkeys_zoom(webview_attributes.zoom_hotkeys_enabled) + .with_general_autofill_enabled(webview_attributes.general_autofill_enabled); if url != "about:blank" { webview_builder = webview_builder.with_url(&url); diff --git a/crates/tauri-runtime/src/webview.rs b/crates/tauri-runtime/src/webview.rs index 1cd929226cf1..d4f7e0145313 100644 --- a/crates/tauri-runtime/src/webview.rs +++ b/crates/tauri-runtime/src/webview.rs @@ -371,6 +371,24 @@ pub struct WebviewAttributes { /// see https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview pub allow_link_preview: bool, pub scroll_bar_style: ScrollBarStyle, + /// Controls the WebView's browser-level general autofill behavior. + /// + /// **This option does not disable password or credit card autofill.** + /// + /// When set to `false`, the WebView will not automatically populate + /// general form fields using previously stored data such as addresses + /// or contact information. + /// + /// If not specified, this is `true` by default. + /// + /// ## Platform-specific + /// + /// - **Windows**: Supported. WebView2's autofill feature (called + /// "Suggestions") may not honor `autocomplete="off"` on input + /// elements in some cases. + /// - **Linux / Android / iOS / macOS**: Unsupported and performs no + /// operation. + pub general_autofill_enabled: bool, /// Allows overriding the keyboard accessory view on iOS. /// Returning `None` effectively removes the view. /// @@ -441,7 +459,8 @@ impl From<&WindowConfig> for WebviewAttributes { #[cfg(windows)] ConfigScrollBarStyle::FluentOverlay => ScrollBarStyle::FluentOverlay, _ => ScrollBarStyle::Default, - }); + }) + .general_autofill_enabled(config.general_autofill_enabled); #[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))] { @@ -516,6 +535,7 @@ impl WebviewAttributes { javascript_disabled: false, allow_link_preview: true, scroll_bar_style: ScrollBarStyle::Default, + general_autofill_enabled: true, #[cfg(target_os = "ios")] input_accessory_view_builder: None, #[cfg(windows)] @@ -807,6 +827,29 @@ impl WebviewAttributes { self.scroll_bar_style = style; self } + + /// Controls the WebView's browser-level general autofill behavior. + /// + /// **This option does not disable password or credit card autofill.** + /// + /// When set to `false`, the WebView will not automatically populate + /// general form fields using previously stored data such as addresses + /// or contact information. + /// + /// By default, this is `true`. + /// + /// ## Platform-specific + /// + /// - **Windows**: Supported. WebView2's autofill feature (called + /// "Suggestions") may not honor `autocomplete="off"` on input + /// elements in some cases. + /// - **Linux / Android / iOS / macOS**: Unsupported and performs no + /// operation. + #[must_use] + pub fn general_autofill_enabled(mut self, enabled: bool) -> Self { + self.general_autofill_enabled = enabled; + self + } } /// IPC handler. diff --git a/crates/tauri-schema-generator/schemas/config.schema.json b/crates/tauri-schema-generator/schemas/config.schema.json index c8d038cb3c03..f4381d54c1f0 100644 --- a/crates/tauri-schema-generator/schemas/config.schema.json +++ b/crates/tauri-schema-generator/schemas/config.schema.json @@ -625,6 +625,11 @@ "string", "null" ] + }, + "generalAutofillEnabled": { + "description": "Controls the WebView's browser-level general autofill behavior.\n\n **This option does not disable password or credit card autofill.**\n\n When set to `false`, the WebView will not automatically populate\n general form fields using previously stored data such as addresses\n or contact information.\n\n If not specified, this is `true` by default.\n\n ## Platform-specific\n\n - **Windows**: Supported. WebView2's autofill feature (called\n \"Suggestions\") may not honor `autocomplete=\"off\"` on input\n elements in some cases.\n - **Linux / Android / iOS / macOS**: Unsupported and performs no\n operation.", + "default": true, + "type": "boolean" } }, "additionalProperties": false diff --git a/crates/tauri-utils/src/config.rs b/crates/tauri-utils/src/config.rs index 060dd5d9668b..e4aeee7ea797 100644 --- a/crates/tauri-utils/src/config.rs +++ b/crates/tauri-utils/src/config.rs @@ -2270,6 +2270,25 @@ pub struct WindowConfig { /// By default the system uses the foreground scene. #[serde(default, alias = "requested-by-scene-identifier")] pub requested_by_scene_identifier: Option, + /// Controls the WebView's browser-level general autofill behavior. + /// + /// **This option does not disable password or credit card autofill.** + /// + /// When set to `false`, the WebView will not automatically populate + /// general form fields using previously stored data such as addresses + /// or contact information. + /// + /// If not specified, this is `true` by default. + /// + /// ## Platform-specific + /// + /// - **Windows**: Supported. WebView2's autofill feature (called + /// "Suggestions") may not honor `autocomplete="off"` on input + /// elements in some cases. + /// - **Linux / Android / iOS / macOS**: Unsupported and performs no + /// operation. + #[serde(default = "default_true", alias = "general-autofill-enabled")] + pub general_autofill_enabled: bool, } impl Default for WindowConfig { @@ -2335,6 +2354,7 @@ impl Default for WindowConfig { activity_name: None, created_by_activity_name: None, requested_by_scene_identifier: None, + general_autofill_enabled: true, } } } @@ -3872,6 +3892,7 @@ mod build { let activity_name = opt_lit(self.activity_name.as_ref()); let created_by_activity_name = opt_lit(self.created_by_activity_name.as_ref()); let requested_by_scene_identifier = opt_lit(self.requested_by_scene_identifier.as_ref()); + let general_autofill_enabled = self.general_autofill_enabled; literal_struct!( tokens, @@ -3935,7 +3956,8 @@ mod build { scroll_bar_style, activity_name, created_by_activity_name, - requested_by_scene_identifier + requested_by_scene_identifier, + general_autofill_enabled ); } } diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index cf300288f4fd..b86eb01a223c 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -1179,6 +1179,29 @@ fn main() { self } + /// Controls the WebView's browser-level general autofill behavior. + /// + /// **This option does not disable password or credit card autofill.** + /// + /// When set to `false`, the WebView will not automatically populate + /// general form fields using previously stored data such as addresses + /// or contact information. + /// + /// By default, this is `true`. + /// + /// ## Platform-specific + /// + /// - **Windows**: Supported. WebView2's autofill feature (called + /// "Suggestions") may not honor `autocomplete="off"` on input + /// elements in some cases. + /// - **Linux / Android / iOS / macOS**: Unsupported and performs no + /// operation. + #[must_use] + pub fn general_autofill_enabled(mut self, enabled: bool) -> Self { + self.webview_attributes = self.webview_attributes.general_autofill_enabled(enabled); + self + } + /// Whether to show a link preview when long pressing on links. Available on macOS and iOS only. /// /// Default is true. diff --git a/crates/tauri/src/webview/webview_window.rs b/crates/tauri/src/webview/webview_window.rs index c49abec0cbe7..95bf67719399 100644 --- a/crates/tauri/src/webview/webview_window.rs +++ b/crates/tauri/src/webview/webview_window.rs @@ -1226,6 +1226,29 @@ impl> WebviewWindowBuilder<'_, R, M> { self } + /// Controls the WebView's browser-level general autofill behavior. + /// + /// **This option does not disable password or credit card autofill.** + /// + /// When set to `false`, the WebView will not automatically populate + /// general form fields using previously stored data such as addresses + /// or contact information. + /// + /// By default, this is `true`. + /// + /// ## Platform-specific + /// + /// - **Windows**: Supported. WebView2's autofill feature (called + /// "Suggestions") may not honor `autocomplete="off"` on input + /// elements in some cases. + /// - **Linux / Android / iOS / macOS**: Unsupported and performs no + /// operation. + #[must_use] + pub fn general_autofill_enabled(mut self, enabled: bool) -> Self { + self.webview_builder = self.webview_builder.general_autofill_enabled(enabled); + self + } + /// Allows overriding the keyboard accessory view on iOS. /// Returning `None` effectively removes the view. /// diff --git a/packages/api/src/webview.ts b/packages/api/src/webview.ts index 47a497fb6c84..968983c02eda 100644 --- a/packages/api/src/webview.ts +++ b/packages/api/src/webview.ts @@ -897,6 +897,25 @@ interface WebviewOptions { * - **Linux / Android / iOS / macOS**: Unsupported. Only supports `Default` and performs no operation. */ scrollBarStyle?: ScrollBarStyle + /** + * Controls the WebView's browser-level general autofill behavior. + * + * **This option does not disable password or credit card autofill.** + * + * When set to `false`, the WebView will not automatically populate general form + * fields using previously stored data such as addresses or contact information. + * + * If not specified, this is `true` by default. + * + * ## Platform-specific + * + * - **Windows**: Supported. WebView2's autofill feature (called "Suggestions") + * may not honor `autocomplete="off"` on input elements in some cases. + * - **Linux / Android / iOS / macOS**: Unsupported and performs no operation. + * + * @since 2.11.0 + */ + generalAutofillEnabled?: boolean } export { Webview, getCurrentWebview, getAllWebviews }