-
Notifications
You must be signed in to change notification settings - Fork 1
/
regexp.go
51 lines (45 loc) · 1.33 KB
/
regexp.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
// Copyright 2023 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 vuln
import (
"regexp"
"strings"
)
const (
ci = "(?i)" // case-insensitive
goRE = "^GO-[0-9]{4}-[0-9]{4,}$"
cveRE = "^CVE-[0-9]{4}-[0-9]+$"
// Regexp adapted from https://github.com/github/advisory-database.
ghsaRE = "^(GHSA)((-[23456789cfghjmpqrvwx]{4}){3})$"
)
// Case-insensitive regexps for vuln IDs/aliases.
var (
goID = regexp.MustCompile(ci + goRE)
cveID = regexp.MustCompile(ci + cveRE)
ghsaID = regexp.MustCompile(ci + ghsaRE)
)
// Canonical returns the canonical form of the given Go ID string
// by correcting the case.
//
// If no canonical form can be found, returns false.
func CanonicalGoID(id string) (_ string, ok bool) {
if goID.MatchString(id) {
return strings.ToUpper(id), true
}
return "", false
}
// Canonical returns the canonical form of the given alias ID string
// (a CVE or GHSA id) by correcting the case.
//
// If no canonical form can be found, returns false.
func CanonicalAlias(id string) (_ string, ok bool) {
if cveID.MatchString(id) {
return strings.ToUpper(id), true
}
parts := ghsaID.FindStringSubmatch(id)
if len(parts) != 4 {
return "", false
}
return strings.ToUpper(parts[1]) + strings.ToLower(parts[2]), true
}