Skip to content

fix(helpers): prevent panic in AllPkgTreeToPurl on empty namespaces or names - #3292

Closed
vishnums2k5 wants to merge 1 commit into
guacsec:mainfrom
vishnums2k5:fix/allpkgtree-bounds-check
Closed

vishnums2k5 wants to merge 1 commit into
guacsec:mainfrom
vishnums2k5:fix/allpkgtree-bounds-check

Conversation

@vishnums2k5

Copy link
Copy Markdown

Description of the PR

AllPkgTreeToPurl unconditionally indexed v.Namespaces[0] and
ns.Names[0]. When the graph returns a package tree with no namespaces
or no names (e.g. a type-level node from a sparse query), the function
panicked with an index-out-of-range error, crashing any guacrest
endpoint that calls it.

This PR adds nil and empty-slice guards so the function returns an empty
string instead of panicking, and simplifies the version lookup to use
the already-bound name variable rather than re-indexing from the root.

Three new table-driven test cases cover: nil input, empty Namespaces
slice, and a namespace with an empty Names slice.

PR Checklist

  • All commits have a Developer Certificate of Origin (DCO) -- they are generated using -s flag to git commit.
  • All new changes are covered by tests
  • If GraphQL schema is changed, make generate has been run
  • If GraphQL schema is changed, GraphQL client updates/additions have been made
  • If OpenAPI spec is changed, make generate has been run
  • If ent schema is changed, make generate has been run
  • If collectsub protobuf has been changed, make proto has been run
  • All CI checks are passing (tests and formatting)
  • All dependent PRs have already been merged

…r names

AllPkgTreeToPurl unconditionally indexed v.Namespaces[0] and
ns.Names[0]. When the graph returns a package tree with no
namespaces or no names (e.g. a type-level node from a sparse
query), the function panicked with an index-out-of-range error,
crashing the guacrest endpoints that call it.

Add nil and empty-slice guards so the function returns an empty
string instead of panicking. Also simplify the version lookup
to use the already-bound name variable instead of re-indexing
from the root.

Signed-off-by: vishnums2k5 <vishnusajeev2005@gmail.com>
@vishnums2k5
vishnums2k5 force-pushed the fix/allpkgtree-bounds-check branch from 2024934 to 103925a Compare October 24, 2026 11:34

@mihaimaruseac mihaimaruseac 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.

This doesn't look like a scenario that happens in practice

@vishnums2k5

Copy link
Copy Markdown
Author

This doesn't look like a scenario that happens in practice

Thank you for the review, mihaimaruseac.

You're right that the current callers (e.g. pkgVersionIDs) filter to
PackageVersion nodes before calling Nodes, so in practice the trie is
always fully populated when AllPkgTreeToPurl is reached.

I also noticed that retrieveSboms.go already guards the call site
(len(Namespaces) > 0 && len(Names) > 0) before invoking
AllPkgTreeToPurl, which makes the function-level guard redundant for
that caller.

Would it be more useful to close this PR and instead:

  1. Add a guard in retrieveDependencies.go:360 where
    Namespaces[0].Names[0].Versions[0].Id is accessed directly after
    the AllPkgTreeToPurl call (that line has no nil/bounds check and
    would panic under the same conditions), and

  2. Add a guard in retrieveVulns.go:123 which calls AllPkgTreeToPurl
    with no preceding bounds check?

Happy to rework the PR in that direction if that would be more
reviewable. Alternatively, I can just close this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants