|
| 1 | +Trusted Origin Support for WebView2 |
| 2 | +=== |
| 3 | + |
| 4 | +# Background |
| 5 | + |
| 6 | +WebView2 applications often require different security and feature policies for different origins based on trust levels. Some applications need to enable specific features only for trusted origins while applying stricter security measures to untrusted content. |
| 7 | + |
| 8 | +Currently, WebView2 applies uniform security policies across all origins, which creates two key challenges: |
| 9 | +- **Feature Access Control**: Applications cannot selectively enable advanced features (such as certain APIs or capabilities) only for origins they trust, forcing them to either expose these features to all origins or disable them entirely. |
| 10 | +- **Performance vs Security Trade-offs**: Security features like Enhanced Security Mode, while important for untrusted content, can impact performance when applied to trusted origins where such protections may be unnecessary. |
| 11 | + |
| 12 | +For example, a content management application might want to allow full feature access and disable security restrictions when loading trusted administrative interfaces, while maintaining strict security policies for user-generated or external content loaded in the same WebView2 instance. |
| 13 | + |
| 14 | +The Trusted Origin API addresses these scenarios by allowing applications to designate specific origins as trusted, enabling fine-grained control over which security and feature policies apply to different content sources. |
| 15 | + |
| 16 | +# Description |
| 17 | + |
| 18 | +This specification introduces the following APIs. |
| 19 | + |
| 20 | +1. On `CoreWebView2Profile` |
| 21 | + |
| 22 | + - **GetOriginSettings**: Get the list of `CoreWebView2OriginSetting` for this profile. |
| 23 | + - **CreateOriginSetting**: Create and return a new `CoreWebView2OriginSetting` object. |
| 24 | +2. `ICoreWebView2OriginSetting`interface, which exposes get and set APIs for the following features. These APIs are used to set the feature state of these features. |
| 25 | + |
| 26 | + - AccentColor |
| 27 | + - EnhancedSecurityMode |
| 28 | + - PersistenceStorage |
| 29 | + - TrackingPrevention |
| 30 | + |
| 31 | + The feature state can have three values which are defined by OriginSettingState { |
| 32 | + Default, |
| 33 | + Allow, |
| 34 | + Block} |
| 35 | + |
| 36 | +# Example |
| 37 | + |
| 38 | +## Set Origin Setting for an Origin Pattern |
| 39 | + |
| 40 | +### C++ example |
| 41 | + |
| 42 | +```cpp |
| 43 | +void ScenarioTrustedOrigin::SetFeatureForOrigins(std::wstring originPattern) |
| 44 | +{ |
| 45 | + auto stagingProfile3 = |
| 46 | + m_webviewProfile.try_query<ICoreWebView2StagingProfile3>(); |
| 47 | + |
| 48 | + wil::com_ptr<ICoreWebView2StagingOriginSettingCollection> originSettingCollection; |
| 49 | + stagingProfile3->GetOriginSettings(&originSettingCollection); |
| 50 | + |
| 51 | + wil::com_ptr<ICoreWebView2StagingOriginSetting> originSetting; |
| 52 | + stagingProfile3->CreateOriginSetting(&originSetting); |
| 53 | + |
| 54 | + CHECK_FAILURE(originSetting->put_OriginPattern(originPattern.c_str())); |
| 55 | + |
| 56 | + // Block Accent Color |
| 57 | + CHECK_FAILURE(originSetting->put_AccentColor(OriginSettingState::OriginSettingState_BLOCK)); |
| 58 | + |
| 59 | + // Allow Persistence Storage |
| 60 | + CHECK_FAILURE(originSetting->put_PersistenceStorage(OriginSettingState::OriginSettingState_ALLOW)); |
| 61 | + |
| 62 | + UINT32 count; |
| 63 | + CHECK_FAILURE(originSettingCollection->get_Count(&count)); |
| 64 | + CHECK_FAILURE(originSettingCollection->InsertValueAtIndex(count, originSetting.get())); |
| 65 | +} |
| 66 | +``` |
| 67 | +
|
| 68 | +### .NET/WinRT |
| 69 | +
|
| 70 | +```c# |
| 71 | +var profile = webView2.CoreWebView2.Profile; |
| 72 | +
|
| 73 | +var originSetting = profile.CreateOriginSetting(); |
| 74 | +originSetting.AccentColor = Block; |
| 75 | +originSetting.PersistenceStorage = Allow; |
| 76 | +
|
| 77 | +var originSettings = profile.GetOriginSettings(); |
| 78 | +originSettings.Append(originSettings); |
| 79 | +
|
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +# API details |
| 84 | + |
| 85 | +## C++ |
| 86 | +```cpp |
| 87 | +[v1_enum] |
| 88 | +typedef enum OriginSettingState { |
| 89 | + /// Default state of the feature |
| 90 | + OriginSettingState_DEFAULT = 0x0, |
| 91 | + /// Allow the feature |
| 92 | + OriginSettingState_ALLOW = 0x1, |
| 93 | + /// Block the feature |
| 94 | + OriginSettingState_BLOCK = 0x2, |
| 95 | +} OriginSettingState; |
| 96 | + |
| 97 | +interface ICoreWebView2StagingProfile3 : IUnknown { |
| 98 | + /// Gets the list of origin settings associated with this profile. |
| 99 | + HRESULT GetOriginSettings( |
| 100 | + [out, retval] ICoreWebView2StagingOriginSettingCollection** value); |
| 101 | + |
| 102 | + /// Creates a new `OriginSetting` object. |
| 103 | + HRESULT CreateOriginSetting( |
| 104 | + [out, retval] ICoreWebView2StagingOriginSetting** value); |
| 105 | +} |
| 106 | + |
| 107 | +/// Represents settings for a specific origin. |
| 108 | +interface ICoreWebView2StagingOriginSetting : IUnknown { |
| 109 | + /// Gets the `AccentColor` property. |
| 110 | + [propget] HRESULT AccentColor([out, retval] OriginSettingState* value); |
| 111 | + |
| 112 | + |
| 113 | + /// Sets the enabled property for Accent Color for the associated origin. |
| 114 | + /// The default value for this is `Block`. When set to `Allow`, the origin is |
| 115 | + /// allowed to use AccentColor keyword or accent-color CSS property to display OS Accent Color. |
| 116 | + [propput] HRESULT AccentColor([in] OriginSettingState value); |
| 117 | + |
| 118 | + |
| 119 | + /// Gets the `EnhancedSecurityMode` property. |
| 120 | + [propget] HRESULT EnhancedSecurityMode([out, retval] OriginSettingState* value); |
| 121 | + |
| 122 | + |
| 123 | + /// Sets the enabled property for Enhanced Security Mode for the associated origin. |
| 124 | + /// When set to `Block`, the origin bypasses Enhanced Security Mode even if |
| 125 | + /// Enhanced Security Mode is enabled for the profile. Similarly, when set to `Allow`, |
| 126 | + /// the origin enforces Enhanced Security Mode even if Enhanced Security Mode is disabled |
| 127 | + /// for the profile. |
| 128 | + [propput] HRESULT EnhancedSecurityMode([in] OriginSettingState value); |
| 129 | + |
| 130 | + |
| 131 | + /// Gets the `OriginPattern` property. |
| 132 | + /// |
| 133 | + /// The caller must free the returned string with `CoTaskMemFree`. See |
| 134 | + /// [API Conventions](/microsoft-edge/webview2/concepts/win32-api-conventions#strings). |
| 135 | + [propget] HRESULT OriginPattern([out, retval] LPWSTR* value); |
| 136 | + |
| 137 | + |
| 138 | + /// Sets the origin pattern associated with this origin setting. |
| 139 | + /// The origins pattern can be both exact origin strings and wildcard patterns. |
| 140 | + /// For wildcard patterns, `*` matches zero or more characters. |
| 141 | + /// Examples: |
| 142 | + /// | Origin Filter String | What It Matches | Description | |
| 143 | + /// |---------|-----------------|-------------| |
| 144 | + /// | `https://contoso.com` | Only `https://contoso.com` | Matches the exact origin with specific protocol and hostname | |
| 145 | + /// | `https://*.contoso.com` | `https://app.contoso.com`,`https://api.contoso.com`,`https://admin.contoso.com` | Matches any subdomain under the specified domain | |
| 146 | + /// | `*://contoso.com` | `https://contoso.com`,`http://contoso.com`,`ftp://contoso.com` | Matches any protocol for the specified hostname | |
| 147 | + /// | `*contoso.*` | `https://www.contoso.com`,`http://app.contoso.com` | Matches any protocol and any subdomain under the hostname | |
| 148 | + /// | `*example/` | `https://app.example/`,`https://api.example/` | Matches any subdomain and top-level domain variations | |
| 149 | + /// | `https://xn--qei.example/` | `https://â¤.example/`,`https://xn--qei.example/` | Normalized punycode matches with corresponding Non-ASCII hostnames | |
| 150 | + /// |
| 151 | + /// Note: `*` is not a valid value for OriginPattern. |
| 152 | + /// |
| 153 | + /// The caller must free the returned string with `CoTaskMemFree`. See |
| 154 | + /// [API Conventions](/microsoft-edge/webview2/concepts/win32-api-conventions#strings). |
| 155 | + [propput] HRESULT OriginPattern([in] LPCWSTR value); |
| 156 | + |
| 157 | + |
| 158 | + /// Gets the `PersistenceStorage` property. |
| 159 | + [propget] HRESULT PersistenceStorage([out, retval] OriginSettingState* value); |
| 160 | + |
| 161 | + |
| 162 | + /// Sets the enabled property for Persistence Storage for the associated origin. |
| 163 | + /// The default value for this is `Block`. When set to `Allow`, the storage created |
| 164 | + /// on this origin persists even when browser evicts storage under low disk space conditions. |
| 165 | + [propput] HRESULT PersistenceStorage([in] OriginSettingState value); |
| 166 | + |
| 167 | + |
| 168 | + /// Gets the `TrackingPrevention` property. |
| 169 | + [propget] HRESULT TrackingPrevention([out, retval] OriginSettingState* value); |
| 170 | + |
| 171 | + |
| 172 | + /// Sets the enabled property for Tracking Prevention for the associated origin. |
| 173 | + /// When set to `Block`, the origin bypasses Tracking Prevention even if |
| 174 | + /// Tracking Prevention is enabled for the profile. Similarly, when set to `Allow`, |
| 175 | + /// the origin enforces Tracking Prevention even if Tracking Prevention is disabled |
| 176 | + /// for the profile. |
| 177 | + [propput] HRESULT TrackingPrevention([in] OriginSettingState value); |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +## .Net/WinRT |
| 182 | + |
| 183 | +```c# |
| 184 | +namespace Microsoft.Web.WebView2.Core |
| 185 | +{ |
| 186 | + enum Originsettingstate |
| 187 | + { |
| 188 | + Default = 0, |
| 189 | + Allow = 1, |
| 190 | + Block = 2, |
| 191 | + }; |
| 192 | + |
| 193 | + runtimeclass CoreWebView2OriginSetting |
| 194 | + { |
| 195 | + Originsettingstate AccentColor { get; set; }; |
| 196 | + |
| 197 | + Originsettingstate EnhancedSecurityMode { get; set; }; |
| 198 | + |
| 199 | + String OriginPattern { get; set; }; |
| 200 | + |
| 201 | + Originsettingstate PersistenceStorage { get; set; }; |
| 202 | + |
| 203 | + Originsettingstate TrackingPrevention { get; set; }; |
| 204 | + } |
| 205 | + |
| 206 | + runtimeclass CoreWebView2Profile |
| 207 | + { |
| 208 | + IVector<CoreWebView2OriginSetting> GetOriginSettings(); |
| 209 | + |
| 210 | + CoreWebView2OriginSetting CreateOriginSetting(); |
| 211 | + } |
| 212 | +} |
| 213 | +``` |
0 commit comments