Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cwe): add cwe-id for category and view #1578

Merged
merged 1 commit into from
Jan 20, 2023

Conversation

MaineK00n
Copy link
Collaborator

What did you implement:

The following logs were seen, and many of these CWE IDs were associated with Category.

[Jan  5 23:13:14] DEBUG [localhost] CWE-ID 254 is not found in English CWE Dict
[Jan  5 23:13:14] DEBUG [localhost] CWE-ID 399 is not found in English CWE Dict
[Jan  5 23:13:14] DEBUG [localhost] CWE-ID 1230 is not found in English CWE Dict
[Jan  5 23:13:14] DEBUG [localhost] CWE-ID 19 is not found in English CWE Dict

The CWE IDs are not only assigned to Weakness, but also to Category and View, so they are added in this PR.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Checklist:

You don't have to satisfy all of the following.

  • Write tests
  • Write documentation
  • Check that there aren't other open pull requests for the same issue/feature
  • Format your source code by make fmt
  • Pass the test by make test
  • Provide verification config / commands
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES

Reference

@MaineK00n MaineK00n self-assigned this Jan 6, 2023
@MaineK00n
Copy link
Collaborator Author

package main

import (
	"archive/zip"
	"bytes"
	"encoding/xml"
	"fmt"
	"io"
	"net/http"
	"os"
	"regexp"
	"strconv"
	"strings"
)

type weaknessCatalog struct {
	Weaknesses []weakness `xml:"Weaknesses>Weakness"`
	Categories []category `xml:"Categories>Category"`
	Views      []view     `xml:"Views>View"`
}

type weakness struct {
	ID                  string `xml:"ID,attr"`
	Name                string `xml:"Name,attr"`
	Description         string `xml:"Description"`
	ExtendedDescription string `xml:"Extended_Description"`
}

type category struct {
	ID      string `xml:"ID,attr"`
	Name    string `xml:"Name,attr"`
	Summary string `xml:"Summary"`
}

type view struct {
	ID        string `xml:"ID,attr"`
	Name      string `xml:"Name,attr"`
	Objective string `xml:"Objective"`
}

func main() {
	if err := exec(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func exec() error {
	resp, err := http.Get("https://cwe.mitre.org/data/xml/cwec_latest.xml.zip")
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("failed to fetch https://cwe.mitre.org/data/xml/cwec_latest.xml.zip")
	}

	bs, err := io.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	r, err := zip.NewReader(bytes.NewReader(bs), int64(len(bs)))
	if err != nil {
		return err
	}

	f, err := r.File[0].Open()
	if err != nil {
		return err
	}
	defer f.Close()

	var catalog weaknessCatalog
	if err := xml.NewDecoder(f).Decode(&catalog); err != nil {
		return err
	}

	fmt.Println("// CweDictEn is the Cwe dictionary (https://cwe.mitre.org/data/xml/cwec_latest.xml.zip)")
	fmt.Println("var CweDictEn = map[string]Cwe{")
	for _, w := range catalog.Weaknesses {
		fmt.Printf("	%s: {\n", strconv.Quote(w.ID))
		fmt.Printf("		CweID:               %s,\n", strconv.Quote(w.ID))
		fmt.Printf("		Name:                %s,\n", strconv.Quote(w.Name))
		fmt.Printf("		Description:         %s,\n", strconv.Quote(strip(w.Description)))
		fmt.Printf("		ExtendedDescription: %s,\n", strconv.Quote(strip(w.ExtendedDescription)))
		fmt.Printf("		Lang:                %s,\n", strconv.Quote("en"))
		fmt.Println("	},")
	}
	for _, w := range catalog.Categories {
		fmt.Printf("	%s: {\n", strconv.Quote(w.ID))
		fmt.Printf("		CweID:               %s,\n", strconv.Quote(w.ID))
		fmt.Printf("		Name:                %s,\n", strconv.Quote(w.Name))
		fmt.Printf("		Description:         %s,\n", strconv.Quote(strip(w.Summary)))
		fmt.Printf("		Lang:                %s,\n", strconv.Quote("en"))
		fmt.Println("	},")
	}
	for _, w := range catalog.Views {
		fmt.Printf("	%s: {\n", strconv.Quote(w.ID))
		fmt.Printf("		CweID:               %s,\n", strconv.Quote(w.ID))
		fmt.Printf("		Name:                %s,\n", strconv.Quote(w.Name))
		fmt.Printf("		Description:         %s,\n", strconv.Quote(strip(w.Objective)))
		fmt.Printf("		Lang:                %s,\n", strconv.Quote("en"))
		fmt.Println("	},")
	}
	fmt.Println("}")

	return nil
}

var rep = regexp.MustCompile(`\s{2,}`)

func strip(s string) string {
	return strings.TrimSpace(rep.ReplaceAllString(strings.NewReplacer("\t", " ", "\n", " ").Replace(s), " "))
}

@kotakanbe kotakanbe merged commit bfe0db7 into master Jan 20, 2023
@kotakanbe kotakanbe deleted the MaineK00n/add-cwe-category-view branch January 20, 2023 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants