github.com/google/osv-scalibr@v0.4.1/detector/govulncheck/binary/message.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package binary
    16  
    17  import (
    18  	"encoding/json"
    19  
    20  	osvpb "github.com/ossf/osv-schema/bindings/go/osvschema"
    21  	"google.golang.org/protobuf/encoding/protojson"
    22  )
    23  
    24  // govulncheckMessage contains the relevant parts of the json output of govulncheck.
    25  type govulncheckMessage struct {
    26  	OSV     *osvpb.Vulnerability `json:"osv,omitempty"`
    27  	Finding *govulncheckFinding  `json:"finding,omitempty"`
    28  }
    29  
    30  // UnmarshalJSON unmarshals the govulncheck message. The OSV field is a proto
    31  // message, so it needs to be unmarshaled with protojson.
    32  func (m *govulncheckMessage) UnmarshalJSON(data []byte) error {
    33  	var raw map[string]json.RawMessage
    34  	if err := json.Unmarshal(data, &raw); err != nil {
    35  		return err
    36  	}
    37  	if osv, ok := raw["osv"]; ok {
    38  		m.OSV = &osvpb.Vulnerability{}
    39  		if err := protojson.Unmarshal(osv, m.OSV); err != nil {
    40  			return err
    41  		}
    42  	}
    43  	if finding, ok := raw["finding"]; ok {
    44  		if err := json.Unmarshal(finding, &m.Finding); err != nil {
    45  			return err
    46  		}
    47  	}
    48  	return nil
    49  }
    50  
    51  // govulncheckFinding is a trimmed down version of govulncheck finding.
    52  type govulncheckFinding struct {
    53  	OSV   string              `json:"osv,omitempty"`
    54  	Trace []*govulncheckFrame `json:"trace,omitempty"`
    55  }
    56  
    57  type govulncheckFrame struct {
    58  	// Function is the detected symbol.
    59  	Function string `json:"function,omitempty"`
    60  }