-
Notifications
You must be signed in to change notification settings - Fork 731
feat: add workflow for blast radius api (CM-1328) #4372
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
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,122 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| import { blastRadiusJobRequestSchema, toBlastRadiusJobEntry } from './blastRadius' | ||
|
|
||
| describe('blastRadiusJobRequestSchema', () => { | ||
| it('accepts a minimal advisory-wide request and defaults force to false', () => { | ||
| const result = blastRadiusJobRequestSchema.parse({ | ||
| advisoryId: 'GHSA-jf85-cpcp-j695', | ||
| ecosystem: 'npm', | ||
| }) | ||
| expect(result).toEqual({ | ||
| advisoryId: 'GHSA-jf85-cpcp-j695', | ||
| ecosystem: 'npm', | ||
| package: undefined, | ||
| force: false, | ||
| }) | ||
| }) | ||
|
|
||
| it('accepts a request scoped to a package with ecosystem and force set', () => { | ||
| const result = blastRadiusJobRequestSchema.parse({ | ||
| advisoryId: 'GHSA-jf85-cpcp-j695', | ||
| ecosystem: 'npm', | ||
| package: 'pkg:npm/lodash', | ||
| force: true, | ||
| }) | ||
| expect(result).toEqual({ | ||
| advisoryId: 'GHSA-jf85-cpcp-j695', | ||
| ecosystem: 'npm', | ||
| package: 'pkg:npm/lodash', | ||
| force: true, | ||
| }) | ||
| }) | ||
|
|
||
| it('accepts explicit null for package (advisory-wide, explicit)', () => { | ||
| const result = blastRadiusJobRequestSchema.parse({ | ||
| advisoryId: 'GHSA-jf85-cpcp-j695', | ||
| ecosystem: 'npm', | ||
| package: null, | ||
| }) | ||
| expect(result.package).toBeNull() | ||
| }) | ||
|
|
||
| it('rejects a missing advisoryId', () => { | ||
| const result = blastRadiusJobRequestSchema.safeParse({ ecosystem: 'npm' }) | ||
| expect(result.success).toBe(false) | ||
| }) | ||
|
|
||
| it('rejects an empty advisoryId', () => { | ||
| const result = blastRadiusJobRequestSchema.safeParse({ | ||
| advisoryId: ' ', | ||
| ecosystem: 'npm', | ||
| }) | ||
| expect(result.success).toBe(false) | ||
| }) | ||
|
|
||
| it('rejects an advisoryId that is not a GHSA or CVE identifier', () => { | ||
| const result = blastRadiusJobRequestSchema.safeParse({ | ||
| advisoryId: 'foo', | ||
| ecosystem: 'npm', | ||
| }) | ||
| expect(result.success).toBe(false) | ||
| }) | ||
|
|
||
| it('accepts a CVE-formatted advisoryId', () => { | ||
| const result = blastRadiusJobRequestSchema.safeParse({ | ||
| advisoryId: 'CVE-2024-12345', | ||
| ecosystem: 'npm', | ||
| }) | ||
| expect(result.success).toBe(true) | ||
| }) | ||
|
|
||
| it('rejects a missing ecosystem', () => { | ||
| const result = blastRadiusJobRequestSchema.safeParse({ advisoryId: 'GHSA-jf85-cpcp-j695' }) | ||
| expect(result.success).toBe(false) | ||
| }) | ||
|
|
||
| it('rejects null ecosystem', () => { | ||
| const result = blastRadiusJobRequestSchema.safeParse({ | ||
| advisoryId: 'GHSA-jf85-cpcp-j695', | ||
| ecosystem: null, | ||
| }) | ||
| expect(result.success).toBe(false) | ||
| }) | ||
|
|
||
| it('rejects an unsupported ecosystem', () => { | ||
| const result = blastRadiusJobRequestSchema.safeParse({ | ||
| advisoryId: 'GHSA-jf85-cpcp-j695', | ||
| ecosystem: 'pypi', | ||
| }) | ||
| expect(result.success).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('toBlastRadiusJobEntry', () => { | ||
| it('builds a pending job entry echoing the request fields', () => { | ||
| const entry = toBlastRadiusJobEntry({ | ||
| analysisId: 'br_01h', | ||
| advisoryId: 'GHSA-jf85-cpcp-j695', | ||
| package: 'pkg:npm/lodash', | ||
| ecosystem: 'npm', | ||
| }) | ||
| expect(entry).toEqual({ | ||
| analysisId: 'br_01h', | ||
| advisoryId: 'GHSA-jf85-cpcp-j695', | ||
| package: 'pkg:npm/lodash', | ||
| ecosystem: 'npm', | ||
| status: 'pending', | ||
| }) | ||
| }) | ||
|
|
||
| it('echoes a null package for an advisory-wide job', () => { | ||
| const entry = toBlastRadiusJobEntry({ | ||
| analysisId: 'br_01h', | ||
| advisoryId: 'GHSA-jf85-cpcp-j695', | ||
| package: null, | ||
| ecosystem: 'npm', | ||
| }) | ||
| expect(entry.package).toBeNull() | ||
| expect(entry.ecosystem).toBe('npm') | ||
| expect(entry.status).toBe('pending') | ||
| }) | ||
| }) |
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,54 @@ | ||
| import { z } from 'zod' | ||
|
|
||
| // The reachability pipeline is npm-only for now — every other ecosystem (including | ||
| // a missing one) is rejected by the schema below before the Temporal workflow is | ||
| // triggered. | ||
| export const SUPPORTED_BLAST_RADIUS_ECOSYSTEMS = ['npm'] as const | ||
|
|
||
| // Always exactly one job per request — advisory-wide (package omitted) or narrowed | ||
| // to a single package. package accepts either a full purl or a bare package name, | ||
| // so it is NOT run through purlFieldSchema/normalizePurl like the other endpoints. | ||
| const ADVISORY_ID_PATTERN = /^(GHSA-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}|CVE-\d{4}-\d{4,})$/ | ||
|
|
||
| export const blastRadiusJobRequestSchema = z.object({ | ||
| advisoryId: z | ||
| .string() | ||
| .trim() | ||
| .regex(ADVISORY_ID_PATTERN, 'advisoryId must be a GHSA or CVE identifier'), | ||
| ecosystem: z.enum(SUPPORTED_BLAST_RADIUS_ECOSYSTEMS, { | ||
| error: `Ecosystem is not supported for blast-radius analysis — only ${SUPPORTED_BLAST_RADIUS_ECOSYSTEMS.join(', ')} supported today`, | ||
|
ulemons marked this conversation as resolved.
|
||
| }), | ||
| package: z.string().trim().min(1).nullish(), | ||
| force: z.boolean().default(false), | ||
| }) | ||
|
|
||
| export type BlastRadiusJobRequest = z.infer<typeof blastRadiusJobRequestSchema> | ||
|
|
||
| export type BlastRadiusJobStatus = 'pending' | 'running' | 'done' | 'failed' | ||
|
|
||
| export type BlastRadiusJobEcosystem = (typeof SUPPORTED_BLAST_RADIUS_ECOSYSTEMS)[number] | ||
|
|
||
| export interface BlastRadiusJobEntry { | ||
| analysisId: string | ||
| advisoryId: string | ||
| package: string | null | ||
| ecosystem: BlastRadiusJobEcosystem | ||
| status: BlastRadiusJobStatus | ||
| } | ||
|
|
||
| // Builds the 2a response body. The pipeline isn't implemented yet, so every freshly | ||
| // submitted job comes back pending — see analyzeBlastRadius in packages_worker. | ||
| export function toBlastRadiusJobEntry(params: { | ||
| analysisId: string | ||
| advisoryId: string | ||
| package: string | null | ||
| ecosystem: BlastRadiusJobEcosystem | ||
| }): BlastRadiusJobEntry { | ||
| return { | ||
| analysisId: params.analysisId, | ||
| advisoryId: params.advisoryId, | ||
| package: params.package, | ||
| ecosystem: params.ecosystem, | ||
| status: 'pending', | ||
| } | ||
| } | ||
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.