Repository navigation
fix: enforce ID validation and source ownership at DB entry boundary - #6461
Conversation
…pipeline Promote OSV vulnerability ID and prefix validation (`IsValidID` and `HasValidPrefix`, with a 256-byte length bound) into `osvutil/schema` and enforce source ownership and ID checks across downstream services: - `recoverer`: validate vulnerability IDs in `HandleGCSRetry`, `HandleGCSMissing`, and `HandleGCSGenMismatch`, verify recovered records match the source's `IDPrefixes` and the requested `vulnID`, and acknowledge permanent validation failures without retry loops. - `worker`: validate `IsValidID` and `HasValidPrefix` in `handleUpdate`, verify `ref.Source == task.SourceID` alongside `ref.Path` in `handleDelete`, guard `notifyPyPI`, and acknowledge non-retryable `ErrInvalidArgument` / `ErrSourceMismatch` errors in `Subscriber`. - `datastore` & `postgres` stores: validate `IsValidID` on `Write` and reject cross-source overwrites transactionally with `models.ErrSourceMismatch`. - `exporter` & `gcp/functions/pypi`: skip invalid vulnerability IDs before constructing GCS/ZIP paths or signing PyPI advisories.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #6461 +/- ##
==========================================
+ Coverage 56.99% 57.17% +0.17%
==========================================
Files 225 225
Lines 23494 23528 +34
==========================================
+ Hits 13391 13452 +61
+ Misses 10100 10073 -27
Partials 3 3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Centralize OSV vulnerability ID and prefix validation (`IsValidID` and `HasValidPrefix`, with `MaxIDLength = 256`) in `osvutil/schema` and enforce checks strictly at the database entry boundaries rather than duplicating ID checks across downstream consumers: - `importer` & `worker.handleUpdate`: share `schema.IsValidID` and `schema.HasValidPrefix` at the ingestion/write boundary; acknowledge non-retryable `ErrInvalidArgument` / `ErrSourceMismatch` errors in `worker.Subscriber`. - `worker.handleDelete`: verify `ref.Source == task.SourceID` alongside `ref.Path == task.PathInSource`. - `recoverer.HandleGCSMissing`: verify the recovered record's ID matches the requested `vulnID` before publishing a re-import task to `worker`. - `datastore` & `postgres` stores: reject cross-source overwrites transactionally with `models.ErrSourceMismatch`. - `go/cmd/tools/audit-vuln-ids`: add a read-only Go CLI to audit existing Datastore `Vulnerability` keys/sources and GCS `all/pb/*.pb` objects.
| } | ||
|
|
||
| return s.withTx(ctx, id, func(q *Queries) error { | ||
| existingSource, err := q.GetVulnerabilitySourceForUpdate(ctx, id) |
There was a problem hiding this comment.
We can probably avoid this extra SELECT, but it'll make the other SQL a bit more complicated, might not be worth it.
| if err == nil && dsVuln.SourceID != "" { | ||
| existingSource, _, _ := strings.Cut(dsVuln.SourceID, ":") | ||
| if existingSource != "" && existingSource != req.Source { | ||
| return fmt.Errorf("%w: %s is owned by %q, cannot overwrite from %q", models.ErrSourceMismatch, req.ID, existingSource, req.Source) |
There was a problem hiding this comment.
Trying to imagine a situation where we might want to intentionally do this, e.g. renaming a datasource (or consolidating e.g. the 3 almalinux ones into a single source, or if we accidentally let one datasource publish another's prefix before them).
Probably would get around this by manually editing the database.
Overview
Enforces OSV vulnerability ID validation and
SourceRepositorydb_prefix(IDPrefixes) checks at the database entry boundary (importerandworker.handleUpdate) and adds transactional cross-source ownership guards (ErrSourceMismatch) in the Datastore and PostgreSQL stores.Note: This pull request was created with AI assistance.
Details
Across the codebase,
worker(go/internal/worker/engine.go) is the sole caller ofVulnerabilityStore.Write. Enforcing ID and prefix validation at the ingestion/write boundary ensures invalid IDs cannot enter the database while allowing downstream readers to assume stored IDs are valid:go/internal/osvutil/schema,go/internal/importer): PromotedIsValidID(^[a-zA-Z0-9:_.-]+$, rejecting./.., bounded toMaxIDLength = 256) andHasValidPrefixintoosvutil/schema, delegatingimporterto the shared helpers.go/internal/worker): EnforcedIsValidIDandHasValidPrefixinhandleUpdate, checkedref.Source == task.SourceIDalongsideref.Path == task.PathInSourceinhandleDelete, and updatedSubscriberto acknowledge permanentErrInvalidArgument/ErrSourceMismatchfailures.go/internal/recoverer): Verified inHandleGCSMissingthat the recovered record's ID matches the requestedvulnIDbefore publishing an update task toworker.go/internal/database/datastore,go/internal/database/postgres,go/internal/models): Addedmodels.ErrSourceMismatchand enforced inside both Datastore (writeBaseEntities) and PostgreSQL (GetVulnerabilitySourceForUpdatewithinwithTx) transactions that an existing record owned by one source cannot be overwritten by another source.agy