2024-02-19 15:16:35 +04:00
|
|
|
package pipenv
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/liamg/jfather"
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
2024-05-07 16:25:52 +04:00
|
|
|
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
|
2024-02-26 09:55:15 +04:00
|
|
|
xio "github.com/aquasecurity/trivy/pkg/x/io"
|
2024-02-19 15:16:35 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type lockFile struct {
|
|
|
|
|
Default map[string]dependency `json:"default"`
|
|
|
|
|
}
|
|
|
|
|
type dependency struct {
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
StartLine int
|
|
|
|
|
EndLine int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Parser struct{}
|
|
|
|
|
|
2024-05-07 16:25:52 +04:00
|
|
|
func NewParser() *Parser {
|
2024-02-19 15:16:35 +04:00
|
|
|
return &Parser{}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 16:25:52 +04:00
|
|
|
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependency, error) {
|
2024-02-19 15:16:35 +04:00
|
|
|
var lockFile lockFile
|
|
|
|
|
input, err := io.ReadAll(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, xerrors.Errorf("failed to read packages.lock.json: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if err := jfather.Unmarshal(input, &lockFile); err != nil {
|
|
|
|
|
return nil, nil, xerrors.Errorf("failed to decode Pipenv.lock: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 16:25:52 +04:00
|
|
|
var pkgs []ftypes.Package
|
|
|
|
|
for pkgName, dep := range lockFile.Default {
|
|
|
|
|
pkgs = append(pkgs, ftypes.Package{
|
2024-03-12 10:56:10 +04:00
|
|
|
Name: pkgName,
|
2024-05-07 16:25:52 +04:00
|
|
|
Version: strings.TrimLeft(dep.Version, "="),
|
|
|
|
|
Locations: []ftypes.Location{
|
2024-03-12 10:56:10 +04:00
|
|
|
{
|
2024-05-07 16:25:52 +04:00
|
|
|
StartLine: dep.StartLine,
|
|
|
|
|
EndLine: dep.EndLine,
|
2024-03-12 10:56:10 +04:00
|
|
|
},
|
|
|
|
|
},
|
2024-02-19 15:16:35 +04:00
|
|
|
})
|
|
|
|
|
}
|
2024-05-07 16:25:52 +04:00
|
|
|
return pkgs, nil, nil
|
2024-02-19 15:16:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UnmarshalJSONWithMetadata needed to detect start and end lines of deps
|
|
|
|
|
func (t *dependency) UnmarshalJSONWithMetadata(node jfather.Node) error {
|
|
|
|
|
if err := node.Decode(&t); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
// Decode func will overwrite line numbers if we save them first
|
|
|
|
|
t.StartLine = node.Range().Start.Line
|
|
|
|
|
t.EndLine = node.Range().End.Line
|
|
|
|
|
return nil
|
|
|
|
|
}
|