Skip to content

fix: enforce ID validation and source ownership at DB entry boundary - #6461

Merged
another-rex merged 4 commits into
google:masterfrom
another-rex:fix-cross-source-hijack
Dec 2, 2026
Merged

another-rex merged 4 commits into
google:masterfrom
another-rex:fix-cross-source-hijack

Conversation

@another-rex

@another-rex another-rex commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Enforces OSV vulnerability ID validation and SourceRepository db_prefix (IDPrefixes) checks at the database entry boundary (importer and worker.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 of VulnerabilityStore.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:

  • Shared Schema Helpers (go/internal/osvutil/schema, go/internal/importer): Promoted IsValidID (^[a-zA-Z0-9:_.-]+$, rejecting . / .., bounded to MaxIDLength = 256) and HasValidPrefix into osvutil/schema, delegating importer to the shared helpers.
  • Worker Write & Delete Boundary (go/internal/worker): Enforced IsValidID and HasValidPrefix in handleUpdate, checked ref.Source == task.SourceID alongside ref.Path == task.PathInSource in handleDelete, and updated Subscriber to acknowledge permanent ErrInvalidArgument / ErrSourceMismatch failures.
  • Recoverer (go/internal/recoverer): Verified in HandleGCSMissing that the recovered record's ID matches the requested vulnID before publishing an update task to worker.
  • Transactional Store Ownership (go/internal/database/datastore, go/internal/database/postgres, go/internal/models): Added models.ErrSourceMismatch and enforced inside both Datastore (writeBaseEntities) and PostgreSQL (GetVulnerabilitySourceForUpdate within withTx) transactions that an existing record owned by one source cannot be overwritten by another source.

agy

…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-commenter

codecov-commenter commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.17%. Comparing base (20d257f) to head (2e9a441).
⚠️ Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
go/internal/worker/engine.go 76.47% 4 Missing ⚠️
go/internal/recoverer/recoverer.go 75.00% 3 Missing ⚠️
go/internal/worker/subscriber.go 25.00% 3 Missing ⚠️
go/internal/database/postgres/vulnerability.go 80.00% 1 Missing ⚠️
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              
Flag Coverage Δ
go 61.31% <80.00%> (+0.24%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.
@another-rex another-rex changed the title fix: enforce source ID prefixes, ownership, and ID validation across pipeline fix: enforce ID validation and source ownership at DB entry boundary Dec 1, 2026
}

return s.withTx(ctx, id, func(q *Queries) error {
existingSource, err := q.GetVulnerabilitySourceForUpdate(ctx, id)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can probably avoid this extra SELECT, but it'll make the other SQL a bit more complicated, might not be worth it.

@another-rex
another-rex marked this pull request as ready for review December 2, 2026 03:48
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)

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.

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.

@another-rex
another-rex merged commit d61f64c into google:master Dec 2, 2026
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants