Class: Pedant::CheckScriptCategory

Inherits:
Check
  • Object
show all
Defined in:
lib/pedant/checks/script_category.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_category.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
# File 'lib/pedant/checks/script_category.rb', line 33

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

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

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

  if sc_nodes.length == 0
    report(:error, "Plugin does not specify a script_category.")
    return fail
  elsif sc_nodes.length > 1
    report(:error, "Plugin specifies multiple script categories:")
    sc_nodes.each { |call| report(:error, call.context()) }
    return fail
  end

  sc_node = sc_nodes.first

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

  if sc_node.args.length > 1
    report(:error, "script_category() was called with too many arguments:\n#{sc_node.context()}")
    return fail
  end
    
  # Pull out argument
  arg = sc_node.args.first.expr
   
  unless sc_node.args.first.expr.is_a? Nasl::Lvalue
    report(
      :error,
      "script_category() was called with the wrong type of argument.\n" +
      "A variable (not a string literal) starting with ACT_ must be provided:\n" +
      arg.context(sc_node)
    )
    return fail
  end

  # Ensure that the script category is valid.
  unless [
    "ACT_INIT",
    "ACT_SCANNER",
    "ACT_SETTINGS",
    "ACT_GATHER_INFO",
    "ACT_ATTACK",
    "ACT_MIXED",
    "ACT_MIXED_ATTACK",
    "ACT_DESTRUCTIVE_ATTACK",
    "ACT_COMPLIANCE_CHECK",
    "ACT_PATCH_SETUP",
    "ACT_PATCH_APPLY",
    "ACT_PATCH_POST_APPLY",
    "ACT_THIRD_PARTY_INFO",
    "ACT_DENIAL",
    "ACT_KILL_HOST",
    "ACT_FLOOD",
    "ACT_END"
  ].include? arg.ident.name
    report(
      :error,
      "Plugin belongs to unknown category #{arg.ident.name}:\n" +
      arg.context(sc_node)
    )
    return fail
  end

  report(:info, "Plugin belongs to script category #{arg.ident.name}:\n#{arg.context(sc_node)}")
  pass
end