-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
145 additions
and
134 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,109 @@ | ||
import events from 'node:events' | ||
import https from 'node:https' | ||
import rl from 'node:readline' | ||
|
||
import constants from '../../constants' | ||
import { getPublicToken } from '../sdk' | ||
|
||
import type { SafeNode } from '../../shadow/arborist/lib/node' | ||
|
||
export type InstallEffect = { | ||
pkgid: SafeNode['pkgid'] | ||
repository_url: string | ||
existing?: SafeNode['pkgid'] | undefined | ||
} | ||
|
||
export type SocketScanArtifactAlert = { | ||
key: string | ||
type: string | ||
severity: string | ||
category: string | ||
action?: string | ||
actionPolicyIndex?: number | ||
file?: string | ||
props?: any | ||
start?: number | ||
end?: number | ||
} | ||
|
||
export type SocketScanArtifact = { | ||
type: string | ||
namespace?: string | ||
name?: string | ||
version?: string | ||
subpath?: string | ||
release?: string | ||
id?: string | ||
author?: string[] | ||
license?: string | ||
licenseDetails?: { | ||
spdxDisj: string | ||
provenance: string | ||
filepath: string | ||
match_strength: number | ||
}[] | ||
licenseAttrib?: { | ||
attribText: string | ||
attribData: { | ||
purl: string | ||
foundInFilepath: string | ||
spdxExpr: string | ||
foundAuthors: string[] | ||
}[] | ||
}[] | ||
score?: { | ||
supplyChain: number | ||
quality: number | ||
maintenance: number | ||
vulnerability: number | ||
license: number | ||
overall: number | ||
} | ||
alerts?: SocketScanArtifactAlert[] | ||
size?: number | ||
batchIndex?: number | ||
} | ||
|
||
const { API_V0_URL, abortSignal } = constants | ||
|
||
export async function* batchScan( | ||
pkgIds: string[] | ||
): AsyncGenerator<SocketScanArtifact> { | ||
const req = https | ||
.request(`${API_V0_URL}/purl?alerts=true`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Basic ${Buffer.from(`${getPublicToken()}:`).toString('base64url')}` | ||
}, | ||
signal: abortSignal | ||
}) | ||
.end( | ||
JSON.stringify({ | ||
components: pkgIds.map(id => ({ purl: `pkg:npm/${id}` })) | ||
}) | ||
) | ||
const { 0: res } = await events.once(req, 'response') | ||
const ok = res.statusCode >= 200 && res.statusCode <= 299 | ||
if (!ok) { | ||
throw new Error(`Socket API Error: ${res.statusCode}`) | ||
} | ||
const rli = rl.createInterface(res) | ||
for await (const line of rli) { | ||
yield JSON.parse(line) | ||
} | ||
} | ||
|
||
export function isAlertFixable(alert: SocketScanArtifactAlert): boolean { | ||
return alert.type === 'socketUpgradeAvailable' || isAlertFixableCve(alert) | ||
} | ||
|
||
export function isAlertFixableCve(alert: SocketScanArtifactAlert): boolean { | ||
const { type } = alert | ||
return ( | ||
(type === 'cve' || | ||
type === 'mediumCVE' || | ||
type === 'mildCVE' || | ||
type === 'criticalCVE') && | ||
!!alert.props?.['firstPatchedVersionIdentifier'] | ||
) | ||
} |
This file contains 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