From 310fd066e91f454b990372ffa30e803cc8120975 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 12:56:40 +0100 Subject: unslug zh-cn: move --- files/zh-cn/web/http/feature_policy/index.html | 153 +++++++++++++++++++++ .../feature_policy/using_feature_policy/index.html | 140 +++++++++++++++++++ 2 files changed, 293 insertions(+) create mode 100644 files/zh-cn/web/http/feature_policy/index.html create mode 100644 files/zh-cn/web/http/feature_policy/using_feature_policy/index.html (limited to 'files/zh-cn/web/http/feature_policy') diff --git a/files/zh-cn/web/http/feature_policy/index.html b/files/zh-cn/web/http/feature_policy/index.html new file mode 100644 index 0000000000..90e83fb04a --- /dev/null +++ b/files/zh-cn/web/http/feature_policy/index.html @@ -0,0 +1,153 @@ +--- +title: Feature Policy +slug: Web/HTTP/策略特征 +translation_of: Web/HTTP/Feature_Policy +--- +
{{SeeCompatTable}}{{HTTPSidebar}}
+ +

特征策略允许web开发者在浏览器中选择启用、禁用和修改确切特征和 API 的行为.比如{{Glossary("CSP","内容安全策略")}},但是它控制的是浏览器的特征非安全行为.

+ +

概述

+ +

特征策略提供了一种机制去声明哪些功能通过你的网络,是可以被用的(或者不被使用的)。这就允许你通过功能可用性来很好的锁定功能,即使代码很老,或者包含第三方的内容。

+ +

有了功能策略,你可以选择一组“策略”,让浏览器强制执行整个网站使用的特定功能。这些策略限制了站点可以访问哪些api,或者修改浏览器对某些特性的默认行为

+ +

使用特性策略可以做什么的示例?:

+ + + +

概念和用法

+ +

特性策略允许您在顶级页面和嵌入式框架中控制哪些源可以使用哪些特性。实际上,您编写了一个策略,它是每个特性允许的起源列表。对于由特性策略控制的每个特性,只有当它的起源与允许的起源列表匹配时,该特性才会在当前文档或框架中启用.

+ +

对于每个策略控制的功能,浏览器都会维护启用该功能的来源列表,称为允许列表。如果您未为功能指定策略,则将使用默认的允许列表。默认的许可列表特定于每个功能.

+ +

编写策略

+ +

使用一组单独的策略指令来描述策略。策略指令是已定义功能名称和可以使用该功能的来源的允许列表的组合.

+ +

指定策略

+ +

功能策略提供了两种方法来指定用于控制功能的策略:

+ + + +

HTTP标头和allow属性之间的主要区别在于allow属性仅控制iframe中的功能。标头控制响应中的功能以及页面内的任何嵌入式内容.

+ +

点此链接查看更多详细信息 Using Feature Policy.

+ +

策略控制功能的类型

+ +

尽管功能策略使用一致的语法提供了对多个功能的控制,但是策略控制功能的行为却有所不同,并取决于多个因素.

+ +

一般原则是,Web开发人员应该有一种直观或不间断的方式来检测或处理禁用该功能的情况。新引入的功能可能具有显示状态的显式API。稍后与功能策略集成的现有功能通常将使用现有机制。一些方法包括:

+ + + +

当前的一组策略控制功能可分为两大类:

+ + + +

良好用户体验的最佳实践

+ +

有几种策略控制的功能可帮助实施最佳实践,以提供良好的性能和用户体验.

+ +

在大多数情况下,策略控制的功能代表的功能在使用时会对用户体验产生负面影响。为避免破坏现有的Web内容,此类策略控制功能的默认设置是允许所有来源使用该功能。然后,通过使用禁用策略控制功能的策略来实施最佳实践。有关更多详细信息,请参见“实施最佳实践以提供良好的用户体验”.

+ +

功能包括:

+ + + +

精细控制某些功能

+ +

Web提供的功能和API如果被滥用,可能会带来隐私或安全风险。在某些情况下,您可能希望严格限制在网站上使用此类功能的方式。有策略控制的功能,允许针对网站中的特定来源或框架启用/禁用功能。该功能在可用时与Permissions API或特定于功能的机制集成在一起,以检查该功能是否可用.

+ +

功能包括:

+ + + +

更多示例

+ + + +

规范

+ + + + + + + + + + + + + + +
说明书状态描述
{{SpecName('Feature Policy','#feature-policy-http-header-field','Feature-Policy')}}{{Spec2('Feature Policy')}}初始化前定义 {{httpheader('Feature-Policy')}} 头. 规范中定义了指令所控制的特性. 有关详细信息,请参阅个别指令页面.
+ +

浏览器兼容性

+ + + +

