forked from facebookincubator/nvdtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.go
174 lines (153 loc) · 5.29 KB
/
vector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) Facebook, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cvss2
import (
"fmt"
"strings"
)
const (
partSeparator = "/"
metricSeparator = ":"
prefix = "("
suffix = ")"
)
// Vector represents a CVSSv3 vector, holds all metrics inside (base, temporal and environmental)
type Vector struct {
BaseMetrics
TemporalMetrics
EnvironmentalMetrics
}
// String returns this vectors representation as a string
// it shouldn't depend on the order of metrics
func (v Vector) String() string {
var sb strings.Builder
fmt.Fprint(&sb, prefix)
defineables := v.definables()
first := true
for _, metric := range order {
def := defineables[metric]
if !def.defined() {
continue
}
if !first {
fmt.Fprint(&sb, partSeparator)
} else {
first = false
}
fmt.Fprintf(&sb, "%s%s%s", metric, metricSeparator, def)
}
fmt.Fprint(&sb, suffix)
return sb.String()
}
// VectorFromString will parse a string into a Vector, or return an error if it can't be parsed
func VectorFromString(str string) (Vector, error) {
var v Vector
str = strings.TrimPrefix(str, prefix)
str = strings.TrimSuffix(str, suffix)
parseables := v.parseables()
for _, part := range strings.Split(str, partSeparator) {
tmp := strings.Split(part, metricSeparator)
if len(tmp) != 2 {
return v, fmt.Errorf("need two values separated by %s, got %q", metricSeparator, part)
}
metric, value := tmp[0], tmp[1]
if p, ok := parseables[metric]; !ok {
return v, fmt.Errorf("undefined metric %s with value %s", metric, value)
} else if err := p.parse(value); err != nil {
return v, fmt.Errorf("error occurred while parsing metric %s: %v", metric, err)
}
}
return v, nil
}
// Absorb will override only metrics in the current vector from the one given which are defined
// If the other vector specifies only a single metric with all others undefined, the resulting
// vector will contain all metrics it previously did, with only the new one overriden
func (v *Vector) Absorb(other Vector) {
parseables := v.parseables()
for metric, defineable := range other.definables() {
if defineable.defined() {
parseables[metric].parse(defineable.String())
}
}
}
// AbsorbIfDefined is like Absorb but will not override vector components that
// are not present in v.
func (v *Vector) AbsorbIfDefined(other Vector) {
parseables := v.parseables()
old := v.definables()
for metric, defineable := range other.definables() {
if old[metric].defined() && defineable.defined() {
parseables[metric].parse(defineable.String())
}
}
}
// helpers
var order = []string{"AV", "AC", "Au", "C", "I", "A", "E", "RL", "RC", "CDP", "TD", "CR", "IR", "AR", "ME", "MRL", "MRC"}
type defineable interface {
defined() bool
String() string
}
type parseable interface {
parse(string) error
}
func (v *Vector) definables() map[string]defineable {
return map[string]defineable{
// base metrics
"AV": v.BaseMetrics.AccessVector,
"AC": v.BaseMetrics.AccessComplexity,
"Au": v.BaseMetrics.Authentication,
"C": v.BaseMetrics.ConfidentialityImpact,
"I": v.BaseMetrics.IntegrityImpact,
"A": v.BaseMetrics.AvailabilityImpact,
// temporal metrics
"E": v.TemporalMetrics.Exploitablity,
"RL": v.TemporalMetrics.RemediationLevel,
"RC": v.TemporalMetrics.ReportConfidence,
// environmental metrics
"CDP": v.EnvironmentalMetrics.CollateralDamagePotential,
"TD": v.EnvironmentalMetrics.TargetDistribution,
"CR": v.EnvironmentalMetrics.ConfidentialityRequirement,
"IR": v.EnvironmentalMetrics.IntegrityRequirement,
"AR": v.EnvironmentalMetrics.AvailabilityRequirement,
// extended environmental metrics
"ME": v.EnvironmentalMetrics.ModifiedExploitablity,
"MRL": v.EnvironmentalMetrics.ModifiedRemediationLevel,
"MRC": v.EnvironmentalMetrics.ModifiedReportConfidence,
}
}
func (v *Vector) parseables() map[string]parseable {
return map[string]parseable{
// base metrics
"AV": &v.BaseMetrics.AccessVector,
"AC": &v.BaseMetrics.AccessComplexity,
"Au": &v.BaseMetrics.Authentication,
"C": &v.BaseMetrics.ConfidentialityImpact,
"I": &v.BaseMetrics.IntegrityImpact,
"A": &v.BaseMetrics.AvailabilityImpact,
// temporal metrics
"E": &v.TemporalMetrics.Exploitablity,
"RL": &v.TemporalMetrics.RemediationLevel,
"RC": &v.TemporalMetrics.ReportConfidence,
// environmental metrics
"CDP": &v.EnvironmentalMetrics.CollateralDamagePotential,
"TD": &v.EnvironmentalMetrics.TargetDistribution,
"CR": &v.EnvironmentalMetrics.ConfidentialityRequirement,
"IR": &v.EnvironmentalMetrics.IntegrityRequirement,
"AR": &v.EnvironmentalMetrics.AvailabilityRequirement,
// extended environmental metrics
"ME": &v.EnvironmentalMetrics.ModifiedExploitablity,
"MRL": &v.EnvironmentalMetrics.ModifiedRemediationLevel,
"MRC": &v.EnvironmentalMetrics.ModifiedReportConfidence,
}
}