Class: Roda::RodaPlugins::ContentSecurityPolicy::Policy

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/plugins/content_security_policy.rb

Overview

Represents a content security policy.

Instance Method Summary collapse

Constructor Details

#initializePolicy

Returns a new instance of Policy.



188
189
190
# File 'lib/roda/plugins/content_security_policy.rb', line 188

def initialize
  clear
end

Instance Method Details

#clearObject

Clear all settings, useful to remove any inherited settings.



193
194
195
# File 'lib/roda/plugins/content_security_policy.rb', line 193

def clear
  @opts = {}
end

#freezeObject

Do not allow future modifications to any settings.



198
199
200
201
202
# File 'lib/roda/plugins/content_security_policy.rb', line 198

def freeze
  @opts.freeze
  header_value.freeze
  super
end

#header_keyObject

The header name to use, depends on whether report only mode has been enabled.



205
206
207
# File 'lib/roda/plugins/content_security_policy.rb', line 205

def header_key
  @report_only ? RodaResponseHeaders::CONTENT_SECURITY_POLICY_REPORT_ONLY : RodaResponseHeaders::CONTENT_SECURITY_POLICY
end

#header_valueObject

The header value to use.



210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/roda/plugins/content_security_policy.rb', line 210

def header_value
  return @header_value if @header_value

  s = String.new
  @opts.each do |k, vs|
    s << k
    unless vs == true
      vs.each{|v| append_formatted_value(s, v)}
    end
    s << '; '
  end
  @header_value = s
end

#report_only(report = true) ⇒ Object

Set whether the Content-Security-Policy-Report-Only header instead of the default Content-Security-Policy header.



226
227
228
# File 'lib/roda/plugins/content_security_policy.rb', line 226

def report_only(report=true)
  @report_only = report
end

#report_only?Boolean

Whether this policy uses report only mode.

Returns:

  • (Boolean)


231
232
233
# File 'lib/roda/plugins/content_security_policy.rb', line 231

def report_only?
  !!@report_only
end

#set_header(headers) ⇒ Object

Set the current policy in the headers hash. If no settings have been made in the policy, does not set a header.



237
238
239
240
# File 'lib/roda/plugins/content_security_policy.rb', line 237

def set_header(headers)
  return if @opts.empty?
  headers[header_key] ||= header_value
end