Skip to content

Commit

Permalink
Integrate OWASP Dependency Check
Browse files Browse the repository at this point in the history
  • Loading branch information
kotakanbe committed Oct 24, 2016
1 parent 6f012fc commit 3e85b74
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
18 changes: 18 additions & 0 deletions contrib/owasp-dependency-check/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"

"github.com/future-architect/vuls/contrib/owasp-dependency-check/parser"
)

func main() {
path := "/Users/kotakanbe/Desktop/dependency-check-report.xml"
cpes, err := parser.Parse(path)
if err != nil {
fmt.Println(err)
}
for _, c := range cpes {
fmt.Println(c)
}
}
68 changes: 68 additions & 0 deletions contrib/owasp-dependency-check/parser/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package parser

import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
)

type analysis struct {
Dependencies []dependency `xml:"dependencies>dependency"`
}

type dependency struct {
Identifiers []identifier `xml:"identifiers>identifier"`
}

type identifier struct {
Name string `xml:"name"`
Type string `xml:"type,attr"`
}

func appendIfMissing(slice []string, str string) []string {
for _, s := range slice {
if s == str {
return slice
}
}
return append(slice, str)
}

// Parse parses XML and collect list of cpe
func Parse(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return []string{}, fmt.Errorf("Failed to open: %s", err)
}
defer file.Close()

b, err := ioutil.ReadAll(file)
if err != nil {
return []string{}, fmt.Errorf("Failed to read: %s", err)
}

var anal analysis
if err := xml.Unmarshal(b, &anal); err != nil {
fmt.Errorf("Failed to unmarshal: %s", err)
}

cpes := []string{}
for _, d := range anal.Dependencies {
for _, ident := range d.Identifiers {
if ident.Type == "cpe" {
name := strings.TrimPrefix(ident.Name, "(")
name = strings.TrimSuffix(name, ")")
cpes = appendIfMissing(cpes, name)
}
}
}
sort.Strings(cpes)
return cpes, nil
}

func getCPEs() []string {
return []string{"hoge"}
}

0 comments on commit 3e85b74

Please sign in to comment.