Skip to content

apk/installed: guard ParseInstalled against short lines - #2328

Merged
stevebeattie merged 2 commits into
chainguard-dev:mainfrom
arpitjain099:fix/parseinstalled-short-line
Aug 4, 2026
Merged

apk/installed: guard ParseInstalled against short lines#2328
stevebeattie merged 2 commits into
chainguard-dev:mainfrom
arpitjain099:fix/parseinstalled-short-line

Conversation

@arpitjain099

Copy link
Copy Markdown
Contributor

A single-character line in an apk installed database panics ParseInstalled instead of failing cleanly.

The per-line check is if len(line) > 1 && line[1:2] != ":", so a length-1 line short-circuits past the delimiter check, and then val := line[2:] slices out of range and panics with slice bounds out of range [2:1]. This is reachable wherever we parse an installed database: pkg/baseimg.New runs it over a base image's installed file (base_image.go:106), and GetInstalled reads it off the apk filesystem. A corrupt or malformed entry (a stray P) takes down apko rather than returning a parse error.

The sibling APKINDEX parser already guards this with if len(line) < 2. This does the same before slicing, so a short line becomes a normal parse error, and adds a regression test in installed_test.go.

Verified go test ./pkg/apk/apk/...: the new test panics on the old code and passes with the guard, and the rest of the package still passes.

arpitjain099 and others added 2 commits July 16, 2026 12:33
The per-line check used len(line) > 1, so a single-character line
skipped the delimiter check and then line[2:] sliced out of range and
panicked (slice bounds out of range [2:1]) instead of returning a parse
error. This is reachable when parsing an apk installed database, for
example a base image's installed file in pkg/baseimg, so a corrupt or
malformed entry crashes apko rather than failing cleanly.

Guard the length before slicing, matching the len(line) < 2 check the
sibling APKINDEX parser already does, and add a regression test.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>

@stevebeattie stevebeattie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the fix!

@stevebeattie
stevebeattie merged commit 7fe6692 into chainguard-dev:main Aug 4, 2026
25 checks passed
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.

2 participants