aboutsummaryrefslogtreecommitdiff
path: root/internal/cvss/v31vector.go
blob: 6e2acb6a2fc9a7d4f66a4ff8ffa59e7cd9a49e41 (plain)
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
package cvss

import (
  "strings"
)

// CVSS v3.1 prefix
var v31Prefix = "CVSS:3.1/"

// CVSS 3.1 vector.
type v31Vector []v3Metric

// Convert vector to string
func (v v31Vector) String() string {
  // convert to slice of metrics
  metrics := []v3Metric(v)

  // build vector
  r := make([]string, len(metrics))
  for i, m := range(metrics) {
    r[i] = m.String()
  }

  // build and return string
  return v31Prefix + strings.Join(r, "/")
}

// Return CVSS version.
func (v31Vector) Version() Version {
  return V31
}

// Return metrics in this vector.
func (v v31Vector) Metrics() []Metric {
  // build slice of metrics
  r := make([]Metric, len(v))
  for i, m := range(v) {
    r[i] = m
  }

  // return result
  return r
}

// create CVSS 3.1 vector from string.
func newV31Vector(s string) (Vector, error) {
  // strip version prefix, split into metric strings
  strs := strings.Split(s[len(v31Prefix):], "/")
  r := make([]v3Metric, len(strs))

  // build results
  for i, ms := range(strs) {
    // get metric from string
    m, err := getV3Metric(V31, ms)
    if err != nil {
      return nil, err
    }

    // add to results
    r[i] = m
  }

  // return result
  return v31Vector(r), nil
}

// Is the given string a CVSSv3.1 vector string?
func isV31VectorString(s string) bool {
  return (len(s) > len(v31Prefix)) && (s[:len(v31Prefix)] == v31Prefix);
}