Class: Otto::Security::CSP::Report

Inherits:
Struct
  • Object
show all
Defined in:
lib/otto/security/csp/report.rb,
lib/otto/security/csp/report.rb

Overview

Normalization behavior for Report. Reopened (rather than defined inside the Struct.new block) so the constants below are plain class constants, not constants-defined-in-a-block.

Constant Summary collapse

FIELD_ALIASES =

Map from a normalized field to the list of raw keys (in priority order) that may hold its value across both wire formats. Legacy kebab-case keys are listed alongside their Reporting API camelCase equivalents.

{
         document_uri:        %w[document-uri documentURL],
             referrer:        %w[referrer referer],
          blocked_uri:        %w[blocked-uri blockedURL],
   violated_directive:        %w[violated-directive violatedDirective],
  effective_directive:        %w[effective-directive effectiveDirective],
      original_policy:        %w[original-policy originalPolicy],
          disposition:        %w[disposition],
          source_file:        %w[source-file sourceFile],
          status_code:        %w[status-code statusCode],
        script_sample:        %w[script-sample sample],
          line_number:        %w[line-number lineNumber],
        column_number:        %w[column-number columnNumber],
}.freeze
NUMERIC_FIELDS =

Fields coerced to an Integer (or nil) rather than kept as whatever scalar the browser sent.

%i[status_code line_number column_number].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#blocked_uriObject

Returns the value of attribute blocked_uri

Returns:

  • (Object)

    the current value of blocked_uri



40
41
42
# File 'lib/otto/security/csp/report.rb', line 40

def blocked_uri
  @blocked_uri
end

#column_numberObject

Returns the value of attribute column_number

Returns:

  • (Object)

    the current value of column_number



40
41
42
# File 'lib/otto/security/csp/report.rb', line 40

def column_number
  @column_number
end

#dispositionObject

Returns the value of attribute disposition

Returns:

  • (Object)

    the current value of disposition



40
41
42
# File 'lib/otto/security/csp/report.rb', line 40

def disposition
  @disposition
end

#document_uriObject

Returns the value of attribute document_uri

Returns:

  • (Object)

    the current value of document_uri



40
41
42
# File 'lib/otto/security/csp/report.rb', line 40

def document_uri
  @document_uri
end

#effective_directiveObject

Returns the value of attribute effective_directive

Returns:

  • (Object)

    the current value of effective_directive



40
41
42
# File 'lib/otto/security/csp/report.rb', line 40

def effective_directive
  @effective_directive
end

#line_numberObject

Returns the value of attribute line_number

Returns:

  • (Object)

    the current value of line_number



40
41
42
# File 'lib/otto/security/csp/report.rb', line 40

def line_number
  @line_number
end

#original_policyObject

Returns the value of attribute original_policy

Returns:

  • (Object)

    the current value of original_policy



40
41
42
# File 'lib/otto/security/csp/report.rb', line 40

def original_policy
  @original_policy
end

#referrerObject

Returns the value of attribute referrer

Returns:

  • (Object)

    the current value of referrer



40
41
42
# File 'lib/otto/security/csp/report.rb', line 40

def referrer
  @referrer
end

#script_sampleObject

Returns the value of attribute script_sample

Returns:

  • (Object)

    the current value of script_sample



40
41
42
# File 'lib/otto/security/csp/report.rb', line 40

def script_sample
  @script_sample
end

#source_fileObject

Returns the value of attribute source_file

Returns:

  • (Object)

    the current value of source_file



40
41
42
# File 'lib/otto/security/csp/report.rb', line 40

def source_file
  @source_file
end

#status_codeObject

Returns the value of attribute status_code

Returns:

  • (Object)

    the current value of status_code



40
41
42
# File 'lib/otto/security/csp/report.rb', line 40

def status_code
  @status_code
end

#violated_directiveObject

Returns the value of attribute violated_directive

Returns:

  • (Object)

    the current value of violated_directive



40
41
42
# File 'lib/otto/security/csp/report.rb', line 40

def violated_directive
  @violated_directive
end

Class Method Details

.from_raw(raw) ⇒ Report?

Build a normalized Report from a single raw per-violation field hash.

Accepts either wire format’s field naming. violated_directive and effective_directive are cross-filled from each other when only one is present, because the two formats disagree on which they send (legacy favors violated-directive; the Reporting API favors effectiveDirective).

Parameters:

  • raw (Hash)

    a single violation’s field hash (already unwrapped from any csp-report/body envelope by the parser).

Returns:

  • (Report, nil)

    the normalized report, or nil when raw is not a usable Hash.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/otto/security/csp/report.rb', line 94

def self.from_raw(raw)
  return nil unless raw.is_a?(Hash)

  attrs = FIELD_ALIASES.each_with_object({}) do |(field, keys), acc|
    value      = first_present(raw, keys)
    acc[field] = NUMERIC_FIELDS.include?(field) ? coerce_int(value) : value
  end

  cross_fill_directives!(attrs)
  new(**attrs)
end