-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
breaking-change(fetch): change from CSV to JSON (#9)
* feat(fetch): change from CSV to JSON * feat(fetch): parse time when fetching * chore: remove unwanted diff in PR Co-authored-by: Kota Kanbe <kotakanbe@gmail.com>
- Loading branch information
Showing
2 changed files
with
35 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,28 @@ | ||
package fetcher | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
|
||
"github.com/gocarina/gocsv" | ||
"github.com/inconshreveable/log15" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/vulsio/go-kev/models" | ||
"github.com/vulsio/go-kev/utils" | ||
) | ||
|
||
var utf8Bom = []byte{239, 187, 191} | ||
|
||
func hasBOM(in []byte) bool { | ||
return bytes.HasPrefix(in, utf8Bom) | ||
} | ||
|
||
func stripBOM(in []byte) []byte { | ||
return bytes.TrimPrefix(in, utf8Bom) | ||
} | ||
|
||
var zeroWidthSpace = []byte{226, 128, 139} | ||
|
||
func hasZeroWidthSpace(in []byte) bool { | ||
return bytes.HasPrefix(in, zeroWidthSpace) || bytes.HasSuffix(in, zeroWidthSpace) | ||
} | ||
|
||
func stripZeroWidthSpace(in []byte) []byte { | ||
return bytes.ReplaceAll(in, zeroWidthSpace, []byte{}) | ||
} | ||
|
||
// FetchKEVuln : | ||
func FetchKEVuln() ([]models.KEVuln, error) { | ||
url := "https://www.cisa.gov/sites/default/files/csv/known_exploited_vulnerabilities.csv" | ||
url := "https://www.cisa.gov/sites/default/files/feeds/known_exploited_vulnerabilities.json" | ||
log15.Info("Fetching", "URL", url) | ||
vulnCsv, err := utils.FetchURL(url) | ||
vulnJSON, err := utils.FetchURL(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if hasBOM(vulnCsv) { | ||
vulnCsv = stripBOM(vulnCsv) | ||
} | ||
|
||
var vulns []models.KEVuln | ||
if err := gocsv.UnmarshalBytes(vulnCsv, &vulns); err != nil { | ||
return nil, err | ||
} | ||
|
||
for i := range vulns { | ||
if hasZeroWidthSpace([]byte(vulns[i].CveID)) { | ||
vulns[i].CveID = string(stripZeroWidthSpace([]byte(vulns[i].CveID))) | ||
} | ||
|
||
if vulns[i].CveID == "" { | ||
return nil, xerrors.New("Failed to fetch vulnerability info. err: CVE-ID is empty. CSV format may have been changed.") | ||
} | ||
kevCatalog := models.KEVCatalog{} | ||
if err := json.Unmarshal(vulnJSON, &kevCatalog); err != nil { | ||
return nil, xerrors.Errorf("failed to decode CISA Known Exploited Vulnerabilities JSON: %w", err) | ||
} | ||
|
||
return vulns, nil | ||
return kevCatalog.Vulnerabilities, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters