Skip to content

Commit 5a0f451

Browse files
authored
Update AdditionalAllowedFrameAncestors.md
1 parent 90f1833 commit 5a0f451

1 file changed

Lines changed: 72 additions & 72 deletions

File tree

specs/AdditionalAllowedFrameAncestors.md

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,65 @@ Due to potential [Clickjacking](https://en.wikipedia.org/wiki/Clickjacking) att
77
However, there are application scenarios that require hosting these sites in the app's UI that is authored as an HTML page.
88
`<webview>` HTML element was provided for these hosting scenarios in previous solutions like Electron and JavaScript UWP apps.
99

10-
For WebView2, we are providing a native API for these hosting scenarios. It let the developers to provide additional allowed frame ancestors as if the site sent these as part of the Content-Security-Policy frame-ancestors directive. An ancestor is allowed if it is allowed by the site's origional http headers or by this addtional allowed frame ancestors.
10+
For WebView2, we are providing a native API for these hosting scenarios. Developers can use it to provide additional allowed frame ancestors as if the site sent these as part of the Content-Security-Policy frame-ancestors directive. The result is that an ancestor is allowed if it is allowed by the site's origional policies or by this addtional allowed frame ancestors.
1111

1212
# Conceptual pages (How To)
1313

14-
To host other sites in an trusted page
14+
To host other sites in an trusted page with modified allowed frame ancestors
1515
- Listen to FrameNavigationStarting event of CoreWebView2.
16-
- Set AdditionalAllowedFrameAncestors property of the NavigationStartingEventArgs to a list of trusted origins that is hosting the site.
16+
- Set AdditionalAllowedFrameAncestors property of the NavigationStartingEventArgs to a list additional allowed frame ancestors using the same syntax as [Content-Security-Policy frame-ancestors directive](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors).
1717

1818
The list should normally only contain the origin of the top page.
19-
If you are hosting other sites through nested iframes and the origins of some of the intermediate iframes are different from the origin of the top page, the list should also include those origins.
19+
If you are hosting other sites through nested iframes and the origins of some of the intermediate iframes are different from the origin of the top page and those origins might not be allowed by the site's original policies, the list should also include those origins.
2020

21-
You should only add an origin to the list if it is fully trusted. You should limit the usage of the API to the targetted iframes whenever possible.
21+
You should only add an origin to the list if it is fully trusted. You should limit the usage of the API to the targetted app scenarios.
2222

2323
# Examples
2424
## Win32 C++
2525
```cpp
26-
const std::wstring myTrustedSite = L"https://example.com/";
27-
const std::wstring siteToHost = L"https://www.microsoft.com/";
26+
const std::wstring myTrustedSite = L"https://example.com/";
27+
const std::wstring siteToHost = L"https://www.microsoft.com/";
2828

29-
bool AreSitesSame(PCWSTR url1, PCWSTR url2)
30-
{
31-
wil::com_ptr<IUri> uri1;
32-
CHECK_FAILURE(CreateUri(url1.c_str(), Uri_CREATE_CANONICALIZE, 0, &uri1));
33-
DWORD scheme1 = URL_SCHEME_INVALID;
34-
DWORD port1 = 0;
35-
wil::unique_bstr host1;
36-
CHECK_FAILURE(uri1->GetScheme(&scheme1));
37-
CHECK_FAILURE(uri1->GetHost(&host1));
38-
CHECK_FAILURE(uri1->GetPort(&port1));
39-
wil::com_ptr<IUri> uri2;
40-
CHECK_FAILURE(CreateUri(url2.c_str(), Uri_CREATE_CANONICALIZE, 0, &uri2));
41-
DWORD scheme2 = URL_SCHEME_INVALID;
42-
DWORD port2 = 0;
43-
wil::unique_bstr host2;
44-
CHECK_FAILURE(uri2->GetScheme(&scheme2));
45-
CHECK_FAILURE(uri2->GetHost(&host2));
46-
CHECK_FAILURE(uri2->GetPort(&port2));
47-
return (scheme1 == scheme2) && (port1 == port2) && (wcscmp(host1.get(), host2.get()) == 0);
48-
}
29+
bool AreSitesSame(PCWSTR url1, PCWSTR url2)
30+
{
31+
wil::com_ptr<IUri> uri1;
32+
CHECK_FAILURE(CreateUri(url1.c_str(), Uri_CREATE_CANONICALIZE, 0, &uri1));
33+
DWORD scheme1 = URL_SCHEME_INVALID;
34+
DWORD port1 = 0;
35+
wil::unique_bstr host1;
36+
CHECK_FAILURE(uri1->GetScheme(&scheme1));
37+
CHECK_FAILURE(uri1->GetHost(&host1));
38+
CHECK_FAILURE(uri1->GetPort(&port1));
39+
wil::com_ptr<IUri> uri2;
40+
CHECK_FAILURE(CreateUri(url2.c_str(), Uri_CREATE_CANONICALIZE, 0, &uri2));
41+
DWORD scheme2 = URL_SCHEME_INVALID;
42+
DWORD port2 = 0;
43+
wil::unique_bstr host2;
44+
CHECK_FAILURE(uri2->GetScheme(&scheme2));
45+
CHECK_FAILURE(uri2->GetHost(&host2));
46+
CHECK_FAILURE(uri2->GetPort(&port2));
47+
return (scheme1 == scheme2) && (port1 == port2) && (wcscmp(host1.get(), host2.get()) == 0);
48+
}
4949

50-
// App specific logic to decide whether the page is fully trusted.
51-
bool IsAppContentUri(PCWSTR pageUrl)
52-
{
53-
return AreSitesSame(pageUrl, myTrustedSite);
54-
}
50+
// App specific logic to decide whether the page is fully trusted.
51+
bool IsAppContentUri(PCWSTR pageUrl)
52+
{
53+
return AreSitesSame(pageUrl, myTrustedSite);
54+
}
5555

56-
// App specific logic to decide whether a site is the one it wants to host.
57-
bool IsTargetSite(PCWSTR siteUrl)
58-
{
59-
return AreSitesSame(pageUrl, siteToHost);
60-
}
56+
// App specific logic to decide whether a site is the one it wants to host.
57+
bool IsTargetSite(PCWSTR siteUrl)
58+
{
59+
return AreSitesSame(siteUrl, siteToHost);
60+
}
6161

62-
void MyApp::HandleHostedSites()
63-
{
62+
void MyApp::HandleHostedSites()
63+
{
6464
CHECK_FAILURE(m_webview->add_FrameCreated(
6565
Callback<ICoreWebView2FrameCreatedEventHandler>(
6666
[this](ICoreWebView2* sender, ICoreWebView2FrameCreatedEventArgs* args)
6767
-> HRESULT
6868
{
69-
wil::com_ptr<ICoreWebView2Frame> webviewFrame;
70-
CHECK_FAILURE(args->get_Frame(&webviewFrame));
7169
wil::unique_cotaskmem_string pageUrl;
7270
CHECK_FAILURE(m_webView->get_Source(&pageUrl));
7371
// IsAppContentUri verifies that pageUrl is app's content.
@@ -76,18 +74,20 @@ You should only add an origin to the list if it is fully trusted. You should lim
7674
// We are on trusted pages. Now check whether it is the iframe we plan
7775
// to host other sites.
7876
const std::wstring siteHostingFrameName = L"my_site_hosting_frame";
77+
wil::com_ptr<ICoreWebView2Frame> webviewFrame;
78+
CHECK_FAILURE(args->get_Frame(&webviewFrame));
7979
wil::unique_cotaskmem_string frameName;
8080
CHECK_FAILURE(webviewFrame->get_Name(&frameName));
8181
if (siteHostingFrameName == frameName.get())
8282
{
8383
// We are hosting sites.
84-
m_hosting_site = true;
84+
m_hostingSite = true;
8585
CHECK_FAILURE(webviewFrame->add_Destroyed(
8686
Microsoft::WRL::Callback<
8787
ICoreWebView2FrameDestroyedEventHandler>(
8888
[this](ICoreWebView2Frame* sender,
8989
IUnknown* args) -> HRESULT {
90-
m_hosting_site = false;
90+
m_hostingSite = false;
9191
return S_OK;
9292
})
9393
.Get(),
@@ -98,32 +98,32 @@ You should only add an origin to the list if it is fully trusted. You should lim
9898
})
9999
.Get(),
100100
nullptr));
101-
CHECK_FAILURE(m_webview->add_FrameNavigationStarting(
102-
Microsoft::WRL::Callback<ICoreWebView2NavigationStartingEventHandler>(
103-
[this](
104-
ICoreWebView2* sender,
105-
ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT
101+
CHECK_FAILURE(m_webview->add_FrameNavigationStarting(
102+
Microsoft::WRL::Callback<ICoreWebView2NavigationStartingEventHandler>(
103+
[this](
104+
ICoreWebView2* sender,
105+
ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT
106+
{
107+
if (m_hostingSite)
106108
{
107-
if (m_hosting_site)
108-
{
109-
wil::unique_cotaskmem_string navigationTargetUri;
110-
CHECK_FAILURE(args->get_Uri(&navigationTargetUri));
111-
wil::com_ptr<
112-
ICoreWebView2NavigationStartingEventArgs_2>
113-
nav_start_args;
114-
if (SUCCEEDED(args->QueryInterface(
115-
IID_PPV_ARGS(&nav_start_args))) &&
116-
IsTargetSite(navigationTargetUri.get()))
117-
{
118-
nav_start_args
119-
->put_AdditionalAllowedFrameAncestors(
120-
myTrustedSite);
121-
}
122-
}
123-
return S_OK;
124-
})
125-
.Get(),
126-
nullptr));
109+
wil::unique_cotaskmem_string navigationTargetUri;
110+
CHECK_FAILURE(args->get_Uri(&navigationTargetUri));
111+
wil::com_ptr<
112+
ICoreWebView2NavigationStartingEventArgs_2>
113+
navigationStartArgs;
114+
if (SUCCEEDED(args->QueryInterface(
115+
IID_PPV_ARGS(&navigationStartArgs))) &&
116+
IsTargetSite(navigationTargetUri.get()))
117+
{
118+
navigationStartArgs
119+
->put_AdditionalAllowedFrameAncestors(
120+
myTrustedSite);
121+
}
122+
}
123+
return S_OK;
124+
})
125+
.Get(),
126+
nullptr));
127127
}
128128
```
129129
## WinRT and .NET
@@ -142,10 +142,10 @@ You should only add an origin to the list if it is fully trusted. You should lim
142142
return AreSitesSame(pageUrl, myTrustedSite);
143143
}
144144
145-
private bool IsTargetSite(string url)
145+
private bool IsTargetSite(string siteUrl)
146146
{
147147
// App specific logic to decide whether the site is the one it wants to host.
148-
return AreSitesSame(url, siteToHost);
148+
return AreSitesSame(siteUrl, siteToHost);
149149
}
150150
151151
private void CoreWebView2_FrameCreated(CoreWebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2FrameCreatedEventArgs args)
@@ -154,14 +154,14 @@ You should only add an origin to the list if it is fully trusted. You should lim
154154
const string siteHostingFrameName = "my_site_hosting_frame";
155155
if (IsAppContentUri(sender.Source) && (args.Frame.Name == siteHostingFrameName))
156156
{
157-
m_hosting_site = true;
157+
m_hostingSite = true;
158158
args.Frame.Destroyed += CoreWebView2_SiteHostingFrameDestroyed;
159159
}
160160
}
161161
162162
private void CoreWebView2_SiteHostingFrameDestroyed(CoreWebView2Frame sender, Object args)
163163
{
164-
m_hosting_site = false;
164+
m_hostingSite = false;
165165
}
166166
167167
private void CoreWebView2_FrameNavigationStarting(CoreWebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationStartingEventArgs args)

0 commit comments

Comments
 (0)