Skip to content

fix: guard against index-out-of-range panics on empty fields - #10996

Open
akshita317 wants to merge 1 commit into
aquasecurity:mainfrom
akshita317:fix/parser-panic-empty-fields
Open

fix: guard against index-out-of-range panics on empty fields#10996
akshita317 wants to merge 1 commit into
aquasecurity:mainfrom
akshita317:fix/parser-panic-empty-fields

Conversation

@akshita317

@akshita317 akshita317 commented Jul 25, 2026

Copy link
Copy Markdown

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.Fields returns an empty slice, so the [0] access panics with index 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, or dependencies = [""] in a pyproject.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 }):

File Fix
pkg/detector/ospkg/amazon/amazon.go (Detect) Leave the version empty and fall through to the existing Amazon Linux 1 default
pkg/detector/ospkg/amazon/amazon.go (IsSupportedVersion) Return false (unsupported) when the version string is empty
pkg/dependency/parser/python/pyproject/pyproject.go continue on an empty dependency entry
pkg/dependency/parser/swift/cocoapods/parse.go continue on an empty dependency string
pkg/dependency/parser/hex/mix/parse.go Guard ss[0] when the field slice is empty
pkg/dependency/parser/ruby/bundler/parse.go Wrap s[0] in if len(s) > 0

A note on the amazon detector

Both Detect and IsSupportedVersion index the version string, and ospkg.Detector.Detect calls them in sequence — IsSupportedVersion first, then Detect unconditionally (detect.go#L129-L145). Guarding only IsSupportedVersion therefore leaves the scan panicking one call later, so both are guarded here.

For Detect I kept the empty version and let it fall through to the existing osVer = "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 0 without its guard and passes with it:

Test Input that triggers the empty slice
amazon: TestScanner_Detect/version-less system-release osVer: ""
amazon: TestScanner_IsSupportedVersion/empty version osVer: ""
mix: TestParser_Parse/no fields "bunt": with nothing after the colon
cocoapods: TestParse/sad path. blank child dependency a child dependency of " "
bundler: TestParser_Parse/blank dependency line a whitespace-only line in the dependency graph
pyproject: TestParser_Parse/empty dependency dependencies = ["", "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.
  • gofmt and go vet clean on the affected packages.

The Conda case mentioned in the issue was already handled by #10955, so it is not included here.

@CLAassistant

CLAassistant commented Jul 25, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revisado

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
akshita317 force-pushed the fix/parser-panic-empty-fields branch from 28c3515 to 85eefbd Compare July 26, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: index out of range [0] panic in dependency parsers and the OS detector from unchecked strings.Fields(...)[0]

2 participants