---
title: Feature-Policy
slug: Web/HTTP/Headers/Feature-Policy
translation_of: Web/HTTP/Headers/Feature-Policy
---
{{HTTPSidebar}} {{SeeCompatTable}}
Feature-Policy
响应头提供了一种可以在本页面或包含的iframe上启用或禁止浏览器特性的机制。
更多的信息,请查看Feature Policy
Header type |
{{Glossary("Response header")}} |
{{Glossary("Forbidden header name")}} |
yes |
语法
Feature-Policy: <directive> <allowlist>
<allowlist>
*
: 允许在当前文档和所有包含的内容(比如iframes)中使用本特性。
'self'
: 允许在当前文档中使用本特性,但在包含的内容(比如iframes)仍使用原值。
'src'
: (只在iframe中允许) 只要在{{HTMLElement('iframe','src','#Attributes')}} 中的URL和加载iframe用的URL相同,则本特性在iframe中允许,
'none'
: 从最上层到包含的内容都禁止本特性。 <origin(s)>: 在特定的源中允许,源URL以空格分割。
*
: 本特性默认在最上层和包含的内容中(iframes)允许。
'self'
: 本特性默认在最上层允许,而包含的内容中(iframes)使用源地址相同设定。也就是说本特性在iframe中不允许跨域访问。
'none'
: 本特性默认在最上层和包含的内容中(iframes)都禁止。
*
(在所有源地址启用)或'none'
(在所有源地址禁用)只允许单独使用,而'self'
和'src'
可以与多个源地址一起使用。
所有的特性都有一个如下的默认的allowlist
*
: 本特性默认在最上层和包含的内容中(iframes)允许。
'self'
: 本特性默认在最上层允许,而包含的内容中(iframes)使用源地址相同设定。也就是说本特性在iframe中不允许跨域访问。
'none'
: 本特性默认在最上层和包含的内容中(iframes)都禁止。
指令
- {{httpheader('Feature-Policy/autoplay','autoplay')}}
- 控制是否允许当前文档自动播放媒体。这种控制是通过接口 {{domxref("HTMLMediaElement")}} 来实现。当这种规则被启用,而且没有用户操作的时候,{{domxref("HTMLMediaElement.play()")}}返回的 {{domxref("Promise")}}会拒绝并抛出一个
DOMException
异常。在{{HTMLELement("audio")}}和{{HTMLELement("video")}}上的autoplay属性会被忽略。
- {{httpheader('Feature-Policy/camera', 'camera')}}
- 控制是否允许当前文档使用视频输入设备。当这种规则被启用时,{{domxref("MediaDevices.getUserMedia()")}}返回的the {{jsxref("Promise")}}会拒绝并抛出错误
NotAllowedError。
- {{httpheader('Feature-Policy/document-domain','document-domain')}}
- 控制是否允许当前文档设置{{domxref("document.domain")}}。当这种规则被启用时,尝试设置{{domxref("document.domain")}}会失败并抛出
SecurityError
{{domxref("DOMException")}}异常。
- {{httpheader('Feature-Policy/encrypted-media', 'encrypted-media')}}
- 控制是否允许当前文档使用Encrypted Media Extensions API (EME)。当这种规则被启用时,{{domxref("Navigator.requestMediaKeySystemAccess()")}}返回的{{domxref("Promise")}}会拒绝并抛出
DOMException
异常。
- {{httpheader('Feature-Policy/fullscreen','fullscreen')}}
- 控制是否允许当前文档使用{{domxref('Element.requestFullScreen()')}}。当这种规则被启用时,返回的{{jsxref('Promise')}}会拒绝并抛出{{jsxref('TypeError')}}。
- {{httpheader('Feature-Policy/geolocation','geolocation')}}
- 控制是否允许当前文档使用{{domxref('Geolocation')}}接口。当这种规则被启用时,调用{{domxref('Geolocation.getCurrentPosition','getCurrentPosition()')}}和{{domxref('Geolocation.watchPosition','watchPosition()')}}会返回包含
PERMISSION_DENIED
的 {{domxref('PositionError')}}。
- {{httpheader('Feature-Policy/microphone','microphone')}}
- 控制是否允许当前文档使用音频输入设备。当这种规则被启用时,{{domxref("MediaDevices.getUserMedia()")}}返回的the {{jsxref("Promise")}}会拒绝并抛出错误
NotAllowedError。
- {{httpheader('Feature-Policy/midi', 'midi')}}
- 控制是否允许当前文档使用Web MIDI API。当这种规则被启用时,{{domxref("Navigator.requestMIDIAccess()")}} 返回的the {{jsxref("Promise")}}会拒绝并抛出错误
DOMException。
- {{httpheader('Feature-Policy/payment', 'payment')}}
- 控制是否允许当前文档使用Payment Request API。当这种规则被启用时,构造器{{domxref("PaymentRequest()")}} 会抛出错误
SecurityError。
- {{httpheader('Feature-Policy/vr', 'vr')}} /
xr
- 控制是否允许当前文档使用WebVR API。当这种规则被启用时,{{domxref("Navigator.getVRDisplays()")}} 返回的the {{jsxref("Promise")}}会拒绝并抛出错误
DOMException。
示例
SecureCorp Inc. 公司想要在应用中禁用震动和定位API,则可以在返回的response中传递以下定义feature policy的HTTP的头部信息:
Feature-Policy: vibrate 'none'; geolocation 'none'
通过使用'none'
关键词,不管原来如何设定,这些特性在所有浏览的上下文中都会被禁用。
规范
Specification |
Status |
Comment |
{{SpecName('Feature Policy','#feature-policy-http-header-field','Feature-Policy')}} |
{{Spec2('Feature Policy')}} |
Initial definition. |
浏览器兼容
{{Compat("http.headers.Feature-Policy")}}
参见