{{Compat("http.headers.Feature-Policy")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/http/feature_policy/using_feature_policy/index.html b/files/zh-cn/web/http/feature_policy/using_feature_policy/index.html new file mode 100644 index 0000000000..9a37fa46f3 --- /dev/null +++ b/files/zh-cn/web/http/feature_policy/using_feature_policy/index.html @@ -0,0 +1,140 @@ +--- +title: Using Feature Policy +slug: Web/HTTP/策略特征/Using_Feature_Policy +translation_of: Web/HTTP/Feature_Policy/Using_Feature_Policy +--- +
{{HTTPSidebar}} {{SeeCompatTable}}
+ +

Feature Policy allows you to control which origins can use which features, both in the top-level page and in embedded frames. Essentially, you write a policy, which is an allowed list of origins for each feature. For every feature controlled by Feature Policy, the feature is only enabled in the current document or frame if its origin matches the allowed list of origins.

+ +

For each policy-controlled feature, the browser maintains a list of origins for which the feature is enabled, known as an allowlist. If you do not specify a policy for a feature, then a default allowlist will be used. The default allowlist is specific to each feature.

+ +

Writing a policy

+ +

A policy is described using a set of individual policy directives. A policy directive is a combination of a defined feature name, and an allowlist of origins that can use the feature.

+ +

allowlist

+ +

allowlist可以使用以下一个或多个值。

+ + + +

*(在所有源地址启用)'none'(在所有源地址禁用)只允许单独使用,而'self''src'可以与多个源地址一起使用。

+ +

所有的特性都有一个如下的默认的allowlist

+ + + +

Specifying your policy

+ +

Feature Policy provides two ways to specify policies to control features:

+ + + +

The primary difference between the HTTP header and the allow attribute is that the allow attribute only controls features within an iframe. The header controls features in the response and any embedded content within the page.

+ +

The Feature-Policy HTTP header

+ +

You can send the Feature-Policy HTTP header with the response of a page. The value of this header is a policy to be enforced by the browser for the given page. It has the following structure.

+ +
Feature-Policy: <feature name> <allowlist of origin(s)>
+ +

For example, to block all content from using the Geolocation API across your site:

+ +
Feature-Policy: geolocation 'none'
+ +

Several features can be controlled at the same time by sending the HTTP header with a semicolon-separated list of policy directives, or by sending a separate header for each policy.

+ +

For example, the following are equivalent:

+ +
Feature-Policy: unsized-media 'none'; geolocation 'self' https://example.com; camera *;
+
+Feature-Policy: unsized-media 'none'
+Feature-Policy: geolocation 'self' https://example.com
+Feature-Policy: camera *;
+
+ +

The iframe allow attribute

+ +

The second way to use Feature Policy is for controlling content within an iframe. Use the allow attribute to specify a policy list for embedded content.

+ +

For example, allow all browsing contexts within this iframe to use fullscreen:

+ +
<iframe src="https://example.com..." allow="fullscreen"></iframe>
+ +

This is equivalent to:

+ +
<iframe src="https://example.com..." allow="fullscreen 'src'"></iframe>
+ +

This example allows <iframe> content on a particular origin to access the user's location:

+ +
<iframe src="https://google-developers.appspot.com/demos/..."
+        allow="geolocation https://google-developers.appspot.com"></iframe>
+
+ +

Similar to the HTTP header, several features can be controlled at the same time by specifying a semicolon-separated list of policy directives.

+ +

For example, this blocks the <iframe> from using the camera and microphone:

+ +
<iframe allow="camera 'none'; microphone 'none'">
+
+ +

Inheritance of policy for embedded content

+ +

Scripts inherit the policy of their browsing context, regardless of their origin. That means that top-level scripts inherit the policy from the main document.

+ +

All iframes inherit the policy of their parent page. If the iframe has an allow attribute, the policies of the parent page and the allow attribute are combined, using the most restrictive subset. For an iframe to have a feature enabled, the origin must be in the allowlist for both the parent page and the allow attribute.

+ +

Disabling a feature in a policy is a one-way toggle. If a feature has been disabled for a child frame by its parent frame, the child cannot re-enable it, and neither can any of the child's descendants.

+ +

Enforcing best practices for good user experiences

+ +

It's difficult to build a website that uses all the latest best practices and provides great performance and user experiences. As the website evolves, it can become even harder to maintain the user experience over time. You can use feature policies to specify the desired best practices, and rely on the browser to enforce the policies to prevent regressions.

+ +

There are several policy-controlled features designed to represent functionality that can negatively impact the user experience. These features include:

+ + + +

To avoid breaking existing web content, the default for such policy-controlled features is to allow the functionality to be used by all origins. That is, the default allowlist is '*' for each feature. Preventing the use of the sub-optimal functionality requires explicitly specifying a policy that disables the features.

+ +

For new content, you can start developing with a policy that disables all the features. This approach ensures that none of the functionality is introduced. When applying a policy to existing content, testing is likely required to verify it continues to work as expected. This is especially important for embedded or third-party content that you do not control.

+ +

To turn on the enforcement of all the best practices, specify the policy as below.

+ +

Send the following the HTTP header:

+ +
Feature-Policy: layout-animations 'none'; unoptimized-images 'none'; oversized-images 'none'; sync-script 'none'; sync-xhr 'none'; unsized-media 'none';
+ +

Using the <iframe> allow attribute:

+ +
<iframe src="https://example.com..." allow="layout-animations 'none'; unoptimized-images 'none'; oversized-images 'none'; sync-script 'none'; sync-xhr 'none'; unsized-media 'none';"></iframe>
+ +

See also

+ + -- cgit v1.2.3-54-g00ecf