File: permissions_policy.rb

package info (click to toggle)
rails 2%3A6.1.7.10%2Bdfsg-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 39,756 kB
  • sloc: ruby: 290,662; javascript: 19,241; yacc: 46; sql: 43; makefile: 32; sh: 18
file content (46 lines) | stat: -rw-r--r-- 1,339 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

module ActionController #:nodoc:
  # HTTP Permissions Policy is a web standard for defining a mechanism to
  # allow and deny the use of browser permissions in its own context, and
  # in content within any <iframe> elements in the document.
  #
  # Full details of HTTP Permissions Policy specification and guidelines can
  # be found at MDN:
  #
  # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Feature-Policy
  #
  # Examples of usage:
  #
  #   # Global policy
  #   Rails.application.config.permissions_policy do |f|
  #     f.camera      :none
  #     f.gyroscope   :none
  #     f.microphone  :none
  #     f.usb         :none
  #     f.fullscreen  :self
  #     f.payment     :self, "https://secure.example.com"
  #   end
  #
  #   # Controller level policy
  #   class PagesController < ApplicationController
  #     permissions_policy do |p|
  #       p.geolocation "https://example.com"
  #     end
  #   end
  module PermissionsPolicy
    extend ActiveSupport::Concern

    module ClassMethods
      def permissions_policy(**options, &block)
        before_action(options) do
          if block_given?
            policy = request.permissions_policy.clone
            yield policy
            request.permissions_policy = policy
          end
        end
      end
    end
  end
end