Class: ActionDispatch::ContentSecurityPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/action_dispatch/http/content_security_policy.rb

Defined Under Namespace

Modules: Request Classes: Middleware

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ ContentSecurityPolicy

Returns a new instance of ContentSecurityPolicy.

Yields:

  • (_self)

Yield Parameters:



152
153
154
155
# File 'lib/action_dispatch/http/content_security_policy.rb', line 152

def initialize
  @directives = {}
  yield self if block_given?
end

Instance Attribute Details

#directivesObject (readonly)

Returns the value of attribute directives.



150
151
152
# File 'lib/action_dispatch/http/content_security_policy.rb', line 150

def directives
  @directives
end

Instance Method Details

#block_all_mixed_content(enabled = true) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/action_dispatch/http/content_security_policy.rb', line 171

def block_all_mixed_content(enabled = true)
  if enabled
    @directives["block-all-mixed-content"] = true
  else
    @directives.delete("block-all-mixed-content")
  end
end

#build(context = nil, nonce = nil, nonce_directives = nil) ⇒ Object



217
218
219
220
# File 'lib/action_dispatch/http/content_security_policy.rb', line 217

def build(context = nil, nonce = nil, nonce_directives = nil)
  nonce_directives = DEFAULT_NONCE_DIRECTIVES if nonce_directives.nil?
  build_directives(context, nonce, nonce_directives).compact.join("; ")
end

#initialize_copy(other) ⇒ Object



157
158
159
# File 'lib/action_dispatch/http/content_security_policy.rb', line 157

def initialize_copy(other)
  @directives = other.directives.deep_dup
end

#plugin_types(*types) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/action_dispatch/http/content_security_policy.rb', line 179

def plugin_types(*types)
  if types.first
    @directives["plugin-types"] = types
  else
    @directives.delete("plugin-types")
  end
end

#report_uri(uri) ⇒ Object



187
188
189
# File 'lib/action_dispatch/http/content_security_policy.rb', line 187

def report_uri(uri)
  @directives["report-uri"] = [uri]
end

#require_sri_for(*types) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/action_dispatch/http/content_security_policy.rb', line 191

def require_sri_for(*types)
  if types.first
    @directives["require-sri-for"] = types
  else
    @directives.delete("require-sri-for")
  end
end

#sandbox(*values) ⇒ Object



199
200
201
202
203
204
205
206
207
# File 'lib/action_dispatch/http/content_security_policy.rb', line 199

def sandbox(*values)
  if values.empty?
    @directives["sandbox"] = true
  elsif values.first
    @directives["sandbox"] = values
  else
    @directives.delete("sandbox")
  end
end

#upgrade_insecure_requests(enabled = true) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/action_dispatch/http/content_security_policy.rb', line 209

def upgrade_insecure_requests(enabled = true)
  if enabled
    @directives["upgrade-insecure-requests"] = true
  else
    @directives.delete("upgrade-insecure-requests")
  end
end