Class: Otto::Security::CSP::Writer::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/otto/security/csp/writer.rb

Overview

Outcome of an apply call.

applied? is the single source of truth for “did a header get written”. policy is the emitted policy on success, or the pre-existing policy when a :backstop deferred to one. skip_reason is one of :disabled, :blank_nonce, :non_html, :existing_csp when skipped, else nil.

Constant Summary collapse

SKIP_REASONS =

Recognized skip reasons, in the order Otto::Security::CSP::Writer.apply evaluates them.

%i[disabled blank_nonce non_html existing_csp].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(applied:, mode:, policy: nil, skip_reason: nil) ⇒ Result

Returns a new instance of Result.



61
62
63
64
65
66
# File 'lib/otto/security/csp/writer.rb', line 61

def initialize(applied:, mode:, policy: nil, skip_reason: nil)
  @applied = applied
  @mode = mode
  @policy = policy
  @skip_reason = skip_reason
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



59
60
61
# File 'lib/otto/security/csp/writer.rb', line 59

def mode
  @mode
end

#policyObject (readonly)

Returns the value of attribute policy.



59
60
61
# File 'lib/otto/security/csp/writer.rb', line 59

def policy
  @policy
end

#skip_reasonObject (readonly)

Returns the value of attribute skip_reason.



59
60
61
# File 'lib/otto/security/csp/writer.rb', line 59

def skip_reason
  @skip_reason
end

Class Method Details

.applied(policy, mode:) ⇒ Object

Build an “applied” result for a written policy.



69
70
71
# File 'lib/otto/security/csp/writer.rb', line 69

def self.applied(policy, mode:)
  new(applied: true, mode: mode, policy: policy)
end

.skipped(reason, mode:, policy: nil) ⇒ Object

Build a “skipped” result. policy carries the pre-existing policy for the :existing_csp case (observability), nil otherwise.



75
76
77
# File 'lib/otto/security/csp/writer.rb', line 75

def self.skipped(reason, mode:, policy: nil)
  new(applied: false, mode: mode, skip_reason: reason, policy: policy)
end

Instance Method Details

#applied?Boolean

Returns true when a CSP header was written.

Returns:

  • (Boolean)

    true when a CSP header was written



80
81
82
# File 'lib/otto/security/csp/writer.rb', line 80

def applied?
  @applied
end

#skipped?Boolean

Returns true when no header was written.

Returns:

  • (Boolean)

    true when no header was written



85
86
87
# File 'lib/otto/security/csp/writer.rb', line 85

def skipped?
  !@applied
end