|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "active_support/core_ext/object/deep_dup" |
| 4 | + |
| 5 | +module ActionDispatch #:nodoc: |
| 6 | + class FeaturePolicy |
| 7 | + class Middleware |
| 8 | + CONTENT_TYPE = "Content-Type" |
| 9 | + POLICY = "Feature-Policy" |
| 10 | + |
| 11 | + def initialize(app) |
| 12 | + @app = app |
| 13 | + end |
| 14 | + |
| 15 | + def call(env) |
| 16 | + request = ActionDispatch::Request.new(env) |
| 17 | + _, headers, _ = response = @app.call(env) |
| 18 | + |
| 19 | + return response unless html_response?(headers) |
| 20 | + return response if policy_present?(headers) |
| 21 | + |
| 22 | + if policy = request.feature_policy |
| 23 | + headers[POLICY] = policy.build(request.controller_instance) |
| 24 | + end |
| 25 | + |
| 26 | + if policy_empty?(policy) |
| 27 | + headers.delete(POLICY) |
| 28 | + end |
| 29 | + |
| 30 | + response |
| 31 | + end |
| 32 | + |
| 33 | + private |
| 34 | + def html_response?(headers) |
| 35 | + if content_type = headers[CONTENT_TYPE] |
| 36 | + content_type =~ /html/ |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + def policy_present?(headers) |
| 41 | + headers[POLICY] |
| 42 | + end |
| 43 | + |
| 44 | + def policy_empty?(policy) |
| 45 | + policy.try(:directives) && policy.directives.empty? |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + module Request |
| 50 | + POLICY = "action_dispatch.feature_policy" |
| 51 | + |
| 52 | + def feature_policy |
| 53 | + get_header(POLICY) |
| 54 | + end |
| 55 | + |
| 56 | + def feature_policy=(policy) |
| 57 | + set_header(POLICY, policy) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + MAPPINGS = { |
| 62 | + self: "'self'", |
| 63 | + none: "'none'", |
| 64 | + }.freeze |
| 65 | + |
| 66 | + # List of available features can be found at |
| 67 | + # https://github.com/WICG/feature-policy/blob/master/features.md#policy-controlled-features |
| 68 | + DIRECTIVES = { |
| 69 | + accelerometer: "accelerometer", |
| 70 | + ambient_light_sensor: "ambient-light-sensor", |
| 71 | + autoplay: "autoplay", |
| 72 | + camera: "camera", |
| 73 | + encrypted_media: "encrypted-media", |
| 74 | + fullscreen: "fullscreen", |
| 75 | + geolocation: "geolocation", |
| 76 | + gyroscope: "gyroscope", |
| 77 | + magnetometer: "magnetometer", |
| 78 | + microphone: "microphone", |
| 79 | + midi: "midi", |
| 80 | + payment: "payment", |
| 81 | + picture_in_picture: "picture-in-picture", |
| 82 | + speaker: "speaker", |
| 83 | + usb: "usb", |
| 84 | + vibrate: "vibrate", |
| 85 | + vr: "vr", |
| 86 | + }.freeze |
| 87 | + |
| 88 | + private_constant :MAPPINGS, :DIRECTIVES |
| 89 | + |
| 90 | + attr_reader :directives |
| 91 | + |
| 92 | + def initialize |
| 93 | + @directives = {} |
| 94 | + yield self if block_given? |
| 95 | + end |
| 96 | + |
| 97 | + def initialize_copy(other) |
| 98 | + @directives = other.directives.deep_dup |
| 99 | + end |
| 100 | + |
| 101 | + DIRECTIVES.each do |name, directive| |
| 102 | + define_method(name) do |*sources| |
| 103 | + if sources.first |
| 104 | + @directives[directive] = apply_mappings(sources) |
| 105 | + else |
| 106 | + @directives.delete(directive) |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + def build(context = nil) |
| 112 | + build_directives(context).compact.join("; ") |
| 113 | + end |
| 114 | + |
| 115 | + private |
| 116 | + def apply_mappings(sources) |
| 117 | + sources.map do |source| |
| 118 | + case source |
| 119 | + when Symbol |
| 120 | + apply_mapping(source) |
| 121 | + when String, Proc |
| 122 | + source |
| 123 | + else |
| 124 | + raise ArgumentError, "Invalid HTTP feature policy source: #{source.inspect}" |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + def apply_mapping(source) |
| 130 | + MAPPINGS.fetch(source) do |
| 131 | + raise ArgumentError, "Unknown HTTP feature policy source mapping: #{source.inspect}" |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + def build_directives(context) |
| 136 | + @directives.map do |directive, sources| |
| 137 | + if sources.is_a?(Array) |
| 138 | + "#{directive} #{build_directive(sources, context).join(' ')}" |
| 139 | + elsif sources |
| 140 | + directive |
| 141 | + else |
| 142 | + nil |
| 143 | + end |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + def build_directive(sources, context) |
| 148 | + sources.map { |source| resolve_source(source, context) } |
| 149 | + end |
| 150 | + |
| 151 | + def resolve_source(source, context) |
| 152 | + case source |
| 153 | + when String |
| 154 | + source |
| 155 | + when Symbol |
| 156 | + source.to_s |
| 157 | + when Proc |
| 158 | + if context.nil? |
| 159 | + raise RuntimeError, "Missing context for the dynamic feature policy source: #{source.inspect}" |
| 160 | + else |
| 161 | + context.instance_exec(&source) |
| 162 | + end |
| 163 | + else |
| 164 | + raise RuntimeError, "Unexpected feature policy source: #{source.inspect}" |
| 165 | + end |
| 166 | + end |
| 167 | + end |
| 168 | +end |
0 commit comments