|
| 1 | +import * as Debug from "debug"; |
| 2 | +import { normalize as normalizePath } from "path"; |
| 3 | +import { ChiselPackage } from "../../analyzer/types"; |
| 4 | +import { decompressZstd } from "../../compression-utils"; |
| 5 | +import { getContentAsBuffer } from "../../extractor"; |
| 6 | +import { ExtractAction, ExtractedLayers } from "../../extractor/types"; |
| 7 | +import { streamToBuffer } from "../../stream-utils"; |
| 8 | + |
| 9 | +const debug = Debug("snyk"); |
| 10 | + |
| 11 | +/** |
| 12 | + * Path to the Chisel manifest file within container images. |
| 13 | + * This file contains package and slice information for Chisel-built images. |
| 14 | + */ |
| 15 | +const CHISEL_MANIFEST_PATH = "/var/lib/chisel/manifest.wall"; |
| 16 | + |
| 17 | +/** |
| 18 | + * Extract action for Ubuntu Chisel manifest files. |
| 19 | + * |
| 20 | + * Chisel is Ubuntu's tool for creating minimal container images by installing |
| 21 | + * only specific "slices" of Debian packages. The manifest.wall file is a |
| 22 | + * zstd-compressed NDJSON (newline-delimited JSON) file that records all |
| 23 | + * installed packages, slices, and files for integrity verification and SBOM generation. |
| 24 | + * |
| 25 | + * See: https://documentation.ubuntu.com/chisel/en/latest/reference/manifest/ |
| 26 | + */ |
| 27 | +export const getChiselManifestAction: ExtractAction = { |
| 28 | + actionName: "chisel-manifest", |
| 29 | + filePathMatches: (filePath) => |
| 30 | + filePath === normalizePath(CHISEL_MANIFEST_PATH), |
| 31 | + callback: streamToBuffer, |
| 32 | +}; |
| 33 | + |
| 34 | +/** |
| 35 | + * Extracts and parses Chisel package information from Docker image layers. |
| 36 | + * |
| 37 | + * Searches for the Chisel manifest file (/var/lib/chisel/manifest.wall), decompresses it, |
| 38 | + * and extracts package entries. The manifest uses NDJSON format where each line is a |
| 39 | + * separate JSON object with a "kind" field indicating the entry type. |
| 40 | + * |
| 41 | + * @param extractedLayers - Layers extracted from the Docker image |
| 42 | + * @returns Array of Chisel packages found in the manifest, or empty array if not found |
| 43 | + */ |
| 44 | +export function getChiselManifestContent( |
| 45 | + extractedLayers: ExtractedLayers, |
| 46 | +): ChiselPackage[] { |
| 47 | + const compressedManifest = getContentAsBuffer( |
| 48 | + extractedLayers, |
| 49 | + getChiselManifestAction, |
| 50 | + ); |
| 51 | + |
| 52 | + if (!compressedManifest) { |
| 53 | + return []; |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + const decompressed = decompressZstd(compressedManifest); |
| 58 | + const manifestText = decompressed.toString("utf8"); |
| 59 | + |
| 60 | + const packages: ChiselPackage[] = []; |
| 61 | + const lines = manifestText.split("\n"); |
| 62 | + |
| 63 | + for (const line of lines) { |
| 64 | + if (!line.trim()) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + const entry = JSON.parse(line); |
| 70 | + // Only extract package entries; manifest also contains "slice", "path", and "content" entries |
| 71 | + if (entry.kind === "package") { |
| 72 | + // Validate required fields exist before creating package object |
| 73 | + if (!entry.name || !entry.version || !entry.sha256 || !entry.arch) { |
| 74 | + debug( |
| 75 | + `Skipping package entry with missing required fields: ${JSON.stringify( |
| 76 | + entry, |
| 77 | + )}`, |
| 78 | + ); |
| 79 | + continue; |
| 80 | + } |
| 81 | + packages.push({ |
| 82 | + kind: entry.kind, |
| 83 | + name: entry.name, |
| 84 | + version: entry.version, |
| 85 | + sha256: entry.sha256, |
| 86 | + arch: entry.arch, |
| 87 | + }); |
| 88 | + } |
| 89 | + } catch (parseError) { |
| 90 | + // Skip malformed JSON lines - manifest may be corrupted or have trailing newlines |
| 91 | + debug( |
| 92 | + `Failed to parse Chisel manifest line: ${ |
| 93 | + parseError instanceof Error |
| 94 | + ? parseError.message |
| 95 | + : String(parseError) |
| 96 | + }`, |
| 97 | + ); |
| 98 | + continue; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + debug(`Found ${packages.length} Chisel packages in manifest`); |
| 103 | + return packages; |
| 104 | + } catch (error) { |
| 105 | + debug( |
| 106 | + `Failed to process Chisel manifest: ${ |
| 107 | + error instanceof Error ? error.message : String(error) |
| 108 | + }`, |
| 109 | + ); |
| 110 | + return []; |
| 111 | + } |
| 112 | +} |
0 commit comments