|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cves |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/url" |
| 19 | + "strings" |
| 20 | +) |
| 21 | + |
| 22 | +type FixCommit struct { |
| 23 | + Repo string |
| 24 | + Commit string |
| 25 | +} |
| 26 | + |
| 27 | +type AffectedVersion struct { |
| 28 | + Introduced string |
| 29 | + Fixed string |
| 30 | +} |
| 31 | + |
| 32 | +type VersionInfo struct { |
| 33 | + FixCommits []FixCommit |
| 34 | + AffectedVersions []AffectedVersion |
| 35 | +} |
| 36 | + |
| 37 | +func extractGitHubCommit(link string) *FixCommit { |
| 38 | + // Example: https://github.com/google/osv/commit/cd4e934d0527e5010e373e7fed54ef5daefba2f5 |
| 39 | + u, err := url.Parse(link) |
| 40 | + if err != nil { |
| 41 | + return nil |
| 42 | + } |
| 43 | + |
| 44 | + if u.Host != "github.com" { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + pathParts := strings.Split(u.Path, "/") |
| 49 | + if pathParts[len(pathParts)-2] != "commit" { |
| 50 | + return nil |
| 51 | + } |
| 52 | + |
| 53 | + // Commit is the last component. |
| 54 | + commit := pathParts[len(pathParts)-1] |
| 55 | + // Stript the /commit/... to get the repo URL. |
| 56 | + u.Path = strings.Join(pathParts[0:len(pathParts)-2], "/") |
| 57 | + repo := u.String() |
| 58 | + |
| 59 | + return &FixCommit{ |
| 60 | + Repo: repo, |
| 61 | + Commit: commit, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func ExtractVersionInfo(cve CVEItem) VersionInfo { |
| 66 | + v := VersionInfo{} |
| 67 | + for _, reference := range cve.CVE.References.ReferenceData { |
| 68 | + // TODO(ochang): Support other common commit URLs. |
| 69 | + if commit := extractGitHubCommit(reference.URL); commit != nil { |
| 70 | + v.FixCommits = append(v.FixCommits, *commit) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + for _, node := range cve.Configurations.Nodes { |
| 75 | + if node.Operator != "OR" { |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + // TODO: Also try to parse description as these are not always reliably set. |
| 80 | + for _, match := range node.CPEMatch { |
| 81 | + if !match.Vulnerable { |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + if match.VersionStartIncluding != "" || match.VersionEndExcluding != "" { |
| 86 | + if match.VersionStartExcluding != "" || match.VersionEndIncluding != "" { |
| 87 | + // TODO: handle these. |
| 88 | + continue |
| 89 | + } |
| 90 | + |
| 91 | + v.AffectedVersions = append(v.AffectedVersions, AffectedVersion{ |
| 92 | + // TODO: make sure these actually match PyPI numbers. |
| 93 | + Introduced: match.VersionStartIncluding, |
| 94 | + Fixed: match.VersionEndExcluding, |
| 95 | + }) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return v |
| 100 | +} |
0 commit comments