Class: CSPUtil::Directive

Inherits:
Object
  • Object
show all
Defined in:
lib/csp_util/directive.rb

Constant Summary collapse

VALID_NAMES =
%w[
  child-src connect-src default-src font-src frame-src img-src
  manifest-src media-src object-src script-src style-src worker-src
  base-uri plugin-types sandbox disown-opener form-action frame-ancestors
  report-uri report-to upgrade-insecure-requests block-all-mixed-content
  require-sri-for
].freeze
DEPRECATED_NAMES =
%w[reflected-xss referrer].freeze
FULLY_DEPRECATED_NAMES =
%w[policy-uri].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, value = nil) ⇒ Directive

Returns a new instance of Directive.



20
21
22
23
24
# File 'lib/csp_util/directive.rb', line 20

def initialize(name=nil, value=nil)
  self.name = name if name
  self.value = value if value
  return self
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/csp_util/directive.rb', line 18

def name
  @name
end

#valueObject

Returns the value of attribute value.



18
19
20
# File 'lib/csp_util/directive.rb', line 18

def value
  @value
end

Instance Method Details

#add_value(value) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/csp_util/directive.rb', line 48

def add_value(value)
  if value.is_a?(Array)
    @value = (@value << value).flatten.uniq 
  else
    @value = (@value << value.strip).uniq
  end
end

#delete_value(value) ⇒ Object



56
57
58
# File 'lib/csp_util/directive.rb', line 56

def delete_value(value)
  @value.delete(value)
end

#parse!(token) ⇒ Object



26
27
28
29
30
31
# File 'lib/csp_util/directive.rb', line 26

def parse!(token)
  name, value = token.split(' ', 2)
  self.name = name
  self.value = value
  return self
end

#same_name?(another_directive) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/csp_util/directive.rb', line 60

def same_name?(another_directive)
  @name.casecmp(another_directive.name).zero?
end

#to_hObject



64
65
66
# File 'lib/csp_util/directive.rb', line 64

def to_h
  { name: @name, value: @value }
end

#to_sObject



68
69
70
# File 'lib/csp_util/directive.rb', line 68

def to_s
  [@name, @value.join(' ')].reject(&:empty?).join(' ')
end