diff --git a/pkg/dependency/parser/hex/mix/parse.go b/pkg/dependency/parser/hex/mix/parse.go index be4fbd3753..88e0896708 100644 --- a/pkg/dependency/parser/hex/mix/parse.go +++ b/pkg/dependency/parser/hex/mix/parse.go @@ -43,6 +43,12 @@ func (p *Parser) Parse(_ context.Context, r xio.ReadSeekerAt) ([]ftypes.Package, ss := strings.FieldsFunc(body, func(r rune) bool { return unicode.IsSpace(r) || r == ',' }) + if len(ss) == 0 { + // An entry with nothing after the colon, e.g. `"bunt":` + // cf. #10976 + p.logger.Warn("Cannot parse dependency", log.String("line", line)) + continue + } if len(ss) < 8 { // In the case where array is empty: s == 8, in other cases s > 8 // git repository doesn't have dependency version // skip these dependencies diff --git a/pkg/dependency/parser/hex/mix/parse_test.go b/pkg/dependency/parser/hex/mix/parse_test.go index 73b2825931..321c51df8f 100644 --- a/pkg/dependency/parser/hex/mix/parse_test.go +++ b/pkg/dependency/parser/hex/mix/parse_test.go @@ -72,6 +72,13 @@ func TestParser_Parse(t *testing.T) { inputFile: "testdata/empty.mix.lock", want: nil, }, + { + // An entry with nothing after the colon carries no version, + // so it is skipped. cf. #10976 + name: "no fields", + inputFile: "testdata/no-fields.mix.lock", + want: nil, + }, } for _, tt := range tests { diff --git a/pkg/dependency/parser/hex/mix/testdata/no-fields.mix.lock b/pkg/dependency/parser/hex/mix/testdata/no-fields.mix.lock new file mode 100644 index 0000000000..e753b720d4 --- /dev/null +++ b/pkg/dependency/parser/hex/mix/testdata/no-fields.mix.lock @@ -0,0 +1,3 @@ +%{ + "bunt": +} diff --git a/pkg/dependency/parser/python/pyproject/pyproject.go b/pkg/dependency/parser/python/pyproject/pyproject.go index b7a26c38b2..a05dc3697e 100644 --- a/pkg/dependency/parser/python/pyproject/pyproject.go +++ b/pkg/dependency/parser/python/pyproject/pyproject.go @@ -75,8 +75,14 @@ func (d *Dependencies) UnmarshalTOML(data any) error { // There are some formats: // e.g. `Flask == 1.1.4`, `Flask==1.1.4`, `Flask(>= 1.0.0)`, `pluggy[pre-commit,tox] (==0.13.1)`, etc. dep = strings.NewReplacer(">", " ", "<", " ", "=", " ", "(", " ", "[", " ").Replace(dep) + fields := strings.Fields(dep) + if len(fields) == 0 { + // An entry that carries no name, e.g. `dependencies = [""]`. + // cf. #10976 + continue + } // Save only the name, normalized (PEP 503) to match the names from poetry.lock/pylock.toml. - d.Set.Append(python.NormalizePkgName(strings.Fields(dep)[0], true)) + d.Set.Append(python.NormalizePkgName(fields[0], true)) } default: return xerrors.Errorf("dependencies must be map, but got: %T", data) diff --git a/pkg/dependency/parser/python/pyproject/pyproject_test.go b/pkg/dependency/parser/python/pyproject/pyproject_test.go index 8975e66d11..29d2a1e915 100644 --- a/pkg/dependency/parser/python/pyproject/pyproject_test.go +++ b/pkg/dependency/parser/python/pyproject/pyproject_test.go @@ -120,6 +120,20 @@ func TestParser_Parse(t *testing.T) { }, wantErr: assert.NoError, }, + { + // A dependency entry that carries no name is skipped. + // cf. #10976 + name: "empty dependency", + file: "testdata/empty_dep.toml", + want: pyproject.PyProject{ + Project: pyproject.Project{ + Dependencies: pyproject.Dependencies{ + Set: set.New[string]("flask"), + }, + }, + }, + wantErr: assert.NoError, + }, { name: "sad path", file: "testdata/sad.toml", diff --git a/pkg/dependency/parser/python/pyproject/testdata/empty_dep.toml b/pkg/dependency/parser/python/pyproject/testdata/empty_dep.toml new file mode 100644 index 0000000000..643697cf5b --- /dev/null +++ b/pkg/dependency/parser/python/pyproject/testdata/empty_dep.toml @@ -0,0 +1,8 @@ +[project] +name = "example" +version = "0.1.0" +requires-python = ">=3.12" +dependencies = [ + "", + "flask == 1.1.4", +] diff --git a/pkg/dependency/parser/ruby/bundler/parse.go b/pkg/dependency/parser/ruby/bundler/parse.go index 5c2ac6b905..3dfe54c76e 100644 --- a/pkg/dependency/parser/ruby/bundler/parse.go +++ b/pkg/dependency/parser/ruby/bundler/parse.go @@ -66,7 +66,12 @@ func (p *Parser) Parse(_ context.Context, r xio.ReadSeekerAt) ([]ftypes.Package, if countLeadingSpace(line) == 6 { line = strings.TrimSpace(line) s := strings.Fields(line) - dependsOn = append(dependsOn, s[0]) // store name only for now + // A whitespace-only line carries no name. Guard the append rather + // than `continue`: lineNum below must still count this line, or + // every following package is reported one line early. + if len(s) > 0 { + dependsOn = append(dependsOn, s[0]) // store name only for now + } } lineNum++ diff --git a/pkg/dependency/parser/ruby/bundler/parse_test.go b/pkg/dependency/parser/ruby/bundler/parse_test.go index 6d904e2248..29b53a67f8 100644 --- a/pkg/dependency/parser/ruby/bundler/parse_test.go +++ b/pkg/dependency/parser/ruby/bundler/parse_test.go @@ -214,6 +214,50 @@ var ( }, }, } + BlankDepLinePkgs = []ftypes.Package{ + { + ID: "i18n@1.6.0", + Name: "i18n", + Version: "1.6.0", + Relationship: ftypes.RelationshipDirect, + Locations: []ftypes.Location{ + { + StartLine: 5, + EndLine: 5, + }, + }, + }, + { + ID: "rake@13.0.1", + Name: "rake", + Version: "13.0.1", + Relationship: ftypes.RelationshipDirect, + Locations: []ftypes.Location{ + { + StartLine: 8, + EndLine: 8, + }, + }, + }, + { + ID: "concurrent-ruby@1.1.5", + Name: "concurrent-ruby", + Version: "1.1.5", + Relationship: ftypes.RelationshipIndirect, + Locations: []ftypes.Location{ + { + StartLine: 4, + EndLine: 4, + }, + }, + }, + } + BlankDepLineDeps = []ftypes.Dependency{ + { + ID: "i18n@1.6.0", + DependsOn: []string{"concurrent-ruby@1.1.5"}, + }, + } Bundler2Deps = []ftypes.Dependency{ { ID: "faker@2.21.0", @@ -261,6 +305,15 @@ func TestParser_Parse(t *testing.T) { wantPkgs: []ftypes.Package{}, wantErr: assert.NoError, }, + { + // A whitespace-only line inside the dependency graph names no gem, + // so it is skipped. cf. #10976 + name: "blank dependency line", + file: "testdata/Gemfile_blank_dep_line.lock", + wantPkgs: BlankDepLinePkgs, + wantDeps: BlankDepLineDeps, + wantErr: assert.NoError, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/dependency/parser/ruby/bundler/testdata/Gemfile_blank_dep_line.lock b/pkg/dependency/parser/ruby/bundler/testdata/Gemfile_blank_dep_line.lock new file mode 100644 index 0000000000..ebaa162551 --- /dev/null +++ b/pkg/dependency/parser/ruby/bundler/testdata/Gemfile_blank_dep_line.lock @@ -0,0 +1,18 @@ +GEM + remote: https://rubygems.org/ + specs: + concurrent-ruby (1.1.5) + i18n (1.6.0) + concurrent-ruby (~> 1.0) + + rake (13.0.1) + +PLATFORMS + ruby + +DEPENDENCIES + i18n (~> 1.6) + rake + +BUNDLED WITH + 1.17.2 diff --git a/pkg/dependency/parser/swift/cocoapods/parse.go b/pkg/dependency/parser/swift/cocoapods/parse.go index 48195f66fa..066f3475a0 100644 --- a/pkg/dependency/parser/swift/cocoapods/parse.go +++ b/pkg/dependency/parser/swift/cocoapods/parse.go @@ -67,7 +67,13 @@ func (p *Parser) Parse(_ context.Context, r xio.ReadSeekerAt) ([]ftypes.Package, if !ok { return nil, nil, xerrors.Errorf("must be string: %q", childDep) } - directDeps[pkg.Name] = append(directDeps[pkg.Name], strings.Fields(s)[0]) + fields := strings.Fields(s) + if len(fields) == 0 { + // A child dependency without a name carries no information. + // cf. #10976 + continue + } + directDeps[pkg.Name] = append(directDeps[pkg.Name], fields[0]) } } } diff --git a/pkg/dependency/parser/swift/cocoapods/parse_test.go b/pkg/dependency/parser/swift/cocoapods/parse_test.go index 35dec6076c..ed295059b0 100644 --- a/pkg/dependency/parser/swift/cocoapods/parse_test.go +++ b/pkg/dependency/parser/swift/cocoapods/parse_test.go @@ -78,6 +78,31 @@ func TestParse(t *testing.T) { name: "sad path. wrong dep format", inputFile: "testdata/sad.lock", }, + { + // A child dependency without a name carries no information, so it is + // skipped and the remaining children are still linked. + // cf. #10976 + name: "sad path. blank child dependency", + inputFile: "testdata/empty-child-dep.lock", + wantPkgs: []ftypes.Package{ + { + ID: "AppCenter@4.2.0", + Name: "AppCenter", + Version: "4.2.0", + }, + { + ID: "AppCenter/Core@4.2.0", + Name: "AppCenter/Core", + Version: "4.2.0", + }, + }, + wantDeps: []ftypes.Dependency{ + { + ID: "AppCenter@4.2.0", + DependsOn: []string{"AppCenter/Core@4.2.0"}, + }, + }, + }, } for _, tt := range tests { diff --git a/pkg/dependency/parser/swift/cocoapods/testdata/empty-child-dep.lock b/pkg/dependency/parser/swift/cocoapods/testdata/empty-child-dep.lock new file mode 100644 index 0000000000..1324e3f662 --- /dev/null +++ b/pkg/dependency/parser/swift/cocoapods/testdata/empty-child-dep.lock @@ -0,0 +1,7 @@ +PODS: + - AppCenter (4.2.0): + - " " + - AppCenter/Core (= 4.2.0) + - AppCenter/Core (4.2.0) + +COCOAPODS: 1.11.2 diff --git a/pkg/detector/ospkg/amazon/amazon.go b/pkg/detector/ospkg/amazon/amazon.go index 681cdb672b..0cde9adc5c 100644 --- a/pkg/detector/ospkg/amazon/amazon.go +++ b/pkg/detector/ospkg/amazon/amazon.go @@ -42,12 +42,7 @@ func NewScanner() *Scanner { // Detect scans the packages using amazon scanner func (s *Scanner) Detect(ctx context.Context, osVer string, _ *ftypes.Repository, pkgs []ftypes.Package) ([]types.DetectedVulnerability, error) { - osVer = strings.Fields(osVer)[0] - // The format `2023.xxx.xxxx` can be used. - osVer = osver.Major(osVer) - if osVer != "2" && osVer != "2022" && osVer != "2023" { - osVer = "1" - } + osVer = majorVersion(osVer) log.InfoContext(ctx, "Detecting vulnerabilities...", log.String("os_version", osVer), log.Int("pkg_num", len(pkgs))) @@ -103,12 +98,22 @@ func (s *Scanner) Detect(ctx context.Context, osVer string, _ *ftypes.Repository // IsSupportedVersion checks if the version is supported. func (s *Scanner) IsSupportedVersion(ctx context.Context, osFamily ftypes.OSType, osVer string) bool { - osVer = strings.Fields(osVer)[0] + return osver.Supported(ctx, eolDates, osFamily, majorVersion(osVer)) +} + +// majorVersion returns the Amazon Linux major version. +func majorVersion(osVer string) string { + fields := strings.Fields(osVer) + // No real "/etc/system-release" is version-less, so this is defensive only: + // fall back to Amazon Linux 1, as for any other unknown version below. + // cf. #10976 + if len(fields) == 0 { + return "1" + } // The format `2023.xxx.xxxx` can be used. - osVer = osver.Major(osVer) - if osVer != "2" && osVer != "2022" && osVer != "2023" { - osVer = "1" + major := osver.Major(fields[0]) + if major != "2" && major != "2022" && major != "2023" { + return "1" } - - return osver.Supported(ctx, eolDates, osFamily, osVer) + return major } diff --git a/pkg/detector/ospkg/amazon/amazon_test.go b/pkg/detector/ospkg/amazon/amazon_test.go index f09eaa87ab..f60421b80f 100644 --- a/pkg/detector/ospkg/amazon/amazon_test.go +++ b/pkg/detector/ospkg/amazon/amazon_test.go @@ -151,6 +151,39 @@ func TestScanner_Detect(t *testing.T) { }, }, }, + { + // A version-less "/etc/system-release" falls back to Amazon Linux 1. + // cf. #10976 + name: "version-less system-release", + fixtures: []string{ + "testdata/fixtures/amazon.yaml", + "testdata/fixtures/data-source.yaml", + }, + args: args{ + osVer: "", + pkgs: []ftypes.Package{ + { + Name: "bind", + Epoch: 32, + Version: "9.8.2", + Release: "0.68.rc1.85.amzn1", + }, + }, + }, + want: []types.DetectedVulnerability{ + { + PkgName: "bind", + VulnerabilityID: "CVE-2020-8625", + InstalledVersion: "32:9.8.2-0.68.rc1.85.amzn1", + FixedVersion: "32:9.8.2-0.68.rc1.86.amzn1", + DataSource: &dbTypes.DataSource{ + ID: vulnerability.Amazon, + Name: "Amazon Linux Security Center", + URL: "https://alas.aws.amazon.com/", + }, + }, + }, + }, { name: "Get returns an error", fixtures: []string{ @@ -244,6 +277,27 @@ func TestScanner_IsSupportedVersion(t *testing.T) { }, want: true, }, + { + // A version-less "/etc/system-release" falls back to Amazon Linux 1, + // so the Amazon Linux 1 EOL date applies. + // cf. #10976 + name: "empty version", + now: time.Date(2020, 12, 1, 0, 0, 0, 0, time.UTC), + args: args{ + osFamily: "amazon", + osVer: "", + }, + want: true, + }, + { + name: "empty version after Amazon Linux 1 EOL", + now: time.Date(2024, 5, 31, 23, 59, 59, 0, time.UTC), + args: args{ + osFamily: "amazon", + osVer: "", + }, + want: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {