-
Notifications
You must be signed in to change notification settings - Fork 730
fix: dedup advisory_affected_ranges via diff-based upsert (CM-1258) #4366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
themarolt
wants to merge
13
commits into
main
Choose a base branch
from
fix/osv-advisory-dedup-CM-1258
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ec93b03
docs: correct advisory write-semantics and record dedup strategy (CM-…
themarolt d9bd7e7
fix: add deleted_at to advisory_affected_ranges (CM-1258)
themarolt 2c031f9
fix: diff-based upsert + soft-delete for advisory ranges (CM-1258)
themarolt 4c75c2d
fix: filter soft-deleted advisory ranges in Tinybird join (CM-1258)
themarolt 5d3d19e
fix: filter deleted ranges in reads, fix reclaim + updated_at (CM-1258)
themarolt 483d87c
docs: note Tinybird resnapshot required for existing zombie rows (CM-…
themarolt 1978360
fix: block deps.dev raw range insert once OSV owns the package (CM-1258)
themarolt d89f9fd
fix: serialize OSV/deps.dev advisory range writes with row locks (CM-…
themarolt d139e46
fix: revive soft-deleted ranges and fix lock ordering deadlock (CM-1258)
themarolt 96bb2ab
test: add integration coverage for OSV range reconcile/supersede (CM-…
themarolt e294bbc
Merge remote-tracking branch 'origin/main' into fix/osv-advisory-dedu…
themarolt 4e01e11
fix: reconcile OSV-dropped packages as range removals (CM-1258)
themarolt a9b7a98
fix: compare updated_at as string in reconcile integration test (CM-1…
themarolt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
backend/src/osspckgs/migrations/V1784567585__advisory_affected_ranges_soft_delete.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ALTER TABLE advisory_affected_ranges ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ; | ||
|
|
||
| CREATE INDEX IF NOT EXISTS advisory_affected_ranges_live_idx | ||
| ON advisory_affected_ranges (advisory_package_id) | ||
| WHERE deleted_at IS NULL; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
services/apps/packages_worker/src/osv/__tests__/reconcileOsvRanges.integration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| import { afterAll, beforeAll, describe, expect, it } from 'vitest' | ||
|
|
||
| import { | ||
| reconcileOsvRanges, | ||
| supersedeDepsDevRanges, | ||
| } from '@crowd/data-access-layer/src/packages/osv' | ||
| import { QueryExecutor, pgpQx } from '@crowd/data-access-layer/src/queryExecutor' | ||
| import { getDbConnection } from '@crowd/database' | ||
|
|
||
| // Integration test: hits the running packages-db. Skipped automatically when | ||
| // any of the DB env vars are missing, matching deriveCriticalFlag.integration.test.ts. | ||
| const HAVE_DB = | ||
| !!process.env.CROWD_PACKAGES_DB_WRITE_HOST && | ||
| !!process.env.CROWD_PACKAGES_DB_PORT && | ||
| !!process.env.CROWD_PACKAGES_DB_USERNAME && | ||
| !!process.env.CROWD_PACKAGES_DB_DATABASE && | ||
| !!process.env.CROWD_PACKAGES_DB_PASSWORD | ||
|
|
||
| const FIXTURE_OSV_ID = 'osv-test-fixture-reconcile' | ||
|
|
||
| interface RangeRow { | ||
| introduced_version: string | null | ||
| fixed_version: string | null | ||
| last_affected: string | null | ||
| range_raw: string | null | ||
| unaffected_raw: string | null | ||
| deleted_at: string | null | ||
| updated_at: string | ||
| } | ||
|
|
||
| async function cleanupFixture(qx: QueryExecutor): Promise<void> { | ||
| await qx.result( | ||
| ` | ||
| DELETE FROM advisory_affected_ranges | ||
| WHERE advisory_package_id IN ( | ||
| SELECT ap.id FROM advisory_packages ap | ||
| JOIN advisories a ON a.id = ap.advisory_id | ||
| WHERE a.osv_id = $(osvId) | ||
| ) | ||
| `, | ||
| { osvId: FIXTURE_OSV_ID }, | ||
| ) | ||
| await qx.result( | ||
| `DELETE FROM advisory_packages WHERE advisory_id IN (SELECT id FROM advisories WHERE osv_id = $(osvId))`, | ||
| { osvId: FIXTURE_OSV_ID }, | ||
| ) | ||
| await qx.result(`DELETE FROM advisories WHERE osv_id = $(osvId)`, { osvId: FIXTURE_OSV_ID }) | ||
| } | ||
|
|
||
| async function liveRanges(qx: QueryExecutor, advisoryPackageId: number): Promise<RangeRow[]> { | ||
| return qx.select( | ||
| ` | ||
| SELECT introduced_version, fixed_version, last_affected, range_raw, unaffected_raw, deleted_at, updated_at | ||
| FROM advisory_affected_ranges | ||
| WHERE advisory_package_id = $(advisoryPackageId) | ||
| ORDER BY id | ||
| `, | ||
| { advisoryPackageId }, | ||
| ) | ||
| } | ||
|
|
||
| describe.skipIf(!HAVE_DB)('reconcileOsvRanges / supersedeDepsDevRanges — real packages-db', () => { | ||
| let qx: QueryExecutor | ||
| let advisoryPackageId: number | ||
|
|
||
| beforeAll(async () => { | ||
| const conn = await getDbConnection({ | ||
| host: process.env.CROWD_PACKAGES_DB_WRITE_HOST ?? '', | ||
| port: parseInt(process.env.CROWD_PACKAGES_DB_PORT ?? '0', 10), | ||
| database: process.env.CROWD_PACKAGES_DB_DATABASE ?? '', | ||
| user: process.env.CROWD_PACKAGES_DB_USERNAME ?? '', | ||
| password: process.env.CROWD_PACKAGES_DB_PASSWORD ?? '', | ||
| }) | ||
| qx = pgpQx(conn) | ||
| await cleanupFixture(qx) | ||
|
|
||
| const advisory = await qx.selectOne( | ||
| ` | ||
| INSERT INTO advisories (osv_id, source, source_url, aliases, severity, cvss, cvss_source, created_at, updated_at) | ||
| VALUES ($(osvId), 'GHSA', NULL, ARRAY[]::text[], 'HIGH', 7.5, 'osv_cvss_v3', NOW(), NOW()) | ||
| RETURNING id | ||
| `, | ||
| { osvId: FIXTURE_OSV_ID }, | ||
| ) | ||
| const advisoryPackage = await qx.selectOne( | ||
| ` | ||
| INSERT INTO advisory_packages (advisory_id, package_id, ecosystem, package_name, created_at, updated_at) | ||
| VALUES ($(advisoryId), NULL, 'npm', 'reconcile-fixture-pkg', NOW(), NOW()) | ||
| RETURNING id | ||
| `, | ||
| { advisoryId: advisory.id }, | ||
| ) | ||
| advisoryPackageId = advisoryPackage.id as number | ||
| }, 30_000) | ||
|
|
||
| afterAll(async () => { | ||
| if (qx) await cleanupFixture(qx) | ||
| }) | ||
|
|
||
| it('inserts the initial OSV range set as live rows', async () => { | ||
| await reconcileOsvRanges(qx, advisoryPackageId, [ | ||
| { | ||
| advisoryPackageId, | ||
| introducedVersion: '1.0.0', | ||
| fixedVersion: '1.2.0', | ||
| lastAffected: null, | ||
| }, | ||
| { | ||
| advisoryPackageId, | ||
| introducedVersion: '2.0.0', | ||
| fixedVersion: null, | ||
| lastAffected: '2.5.0', | ||
| }, | ||
| ]) | ||
|
|
||
| const rows = await liveRanges(qx, advisoryPackageId) | ||
| expect(rows).toHaveLength(2) | ||
| expect(rows.every((r) => r.deleted_at === null)).toBe(true) | ||
| }) | ||
|
|
||
| it('leaves an unchanged tuple untouched on a no-op resync (no updated_at bump)', async () => { | ||
| const before = await liveRanges(qx, advisoryPackageId) | ||
| const beforeUpdatedAt = before.find((r) => r.introduced_version === '1.0.0')?.updated_at | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 1100)) | ||
| await reconcileOsvRanges(qx, advisoryPackageId, [ | ||
| { | ||
| advisoryPackageId, | ||
| introducedVersion: '1.0.0', | ||
| fixedVersion: '1.2.0', | ||
| lastAffected: null, | ||
| }, | ||
| { | ||
| advisoryPackageId, | ||
| introducedVersion: '2.0.0', | ||
| fixedVersion: null, | ||
| lastAffected: '2.5.0', | ||
| }, | ||
| ]) | ||
|
|
||
| const after = await liveRanges(qx, advisoryPackageId) | ||
| const afterUpdatedAt = after.find((r) => r.introduced_version === '1.0.0')?.updated_at | ||
| expect(afterUpdatedAt).toBe(beforeUpdatedAt) | ||
| }) | ||
|
|
||
| it('soft-deletes a stale tuple dropped from the new range set', async () => { | ||
| await reconcileOsvRanges(qx, advisoryPackageId, [ | ||
| { | ||
| advisoryPackageId, | ||
| introducedVersion: '1.0.0', | ||
| fixedVersion: '1.2.0', | ||
| lastAffected: null, | ||
| }, | ||
| // '2.0.0'..'2.5.0' range dropped — OSV no longer reports it. | ||
| ]) | ||
|
|
||
| const rows = await qx.select( | ||
| ` | ||
| SELECT introduced_version, deleted_at | ||
| FROM advisory_affected_ranges | ||
| WHERE advisory_package_id = $(advisoryPackageId) AND introduced_version = '2.0.0' | ||
| `, | ||
| { advisoryPackageId }, | ||
| ) | ||
| expect(rows).toHaveLength(1) | ||
| expect(rows[0].deleted_at).not.toBeNull() | ||
| }) | ||
|
|
||
| it('revives a tombstoned tuple that reappears in a later sync', async () => { | ||
| await reconcileOsvRanges(qx, advisoryPackageId, [ | ||
| { | ||
| advisoryPackageId, | ||
| introducedVersion: '1.0.0', | ||
| fixedVersion: '1.2.0', | ||
| lastAffected: null, | ||
| }, | ||
| { | ||
| advisoryPackageId, | ||
| introducedVersion: '2.0.0', | ||
| fixedVersion: null, | ||
| lastAffected: '2.5.0', | ||
| }, | ||
| ]) | ||
|
|
||
| const rows = await qx.select( | ||
| ` | ||
| SELECT deleted_at | ||
| FROM advisory_affected_ranges | ||
| WHERE advisory_package_id = $(advisoryPackageId) AND introduced_version = '2.0.0' | ||
| `, | ||
| { advisoryPackageId }, | ||
| ) | ||
| expect(rows).toHaveLength(1) | ||
| expect(rows[0].deleted_at).toBeNull() | ||
| }) | ||
|
|
||
| it('supersedes a live deps.dev raw row once OSV owns the package', async () => { | ||
| await qx.result( | ||
| ` | ||
| INSERT INTO advisory_affected_ranges | ||
| (advisory_package_id, range_raw, unaffected_raw, introduced_version, created_at, updated_at) | ||
| VALUES ($(advisoryPackageId), '>=1.0.0 <1.2.0', NULL, NULL, NOW(), NOW()) | ||
| `, | ||
| { advisoryPackageId }, | ||
| ) | ||
|
|
||
| await supersedeDepsDevRanges(qx, advisoryPackageId) | ||
|
|
||
| const rows = await qx.select( | ||
| ` | ||
| SELECT deleted_at | ||
| FROM advisory_affected_ranges | ||
| WHERE advisory_package_id = $(advisoryPackageId) AND range_raw = '>=1.0.0 <1.2.0' | ||
| `, | ||
| { advisoryPackageId }, | ||
| ) | ||
| expect(rows).toHaveLength(1) | ||
| expect(rows[0].deleted_at).not.toBeNull() | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.