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

feat(python): add line number support for requirements.txt files #6729

Merged
merged 5 commits into from
May 20, 2024
Merged
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
4 changes: 2 additions & 2 deletions docs/docs/coverage/language/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ The following table provides an outline of the features Trivy offers.

| Package manager | File | Transitive dependencies | Dev dependencies | [Dependency graph][dependency-graph] | Position |
|-----------------|------------------|:-----------------------:|:----------------:|:------------------------------------:|:--------:|
| pip | requirements.txt | - | Include | - | - |
| pip | requirements.txt | - | Include | - | |
| Pipenv | Pipfile.lock | ✓ | Include | - | ✓ |
| Poetry | poetry.lock | ✓ | Exclude | ✓ | |
| Poetry | poetry.lock | ✓ | Exclude | ✓ | - |


| Packaging | Dependency graph |
Expand Down
74 changes: 58 additions & 16 deletions integration/testdata/pip.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -25,64 +25,106 @@
"Name": "Flask",
"Identifier": {
"PURL": "pkg:pypi/[email protected]",
"UID": "301ccf5fd90d6082"
"UID": "8b02ba2c070d72c6"
},
"Version": "2.0.0",
"Layer": {}
"Layer": {},
"Locations": [
{
"StartLine": 2,
"EndLine": 2
}
]
},
{
"Name": "Jinja2",
"Identifier": {
"PURL": "pkg:pypi/[email protected]",
"UID": "212193e1595e68cc"
"UID": "476df0c1e49c8f99"
},
"Version": "3.0.0",
"Layer": {}
"Layer": {},
"Locations": [
{
"StartLine": 4,
"EndLine": 4
}
]
},
{
"Name": "Werkzeug",
"Identifier": {
"PURL": "pkg:pypi/[email protected]",
"UID": "56b919b561299a48"
"UID": "4163de19df046f49"
},
"Version": "0.11",
"Layer": {}
"Layer": {},
"Locations": [
{
"StartLine": 6,
"EndLine": 6
}
]
},
{
"Name": "click",
"Identifier": {
"PURL": "pkg:pypi/[email protected]",
"UID": "d58cb56b4e8b1ffd"
"UID": "71e4c8ef31456bf"
},
"Version": "8.0.0",
"Layer": {}
"Layer": {},
"Locations": [
{
"StartLine": 1,
"EndLine": 1
}
]
},
{
"Name": "itsdangerous",
"Identifier": {
"PURL": "pkg:pypi/[email protected]",
"UID": "9bf39d440e409733"
"UID": "389c7cbc34cb6b32"
},
"Version": "2.0.0",
"Layer": {}
"Layer": {},
"Locations": [
{
"StartLine": 3,
"EndLine": 3
}
]
},
{
"Name": "oauth2-client",
"Identifier": {
"PURL": "pkg:pypi/[email protected]",
"UID": "ffc67df5ef686f77"
"UID": "c63f60db796a16ed"
},
"Version": "4.0.0",
"Layer": {}
"Layer": {},
"Locations": [
{
"StartLine": 7,
"EndLine": 7
}
]
},
{
"Name": "python-gitlab",
"Identifier": {
"PURL": "pkg:pypi/[email protected]",
"UID": "f9cbb9736717c4d4"
"UID": "ccad39abab737d13"
},
"Version": "2.0.0",
"Layer": {}
"Layer": {},
"Locations": [
{
"StartLine": 8,
"EndLine": 8
}
]
}
],
"Vulnerabilities": [
Expand All @@ -91,7 +133,7 @@
"PkgName": "Werkzeug",
"PkgIdentifier": {
"PURL": "pkg:pypi/[email protected]",
"UID": "56b919b561299a48"
"UID": "4163de19df046f49"
},
"InstalledVersion": "0.11",
"FixedVersion": "0.15.3",
Expand Down Expand Up @@ -148,7 +190,7 @@
"PkgName": "Werkzeug",
"PkgIdentifier": {
"PURL": "pkg:pypi/[email protected]",
"UID": "56b919b561299a48"
"UID": "4163de19df046f49"
},
"InstalledVersion": "0.11",
"FixedVersion": "0.11.6",
Expand Down
8 changes: 8 additions & 0 deletions pkg/dependency/parser/python/pip/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc

scanner := bufio.NewScanner(decodedReader)
var pkgs []ftypes.Package
var lineNumber int
for scanner.Scan() {
lineNumber++
line := scanner.Text()
line = strings.ReplaceAll(line, " ", "")
line = strings.ReplaceAll(line, `\`, "")
Expand All @@ -52,6 +54,12 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
pkgs = append(pkgs, ftypes.Package{
Name: s[0],
Version: s[1],
Locations: []ftypes.Location{
{
StartLine: lineNumber,
EndLine: lineNumber,
},
},
})
}
if err := scanner.Err(); err != nil {
Expand Down
Loading