Class: CvssCli::Cvss

Inherits:
Object
  • Object
show all
Defined in:
lib/cvss_cli/cvss.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cvss_string = nil) ⇒ Cvss

Returns a new instance of Cvss.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cvss_cli/cvss.rb', line 8

def initialize(cvss_string = nil)
  @groups = Hash.new
  @metrics = Hash.new

  file = YAML.load_file(File.join(ENV['CVSS_CLI_PATH'], "cvss3_1.yaml"))
  file['metric_groups'].each do |name, metric_group|
    group = MetricGroup.new(name, metric_group)
    @groups[name] = group
    @metrics.merge!(group.metrics)
  end

  parse_cvss_string(cvss_string) unless cvss_string.nil?
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



6
7
8
# File 'lib/cvss_cli/cvss.rb', line 6

def groups
  @groups
end

#metricsObject (readonly)

Returns the value of attribute metrics.



6
7
8
# File 'lib/cvss_cli/cvss.rb', line 6

def metrics
  @metrics
end

Instance Method Details

#clearObject



28
29
30
31
32
# File 'lib/cvss_cli/cvss.rb', line 28

def clear
  @metrics.values.each do |metric|
    metric.clear_value
  end
end

#get_rating(score) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cvss_cli/cvss.rb', line 53

def get_rating(score)
  if score != roundup(score)
    raise RuntimeError.new "Called with invalid value. Please round up your score to the smallest number, specified to 1 decimal place, that is equal to or higher than its input"
  end

  if score < 0 or score > 10
    raise RuntimeError.new "Score must be betweeen 0 and 10."
  end

  if score == 0.0
    'None'
  elsif score >= 0.1 and score <= 3.9
    'Low'
  elsif score >= 4.0 and score <= 6.9
    'Medium'
  elsif score >= 7.0 and score <= 8.9
    'High'
  elsif score >= 9.0 and score <= 10
    'Critical'
  end
end

#get_scoreObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/cvss_cli/cvss.rb', line 42

def get_score
  score = calculate_base_score
  if @groups['temporal'].any_metric_set?
    score = calculate_temporal_score(score)
  end
  if @groups['environmental'].any_metric_set?
    score = calculate_environmental_score
  end
  score
end

#parse_cvss_string(cvss_string) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/cvss_cli/cvss.rb', line 83

def parse_cvss_string(cvss_string)
  s = cvss_string.delete_prefix('CVSS:3.1/')
  s.split('/').each do |pair|
    metric = pair.split(':')[0]
    value = pair.split(':')[1]
    set_metric(metric, value)
  end
end


34
35
36
37
38
39
40
# File 'lib/cvss_cli/cvss.rb', line 34

def print_status(full = false)
  @groups.values.each do |group|
    print_group(group, full)
  end
  print_score
  print_cvss
end

#set_metric(metric, value) ⇒ Object



22
23
24
25
26
# File 'lib/cvss_cli/cvss.rb', line 22

def set_metric(metric, value)
  if value != 'NIL'
    @metrics[metric.downcase].set_value(value)
  end
end

#to_sObject



75
76
77
78
79
80
81
# File 'lib/cvss_cli/cvss.rb', line 75

def to_s
  s = ""
  @groups.values.each do |group|
    s += group.get_cvss_string
  end
  "CVSS:3.1/" + s.delete_suffix('/')
end