github.com/khulnasoft-lab/tunnel-db@v0.0.0-20231117205118-74e1113bd007/pkg/types/types.go (about) 1 package types 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "time" 7 ) 8 9 type Severity int 10 11 type VendorSeverity map[SourceID]Severity 12 13 type CVSS struct { 14 V2Vector string `json:"V2Vector,omitempty"` 15 V3Vector string `json:"V3Vector,omitempty"` 16 V2Score float64 `json:"V2Score,omitempty"` 17 V3Score float64 `json:"V3Score,omitempty"` 18 } 19 20 type CVSSVector struct { 21 V2 string `json:"v2,omitempty"` 22 V3 string `json:"v3,omitempty"` 23 } 24 25 type VendorCVSS map[SourceID]CVSS 26 27 const ( 28 SeverityUnknown Severity = iota 29 SeverityLow 30 SeverityMedium 31 SeverityHigh 32 SeverityCritical 33 ) 34 35 var ( 36 SeverityNames = []string{ 37 "UNKNOWN", 38 "LOW", 39 "MEDIUM", 40 "HIGH", 41 "CRITICAL", 42 } 43 ) 44 45 func NewSeverity(severity string) (Severity, error) { 46 for i, name := range SeverityNames { 47 if severity == name { 48 return Severity(i), nil 49 } 50 } 51 return SeverityUnknown, fmt.Errorf("unknown severity: %s", severity) 52 } 53 54 func CompareSeverityString(sev1, sev2 string) int { 55 s1, _ := NewSeverity(sev1) 56 s2, _ := NewSeverity(sev2) 57 return int(s2) - int(s1) 58 } 59 60 func (s Severity) String() string { 61 return SeverityNames[s] 62 } 63 64 type LastUpdated struct { 65 Date time.Time 66 } 67 type VulnerabilityDetail struct { 68 ID string `json:",omitempty"` // e.g. CVE-2019-8331, OSVDB-104365 69 CvssScore float64 `json:",omitempty"` 70 CvssVector string `json:",omitempty"` 71 CvssScoreV3 float64 `json:",omitempty"` 72 CvssVectorV3 string `json:",omitempty"` 73 Severity Severity `json:",omitempty"` 74 SeverityV3 Severity `json:",omitempty"` 75 CweIDs []string `json:",omitempty"` // e.g. CWE-78, CWE-89 76 References []string `json:",omitempty"` 77 Title string `json:",omitempty"` 78 Description string `json:",omitempty"` 79 PublishedDate *time.Time `json:",omitempty"` // Take from NVD 80 LastModifiedDate *time.Time `json:",omitempty"` // Take from NVD 81 } 82 83 type AdvisoryDetail struct { 84 PlatformName string 85 PackageName string 86 AdvisoryItem interface{} 87 } 88 89 // SourceID represents data source such as NVD. 90 type SourceID string 91 92 type DataSource struct { 93 ID SourceID `json:",omitempty"` 94 Name string `json:",omitempty"` 95 URL string `json:",omitempty"` 96 } 97 98 type Advisory struct { 99 VulnerabilityID string `json:",omitempty"` // CVE-ID or vendor ID 100 VendorIDs []string `json:",omitempty"` // e.g. RHSA-ID and DSA-ID 101 102 Arches []string `json:",omitempty"` 103 104 // It is filled only when FixedVersion is empty since it is obvious the state is "Fixed" when FixedVersion is not empty. 105 // e.g. Will not fix and Affected 106 Status Status `json:"-"` 107 108 // Tunnel DB has "vulnerability" bucket and severities are usually stored in the bucket per a vulnerability ID. 109 // In some cases, the advisory may have multiple severities depending on the packages. 110 // For example, CVE-2015-2328 in Debian has "unimportant" for mongodb and "low" for pcre3. 111 // e.g. https://security-tracker.debian.org/tracker/CVE-2015-2328 112 Severity Severity `json:",omitempty"` 113 114 // Versions for os package 115 FixedVersion string `json:",omitempty"` 116 AffectedVersion string `json:",omitempty"` // Only for Arch Linux 117 118 // MajorVersion ranges for language-specific package 119 // Some advisories provide VulnerableVersions only, others provide PatchedVersions and UnaffectedVersions 120 VulnerableVersions []string `json:",omitempty"` 121 PatchedVersions []string `json:",omitempty"` 122 UnaffectedVersions []string `json:",omitempty"` 123 124 // DataSource holds where the advisory comes from 125 DataSource *DataSource `json:",omitempty"` 126 127 // Custom is basically for extensibility and is not supposed to be used in OSS 128 Custom interface{} `json:",omitempty"` 129 } 130 131 // _Advisory is an internal struct for Advisory to avoid infinite MarshalJSON loop. 132 type _Advisory Advisory 133 134 type dbAdvisory struct { 135 _Advisory 136 IntStatus int `json:"Status,omitempty"` 137 } 138 139 // MarshalJSON customizes how an Advisory is marshaled to JSON. 140 // It is used when saving the Advisory to the BoltDB database. 141 // To reduce the size of the database, the Status field is converted to an integer before being saved, 142 // while the status is normally exported as a string in JSON. 143 // This is done by creating an anonymous struct that has all the same fields as Advisory, 144 // but with the Status field replaced by an IntStatus field of type int. 145 func (a *Advisory) MarshalJSON() ([]byte, error) { 146 advisory := dbAdvisory{ 147 _Advisory: _Advisory(*a), 148 IntStatus: int(a.Status), 149 } 150 return json.Marshal(advisory) 151 } 152 153 func (a *Advisory) UnmarshalJSON(data []byte) error { 154 var advisory dbAdvisory 155 if err := json.Unmarshal(data, &advisory); err != nil { 156 return err 157 } 158 advisory._Advisory.Status = Status(advisory.IntStatus) 159 *a = Advisory(advisory._Advisory) 160 return nil 161 } 162 163 // Advisories saves fixed versions for each arches/vendorIDs 164 // e.g. this is required when CVE has different fixed versions for different arches 165 type Advisories struct { 166 FixedVersion string `json:",omitempty"` // For backward compatibility 167 Entries []Advisory `json:",omitempty"` 168 // Custom is basically for extensibility and is not supposed to be used in OSS 169 Custom interface{} `json:",omitempty"` // For backward compatibility 170 } 171 172 type Vulnerability struct { 173 Title string `json:",omitempty"` 174 Description string `json:",omitempty"` 175 Severity string `json:",omitempty"` // Selected from VendorSeverity, depending on a scan target 176 CweIDs []string `json:",omitempty"` // e.g. CWE-78, CWE-89 177 VendorSeverity VendorSeverity `json:",omitempty"` 178 CVSS VendorCVSS `json:",omitempty"` 179 References []string `json:",omitempty"` 180 PublishedDate *time.Time `json:",omitempty"` // Take from NVD 181 LastModifiedDate *time.Time `json:",omitempty"` // Take from NVD 182 183 // Custom is basically for extensibility and is not supposed to be used in OSS 184 Custom interface{} `json:",omitempty"` 185 } 186 187 // Ecosystem represents language-specific ecosystem 188 type Ecosystem string