import {z} from 'zod' export type PageSize = number; export type PageCursor = string; export type ExternalFindingSeverity = 'CRITICAL' | 'HIGH' | 'LOW' | 'MEDIUM'; export type GetApiAuditsAuditIdVulnerabilitiesArgs = {auditId: string, query?: string | undefined, pageSize?: PageSize | undefined, pageCursor?: PageCursor | undefined, isDeactivated?: boolean | undefined, externalVulnerabilityId?: string | undefined, isFixAvailable?: boolean | undefined, packageIdentifier?: string | undefined, slaDeadlineAfterDate?: string | undefined, slaDeadlineBeforeDate?: string | undefined, severity?: ExternalFindingSeverity | undefined, integrationId?: string | undefined, includeVulnerabilitiesWithoutSlas?: boolean | undefined, vulnerableAssetId?: string | undefined}; export type GetApiAuditsAuditIdVulnerabilitiesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const vulnerabilityType = z.enum(["CONFIGURATION", "COMMON", "GROUPED"]); export const externalFindingSeverityType = z.enum(["CRITICAL", "HIGH", "LOW", "MEDIUM"]); export const auditorApiVulnerability = z.object({id: z.string(), name: z.string(), description: z.string(), integrationId: z.string(), packageIdentifier: z.string().nullable(), vulnerabilityType: vulnerabilityType, targetId: z.string(), firstDetectedDate: z.string(), sourceDetectedDate: z.string().nullable(), lastDetectedDate: z.string().nullable(), severity: externalFindingSeverityType, cvssSeverityScore: z.number().nullable(), scannerScore: z.number().nullable(), isFixable: z.boolean(), remediateByDate: z.string().nullable(), relatedVulns: z.array(z.string()), relatedUrls: z.array(z.string()), externalURL: z.string(), scanSource: z.string().optional(), deactivateMetadata: z.object({isVulnDeactivatedIndefinitely: z.boolean(), deactivatedUntilDate: z.string().nullable(), deactivationReason: z.string(), deactivatedOnDate: z.string(), deactivatedBy: z.string()}).nullable()}); export const pageInfo = z.object({endCursor: z.string().nullable(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean(), startCursor: z.string().nullable()}); export const paginatedResponseAuditorApiVulnerability = z.object({results: z.object({data: z.array(auditorApiVulnerability), pageInfo: pageInfo})}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List vulnerabilities within the scope of a given audit * * GET /audits/{auditId}/vulnerabilities */ export const getApiAuditsAuditIdVulnerabilities = async (args: GetApiAuditsAuditIdVulnerabilitiesArgs, options?: GetApiAuditsAuditIdVulnerabilitiesOptions) => { const {baseUrl = "https://api.vanta.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageCursor != null) query.append("pageCursor", String(args.pageCursor)) if (args.isDeactivated != null) query.append("isDeactivated", String(args.isDeactivated)) if (args.externalVulnerabilityId != null) query.append("externalVulnerabilityId", String(args.externalVulnerabilityId)) if (args.isFixAvailable != null) query.append("isFixAvailable", String(args.isFixAvailable)) if (args.packageIdentifier != null) query.append("packageIdentifier", String(args.packageIdentifier)) if (args.slaDeadlineAfterDate != null) query.append("slaDeadlineAfterDate", String(args.slaDeadlineAfterDate)) if (args.slaDeadlineBeforeDate != null) query.append("slaDeadlineBeforeDate", String(args.slaDeadlineBeforeDate)) if (args.severity != null) query.append("severity", String(args.severity)) if (args.integrationId != null) query.append("integrationId", String(args.integrationId)) if (args.includeVulnerabilitiesWithoutSlas != null) query.append("includeVulnerabilitiesWithoutSlas", String(args.includeVulnerabilitiesWithoutSlas)) if (args.vulnerableAssetId != null) query.append("vulnerableAssetId", String(args.vulnerableAssetId)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/audits/${encodeURIComponent(String(args.auditId))}/vulnerabilities${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paginatedResponseAuditorApiVulnerability, await res.json()) };