Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 57 additions & 6 deletions extractor/filesystem/language/python/poetrylock/poetrylock.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
package poetrylock

import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"path/filepath"
"regexp"
"slices"
"strings"

"github.com/BurntSushi/toml"
"github.com/google/osv-scalibr/extractor"
Expand Down Expand Up @@ -97,22 +102,30 @@ func resolveGroups(pkg poetryLockPackage) []string {

// Extract extracts packages from poetry.lock files passed through the scan input.
func (e Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) (inventory.Inventory, error) {
var parsedLockfile *poetryLockFile

_, err := toml.NewDecoder(input.Reader).Decode(&parsedLockfile)

content, err := io.ReadAll(input.Reader)
if err != nil {
return inventory.Inventory{}, fmt.Errorf("could not read file: %w", err)
}

var parsedLockfile *poetryLockFile
if err := toml.Unmarshal(content, &parsedLockfile); err != nil {
return inventory.Inventory{}, fmt.Errorf("could not extract: %w", err)
}

packageNames := make([]string, 0, len(parsedLockfile.Packages))
for _, p := range parsedLockfile.Packages {
packageNames = append(packageNames, p.Name)
}
lineNums := findPackageLineNumbers(content, packageNames)

packages := make([]*extractor.Package, 0, len(parsedLockfile.Packages))

for _, lockPackage := range parsedLockfile.Packages {
for i, lockPackage := range parsedLockfile.Packages {
pkgDetails := &extractor.Package{
Name: lockPackage.Name,
Version: lockPackage.Version,
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath(input.Path),
Location: extractor.LocationFromPathAndLine(input.Path, lineNums[i]),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: resolveGroups(lockPackage),
},
Expand All @@ -128,4 +141,42 @@ func (e Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) (in
return inventory.Inventory{Packages: packages}, nil
}

var nameRegex = regexp.MustCompile(`^name\s*=\s*["']([^"']+)["']`)

func findPackageLineNumbers(content []byte, packageNames []string) []int {
lineNums := make([]int, len(packageNames))
scanner := bufio.NewScanner(bytes.NewReader(content))
currentLine := 0
pkgIdx := 0
inPackageBlock := false

for scanner.Scan() {
currentLine++
line := strings.TrimSpace(scanner.Text())

if line == "[[package]]" {
inPackageBlock = true
continue
}

if line == "[metadata]" {
break
}

if inPackageBlock && strings.HasPrefix(line, "[") && !strings.HasPrefix(line, "[[package]]") {
inPackageBlock = false
}

if inPackageBlock && pkgIdx < len(packageNames) {
matches := nameRegex.FindStringSubmatch(line)
if len(matches) > 1 && matches[1] == packageNames[pkgIdx] {
lineNums[pkgIdx] = currentLine
pkgIdx++
inPackageBlock = false
}
}
}
return lineNums
}

var _ filesystem.Extractor = Extractor{}
37 changes: 19 additions & 18 deletions extractor/filesystem/language/python/poetrylock/poetrylock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func TestExtractor_FileRequired(t *testing.T) {
}

func TestExtractor_Extract(t *testing.T) {
loc := extractor.LocationFromPathAndLine
tests := []extracttest.TestTableEntry{
{
Name: "invalid toml",
Expand All @@ -108,7 +109,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "numpy",
Version: "1.23.3",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/one-package.lock"),
Location: loc("testdata/one-package.lock", 2),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{},
},
Expand All @@ -125,7 +126,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "proto-plus",
Version: "1.22.0",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/two-packages.lock"),
Location: loc("testdata/two-packages.lock", 2),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{},
},
Expand All @@ -134,7 +135,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "protobuf",
Version: "4.21.5",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/two-packages.lock"),
Location: loc("testdata/two-packages.lock", 16),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{},
},
Expand All @@ -151,7 +152,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "emoji",
Version: "2.0.0",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/one-package-with-metadata.lock"),
Location: loc("testdata/one-package-with-metadata.lock", 2),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{},
},
Expand All @@ -168,7 +169,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "ike",
Version: "0.2.0",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/source-git.lock"),
Location: loc("testdata/source-git.lock", 2),
SourceCode: &extractor.SourceCodeIdentifier{
Commit: "cd66602cd29f61a2d2e7fb995fef1e61708c034d",
},
Expand All @@ -188,7 +189,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "appdirs",
Version: "1.4.4",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/source-legacy.lock"),
Location: loc("testdata/source-legacy.lock", 2),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{},
},
Expand All @@ -205,7 +206,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "numpy",
Version: "1.23.3",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/optional-package.lock"),
Location: loc("testdata/optional-package.lock", 2),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{"optional"},
},
Expand All @@ -222,7 +223,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "async-timeout",
Version: "5.0.1",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/multiple-packages.v2.lock"),
Location: loc("testdata/multiple-packages.v2.lock", 4),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{"optional"},
},
Expand All @@ -231,7 +232,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "factory-boy",
Version: "3.3.1",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/multiple-packages.v2.lock"),
Location: loc("testdata/multiple-packages.v2.lock", 17),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{"dev"},
},
Expand All @@ -240,7 +241,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "faker",
Version: "33.3.0",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/multiple-packages.v2.lock"),
Location: loc("testdata/multiple-packages.v2.lock", 36),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{"dev", "test"},
},
Expand All @@ -249,7 +250,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "proto-plus",
Version: "1.22.0",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/multiple-packages.v2.lock"),
Location: loc("testdata/multiple-packages.v2.lock", 52),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{},
},
Expand All @@ -258,7 +259,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "proto-plus",
Version: "1.23.0",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/multiple-packages.v2.lock"),
Location: loc("testdata/multiple-packages.v2.lock", 71),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{},
},
Expand All @@ -267,7 +268,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "protobuf",
Version: "4.25.5",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/multiple-packages.v2.lock"),
Location: loc("testdata/multiple-packages.v2.lock", 90),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{},
},
Expand All @@ -276,7 +277,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "python-dateutil",
Version: "2.9.0.post0",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/multiple-packages.v2.lock"),
Location: loc("testdata/multiple-packages.v2.lock", 111),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{"dev", "test"},
},
Expand All @@ -285,7 +286,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "six",
Version: "1.17.0",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/multiple-packages.v2.lock"),
Location: loc("testdata/multiple-packages.v2.lock", 146),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{},
},
Expand All @@ -294,7 +295,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "typing-extensions",
Version: "4.12.2",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/multiple-packages.v2.lock"),
Location: loc("testdata/multiple-packages.v2.lock", 158),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{"dev", "test"},
},
Expand All @@ -303,7 +304,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "urllib3",
Version: "2.3.0",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/multiple-packages.v2.lock"),
Location: loc("testdata/multiple-packages.v2.lock", 170),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{"dev"},
},
Expand All @@ -312,7 +313,7 @@ func TestExtractor_Extract(t *testing.T) {
Name: "redis",
Version: "5.2.1",
PURLType: purl.TypePyPi,
Location: extractor.LocationFromPath("testdata/multiple-packages.v2.lock"),
Location: loc("testdata/multiple-packages.v2.lock", 126),
Metadata: &osv.DepGroupMetadata{
DepGroupVals: []string{"optional"},
},
Expand Down
Loading