fix: guard against index-out-of-range panics on empty fields - #10996
Open
akshita317 wants to merge 1 commit into
Open
fix: guard against index-out-of-range panics on empty fields#10996akshita317 wants to merge 1 commit into
akshita317 wants to merge 1 commit into
Conversation
|
|
Several dependency parsers and the Amazon OS detector index strings.Fields(...)[0] (or a field slice) without checking the slice is non-empty. When a string reduces to only whitespace (e.g. dependencies = [""] in pyproject.toml, or a version-less /etc/system-release), strings.Fields returns an empty slice and the [0] access panics, crashing the entire scan. Add length guards before indexing and skip malformed entries gracefully, following the existing pattern in bundler/parse.go. Covers amazon, pyproject, cocoapods, mix, and bundler. The amazon detector indexes the version in both Detect and IsSupportedVersion, and ospkg.Detector calls Detect unconditionally after IsSupportedVersion, so guarding only the latter still left the scan panicking one call later. Detect now leaves an empty version untouched and falls through to the existing Amazon Linux 1 default, matching how any other unrecognized version string is treated today. Add a regression test for each of the five guards; every one panics without its guard in place. Fixes aquasecurity#10976 Signed-off-by: Akshita <110122283+akshita317@users.noreply.github.com>
akshita317
force-pushed
the
fix/parser-panic-empty-fields
branch
from
July 26, 2026 17:39
28c3515 to
85eefbd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #10976
Several dependency parsers and the Amazon OS detector call
strings.Fields(...)[0](or index a field slice) without checking that the slice is non-empty. When the input string reduces to only whitespace after normalization,strings.Fieldsreturns an empty slice, so the[0]access panics withindex out of range [0] with length 0— crashing the entire scan. Some of these are externally triggerable (e.g. a version-less/etc/system-release, ordependencies = [""]in apyproject.toml).Changes
Added a length guard before indexing at each location, skipping malformed entries gracefully — following the pattern already used in
bundler/parse.go(if len(ss) == 0 { continue }):pkg/detector/ospkg/amazon/amazon.go(Detect)pkg/detector/ospkg/amazon/amazon.go(IsSupportedVersion)false(unsupported) when the version string is emptypkg/dependency/parser/python/pyproject/pyproject.gocontinueon an empty dependency entrypkg/dependency/parser/swift/cocoapods/parse.gocontinueon an empty dependency stringpkg/dependency/parser/hex/mix/parse.goss[0]when the field slice is emptypkg/dependency/parser/ruby/bundler/parse.gos[0]inif len(s) > 0A note on the amazon detector
Both
DetectandIsSupportedVersionindex the version string, andospkg.Detector.Detectcalls them in sequence —IsSupportedVersionfirst, thenDetectunconditionally (detect.go#L129-L145). Guarding onlyIsSupportedVersiontherefore leaves the scan panicking one call later, so both are guarded here.For
DetectI kept the empty version and let it fall through to the existingosVer = "1"fallback, which is how any other unrecognized version string is already treated. Happy to return early with no vulnerabilities instead if you'd prefer that an unknown version not be matched against Amazon Linux 1 advisories — it's a behavior change either way, so I went with the option consistent with the current code.Testing
Added a regression test per guard. Each one panics with
index out of range [0] with length 0without its guard and passes with it:amazon:TestScanner_Detect/version-less system-releaseosVer: ""amazon:TestScanner_IsSupportedVersion/empty versionosVer: ""mix:TestParser_Parse/no fields"bunt":with nothing after the coloncocoapods:TestParse/sad path. blank child dependency" "bundler:TestParser_Parse/blank dependency linepyproject:TestParser_Parse/empty dependencydependencies = ["", "flask == 1.1.4"]go test ./pkg/detector/ospkg/... ./pkg/dependency/parser/hex/mix/... ./pkg/dependency/parser/python/pyproject/... ./pkg/dependency/parser/swift/cocoapods/... ./pkg/dependency/parser/ruby/bundler/...— all pass.gofmtandgo vetclean on the affected packages.The Conda case mentioned in the issue was already handled by #10955, so it is not included here.