Skip to content

Commit faa9d92

Browse files
authored
fix(python): compare pkg names from poetry.lock and pyproject.toml in lowercase (#6852)
1 parent 7d083bc commit faa9d92

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

pkg/dependency/parser/python/poetry/parse.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (p *Parser) parseDependencies(deps map[string]any, pkgVersions map[string][
105105
}
106106

107107
func (p *Parser) parseDependency(name string, versRange any, pkgVersions map[string][]string) (string, error) {
108-
name = normalizePkgName(name)
108+
name = NormalizePkgName(name)
109109
vers, ok := pkgVersions[name]
110110
if !ok {
111111
return "", xerrors.Errorf("no version found for %q", name)
@@ -149,9 +149,11 @@ func matchVersion(currentVersion, constraint string) (bool, error) {
149149
return c.Check(v), nil
150150
}
151151

152-
func normalizePkgName(name string) string {
152+
// NormalizePkgName normalizes the package name based on pep-0426
153+
func NormalizePkgName(name string) string {
153154
// The package names don't use `_`, `.` or upper case, but dependency names can contain them.
154155
// We need to normalize those names.
156+
// cf. https://peps.python.org/pep-0426/#name
155157
name = strings.ToLower(name) // e.g. https://github.com/python-poetry/poetry/blob/c8945eb110aeda611cc6721565d7ad0c657d453a/poetry.lock#L819
156158
name = strings.ReplaceAll(name, "_", "-") // e.g. https://github.com/python-poetry/poetry/blob/c8945eb110aeda611cc6721565d7ad0c657d453a/poetry.lock#L50
157159
name = strings.ReplaceAll(name, ".", "-") // e.g. https://github.com/python-poetry/poetry/blob/c8945eb110aeda611cc6721565d7ad0c657d453a/poetry.lock#L816

pkg/fanal/analyzer/language/python/poetry/poetry.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"path/filepath"
1010

11+
"github.com/samber/lo"
1112
"golang.org/x/xerrors"
1213

1314
"github.com/aquasecurity/trivy/pkg/dependency/parser/python/poetry"
@@ -102,8 +103,8 @@ func (a poetryAnalyzer) mergePyProject(fsys fs.FS, dir string, app *types.Applic
102103
return xerrors.Errorf("unable to parse %s: %w", path, err)
103104
}
104105

106+
// Identify the direct/transitive dependencies
105107
for i, pkg := range app.Packages {
106-
// Identify the direct/transitive dependencies
107108
if _, ok := p[pkg.Name]; ok {
108109
app.Packages[i].Relationship = types.RelationshipDirect
109110
} else {
@@ -127,5 +128,11 @@ func (a poetryAnalyzer) parsePyProject(fsys fs.FS, path string) (map[string]any,
127128
if err != nil {
128129
return nil, err
129130
}
131+
132+
// Packages from `pyproject.toml` can use uppercase characters, `.` and `_`.
133+
parsed = lo.MapKeys(parsed, func(_ any, pkgName string) string {
134+
return poetry.NormalizePkgName(pkgName)
135+
})
136+
130137
return parsed, nil
131138
}

pkg/fanal/analyzer/language/python/poetry/testdata/happy/pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ authors = ["Trivy"]
66

77
[tool.poetry.dependencies]
88
python = "^3.9"
9-
flask = "^1.0"
9+
Flask = "^1.0"
1010
requests = {version = "2.28.1", optional = true}
1111

1212
[tool.poetry.dev-dependencies]

0 commit comments

Comments
 (0)