-
Notifications
You must be signed in to change notification settings - Fork 1
/
vulns.go
164 lines (149 loc) · 4.21 KB
/
vulns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package frontend
import (
"context"
"errors"
"net/http"
"net/url"
"strings"
"github.com/khulnasoft-lab/godep/internal"
"github.com/khulnasoft-lab/godep/internal/derrors"
"github.com/khulnasoft-lab/godep/internal/frontend/page"
"github.com/khulnasoft-lab/godep/internal/frontend/serrors"
"github.com/khulnasoft-lab/godep/internal/osv"
"github.com/khulnasoft-lab/godep/internal/vuln"
)
const (
githubAdvisoryUrlPrefix = "https://github.com/advisories/"
mitreAdvisoryUrlPrefix = "https://www.cve.org/CVERecord?id="
nistAdvisoryUrlPrefix = "https://nvd.nist.gov/vuln/detail/"
)
// VulnListPage holds the information for a page that lists vuln entries.
type VulnListPage struct {
page.BasePage
Entries []*osv.Entry
}
// VulnEntryPage holds the information for a page that displays a single
// vuln entry.
type VulnEntryPage struct {
page.BasePage
Entry *osv.Entry
AffectedPackages []*vuln.AffectedPackage
AliasLinks []link
AdvisoryLinks []link
}
func (s *Server) serveVuln(w http.ResponseWriter, r *http.Request, _ internal.DataSource) error {
if s.vulnClient == nil {
return serrors.DatasourceNotSupportedError()
}
vp, err := newVulnPage(r.Context(), r.URL, s.vulnClient)
if err != nil {
var serr *serrors.ServerError
if !errors.As(err, &serr) {
serr = &serrors.ServerError{Status: derrors.ToStatus(err), Err: err}
}
return serr
}
page := vp.page
page.SetBasePage(s.newBasePage(r, vp.title))
s.servePage(r.Context(), w, vp.template, page)
return nil
}
type vulnPage struct {
page interface{ SetBasePage(page.BasePage) }
template string
title string
}
func newVulnPage(ctx context.Context, url *url.URL, vc *vuln.Client) (*vulnPage, error) {
path := strings.TrimPrefix(url.Path, "/vuln")
switch path {
case "/":
// Serve a list of the 5 most recent entries.
page, err := newVulnListPage(ctx, vc, 5)
if err != nil {
return nil, err
}
return &vulnPage{
page: page,
template: "vuln/main",
title: "Go Vulnerability Database"}, nil
case "/list":
// Serve a list of all entries.
page, err := newVulnListPage(ctx, vc, -1)
if err != nil {
return nil, err
}
return &vulnPage{
page: page,
template: "vuln/list",
title: "Vulnerability Reports"}, nil
default: // the path should be "/<ID>", e.g. "/GO-2021-0001".
id, ok := vuln.CanonicalGoID(strings.TrimPrefix(path, "/"))
if !ok {
if url.Query().Has("q") {
return nil, derrors.NotFound
}
return nil, &serrors.ServerError{
Status: http.StatusBadRequest,
ResponseText: "invalid Go vuln ID; should be GO-YYYY-NNNN",
}
}
page, err := newVulnEntryPage(ctx, vc, id)
if err != nil {
return nil, err
}
return &vulnPage{
page: page,
template: "vuln/entry",
title: id}, nil
}
}
func newVulnEntryPage(ctx context.Context, client *vuln.Client, id string) (*VulnEntryPage, error) {
entry, err := client.ByID(ctx, id)
if err != nil {
return nil, derrors.VulnDBError
}
if entry == nil {
return nil, derrors.NotFound
}
return &VulnEntryPage{
Entry: entry,
AffectedPackages: vuln.AffectedPackages(entry),
AliasLinks: aliasLinks(entry),
AdvisoryLinks: advisoryLinks(entry),
}, nil
}
func newVulnListPage(ctx context.Context, client *vuln.Client, n int) (*VulnListPage, error) {
entries, err := client.Entries(ctx, n)
if err != nil {
return nil, derrors.VulnDBError
}
return &VulnListPage{Entries: entries}, nil
}
// aliasLinks generates links to reference pages for vuln aliases.
func aliasLinks(e *osv.Entry) []link {
var links []link
for _, a := range e.Aliases {
prefix, _, _ := strings.Cut(a, "-")
switch prefix {
case "CVE":
links = append(links, link{Body: a, Href: mitreAdvisoryUrlPrefix + a})
case "GHSA":
links = append(links, link{Body: a, Href: githubAdvisoryUrlPrefix + a})
default:
links = append(links, link{Body: a})
}
}
return links
}
func advisoryLinks(e *osv.Entry) []link {
var links []link
for _, r := range e.References {
if r.Type == "ADVISORY" {
links = append(links, link{Body: r.URL, Href: r.URL})
}
}
return links
}