-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor audit-utils and add bulk advisory fallback (async) #340
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
15 commits
Select commit
Hold shift + click to select a range
7043b28
feat(security): implement bulk advisory audit fallback for deprecated…
leynos c4da648
refactor(security): improve dependency tree walking and bulk advisory…
leynos 172c334
fix(security): preserve advisory ID casing and reject blank bulk audi…
leynos 6550eaf
chore: archive backup for session e3ff12f2
d96bd63
Refine audit utils helpers
leynos fa72f9f
Extract dependency section walker
leynos 03771bb
Reduce audit utils complexity
leynos 1a6f8ac
Harden bulk audit advisory parsing
leynos 81aabee
Align audit utils normalize spelling
leynos aab818f
Extract bulk audit request helpers
leynos d5e0ecb
Harden bulk advisory input validation
leynos ed5ee0a
Harden audit payload validation
leynos 2aa1769
Extract audit utility predicates
leynos b01c602
Reuse advisory object guard
leynos a755235
Normalize extracted advisory IDs
leynos 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,252 @@ | ||
| /** @file Tests the shared audit helper, including the bulk advisory fallback. */ | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const execFileSyncMock = vi.fn(); | ||
| const spawnSyncMock = vi.fn(); | ||
| const githubAdvisoryIdKey = 'github_advisory_id'; | ||
| const packageNameKey = 'package_name'; | ||
|
|
||
| vi.mock('node:child_process', () => ({ | ||
| execFileSync: execFileSyncMock, | ||
| spawnSync: spawnSyncMock, | ||
| })); | ||
|
|
||
| const originalFetch = globalThis.fetch; | ||
|
|
||
| /** | ||
| * Create a pnpm-like child-process result for audit command tests. | ||
| * @param {{ status?: number, stdout?: string, error?: Error | undefined }} [options={}] Result overrides. | ||
| * @param {number} [options.status=0] Process exit status. | ||
| * @param {string} [options.stdout=''] Command stdout payload. | ||
| * @param {Error | undefined} [options.error=undefined] Spawn error to surface. | ||
| * @returns {{ error: Error | undefined, status: number, stdout: string }} Mocked pnpm result object. | ||
| */ | ||
| function createPnpmResult({ status = 0, stdout = '', error = undefined } = {}) { | ||
| return { error, status, stdout }; | ||
| } | ||
|
|
||
| /** | ||
| * Configure the retired-endpoint pnpm audit flow for fallback tests. | ||
| * @param {unknown[]} [lsPayload=[{ name: 'frontend-pwa', dependencies: {} }]] Parsed `pnpm ls` payload for the second mock result. | ||
| * @returns {void} | ||
| */ | ||
| function setupRetiredPnpmAudit(lsPayload = [{ name: 'frontend-pwa', dependencies: {} }]) { | ||
| spawnSyncMock | ||
| .mockReturnValueOnce( | ||
| createPnpmResult({ | ||
| status: 1, | ||
| stdout: JSON.stringify({ | ||
| error: { | ||
| code: 'ERR_PNPM_AUDIT_BAD_RESPONSE', | ||
| message: | ||
| 'The audit endpoint responded with 410: {"error":"This endpoint is being retired. Use the bulk advisory endpoint instead."}', | ||
| }, | ||
| }), | ||
| }), | ||
| ) | ||
| .mockReturnValueOnce( | ||
| createPnpmResult({ | ||
| stdout: JSON.stringify(lsPayload), | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Dynamically import the shared audit utility module under test. | ||
| * @returns {Promise<typeof import('../../security/audit-utils.js')>} Imported audit utility module. | ||
| */ | ||
| async function loadAuditUtils() { | ||
| const module = await import('../../security/audit-utils.js'); | ||
| return module; | ||
| } | ||
|
|
||
| describe('runAuditJson', () => { | ||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| vi.clearAllMocks(); | ||
| globalThis.fetch = vi.fn(); | ||
| vi.unstubAllEnvs(); | ||
| vi.stubEnv('npm_config_registry', ''); | ||
| vi.stubEnv('NPM_CONFIG_REGISTRY', ''); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| globalThis.fetch = originalFetch; | ||
| }); | ||
|
|
||
| it('returns pnpm audit output when the native command succeeds', async () => { | ||
| spawnSyncMock.mockReturnValueOnce( | ||
| createPnpmResult({ | ||
| status: 1, | ||
| stdout: JSON.stringify({ | ||
| advisories: { | ||
| validator: { | ||
| [githubAdvisoryIdKey]: 'GHSA-vghf-hv5q-vc2g', | ||
| title: 'Validator SSRF', | ||
| }, | ||
| }, | ||
| }), | ||
| }), | ||
| ); | ||
| const { runAuditJson } = await loadAuditUtils(); | ||
|
|
||
| const result = await runAuditJson(); | ||
|
|
||
| expect(result).toEqual({ | ||
| json: { | ||
| advisories: { | ||
| validator: { | ||
| [githubAdvisoryIdKey]: 'GHSA-vghf-hv5q-vc2g', | ||
| title: 'Validator SSRF', | ||
| }, | ||
| }, | ||
| }, | ||
| status: 1, | ||
| }); | ||
| expect(fetch).not.toHaveBeenCalled(); | ||
| expect(execFileSyncMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('falls back to the bulk advisory endpoint when pnpm audit hits the retired endpoint', async () => { | ||
| setupRetiredPnpmAudit([ | ||
| { | ||
| name: 'frontend-pwa', | ||
| path: '/tmp/frontend-pwa', | ||
| dependencies: { | ||
| '@app/types': { | ||
| version: 'link:../packages/types', | ||
| }, | ||
| validator: { | ||
| version: '13.15.23', | ||
| dependencies: { | ||
| nanoid: { | ||
| version: '3.3.11', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ]); | ||
| execFileSyncMock.mockReturnValueOnce('https://registry.npmjs.org/\n'); | ||
| fetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| status: 200, | ||
| statusText: 'OK', | ||
| text: async () => | ||
| JSON.stringify({ | ||
| validator: [ | ||
| { | ||
| id: 100000, | ||
| url: 'https://github.com/advisories/GHSA-vghf-hv5q-vc2g', | ||
| title: 'Validator SSRF', | ||
| severity: 'high', | ||
| }, | ||
| ], | ||
| nanoid: [], | ||
| }), | ||
| }); | ||
| const { runAuditJson } = await loadAuditUtils(); | ||
|
|
||
| const result = await runAuditJson(); | ||
|
|
||
| expect(spawnSyncMock).toHaveBeenNthCalledWith( | ||
| 1, | ||
| 'pnpm', | ||
| ['audit', '--json'], | ||
| expect.objectContaining({ encoding: 'utf8' }), | ||
| ); | ||
| expect(spawnSyncMock).toHaveBeenNthCalledWith( | ||
| 2, | ||
| 'pnpm', | ||
| ['ls', '--json', '--depth', 'Infinity'], | ||
| expect.objectContaining({ encoding: 'utf8' }), | ||
| ); | ||
| expect(execFileSyncMock).toHaveBeenCalledWith( | ||
| 'pnpm', | ||
| ['config', 'get', 'registry'], | ||
| expect.objectContaining({ encoding: 'utf8' }), | ||
| ); | ||
| expect(String(fetch.mock.calls[0][0])).toBe( | ||
| 'https://registry.npmjs.org/-/npm/v1/security/advisories/bulk', | ||
| ); | ||
| expect(JSON.parse(fetch.mock.calls[0][1].body)).toEqual({ | ||
| nanoid: ['3.3.11'], | ||
| validator: ['13.15.23'], | ||
| }); | ||
| expect(result).toEqual({ | ||
| json: { | ||
| advisories: { | ||
| 'GHSA-vghf-hv5q-vc2g': { | ||
| [githubAdvisoryIdKey]: 'GHSA-vghf-hv5q-vc2g', | ||
| id: 100000, | ||
| [packageNameKey]: 'validator', | ||
| severity: 'high', | ||
| title: 'Validator SSRF', | ||
| url: 'https://github.com/advisories/GHSA-vghf-hv5q-vc2g', | ||
| }, | ||
| }, | ||
| }, | ||
| status: 1, | ||
| }); | ||
| }); | ||
|
|
||
| it('throws a clear error when the bulk advisory endpoint fails', async () => { | ||
| setupRetiredPnpmAudit(); | ||
| fetch.mockResolvedValueOnce({ | ||
| ok: false, | ||
| status: 503, | ||
| statusText: 'Service Unavailable', | ||
| text: async () => '{"error":"upstream unavailable"}', | ||
| }); | ||
| const { runAuditJson } = await loadAuditUtils(); | ||
|
|
||
| await expect(runAuditJson()).rejects.toThrow( | ||
| 'Bulk advisory audit failed (503 Service Unavailable)', | ||
| ); | ||
| }); | ||
|
|
||
| it('normalizes advisory IDs from the bulk payload URL to lowercase groups', async () => { | ||
| setupRetiredPnpmAudit(); | ||
| fetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| status: 200, | ||
| statusText: 'OK', | ||
| text: async () => | ||
| JSON.stringify({ | ||
| validator: [ | ||
| { | ||
| id: 100000, | ||
| url: 'https://github.com/advisories/GHSA-Vghf-HV5Q-vC2G', | ||
| title: 'Validator SSRF', | ||
| }, | ||
| ], | ||
| }), | ||
| }); | ||
| const { runAuditJson } = await loadAuditUtils(); | ||
|
|
||
| const result = await runAuditJson(); | ||
|
|
||
| expect(result.json.advisories).toEqual({ | ||
| 'GHSA-vghf-hv5q-vc2g': expect.objectContaining({ | ||
| [githubAdvisoryIdKey]: 'GHSA-vghf-hv5q-vc2g', | ||
| [packageNameKey]: 'validator', | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| it('rejects blank bulk advisory responses instead of treating them as empty JSON', async () => { | ||
| setupRetiredPnpmAudit(); | ||
| fetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| status: 200, | ||
| statusText: 'OK', | ||
| text: async () => ' ', | ||
| }); | ||
| const { runAuditJson } = await loadAuditUtils(); | ||
|
|
||
| await expect(runAuditJson()).rejects.toThrow( | ||
| 'Failed to parse bulk advisory audit JSON: response body was empty.', | ||
| ); | ||
| }); | ||
| }); | ||
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
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.