Class: Qualys::WAS::Vulnerability

Inherits:
Object
  • Object
show all
Defined in:
lib/qualys/was/vulnerability.rb

Overview

This class represents each of the WAS_SCAN_REPORT/RESULTS/VULNERABILITY_LIST/ VULNERABILITY elements in the Qualys WAS XML document.

It provides a convenient way to access the information scattered all over the XML in attributes and nested tags.

Instead of providing separate methods for each supported property we rely on Ruby’s #method_missing to do most of the work.

Instance Method Summary collapse

Constructor Details

#initialize(xml_node) ⇒ Vulnerability

Accepts an XML node from Nokogiri::XML.



12
13
14
# File 'lib/qualys/was/vulnerability.rb', line 12

def initialize(xml_node)
  @xml = xml_node
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

This method is invoked by Ruby when a method that is not defined in this instance is called.

In our case we inspect the @method@ parameter and try to find the attribute, simple descendent or collection that it maps to in the XML tree.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/qualys/was/vulnerability.rb', line 41

def method_missing(method, *args)
  # We could remove this check and return nil for any non-recognized tag.
  # The problem would be that it would make tricky to debug problems with
  # typos. For instance: <>.potr would return nil instead of raising an
  # exception
  unless supported_tags.include?(method)
    super
    return
  end

  # Any fields where a simple .camelcase() won't work we need to translate,
  # this includes acronyms (e.g. :cwe would become 'Cwe') and simple nested
  # tags.
  translations_table = {
    access_paths:       'ACCESS_PATH/URL',
    request_headers:    'PAYLOADS/PAYLOAD/REQUEST/HEADERS',
    request_method:     'PAYLOADS/PAYLOAD/REQUEST/METHOD',
    request_url:        'PAYLOADS/PAYLOAD/REQUEST/URL',
    response_contents:  'PAYLOADS/PAYLOAD/RESPONSE/CONTENTS',
    response_evidence:  'PAYLOADS/PAYLOAD/RESPONSE/EVIDENCE'
  }

  method_name = translations_table.fetch(method, method.to_s.upcase)

  # Then we try simple children tags: TITLE, LAST_UPDATE, CVSS_BASE...
  tag = @xml.at_xpath("./#{method_name}")
  if tag && !tag.text.blank?
    if tags_with_base64.include?(method)
      return decode_base64(tag)
    else
      return tag.text
    end
  else
    'n/a'
  end

end

Instance Method Details

#respond_to?(method, include_private = false) ⇒ Boolean

This allows external callers (and specs) to check for implemented properties

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/qualys/was/vulnerability.rb', line 30

def respond_to?(method, include_private=false)
  return true if supported_tags.include?(method.to_sym)
  super
end

#supported_tagsObject

List of supported tags. They can be attributes, simple descendans or collections (e.g. <references/>, <tags/>)



18
19
20
21
22
23
24
25
26
# File 'lib/qualys/was/vulnerability.rb', line 18

def supported_tags
  [
    # simple tags
    :access_paths, :ajax, :authentication, :ignored, :potential, :url,

    # nested tags
    :response_contents, :response_evidence, :request_headers, :request_method, :request_url
  ]
end