toolhq.io

All posts
7 min readby Jameel Haider

The Permissions-Policy header: turning off browser features your site never needs

Most sites never use the camera, the microphone, the visitor's location or a USB device. The browser does not know that. Unless you say otherwise, any script running on your page can ask for them, including a script you did not write.

Permissions-Policy is how you say otherwise. It is the successor to Feature-Policy, and it declares which powerful browser features a document and its iframes may use: camera, microphone, geolocation, payment, usb, midi, fullscreen, autoplay, display-capture, and similar.

What it does and does not do

The header does not grant anything. A page that is allowed to use the camera still has to ask, and the user still has to consent to the prompt.

What the header does is remove the feature entirely. With camera=() in place, a call to the camera API fails immediately and no prompt appears. A compromised script or an embedded third party cannot even ask.

That is the value. A permission prompt showing your domain name is a persuasive thing, and an injected script that triggers one is borrowing your reputation. If the feature is switched off, there is nothing to borrow. It complements a content security policy: CSP limits which scripts run, and this header limits what the ones that do run can reach.

The syntax

The value is a comma separated list of feature=(allowlist) items. The allowlist is a space separated set inside parentheses.

AllowlistMeaning
()Denied everywhere, including your own pages
(self)Allowed for the document's own origin
(self "https://embed.example.com")Own origin plus one named origin
*Allowed everywhere

A sensible default for a site that uses none of these features:

Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=(), usb=()

Two details trip people up.

Quoting. Origins go in double quotes. The keywords self and src are written bare, with no quotes at all. (self "https://maps.example.com") is correct. Quoting self, or leaving an origin unquoted, does not mean what you intended.

Old syntax. Feature-Policy used a different format, with semicolons between features and single quoted keywords such as 'none'. Pasting that into the new header does not work. If you are migrating, rewrite each directive instead of renaming the header.

Typos fail silently

An unrecognised feature name in the list is ignored. It does not fail the whole header.

This is deliberate, and it is what makes the header safe to extend. You can add a newer feature name and browsers that have never heard of it carry on with the rest of the policy.

The cost is that a typo silently does nothing. geolocaton=() produces no error and no protection, and a scanner that only checks whether the header is present will still pass it. Read the value itself after you set it, and check the browser console, where some browsers log a warning for names they do not recognise.

Feature names are also not interchangeable between browsers. Support varies, both for individual features and for the header as a whole, so a name that one browser enforces may be unknown to another. Treat the header as defence in depth and not as a guarantee. Your application still has to behave sensibly on a browser that ignored the policy.

How it works with the iframe allow attribute

This is the part people find confusing.

There are two controls. The header on your top level response, and the allow attribute on each iframe:

<iframe src="https://meet.example.com/room" allow="camera; microphone"></iframe>

They are not alternatives. An iframe can only be granted features the parent document still has. The header sets the ceiling and allow picks from underneath it.

In practice that produces three cases:

  • Header denies, iframe asks. With camera=() on the page, adding allow="camera" to an iframe does nothing. The parent has no camera access to hand down.
  • Header allows only self, iframe is another origin. With camera=(self), your own pages can use the camera but an embedded frame from another origin cannot, even with allow="camera". The frame's origin has to be in the header's allowlist too.
  • Header allows the origin, iframe has no allow. For most powerful features, a cross origin frame gets nothing by default. The header permits it, but nobody delegated it.

So an embedded video call needs both halves:

Permissions-Policy: camera=(self "https://meet.example.com"), microphone=(self "https://meet.example.com")

plus the allow attribute on the frame. Note that the two are written differently. The attribute separates features with semicolons and does not put origins in double quotes, while the header uses commas and does.

When an embedded widget stops working after you add the header, this is nearly always the cause. The widget's own code is fine. It was simply never handed the feature.

Rolling it out on an existing site

On a new site, set the strict default from the first deploy and loosen it when a feature needs it. On an existing site, do not guess.

There is a Permissions-Policy-Report-Only variant that takes the same value and blocks nothing. Combined with the Reporting API, it sends a report to an endpoint you nominate each time something on the page would have been stopped. That is the right way to try a policy before enforcing it. As with CSP reporting, confirm that reports actually arrive before you trust an empty inbox.

Then work in this order:

  1. Deny what you are certain you do not use. For most content sites that is camera, microphone, geolocation, payment, usb and midi. This step rarely breaks anything.
  2. Check embedded widgets. Maps want geolocation. Video players want fullscreen and autoplay. Payment frames want payment. These are the things that actually break, and they break on the one page that has the embed, not on the home page you tested.
  3. Tighten. Replace any * with (self) or a named origin, and extend the list as you confirm each feature is unused.

Send the header on every HTML response, including error pages, and set it in one layer only. The general rollout rules in the security headers checklist apply here as well, and the header pairs naturally with Referrer-Policy as one of the low risk ones to add early.

When it is live, run the page through the Security Headers Scanner to confirm the header is present on the response the browser really receives, then read the value character by character. Presence is easy to verify. A misspelt feature name is not, and it is the mistake this header is most forgiving about.