|
| 1 | +Origin Feature Configuration for WebView2 |
| 2 | +=== |
| 3 | + |
| 4 | +# Background |
| 5 | + |
| 6 | +Many WebView2 applications need to apply different security and feature |
| 7 | +policies depending on the origin of the content they host. In some scenarios, |
| 8 | +applications must enable advanced capabilities for specific origins while |
| 9 | +enforcing stricter protections for other origins. |
| 10 | + |
| 11 | +By default, WebView2 enforces a single, uniform security model across all |
| 12 | +origins. This creates two primary limitations: |
| 13 | + |
| 14 | +- **Feature Access Control**: Applications cannot selectively enable |
| 15 | + privileged features—such as specific APIs or advanced capabilities—for |
| 16 | + certain origins only. As a result, developers must either expose these |
| 17 | + features to all content or disable them entirely. |
| 18 | + |
| 19 | +- **Performance and Security Trade-offs**: Security mechanisms such as |
| 20 | + Enhanced Security Mode are essential for external content but may introduce |
| 21 | + unnecessary overhead when applied to first‑party experiences. |
| 22 | + |
| 23 | +For example, a content management system may need to grant full feature access |
| 24 | +and relax certain security restrictions for its administrative interface, |
| 25 | +while still applying strict security policies to user‑generated or external |
| 26 | +content displayed within the same WebView2 instance. |
| 27 | + |
| 28 | +The Origin Configuration API enables these scenarios by allowing applications |
| 29 | +to configure origin-specific settings for different features. Applications can |
| 30 | +define feature policies for specific origins or origin patterns, giving |
| 31 | +developers fine‑grained control over how features and security mechanisms are |
| 32 | +applied to content from various sources. |
| 33 | + |
| 34 | +# Description |
| 35 | + |
| 36 | +This specification introduces the following interfaces: |
| 37 | + |
| 38 | +1. `ICoreWebView2Profile3`: |
| 39 | + |
| 40 | + The ICoreWebView2Profile3 interface provides APIs for defining, applying, |
| 41 | + and retrieving origin feature settings. It introduces the following |
| 42 | + members: |
| 43 | + |
| 44 | + - **CreateOriginFeatureSetting**: Creates a new |
| 45 | + CoreWebView2OriginFeatureSetting object. The returned object can be |
| 46 | + added to the collection passed to SetOriginFeatures to configure |
| 47 | + feature behavior for origins. |
| 48 | + |
| 49 | + - **SetOriginFeatures**: Applies the specified origin feature settings to |
| 50 | + one or more origins associated with this profile. |
| 51 | + |
| 52 | + - **GetEffectiveFeaturesForOrigin**: Asynchronously retrieves the origin feature |
| 53 | + settings—both the feature identifier and its enabled/disabled state—for |
| 54 | + a specified origin. |
| 55 | + |
| 56 | +2. `ICoreWebView2OriginFeatureSetting`: |
| 57 | + |
| 58 | + The ICoreWebView2OriginFeatureSetting interface represents a simple |
| 59 | + pairing of a feature enumeration value and its corresponding feature |
| 60 | + state (enabled or disabled). Currently, the feature enumeration supports |
| 61 | + the following values: |
| 62 | + |
| 63 | + - AccentColor |
| 64 | + - EnhancedSecurityMode |
| 65 | + |
| 66 | +# Example |
| 67 | + |
| 68 | +## Configure Origin Settings for Origin Patterns |
| 69 | + |
| 70 | +### C++ example |
| 71 | + |
| 72 | +```cpp |
| 73 | +void SetOriginFeatures() |
| 74 | +{ |
| 75 | + auto stagingProfile3 = |
| 76 | + m_webviewProfile.try_query<ICoreWebView2StagingProfile3>(); |
| 77 | + |
| 78 | + // Create feature settings |
| 79 | + wil::com_ptr<ICoreWebView2StagingOriginFeatureSetting> accentColorSetting; |
| 80 | + CHECK_FAILURE(stagingProfile3->CreateOriginFeatureSetting( |
| 81 | + COREWEBVIEW2_ORIGIN_FEATURE_ACCENT_COLOR, |
| 82 | + COREWEBVIEW2_ORIGIN_FEATURE_STATE_ENABLED, |
| 83 | + &accentColorSetting)); |
| 84 | + |
| 85 | + wil::com_ptr<ICoreWebView2StagingOriginFeatureSetting> enhancedSecuritySetting; |
| 86 | + CHECK_FAILURE(stagingProfile3->CreateOriginFeatureSetting( |
| 87 | + COREWEBVIEW2_ORIGIN_FEATURE_ENHANCED_SECURITY_MODE, |
| 88 | + COREWEBVIEW2_ORIGIN_FEATURE_STATE_ENABLED, |
| 89 | + &enhancedSecuritySetting)); |
| 90 | + |
| 91 | + // Set features for origin patterns |
| 92 | + ICoreWebView2StagingOriginFeatureSetting* features[] = { |
| 93 | + accentColorSetting.get(), |
| 94 | + enhancedSecuritySetting.get() |
| 95 | + }; |
| 96 | + |
| 97 | + LPCWSTR origins[] = { L"https://*.contoso.com" }; |
| 98 | + |
| 99 | + CHECK_FAILURE(stagingProfile3->SetOriginFeatures( |
| 100 | + ARRAYSIZE(origins), |
| 101 | + origins, |
| 102 | + ARRAYSIZE(features), |
| 103 | + features)); |
| 104 | +} |
| 105 | + |
| 106 | +void GetFeatureSettingsForOrigin() |
| 107 | +{ |
| 108 | + auto stagingProfile3 = |
| 109 | + m_webviewProfile.try_query<ICoreWebView2StagingProfile3>(); |
| 110 | + |
| 111 | + std::wstring origin = L"https://www.microsoft.com"; |
| 112 | + |
| 113 | + CHECK_FAILURE( |
| 114 | + stagingProfile3->GetEffectiveFeaturesForOrigin( |
| 115 | + origin.c_str(), |
| 116 | + Callback<ICoreWebView2StagingGetEffectiveFeaturesForOriginCompletedHandler>( |
| 117 | + [this, origin](HRESULT errorCode, |
| 118 | + ICoreWebView2StagingOriginFeatureSettingCollectionView* result) -> HRESULT |
| 119 | + { |
| 120 | + if (SUCCEEDED(errorCode)) |
| 121 | + { |
| 122 | + UINT32 count = 0; |
| 123 | + CHECK_FAILURE(result->get_Count(&count)); |
| 124 | + |
| 125 | + std::wstring message = L"Features for origin: " + origin + L"\n"; |
| 126 | + for (UINT32 i = 0; i < count; i++) |
| 127 | + { |
| 128 | + wil::com_ptr<ICoreWebView2StagingOriginFeatureSetting> setting; |
| 129 | + CHECK_FAILURE(result->GetValueAtIndex(i, &setting)); |
| 130 | + |
| 131 | + COREWEBVIEW2_ORIGIN_FEATURE feature; |
| 132 | + COREWEBVIEW2_ORIGIN_FEATURE_STATE State; |
| 133 | + CHECK_FAILURE(setting->get_Feature(&feature)); |
| 134 | + CHECK_FAILURE(setting->get_State(&State)); |
| 135 | + |
| 136 | + message += L"Feature: " + std::to_wstring(static_cast<int>(feature)) + |
| 137 | + L", Enabled: " + (State ? L"True" : L"False") + L"\n"; |
| 138 | + } |
| 139 | + |
| 140 | + MessageBoxW(m_appWindow->GetMainWindow(), message.c_str(), L"Origin Features", MB_OK); |
| 141 | + } |
| 142 | + return S_OK; |
| 143 | + }).Get())); |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +### .NET/WinRT |
| 148 | + |
| 149 | +```c# |
| 150 | +public void SetOriginFeatures() |
| 151 | +{ |
| 152 | + // Create feature settings collection |
| 153 | + var features = new Dictionary<CoreWebView2OriginFeature, CoreWebView2OriginFeatureState> |
| 154 | + { |
| 155 | + { CoreWebView2OriginFeature.AccentColor, CoreWebView2OriginFeatureState.Enabled }, |
| 156 | + { CoreWebView2OriginFeature.EnhancedSecurityMode, CoreWebView2OriginFeatureState.Enabled }, |
| 157 | + }; |
| 158 | + |
| 159 | + // Set features for origin patterns |
| 160 | + var origins = new[] { "https://*.contoso.com" }; |
| 161 | + m_webviewProfile.SetOriginFeatures(origins, features); |
| 162 | +} |
| 163 | + |
| 164 | +private async Task GetFeatureSettingsForOriginAsync() |
| 165 | +{ |
| 166 | + var settings = await m_webviewProfile.GetEffectiveFeaturesForOriginAsync("https://www.microsoft.com/"); |
| 167 | + var builder = new StringBuilder(); |
| 168 | + foreach (var setting in settings) { |
| 169 | + builder.AppendLine($"Feature: {setting.Feature}, Enabled = {setting.State}"); |
| 170 | + } |
| 171 | + m_appWindow.AsyncMessageBox(builder.ToString(), "Origin features"); |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | + |
| 176 | +# API details |
| 177 | + |
| 178 | +## C++ |
| 179 | +```cpp |
| 180 | +/// Specifies the feature types that can be configured for origins. |
| 181 | +[v1_enum] |
| 182 | +typedef enum COREWEBVIEW2_ORIGIN_FEATURE { |
| 183 | + /// Specifies the accent color feature for the origin. |
| 184 | + /// This controls whether the origin can use the AccentColor CSS keyword, which provides |
| 185 | + /// access to the system's accent color. The AccentColor keyword can be used in CSS color |
| 186 | + /// properties to match the operating system's accent color, enabling |
| 187 | + /// better integration with the native UI theme. |
| 188 | + /// By default, the accent color feature is disabled for all origins. |
| 189 | + /// |
| 190 | + /// For more information about CSS color keywords, see: |
| 191 | + /// https://developer.mozilla.org/en-US/docs/Web/CSS/color_value |
| 192 | + COREWEBVIEW2_ORIGIN_FEATURE_ACCENT_COLOR, |
| 193 | + /// Specifies enhanced security mode settings for the origin. |
| 194 | + /// Enhanced Security Mode provides additional protections by disabling or restricting |
| 195 | + /// certain web platform features that may pose security risks, such as JIT compilation, |
| 196 | + /// certain JavaScript APIs, and other potentially dangerous capabilities. This feature |
| 197 | + /// is particularly useful for protecting against zero-day exploits and reducing attack |
| 198 | + /// surface. When enabled for an origin, that origin will have Enhanced Security Mode |
| 199 | + /// applied; when disabled, normal security mode is used. |
| 200 | + /// Enhanced security mode can be configured globally via EnhancedSecurityModeState API on profile. |
| 201 | + /// If Enhanced Security Mode is not configured for an origin, the global profile setting will apply. |
| 202 | + /// |
| 203 | + /// For more information about Enhanced Security Mode, see: |
| 204 | + /// https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/security |
| 205 | + COREWEBVIEW2_ORIGIN_FEATURE_ENHANCED_SECURITY_MODE, |
| 206 | +} COREWEBVIEW2_ORIGIN_FEATURE; |
| 207 | + |
| 208 | +/// Specifies the state of the origin feature. |
| 209 | +[v1_enum] |
| 210 | +typedef enum COREWEBVIEW2_ORIGIN_FEATURE_STATE { |
| 211 | + /// Sets the enabled state of the origin feature. |
| 212 | + COREWEBVIEW2_ORIGIN_FEATURE_STATE_ENABLED, |
| 213 | + /// Sets the disabled state of the origin feature. |
| 214 | + COREWEBVIEW2_ORIGIN_FEATURE_STATE_DISABLED, |
| 215 | +} COREWEBVIEW2_ORIGIN_FEATURE_STATE; |
| 216 | + |
| 217 | +/// Receives the result of the `GetEffectiveFeaturesForOrigin` method. |
| 218 | +interface ICoreWebView2StagingGetEffectiveFeaturesForOriginCompletedHandler : IUnknown { |
| 219 | + /// Provides the result of the corresponding asynchronous method. |
| 220 | + HRESULT Invoke([in] HRESULT errorCode, [in] ICoreWebView2StagingOriginFeatureSettingCollectionView* result); |
| 221 | +} |
| 222 | + |
| 223 | + |
| 224 | +/// This is the ICoreWebView2Profile interface for origin feature management. |
| 225 | +interface ICoreWebView2StagingProfile3 : IUnknown { |
| 226 | + /// Creates a CoreWebView2OriginFeatureSetting objects. This object represents a specific feature and its state (enabled or disabled). |
| 227 | + /// This method allows creating a feature settings object that can be used with |
| 228 | + /// SetOriginFeatures to configure features for origins. |
| 229 | + HRESULT CreateOriginFeatureSetting( |
| 230 | + [in] COREWEBVIEW2_ORIGIN_FEATURE featureKind, |
| 231 | + [in] COREWEBVIEW2_ORIGIN_FEATURE_STATE featureState |
| 232 | + , [out, retval] ICoreWebView2StagingOriginFeatureSetting** value); |
| 233 | + |
| 234 | + /// Configures one or more feature settings for the specified origins. |
| 235 | + /// |
| 236 | + /// This method applies feature configurations—such as accent color support, |
| 237 | + /// or enhanced security mode—to origins. Origins |
| 238 | + /// may be provided as exact origin strings or as wildcard patterns. |
| 239 | + /// |
| 240 | + /// The origin pattern can be an exact origin string or a wildcard pattern. |
| 241 | + /// For wildcard patterns, `*` matches zero or more characters. |
| 242 | + /// Examples: |
| 243 | + /// | Origin Filter String | What It Matches | Description | |
| 244 | + /// |---------|-----------------|-------------| |
| 245 | + /// | `https://contoso.com` | Only `https://contoso.com` | Matches the exact origin with specific protocol and hostname | |
| 246 | + /// | `https://[*.]contoso.com` | `https://app.contoso.com`,`https://api.contoso.com`,`https://admin.contoso.com` | Matches any subdomain under the specified domain | |
| 247 | + /// | `*://contoso.com` | `https://contoso.com`,`http://contoso.com`,`ftp://contoso.com` | Matches any protocol for the specified hostname | |
| 248 | + /// | `https://www.microsoft.com:*` | `https://www.microsoft.com:123` | Matches any port for the specified origin | |
| 249 | + /// | `[*.]example.com` | `https://www.example.com`,`http://abc.example.com` | Matches any subdomain with any protocol for the specified domain | |
| 250 | + /// | `https://xn--qei.example/` | `https://❤.example/`,`https://xn--qei.example/` | Normalized punycode matches with corresponding Non-ASCII hostnames | |
| 251 | + /// |
| 252 | + /// Calling this method multiple times with the same (featureKind, featureState) |
| 253 | + /// pair overwrites the previous configuration; the most recent call takes |
| 254 | + /// precedence. |
| 255 | + /// |
| 256 | + /// When multiple configurations exist for the same feature but specify |
| 257 | + /// different featureState values, the configuration whose origin pattern is |
| 258 | + /// more specific takes precedence. |
| 259 | + /// |
| 260 | + /// The specificity of an origin pattern is determined by the presence and |
| 261 | + /// placement of wildcards. Three wildcard categories influence specificity: |
| 262 | + /// - Wildcards in the port (for example, `https://www.example.com:*/*`) |
| 263 | + /// - Wildcards in the scheme (for example, `*://www.example.com:123/*`) |
| 264 | + /// - Wildcards in the hostname (for example, `https://*.example.com:123/*`) |
| 265 | + /// |
| 266 | + /// If one pattern is more specific in one component but less specific in |
| 267 | + /// another, specificity is evaluated in the following order: |
| 268 | + /// 1. Hostname |
| 269 | + /// 2. Scheme |
| 270 | + /// 3. Port |
| 271 | + /// |
| 272 | + /// For example, the following patterns are ordered by precedence: |
| 273 | + /// `https://www.example.com:*/*` → `*://www.example.com:123/*` → `https://*.example.com:123/*`. |
| 274 | + /// |
| 275 | + /// To reset the origin list for a particular (feature, state) pair, call |
| 276 | + /// SetOriginFeatures with an empty origin list or nullptr for the same |
| 277 | + /// feature setting. |
| 278 | + /// For example, |
| 279 | + /// SetOriginFeatures(1, {"https://microsoft.com"}, 1, {{ESM: Enabled}}); |
| 280 | + /// SetOriginFeatures(0, {}, 1, {{ESM: Enabled}}); // Resets {ESM: Enabled} |
| 281 | + HRESULT SetOriginFeatures( |
| 282 | + [in] UINT32 originsCount, |
| 283 | + [in] LPCWSTR* originPatterns, |
| 284 | + [in] UINT32 featureCount, |
| 285 | + [in] ICoreWebView2StagingOriginFeatureSetting** features |
| 286 | + ); |
| 287 | + |
| 288 | + /// Gets the effective feature settings for a specified origin. |
| 289 | + /// Returns a collection of feature settings for all the features in |
| 290 | + /// `COREWEBVIEW2_ORIGIN_FEATURE`. This collection contains the effective |
| 291 | + /// (computed) setting values for all features, including features that were |
| 292 | + /// not explicitly configured via `SetOriginFeatures` and thus have their |
| 293 | + /// default values. |
| 294 | + /// The order of features in the returned collection is not guaranteed. |
| 295 | + /// The origin should have a valid scheme and host (e.g. "https://www.example.com"), |
| 296 | + /// otherwise the method fails with `E_INVALIDARG`. |
| 297 | + HRESULT GetEffectiveFeaturesForOrigin( |
| 298 | + [in] LPCWSTR origin |
| 299 | + , [in] ICoreWebView2StagingGetEffectiveFeaturesForOriginCompletedHandler* handler); |
| 300 | +} |
| 301 | + |
| 302 | +/// Represents a feature setting configuration for a origin. |
| 303 | +interface ICoreWebView2StagingOriginFeatureSetting : IUnknown { |
| 304 | + /// The feature type for this setting. |
| 305 | + [propget] HRESULT Feature([out, retval] COREWEBVIEW2_ORIGIN_FEATURE* value); |
| 306 | + |
| 307 | + /// Indicates whether the feature is enabled for the origin. |
| 308 | + [propget] HRESULT State([out, retval] COREWEBVIEW2_ORIGIN_FEATURE_STATE* value); |
| 309 | +} |
| 310 | + |
| 311 | + |
| 312 | +/// A collection of origin settings. |
| 313 | +interface ICoreWebView2StagingOriginFeatureSettingCollectionView : IUnknown { |
| 314 | + /// Gets the number of objects contained in the `OriginFeatureSettingCollection`. |
| 315 | + [propget] HRESULT Count([out, retval] UINT32* value); |
| 316 | + |
| 317 | + /// Gets the object at the specified index. |
| 318 | + HRESULT GetValueAtIndex( |
| 319 | + [in] UINT32 index |
| 320 | + , [out, retval] ICoreWebView2StagingOriginFeatureSetting** value); |
| 321 | +} |
| 322 | +``` |
| 323 | + |
| 324 | +## MIDL3 |
| 325 | + |
| 326 | +```c# |
| 327 | +namespace Microsoft.Web.WebView2.Core |
| 328 | +{ |
| 329 | + |
| 330 | + enum CoreWebView2OriginFeatureState |
| 331 | + { |
| 332 | + Enabled = 0, |
| 333 | + Disabled = 1, |
| 334 | + }; |
| 335 | + enum CoreWebView2OriginFeature |
| 336 | + { |
| 337 | + AccentColor = 0, |
| 338 | + EnhancedSecurityMode = 1, |
| 339 | + }; |
| 340 | + |
| 341 | + runtimeclass CoreWebView2OriginFeatureSetting |
| 342 | + { |
| 343 | + // ICoreWebView2StagingOriginFeatureSetting members |
| 344 | + CoreWebView2OriginFeature Feature { get; }; |
| 345 | + |
| 346 | + CoreWebView2OriginFeatureState State { get; }; |
| 347 | + } |
| 348 | + |
| 349 | + runtimeclass CoreWebView2Profile |
| 350 | + { |
| 351 | + [interface_name("Microsoft.Web.WebView2.Core.CoreWebView2Profile_Manual5")] |
| 352 | + { |
| 353 | + void SetOriginFeatures( |
| 354 | + Windows.Foundation.Collections.IIterable<String> origins, |
| 355 | + Windows.Foundation.Collections.IIterable< |
| 356 | + Windows.Foundation.Collections.IKeyValuePair<CoreWebView2OriginFeature, CoreWebView2OriginFeatureState> > features); |
| 357 | + |
| 358 | + Windows.Foundation.IAsyncOperation<IVectorView<CoreWebView2OriginFeatureSetting> > GetEffectiveFeaturesForOriginAsync(String origin); |
| 359 | + } |
| 360 | + } |
| 361 | +} |
| 362 | +``` |
0 commit comments