-
Notifications
You must be signed in to change notification settings - Fork 95
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
feat(python): add pypa support #96
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f4e02b8
feat(python): add pypa support
AndreyLevchenko a33d1f9
feat(python): add pypa support
AndreyLevchenko 8a06a66
feat(python): add pypa support
AndreyLevchenko 95ea5cc
fix initial set of PR notes
AndreyLevchenko cfd2587
add package based subfolders
AndreyLevchenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package pypa | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/aquasecurity/vuln-list-update/types" | ||
"github.com/aquasecurity/vuln-list-update/utils" | ||
"github.com/cheggaaa/pb" | ||
"github.com/spf13/afero" | ||
"golang.org/x/xerrors" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
const ( | ||
pypaDir = "pypa" | ||
securityTrackerURL = "https://github.com/pypa/advisory-db/archive/refs/heads/main.zip" | ||
yamlExt = ".yaml" | ||
) | ||
|
||
type options struct { | ||
url string | ||
dir string | ||
} | ||
type option func(*options) | ||
|
||
type PyPA struct { | ||
opts *options | ||
AppFs afero.Fs | ||
} | ||
|
||
func WithURL(url string) option { | ||
return func(opts *options) { opts.url = url } | ||
} | ||
|
||
func WithDir(dir string) option { | ||
return func(opts *options) { opts.dir = dir } | ||
} | ||
|
||
func NewPypa(opts ...option) PyPA { | ||
o := &options{ | ||
url: securityTrackerURL, | ||
dir: filepath.Join(utils.VulnListDir(), pypaDir), | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(o) | ||
} | ||
|
||
return PyPA{ | ||
opts: o, | ||
} | ||
|
||
} | ||
|
||
func (pypa *PyPA) Update() error { | ||
dir, err := utils.DownloadToTempDir(context.Background(), pypa.opts.url) | ||
|
||
if err != nil { | ||
return xerrors.Errorf("failed to download %s: %w", pypa.opts.url, err) | ||
} | ||
|
||
vulnDir := filepath.Join(dir, "advisory-db-main", "vulns") | ||
|
||
yamlFiles, err := getYamlFiles(vulnDir) | ||
|
||
if err != nil { | ||
return xerrors.Errorf("failed to find vulnerability files in the directory %s: %w", vulnDir, err) | ||
} | ||
|
||
bar := pb.StartNew(len(yamlFiles)) | ||
|
||
for _, file := range yamlFiles { | ||
data, err := os.ReadFile(file) | ||
|
||
if err != nil { | ||
return xerrors.Errorf("unable to read %s: %w", file, err) | ||
} | ||
|
||
osv := &types.Osv{} | ||
|
||
err = yaml.Unmarshal(data, osv) | ||
|
||
if err != nil { | ||
return xerrors.Errorf("unable to parse yaml %s: %w", file, err) | ||
} | ||
|
||
if err := utils.WriteJSON(afero.NewOsFs(), filepath.Join(pypa.opts.dir, osv.Package.Name), fmt.Sprintf("%s.json", osv.Id), osv); err != nil { | ||
return xerrors.Errorf("failed to write file: %w", err) | ||
} | ||
|
||
bar.Increment() | ||
} | ||
bar.Finish() | ||
return nil | ||
} | ||
func getYamlFiles(vulnDir string) ([]string, error) { | ||
yamlFiles := make([]string, 0) | ||
|
||
err := filepath.WalkDir(vulnDir, | ||
func(path string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !d.IsDir() && filepath.Ext(path) == yamlExt { | ||
yamlFiles = append(yamlFiles, path) | ||
} | ||
|
||
return nil | ||
}) | ||
return yamlFiles, err | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package pypa_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/aquasecurity/vuln-list-update/pypa" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Update(t *testing.T) { | ||
type fstruct struct { | ||
pkg string | ||
name string | ||
} | ||
tests := []struct { | ||
name string | ||
inputArchive string | ||
wantFiles []fstruct | ||
wantErr string | ||
}{ | ||
{ | ||
name: "happy path", | ||
inputArchive: "testdata/pypa.zip", | ||
wantFiles: []fstruct{{ | ||
"trac", "PYSEC-2005-1.json"}, {"cherrypy", "PYSEC-2006-1.json"}, {"trac", "PYSEC-2006-2.json"}}, | ||
}, | ||
{ | ||
name: "sad path, unable to download archive", | ||
wantErr: "connection refused", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
b, err := os.ReadFile(tt.inputArchive) | ||
require.NoError(t, err) | ||
|
||
w.Write(b) | ||
})) | ||
|
||
defer ts.Close() | ||
|
||
// Intentionally close to induce network errors | ||
if tt.inputArchive == "" { | ||
ts.Close() | ||
} | ||
|
||
dir := t.TempDir() | ||
c := pypa.NewPypa(pypa.WithURL(ts.URL+"/"+tt.inputArchive), pypa.WithDir(filepath.Join(dir))) | ||
err := c.Update() | ||
if tt.wantErr != "" { | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), tt.wantErr) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
|
||
for _, f := range tt.wantFiles { | ||
|
||
filePath := filepath.Join(f.pkg, f.name) | ||
gotJSON, err := os.ReadFile(filepath.Join(dir, filePath)) | ||
require.NoError(t, err) | ||
|
||
wantJSON, err := os.ReadFile(filepath.Join("testdata", "golden", filePath)) | ||
require.NoError(t, err) | ||
|
||
assert.JSONEq(t, string(wantJSON), string(gotJSON)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"id": "PYSEC-2006-1", | ||
"modified": "2021-07-05T00:01:17.388273Z", | ||
"published": "2006-02-22T02:02:00Z", | ||
"aliases": [ | ||
"CVE-2006-0847" | ||
], | ||
"package": { | ||
"ecosystem": "PyPI", | ||
"name": "cherrypy" | ||
}, | ||
"details": "Directory traversal vulnerability in the staticfilter component in CherryPy before 2.1.1 allows remote attackers to read arbitrary files via \"..\" sequences in unspecified vectors.", | ||
"affects": { | ||
"ranges": [ | ||
{ | ||
"type": "ECOSYSTEM", | ||
"fixed": "2.1.1" | ||
} | ||
], | ||
"versions": [ | ||
"0.10", | ||
"2.0.0-final", | ||
"2.0.0b", | ||
"2.1.0", | ||
"2.1.0-beta" | ||
] | ||
}, | ||
"references": [ | ||
{ | ||
"type": "WEB", | ||
"url": "http://sourceforge.net/project/shownotes.php?release_id=384316\u0026group_id=56099" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://groups.google.com/group/cherrypy-announce/browse_thread/thread/92b2972f774fe6df/2f63afc9433dc306#2f63afc9433dc306" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://www.securityfocus.com/bid/16760" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://www.cherrypy.org/" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://secunia.com/advisories/18944" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://www.gentoo.org/security/en/glsa/glsa-200605-16.xml" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://secunia.com/advisories/20344" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://www.vupen.com/english/advisories/2006/0677" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "https://exchange.xforce.ibmcloud.com/vulnerabilities/24809" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"id": "PYSEC-2005-1", | ||
"modified": "2021-07-16T01:31:33.917972Z", | ||
"published": "2005-12-31T05:00:00Z", | ||
"aliases": [ | ||
"CVE-2005-4644" | ||
], | ||
"package": { | ||
"ecosystem": "PyPI", | ||
"name": "trac" | ||
}, | ||
"details": "Cross-site scripting (XSS) vulnerability in the HTML WikiProcessor in Edgewall Trac 0.9.2 allows remote attackers to inject arbitrary web script or HTML via javascript in the SRC attribute of an IMG tag.", | ||
"affects": { | ||
"ranges": [ | ||
{ | ||
"type": "ECOSYSTEM", | ||
"fixed": "0.10" | ||
} | ||
], | ||
"versions": [ | ||
"0.8.4", | ||
"0.9" | ||
] | ||
}, | ||
"references": [ | ||
{ | ||
"type": "WEB", | ||
"url": "http://projects.edgewall.com/trac/ticket/2473" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://www.securityfocus.com/bid/16198" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://secunia.com/advisories/18465" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://www.debian.org/security/2006/dsa-951" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://secunia.com/advisories/18555" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://trac.edgewall.org/ticket/2473" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "http://www.vupen.com/english/advisories/2006/0226" | ||
}, | ||
{ | ||
"type": "WEB", | ||
"url": "https://exchange.xforce.ibmcloud.com/vulnerabilities/24183" | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not using https://osv-vulnerabilities.storage.googleapis.com/PyPI/all.zip ?
https://github.com/google/osv/blob/master/README.md#current-data-sources
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AndreyLevchenko Could you check if we can use the above URL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@knqyf263 I think I can use the url above, but I need to understand benefits better. For now it looks pretty same to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The benefit is we can have a single implementation for some languages (Python, Go and Rust).