Class: Pedant::CheckScriptId

Inherits:
Check
  • Object
show all
Defined in:
lib/pedant/checks/script_id.rb

Instance Attribute Summary

Attributes inherited from Check

#result

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Check

all, depends, #fail, #fatal, friendly_name, inherited, #initialize, initialize!, list, #pass, provides, ready?, #report, run_checks_in_dependency_order, #skip, #warn

Constructor Details

This class inherits a constructor from Pedant::Check

Class Method Details

.requiresObject



29
30
31
# File 'lib/pedant/checks/script_id.rb', line 29

def self.requires
  super + [:main, :trees]
end

Instance Method Details

#runObject



33
34
35
36
37
38
39
40
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pedant/checks/script_id.rb', line 33

def run
  # This check only applies to plugins.
  return skip unless @kb[:main].extname == '.nasl'

  si_nodes = []
  tree = @kb[:trees][@kb[:main]]

  tree.all(:Call).each do |node|
    next unless node.name.ident.name == 'script_id'
    next unless node.name.indexes == []
    si_nodes << node
  end

  if si_nodes.length == 0
    report(:error, "Plugin does not call script_id() in the description.")
    return fail
  elsif si_nodes.length > 1
    report(:error, "Plugin specifies multiple script IDs:")
    si_nodes.each { |call| report(:error, call.context()) }
    return fail
  end

  si_node = si_nodes.first

  if si_node.args.empty?
    report(:error, "script_id() was called with no arguments:\n#{si_node.context()}")
    return fail
  end

  if si_node.args.length > 1
    report(:error, "script_id() was called with too many arguments:\n#{si_node.context()}")
    return fail
  end
    
  # Pull out argument
  type = si_node.args.first.type
  arg = si_node.args.first.expr

  if type != :anonymous
    report(
      :error,
      "script_id() was called using a named parameter.  It requires using one positional parameter.\n" +
      arg.context(si_node)
    )
    return fail
  end
   
  unless arg.is_a? Nasl::Integer
    report(
      :error,
      "script_id() was called with the wrong type of argument.\n" +
      "An integer literal between 10001 and 999999 inclusive is required:\n" +
      arg.context(si_node)
    )
    return fail
  end

  # Ensure that the script id is valid.
  if arg.value < 10001 or arg.value > 999999
    report(
      :error,
      "script_id() was called with an invalid argument.\n" +
      "An integer literal between 10001 and 999999 inclusive is required:\n" +
      arg.context(si_node)
    )
    return fail
  end

  # Ensure that the script id is valid.
  if arg.value >= 900001 and arg.value <= 999999
    report(
      :warn,
      "Uses a script id reserved for custom plugins / plugins in development.\n" +
      arg.context(si_node)
    )
    return warn
  end

  report(:info, "Plugin has script id #{arg.value}:\n#{arg.context(si_node)}")
  pass
end