|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "go/build" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "time" |
| 9 | + |
| 10 | + "golang.org/x/vulndb/osv" |
| 11 | +) |
| 12 | + |
| 13 | +// NOTE: this cache implementation should be internal to the go tooling |
| 14 | +// (i.e. cmd/go/internal/something) so that the vulndb cache is owned |
| 15 | +// by the go command. Also it is currently NOT CONCURRENCY SAFE since |
| 16 | +// it does not implement file locking. When ported to the stdlib it |
| 17 | +// should use cmd/go/internal/lockedfile. |
| 18 | + |
| 19 | +// The cahce uses a single JSON index file for each vulnerability database |
| 20 | +// which contains the map from packages to the time the last |
| 21 | +// vulnerability for that package was added/modified and the time that |
| 22 | +// the index was retrieved from the vulnerability database. The JSON |
| 23 | +// format is as follows: |
| 24 | +// |
| 25 | +// $GOPATH/pkg/mod/cache/download/vulndb/{db hostname}/indexes/index.json |
| 26 | +// { |
| 27 | +// Retrieved time.Time |
| 28 | +// Index map[string]time.Time |
| 29 | +// } |
| 30 | +// |
| 31 | +// Each package also has a JSON file which contains the array of vulnerability |
| 32 | +// entries for the package. The JSON format is as follows: |
| 33 | +// |
| 34 | +// $GOPATH/pkg/mod/cache/download/vulndb/{db hostname}/{import path}/vulns.json |
| 35 | +// []*osv.Entry |
| 36 | + |
| 37 | +type Cache interface { |
| 38 | + ReadIndex(string) (map[string]time.Time, time.Time, error) |
| 39 | + WriteIndex(string, map[string]time.Time, time.Time) error |
| 40 | + ReadEntries(string, string) ([]*osv.Entry, error) |
| 41 | + WriteEntries(string, string, []*osv.Entry) error |
| 42 | +} |
| 43 | + |
| 44 | +type fsCache struct{} |
| 45 | + |
| 46 | +// should be cfg.GOMODCACHE when doing this inside the cmd/go/internal |
| 47 | +var cacheRoot = filepath.Join(build.Default.GOPATH, "/pkg/mod/cache/downlaod/vulndb") |
| 48 | + |
| 49 | +type cachedIndex struct { |
| 50 | + Retrieved time.Time |
| 51 | + Index map[string]time.Time |
| 52 | +} |
| 53 | + |
| 54 | +func (c *fsCache) ReadIndex(dbName string) (map[string]time.Time, time.Time, error) { |
| 55 | + b, err := os.ReadFile(filepath.Join(cacheRoot, dbName, "index.json")) |
| 56 | + if err != nil { |
| 57 | + if os.IsNotExist(err) { |
| 58 | + return nil, time.Time{}, nil |
| 59 | + } |
| 60 | + return nil, time.Time{}, err |
| 61 | + } |
| 62 | + var index cachedIndex |
| 63 | + if err := json.Unmarshal(b, &index); err != nil { |
| 64 | + return nil, time.Time{}, err |
| 65 | + } |
| 66 | + return index.Index, index.Retrieved, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (c *fsCache) WriteIndex(dbName string, index map[string]time.Time, retrieved time.Time) error { |
| 70 | + path := filepath.Join(cacheRoot, dbName) |
| 71 | + if err := os.MkdirAll(path, 0777); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + j, err := json.Marshal(cachedIndex{ |
| 75 | + Index: index, |
| 76 | + Retrieved: retrieved, |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + if err := os.WriteFile(filepath.Join(path, "index.json"), j, 0666); err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +func (c *fsCache) ReadEntries(dbName string, p string) ([]*osv.Entry, error) { |
| 88 | + b, err := os.ReadFile(filepath.Join(cacheRoot, dbName, p, "vulns.json")) |
| 89 | + if err != nil { |
| 90 | + if os.IsNotExist(err) { |
| 91 | + return nil, nil |
| 92 | + } |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + var entries []*osv.Entry |
| 96 | + if err := json.Unmarshal(b, &entries); err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + return entries, nil |
| 100 | +} |
| 101 | + |
| 102 | +func (c *fsCache) WriteEntries(dbName string, p string, entries []*osv.Entry) error { |
| 103 | + path := filepath.Join(cacheRoot, dbName, p) |
| 104 | + if err := os.MkdirAll(path, 0777); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + j, err := json.Marshal(entries) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + if err := os.WriteFile(filepath.Join(path, "vulns.json"), j, 0666); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + return nil |
| 115 | +} |
0 commit comments