|
| 1 | +EnhancedSecurityModeLevel |
| 2 | +=== |
| 3 | + |
| 4 | +# Background |
| 5 | + |
| 6 | +Enhanced Security Mode (ESM) is a Microsoft Edge security feature that reduces |
| 7 | +the risk of memory-related vulnerabilities by disabling JavaScript Just-in-Time |
| 8 | +(JIT) compilation and enabling additional operating system protections. |
| 9 | + |
| 10 | +In WebView2, ESM is off by default to avoid performance impact. You can enable |
| 11 | +ESM for stricter security when rendering untrusted sites. While this improves |
| 12 | +security, it may reduce JavaScript performance. |
| 13 | + |
| 14 | +In Microsoft Edge, ESM offers two levels: |
| 15 | + |
| 16 | +- Balanced – Enhanced security is used for unfamiliar sites based on browser usage patterns. |
| 17 | +- Strict – Enhanced security is used for all sites. |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +Unlike Microsoft Edge, WebView2 does not support the heuristic-based "Balanced" |
| 22 | +level; only Off and Strict are available. |
| 23 | + |
| 24 | +Today, the ESM level in WebView2 can be set only at environment creation by using |
| 25 | +the `--sdsm-state` browser feature flag ([webview2 browser flag docs](https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/webview-features-flags?tabs=dotnetcsharp)). |
| 26 | +The setting applies globally to all profiles and cannot be changed at runtime. |
| 27 | + |
| 28 | +This proposal introduces an API to configure the Enhanced Security Mode level. |
| 29 | +The configuration is persisted for a WebView2 profile within the user data folder. |
| 30 | + |
| 31 | +# Description |
| 32 | + |
| 33 | +The `EnhancedSecurityModeLevel` property allows you to control the Enhanced Security |
| 34 | +Mode level for WebView2 instances associated with a profile. This level applies to the |
| 35 | +context of the profile. That is, all WebView2s sharing the same profile are affected |
| 36 | +and the value is persisted in the user data folder. |
| 37 | + |
| 38 | +The default value is `COREWEBVIEW2_ENHANCED_SECURITY_MODE_LEVEL_OFF`. |
| 39 | + |
| 40 | +When set to `Off`, Enhanced Security Mode is completely disabled. When set to |
| 41 | +`Strict`, the feature is enabled and applies strict security policies to all sites. |
| 42 | + |
| 43 | +See `COREWEBVIEW2_ENHANCED_SECURITY_MODE_LEVEL` for descriptions of levels. |
| 44 | + |
| 45 | +Changes apply to future navigations; reload may be required to see the effect. |
| 46 | + |
| 47 | +# Examples |
| 48 | + |
| 49 | +## EnhancedSecurityModeLevel |
| 50 | + |
| 51 | +Set Enhanced Security Mode level for a profile. |
| 52 | + |
| 53 | +```c# |
| 54 | +void SetEnhancedSecurityModeLevel(CoreWebView2EnhancedSecurityModeLevel value) |
| 55 | +{ |
| 56 | + var profile = webView2.CoreWebView2.Profile; |
| 57 | + profile.EnhancedSecurityModeLevel = value; |
| 58 | + MessageBox.Show(this, |
| 59 | + "Enhanced Security Mode level is set successfully", |
| 60 | + "Enhanced Security Mode"); |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +```cpp |
| 65 | +void SettingsComponent::SetEnhancedSecurityModeLevel( |
| 66 | + COREWEBVIEW2_ENHANCED_SECURITY_MODE_LEVEL value) |
| 67 | +{ |
| 68 | + wil::com_ptr<ICoreWebView2_13> webView2_13; |
| 69 | + webView2_13 = m_webView.try_query<ICoreWebView2_13>(); |
| 70 | + |
| 71 | + if (webView2_13) |
| 72 | + { |
| 73 | + wil::com_ptr<ICoreWebView2Profile> profile; |
| 74 | + CHECK_FAILURE(webView2_13->get_Profile(&profile)); |
| 75 | + |
| 76 | + auto profile12 = profile.try_query<ICoreWebView2Profile12>(); |
| 77 | + if (profile12) |
| 78 | + { |
| 79 | + CHECK_FAILURE(profile12->put_EnhancedSecurityModeLevel(value)); |
| 80 | + MessageBox( |
| 81 | + nullptr, |
| 82 | + L"Enhanced Security Mode level is set successfully", |
| 83 | + L"Enhanced Security Mode", MB_OK); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | +
|
| 89 | +# API Details |
| 90 | +
|
| 91 | +``` |
| 92 | +/// Enhanced Security Mode levels. |
| 93 | +[v1_enum] |
| 94 | +typedef enum COREWEBVIEW2_ENHANCED_SECURITY_MODE_LEVEL { |
| 95 | + /// Enhanced Security Mode is turned off. |
| 96 | + COREWEBVIEW2_ENHANCED_SECURITY_MODE_LEVEL_OFF, |
| 97 | + /// Enhanced Security Mode is enabled in Strict level. Disables JavaScript |
| 98 | + /// Just-in-Time (JIT) compilation and enables additional OS protections. |
| 99 | + /// |
| 100 | + /// This level applies enhanced security for all sites but may reduce JavaScript performance. |
| 101 | + /// |
| 102 | + /// See [Browsing more safely with Microsoft Edge](https://learn.microsoft.com/en-us/DeployEdge/microsoft-edge-security-browse-safer) |
| 103 | + /// for more details about Enhanced Security Mode. |
| 104 | + COREWEBVIEW2_ENHANCED_SECURITY_MODE_LEVEL_STRICT, |
| 105 | +} COREWEBVIEW2_ENHANCED_SECURITY_MODE_LEVEL; |
| 106 | + |
| 107 | +/// Extension of ICoreWebView2Profile to control Enhanced Security Mode (ESM) level. |
| 108 | +/// |
| 109 | +/// ESM reduces the risk of memory-related vulnerabilities by disabling JavaScript |
| 110 | +/// Just-in-Time (JIT) compilation and enabling additional OS protections. |
| 111 | +/// This property applies to all WebView2 instances sharing the same profile and |
| 112 | +/// is persisted in the user data folder. |
| 113 | +/// |
| 114 | +/// See `COREWEBVIEW2_ENHANCED_SECURITY_MODE_LEVEL` for descriptions of levels. |
| 115 | +/// |
| 116 | +/// If `EnhancedSecurityModeLevel` is set, the property value is persisted in the profile. |
| 117 | +/// Changes apply to future navigations; reload may be required. |
| 118 | +/// |
| 119 | +/// Enabling ESM improves security but may reduce JavaScript performance. |
| 120 | +[uuid(450fab50-f6d8-4517-baea-964ec12d5c8c), object, pointer_default(unique)] |
| 121 | +interface ICoreWebView2Profile12 : IUnknown { |
| 122 | + /// Gets the `EnhancedSecurityModeLevel` property. |
| 123 | + [propget] HRESULT EnhancedSecurityModeLevel([out, retval] COREWEBVIEW2_ENHANCED_SECURITY_MODE_LEVEL* value); |
| 124 | + |
| 125 | + /// Sets the `EnhancedSecurityModeLevel` property. |
| 126 | + [propput] HRESULT EnhancedSecurityModeLevel([in] COREWEBVIEW2_ENHANCED_SECURITY_MODE_LEVEL value); |
| 127 | +} |
| 128 | +``` |
| 129 | +
|
| 130 | +```c# (but really MIDL3) |
| 131 | +namespace Microsoft.Web.WebView2.Core |
| 132 | +{ |
| 133 | + enum CoreWebView2EnhancedSecurityModeLevel |
| 134 | + { |
| 135 | + Off = 0, |
| 136 | + Strict = 1 |
| 137 | + }; |
| 138 | +
|
| 139 | + runtimeclass CoreWebView2Profile |
| 140 | + { |
| 141 | + // ... |
| 142 | + [interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2Profile12")] |
| 143 | + { |
| 144 | + // ICoreWebView2Profile12 members |
| 145 | + CoreWebView2EnhancedSecurityModeLevel EnhancedSecurityModeLevel { get; set; }; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
0 commit comments