Fix ArgumentError in PackageMetadata::GlobalAdvisoryScanWorker for non-default branch contexts
Summary
Production exception occurring in PackageMetadata::GlobalAdvisoryScanWorker when processing SBOM occurrences from pipelines on non-default branches that don't have a tracked context created yet.
Error Details
ArgumentError: Expected context to already exist for non-default branches
Backtrace:
ee/app/services/security/ingestion/tracked_context_finder.rb:22:in `find_or_create_context'
ee/app/services/security/ingestion/tracked_context_finder.rb:9:in `find_or_create_from_pipeline'
ee/lib/gitlab/vulnerability_scanning/advisory_scanner.rb:166:in `tracked_context'
ee/lib/gitlab/vulnerability_scanning/advisory_scanner.rb:102:in `block in bulk_vulnerability_ingestion'
ee/lib/gitlab/vulnerability_scanning/advisory_scanner.rb:84:in `each'
ee/lib/gitlab/vulnerability_scanning/advisory_scanner.rb:84:in `filter_map'
ee/lib/gitlab/vulnerability_scanning/advisory_scanner.rb:84:in `bulk_vulnerability_ingestion'
ee/lib/gitlab/vulnerability_scanning/advisory_scanner.rb:44:in `block (2 levels) in execute'
ee/app/finders/sbom/possibly_affected_occurrences_finder.rb:22:in `block in execute_in_batches'
Root Cause
The Security::ProjectTrackedContexts::FindOrCreateService is designed to only create tracked contexts for default branches. When called with a non-default branch (via from_pipeline), it expects the context to already exist and raises an ArgumentError if it doesn't.
In the advisory scanner flow:
-
AdvisoryScanner#bulk_vulnerability_ingestionprocesses SBOM occurrences - For each occurrence, it calls
tracked_context(affected_component.pipeline)(line 102) - This calls
TrackedContextFinder#find_or_create_from_pipeline(line 166) - Which calls
FindOrCreateService.from_pipeline(pipeline).execute(line 19) - If the pipeline is on a non-default branch and no tracked context exists, the service returns an error
- The error is raised as an
ArgumentErrorat line 22 oftracked_context_finder.rb
Impact
- Production worker failures in
PackageMetadata::GlobalAdvisoryScanWorker - Advisory scanning cannot complete for projects with SBOM occurrences from non-default branches
- Potential security vulnerabilities not being detected and reported for non-default branches
Proposed Solution
The advisory scanner needs to handle cases where tracked contexts don't exist for non-default branches. Options include:
-
Skip occurrences without tracked contexts: Modify
AdvisoryScanner#bulk_vulnerability_ingestionto gracefully handle whentracked_contextreturns nil or fails - Create contexts for non-default branches: Update the logic to allow creating tracked contexts for non-default branches in this flow (may require broader architectural discussion)
- Filter occurrences earlier: Only process SBOM occurrences from pipelines that have tracked contexts
-
Handle the error gracefully: Catch the
ArgumentErrorand log/skip the occurrence rather than failing the entire worker
The solution should consider:
- The intended behavior for vulnerabilities on non-default branches in the advisory scanning context
- Whether the
set_tracked_context_during_ingestionfeature flag affects this behavior - Alignment with the broader "Vulnerabilities Across Multiple Branches" initiative
Related
Edited by 🤖 GitLab Bot 🤖