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.



185
186
187
# File 'lib/roda/plugins/content_security_policy.rb', line 185

def initialize
  clear
end

Instance Method Details

#clearObject

Clear all settings, useful to remove any inherited settings.



190
191
192
# File 'lib/roda/plugins/content_security_policy.rb', line 190

def clear
  @opts = {}
end

#freezeObject

Do not allow future modifications to any settings.



195
196
197
198
199
# File 'lib/roda/plugins/content_security_policy.rb', line 195

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.



202
203
204
# File 'lib/roda/plugins/content_security_policy.rb', line 202

def header_key
  @report_only ? 'Content-Security-Policy-Report-Only' : 'Content-Security-Policy'
end

#header_valueObject

The header value to use.



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/roda/plugins/content_security_policy.rb', line 207

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.



223
224
225
# File 'lib/roda/plugins/content_security_policy.rb', line 223

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

#report_only?Boolean

Whether this policy uses report only mode.

Returns:

  • (Boolean)


228
229
230
# File 'lib/roda/plugins/content_security_policy.rb', line 228

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.



234
235
236
237
# File 'lib/roda/plugins/content_security_policy.rb', line 234

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