aboutsummaryrefslogtreecommitdiff
path: root/cisa/catalog_test.go
blob: b86d21cc88c7662dbb844d2f140c2e571dd76a7f (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
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
package cisa

import (
  "compress/gzip"
  "encoding/json"
  "github.com/pablotron/cvez/feed"
  "os"
  "reflect"
  "testing"
  "time"
)

// catalog test data
type catalogTestData struct {
  CveIds map[string]feed.CveId `json:"cves"`
  Dates  map[string]Date `json:"dates"`
  Times  map[string]time.Time `json:"times"`
}

func getCatalogTestData(path string) (catalogTestData, error) {
  var r catalogTestData

  // open file
  f, err := os.Open(path)
  if err != nil {
    return r, err
  }
  defer f.Close()

  // open reader
  gz, err := gzip.NewReader(f)
  if err != nil {
    return r, err
  }
  defer gz.Close()

  // create decoder
  d := json.NewDecoder(gz)

  // unmarshal json, return result
  return r, d.Decode(&r)
}

// read gzipped JSON CISA KEV catalog.
func readTestCatalog(path string, fn func(Catalog)) error {
  // open file
  f, err := os.Open(path)
  if err != nil {
    return err
  }
  defer f.Close()

  // create gzip reader
  r, err := gzip.NewReader(f)
  if err != nil {
    return err
  }
  defer r.Close()

  // create decoder
  d := json.NewDecoder(r)

  // unmarshal catalog
  var c Catalog
  if err = d.Decode(&c); err != nil {
    return err
  }

  // invoke callback, return result
  fn(c)

  // return success
  return nil
}


//   "title": "CISA Catalog of Known Exploited Vulnerabilities",
//   "catalogVersion": "2022.03.07",
//   "dateReleased": "2022-03-07T12:45:26.2626Z",
//   "count": 489,
//   "vulnerabilities": [
//     {
//       "cveID": "CVE-2021-27104",
//       "vendorProject": "Accellion",
//       "product": "FTA",
//       "vulnerabilityName": "Accellion FTA OS Command Injection Vulnerability",
//       "dateAdded": "2021-11-03",
//       "shortDescription": "Accellion FTA 9_12_370 and earlier is affected by OS command execution via a crafted POST request to various admin endpoints.",
//       "requiredAction": "Apply updates per vendor instructions.",
//       "dueDate": "2021-11-17"
//     },

func TestCatalogUnmarshal(t *testing.T) {
  data, err := getCatalogTestData("testdata/catalog-test-data.json.gz")
  if err != nil {
    t.Error(err)
    return
  }

  tests := []struct {
    path string
    exp  Catalog
  } {{
    path: "testdata/cisa-kevc-20220313-tiny.json.gz",
    exp: Catalog {
      Title: "CISA Catalog of Known Exploited Vulnerabilities",
      Version: "2022.03.07",
      DateReleased: data.Times["2022-03-07T12:45:26.2626Z"],
      Count: 489,
      Vulnerabilities: []Vulnerability {
        Vulnerability {
          CveId: data.CveIds["CVE-2021-27104"],
          VendorProject: "Accellion",
          Product: "FTA",
          Name: "Accellion FTA OS Command Injection Vulnerability",
          DateAdded: data.Dates["2021-11-03"],
          ShortDescription: "Accellion FTA 9_12_370 and earlier is affected by OS command execution via a crafted POST request to various admin endpoints.",
          RequiredAction: "Apply updates per vendor instructions.",
          DueDate: data.Dates["2021-11-17"],
        },

        Vulnerability {
          CveId: data.CveIds["CVE-2021-27102"],
          VendorProject: "Accellion",
          Product: "FTA",
          Name: "Accellion FTA OS Command Injection Vulnerability",
          DateAdded: data.Dates["2021-11-03"],
          ShortDescription: "Accellion FTA 9_12_411 and earlier is affected by OS command execution via a local web service call.",
          RequiredAction: "Apply updates per vendor instructions.",
          DueDate: data.Dates["2021-11-17"],
        },

        Vulnerability {
          CveId: data.CveIds["CVE-2013-0625"],
          VendorProject: "Adobe",
          Product: "ColdFusion",
          Name: "Adobe ColdFusion Authentication Bypass Vulnerability",
          DateAdded: data.Dates["2022-03-07"],
          ShortDescription: "Adobe Coldfusion contains an authentication bypass vulnerability, which could result in an unauthorized user gaining administrative access.",
          RequiredAction: "Apply updates per vendor instructions.",
          DueDate: data.Dates["2022-09-07"],
        },

        Vulnerability {
          CveId: data.CveIds["CVE-2009-3960"],
          VendorProject: "Adobe ",
          Product: "BlazeDS",
          Name: "Adobe BlazeDS Information Disclosure Vulnerability",
          DateAdded: data.Dates["2022-03-07"],
          ShortDescription: "Adobe BlazeDS, which is utilized in LifeCycle and Coldfusion, contains a vulnerability which allows for information disclosure.",
          RequiredAction: "Apply updates per vendor instructions.",
          DueDate: data.Dates["2022-09-07"],
        },
      },
    },
  }}

  for _, test := range(tests) {
    t.Run(test.path, func(t *testing.T) {
      err := readTestCatalog(test.path, func(got Catalog) {
        if !reflect.DeepEqual(got, test.exp) {
          t.Errorf("got %v, exp %v", got, test.exp)
        }
      })

      if err != nil {
        t.Error(err)
        return
      }
    })
  }
}