Class: OpenVAS::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/openvas/result.rb

Overview

This class represents each of the /report/report/results/result elements in the OpenVAS 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.

Direct Known Subclasses

V6::Result, V7::Result

Instance Method Summary collapse

Constructor Details

#initialize(xml_node) ⇒ Result

Accepts an XML node from Nokogiri::XML.



13
14
15
# File 'lib/openvas/result.rb', line 13

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.



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/openvas/result.rb', line 49

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

  # first we try the attributes: port, svc_name, protocol, severity,
  #   plugin_id, plugin_name, plugin_family
  # translations_table = {
  #   # @port           = xml.attributes["port"]
  #   # @svc_name       = xml.attributes["svc_name"]
  #   # @protocol       = xml.attributes["protocol"]
  #   # @severity       = xml.attributes["severity"]
  #   :plugin_id => 'pluginID',
  #   :plugin_name => 'pluginName',
  #   :plugin_family  => 'pluginFamily'
  # }
  # method_name = translations_table.fetch(method, method.to_s)
  # return @xml.attributes[method_name].value if @xml.attributes.key?(method_name)
  method_name = method.to_s


  # first we try the children tags: :threat, :description, :original_threat,
  # :notes, :overrides
  tag = @xml.at_xpath("./#{method_name}")
  if tag
    return tag.text
  end


  # nested tags: :name, :cvss_base, :risk_factor, :cve, :bid, :xref,
  tag = @xml.at_xpath("./nvt/#{method_name}")
  if tag
    return tag.text
  end

  # fields inside :description
  if description_fields.key?(method)
    return description_fields[method]
  end

  # nothing found, the tag is valid but not present in this ReportItem
  return nil
end

Instance Method Details

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

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

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/openvas/result.rb', line 38

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. <bid/>, <cve/>, <xref/>)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/openvas/result.rb', line 19

def supported_tags
  [
    # attributes
    # NONE

    # simple tags
    :port, :threat, :description, :original_threat, :notes, :overrides,

    # nested tags
    :name, :cvss_base, :risk_factor, :cve, :bid, :xref,

    # fields inside :tags
    :summary, :info_gathered, :cvss_base_vector, :insight, :impact,
    :impact_level, :affected_software, :solution, :solution_type, :vuldetect
  ]
end