Feature Policy Permission-Policy
Feature Policy Permission-Policy
Policy
We use lot of third party scripts / iFrame in our app. What if they try to access
geolocation, audio, video, mic etc without our permission. How can we trust them
? We need to ensure security. For that we use Permissions-policy
Restrict a site from using sensitive devices like the camera, microphone, or
speakers.
Stop items from being scripted if they are not visible in the viewport, to
improve performance.
Syntax
Permissions-Policy: <directive>=<allowlist>
Allowlists
An allowlist is a list of origins that takes one or more of the following values
contained in parentheses, separated by spaces:
: The feature will be allowed in this document, and all nested browsing
contexts ( <iframe> s) regardless of their origin.
: The feature will be allowed in this document, and in all nested browsing
self
contexts ( <iframe> s) in the same origin only. The feature is not allowed in
cross-origin documents in nested browsing contexts. self can be considered
shorthand for https://your-site.example.com . The equivalent
for <iframe> allow attributes is self .
src: The feature will be allowed in this <iframe> , as long as the document
loaded into it comes from the same origin as the URL in its src attribute. This
value is only used in the <iframe> allow attribute, and is
the default allowlist value in <iframe> s.
The values * and () may only be used on their own, while self and src may be
used in combination with one or more origins.
Directives
Example-
index js
<script>
function getGeolocation() {
// Check if the Geolocation API is supported by the
browser
if (navigator.geolocation) {
// Geolocation is supported
navigator.geolocation.getCurrentPosition(
function (position) {
// Success callback - position object contain
s the user's location
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
}
</script>
</body>
</html>