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

Implement global security advisories API #2993

Merged
merged 6 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 88 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 122 additions & 0 deletions github/security_advisories.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,81 @@
State string `url:"state,omitempty"`
}

// ListGlobalSecurityAdvisoriesOptions specifies the optional parameters to list the global security advisories.
type ListGlobalSecurityAdvisoriesOptions struct {
ListCursorOptions

// If specified, only advisories with this GHSA (GitHub Security Advisory) identifier will be returned.
GHSAID string `url:"ghsa_id,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved

// If specified, only advisories of this type will be returned.
// By default, a request with no other parameters defined will only return reviewed advisories that are not malware.
// Default: reviewed
// Can be one of: reviewed, malware, unreviewed
Type string `url:"type,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved

// If specified, only advisories with this CVE (Common Vulnerabilities and Exposures) identifier will be returned.
CVEID string `url:"cve_id,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved

// If specified, only advisories for these ecosystems will be returned.
// Can be one of: actions, composer, erlang, go, maven, npm, nuget, other, pip, pub, rubygems, rust
Ecosystem string `url:"ecosystem,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved

// If specified, only advisories with these severities will be returned.
// Can be one of: unknown, low, medium, high, critical
Severity string `url:"severity,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved

// If specified, only advisories with these Common Weakness Enumerations (CWEs) will be returned.
// Example: cwes=79,284,22 or cwes[]=79&cwes[]=284&cwes[]=22
CWEs []string `url:"cwes,omitempty"`

// Whether to only return advisories that have been withdrawn.
IsWithdrawn bool `url:"is_withdrawn,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved

// If specified, only return advisories that affect any of package or package@version.
// A maximum of 1000 packages can be specified. If the query parameter causes
// the URL to exceed the maximum URL length supported by your client, you must specify fewer packages.
// Example: affects=package1,package2@1.0.0,package3@^2.0.0 or affects[]=package1&affects[]=package2@1.0.0
Affects string `url:"affects,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved

// If specified, only return advisories that were published on a date or date range.
Published string `url:"published,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved

// If specified, only return advisories that were updated on a date or date range.
Updated string `url:"updated,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved

// If specified, only show advisories that were updated or published on a date or date range.
Modified string `url:"modified,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved
}

// GlobalSecurityAdvisory represents the global security advisory object response.
type GlobalSecurityAdvisory struct {
SecurityAdvisory
ID *int64 `json:"id,omitempty"`
RepositoryAdvisoryURL *string `json:"repository_advisory_url,omitempty"`
Type *string `json:"type,omitempty"`
SourceCodeLocation *string `json:"source_code_location,omitempty"`
References []string `json:"references,omitempty"`
Vulnerabilities []*Vulnerabilities `json:"vulnerabilities,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved
GitHubReviewedAt *Timestamp `json:"github_reviewed_at,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved
NVDPublishedAt *Timestamp `json:"nvd_published_at,omitempty"`
Credits []*Credits `json:"credits,omitempty"`
cpanato marked this conversation as resolved.
Show resolved Hide resolved
}

// Vulnerabilities represents the Vulnerabilities for the global security advisory.
type Vulnerabilities struct {
Package *VulnerabilityPackage `json:"package,omitempty"`
FirstPatchedVersion *string `json:"first_patched_version,omitempty"`
VulnerableVersionRange *string `json:"vulnerable_version_range,omitempty"`
VulnerableFunctions []string `json:"vulnerable_functions,omitempty"`
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete this struct.


// Credits represents the credit object for a global security advisory.
type Credits struct {
cpanato marked this conversation as resolved.
Show resolved Hide resolved
User *User
cpanato marked this conversation as resolved.
Show resolved Hide resolved
Type *string `json:"type,omitempty"`
}

// RequestCVE requests a Common Vulnerabilities and Exposures (CVE) for a repository security advisory.
// The ghsaID is the GitHub Security Advisory identifier of the advisory.
//
Expand Down Expand Up @@ -124,3 +199,50 @@

return advisories, resp, nil
}

// ListGlobalSecurityAdvisories Lists all global security advisories.
cpanato marked this conversation as resolved.
Show resolved Hide resolved
//
// GitHub API docs: https://docs.github.com/rest/security-advisories/global-advisories#list-global-security-advisories
//
//meta:operation GET /advisories
func (s *SecurityAdvisoriesService) ListGlobalSecurityAdvisories(ctx context.Context, opt *ListGlobalSecurityAdvisoriesOptions) ([]*GlobalSecurityAdvisory, *Response, error) {
cpanato marked this conversation as resolved.
Show resolved Hide resolved
url := "advisories"
url, err := addOptions(url, opt)
if err != nil {
return nil, nil, err
}

Check warning on line 213 in github/security_advisories.go

View check run for this annotation

Codecov / codecov/patch

github/security_advisories.go#L212-L213

Added lines #L212 - L213 were not covered by tests

req, err := s.client.NewRequest("GET", url, nil)
if err != nil {
return nil, nil, err
}

var advisories []*GlobalSecurityAdvisory
resp, err := s.client.Do(ctx, req, &advisories)
if err != nil {
return nil, resp, err
}

return advisories, resp, nil
}

// GetGlobalSecurityAdvisories Gets a global security advisory using its GitHub Security Advisory (GHSA) identifier.
cpanato marked this conversation as resolved.
Show resolved Hide resolved
//
// GitHub API docs: https://docs.github.com/rest/security-advisories/global-advisories#get-a-global-security-advisory
//
//meta:operation GET /advisories/{ghsa_id}
func (s *SecurityAdvisoriesService) GetGlobalSecurityAdvisories(ctx context.Context, ghsaID string) (*GlobalSecurityAdvisory, *Response, error) {
url := fmt.Sprintf("advisories/%s", ghsaID)
req, err := s.client.NewRequest("GET", url, nil)
if err != nil {
return nil, nil, err
}

var advisory *GlobalSecurityAdvisory
resp, err := s.client.Do(ctx, req, &advisory)
if err != nil {
return nil, resp, err
}

return advisory, resp, nil
}