1.0.37
This commit is contained in:
101 files changed
+5097
-642
No files matched your search
@@ -113,6 +113,7 @@ export const POST: RequestHandler = async (event) => {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const totalVulns = results.reduce((sum, r) => sum + r.vulnerabilities.length, 0);
|
||||
sendData({
|
||||
status: 'scan-complete',
|
||||
|
||||
@@ -1,30 +1,10 @@
|
||||
import { json, type RequestHandler } from '@sveltejs/kit';
|
||||
import { scanImage, type ScanProgress, type ScanResult } from '$lib/server/scanner';
|
||||
import { scanImage, scanResultToDbFormat, type ScanProgress } from '$lib/server/scanner';
|
||||
import { saveVulnerabilityScan, getLatestScanForImage } from '$lib/server/db';
|
||||
import { inspectImage } from '$lib/server/docker';
|
||||
import { authorize } from '$lib/server/authorize';
|
||||
import { createJobResponse } from '$lib/server/sse';
|
||||
|
||||
// Helper to convert ScanResult to database format
|
||||
function scanResultToDbFormat(result: ScanResult, envId?: number) {
|
||||
return {
|
||||
environmentId: envId ?? null,
|
||||
imageId: result.imageId || result.imageName, // Fallback to imageName if imageId is undefined
|
||||
imageName: result.imageName,
|
||||
scanner: result.scanner,
|
||||
scannedAt: result.scannedAt,
|
||||
scanDuration: result.scanDuration,
|
||||
criticalCount: result.summary.critical,
|
||||
highCount: result.summary.high,
|
||||
mediumCount: result.summary.medium,
|
||||
lowCount: result.summary.low,
|
||||
negligibleCount: result.summary.negligible,
|
||||
unknownCount: result.summary.unknown,
|
||||
vulnerabilities: JSON.stringify(result.vulnerabilities),
|
||||
error: result.error ?? null
|
||||
};
|
||||
}
|
||||
|
||||
// POST - Start a scan (returns { jobId } for progress polling, or synchronous JSON for Accept: application/json)
|
||||
export const POST: RequestHandler = async ({ request, url, cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Per-image vulnerability export (#415): reformats the cached scan for one image
|
||||
* as json | csv | sarif for CI / DefectDojo / Dependency-Track integration.
|
||||
* Read-only over persisted scans; no new scanning. Auth via cookie or Bearer
|
||||
* token (CI), with RBAC + enterprise environment scoping.
|
||||
*/
|
||||
import { getScansForImage, getEnvironment } from '$lib/server/db';
|
||||
import { flattenScansToFindings } from '$lib/server/vulnerabilities';
|
||||
import { authorizeVulnAccess } from '$lib/server/vuln-access';
|
||||
import { rowsToCSV } from '$lib/server/csv';
|
||||
import { exportResponse, jsonError, slugify } from '$lib/server/export-response';
|
||||
import type { Finding } from '$lib/utils/vulnerability';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
function toCSV(findings: Finding[]): string {
|
||||
const headers = ['CVE', 'Severity', 'Package', 'Installed Version', 'Fixed Version', 'Image', 'Scanned', 'Description', 'Link'];
|
||||
const rows = findings.map((f) => [
|
||||
f.cve, f.severity, f.package, f.installedVersion, f.fixedVersion, f.imageName, f.scannedAt, f.description, f.link
|
||||
]);
|
||||
return rowsToCSV(headers, rows);
|
||||
}
|
||||
|
||||
export const GET: RequestHandler = async ({ url, cookies }) => {
|
||||
const imageId = url.searchParams.get('imageId') || url.searchParams.get('image');
|
||||
if (!imageId) return jsonError('imageId is required', 400);
|
||||
|
||||
// RBAC (images:view) + enterprise environment scoping, shared with the other
|
||||
// vuln endpoints. Auth via cookie or Bearer token is handled inside authorize().
|
||||
const { envIdNum, denied, authEnabled } = await authorizeVulnAccess(cookies, url);
|
||||
if (denied) return jsonError(denied.message, denied.status);
|
||||
|
||||
// The same image SHA can be scanned in multiple environments. When auth is on,
|
||||
// require an explicit env so the tenant-scoping check in authorizeVulnAccess
|
||||
// actually runs — otherwise a user scoped to env A could read env B's scans by
|
||||
// omitting env (CWE-639). Free/open mode has no tenant boundary to enforce.
|
||||
if (authEnabled && envIdNum === undefined) {
|
||||
return jsonError('env is required', 400);
|
||||
}
|
||||
|
||||
try {
|
||||
// Always scope the query by env so cross-environment scans are never returned,
|
||||
// even if an access check were somehow bypassed (defense in depth).
|
||||
const scans = await getScansForImage(imageId, envIdNum);
|
||||
const findings = flattenScansToFindings(scans);
|
||||
|
||||
const timestamp = new Date().toISOString().split('T')[0];
|
||||
const imageSlug = findings[0]?.imageName
|
||||
? slugify(findings[0].imageName, 'image')
|
||||
: slugify(imageId.replace('sha256:', '').slice(0, 12), 'image');
|
||||
|
||||
let envSlug = '';
|
||||
if (envIdNum) {
|
||||
const env = await getEnvironment(envIdNum);
|
||||
if (env?.name) envSlug = `-${slugify(env.name, 'env')}`;
|
||||
}
|
||||
|
||||
return exportResponse({
|
||||
format: url.searchParams.get('format') || 'json',
|
||||
filenameBase: `vulnerabilities-${imageSlug}${envSlug}-${timestamp}`,
|
||||
findings,
|
||||
jsonBody: { imageId, findings },
|
||||
toCSV
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error exporting image vulnerabilities:', error);
|
||||
return jsonError('Failed to export image vulnerabilities', 500);
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user