| |
| package vulstruct |
|
|
| import ( |
| "fmt" |
| "strings" |
|
|
| "github.com/Tencent/AI-Infra-Guard/common/fingerprints/parser" |
| "gopkg.in/yaml.v3" |
| ) |
|
|
| |
| |
| type Info struct { |
| FingerPrintName string `yaml:"name" json:"name"` |
| CVEName string `yaml:"cve" json:"cve"` |
| Summary string `yaml:"summary" json:"summary"` |
| Details string `yaml:"details" json:"details"` |
| CVSS string `yaml:"cvss" json:"cvss"` |
| Severity string `yaml:"severity" json:"severity"` |
| SecurityAdvise string `yaml:"security_advise,omitempty" json:"security_advise"` |
| References []string `yaml:"references" json:"references"` |
| Author string `yaml:"author,omitempty" json:"author,omitempty"` |
| } |
|
|
| |
| |
| type VersionVul struct { |
| Info Info `yaml:"info" json:"info"` |
| Rule string `yaml:"rule" json:"rule"` |
| RuleCompile *parser.Rule `yaml:"-" json:"-"` |
| References []string `yaml:"references" json:"references"` |
| } |
|
|
| |
| func (v *VersionVul) UnmarshalYAML(unmarshal func(interface{}) error) error { |
| |
| type tmpStruct struct { |
| Info Info `yaml:"info"` |
| Rule *string `yaml:"rule"` |
| References []string `yaml:"references"` |
| } |
|
|
| var tmp tmpStruct |
| if err := unmarshal(&tmp); err != nil { |
| return err |
| } |
|
|
| |
| if tmp.Rule == nil { |
| return fmt.Errorf("missing required field 'rule'") |
| } |
|
|
| |
| v.Info = tmp.Info |
| v.Rule = *tmp.Rule |
| v.References = tmp.References |
|
|
| return nil |
| } |
|
|
| |
| |
| func ReadVersionVul(body []byte) (*VersionVul, error) { |
| |
| |
| var advisory VersionVul |
| err := yaml.Unmarshal(body, &advisory) |
| if err != nil { |
| return nil, err |
| } |
| advisory.Info.Details = strings.TrimSpace(advisory.Info.Details) |
| advisory.Info.References = advisory.References |
|
|
| if advisory.Rule == "" { |
| advisory.RuleCompile = nil |
| return &advisory, nil |
| } |
|
|
| |
| |
| tokens, err := parser.ParseAdvisorTokens(advisory.Rule) |
| if err != nil { |
| return nil, err |
| } |
|
|
| |
| |
| err = parser.CheckBalance(tokens) |
| if err != nil { |
| return nil, err |
| } |
|
|
| |
| |
| dsl, err := parser.TransFormExp(tokens) |
| if err != nil { |
| return nil, err |
| } |
|
|
| advisory.RuleCompile = dsl |
| return &advisory, nil |
| } |
|
|