From 8fa5cfd0db68b10f5257bbbf70f8fbe0318ffab5 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 2 Dec 2026 19:42:42 +0800 Subject: [PATCH] refactor(oci): share metadata string pair parsing (#15899) --- src/oci/OciImageLayoutParser.test.ts | 17 ++++ src/oci/OciImageLayoutParser.ts | 141 +++++++++++---------------- 2 files changed, 74 insertions(+), 84 deletions(-) diff --git a/src/oci/OciImageLayoutParser.test.ts b/src/oci/OciImageLayoutParser.test.ts index d5d5c87dc3..934c908cc0 100644 --- a/src/oci/OciImageLayoutParser.test.ts +++ b/src/oci/OciImageLayoutParser.test.ts @@ -3921,6 +3921,23 @@ const rewriteManifestDocument = async ( return digest; }; +it("reports invalid layer annotations without retaining valid pairs", async () => { + const root = await createTestTempDirectory("rea-oci-layer-annotations-"); + await rewriteManifestDocument(root, (manifest) => { + const [layer] = manifest.layers as Record[]; + if (layer === undefined) throw new Error("Expected a layer descriptor"); + layer.annotations = { large: "a".repeat(2048), invalid: 1 }; + }); + const report = await inspectLayout(root, { max_descriptor_bytes: 1024 }); + expect(report.images[0]?.layers).toHaveLength(1); + expect(report.findings).toContainEqual( + expect.objectContaining({ + kind: "malformed_blob_json", + message: expect.stringContaining("layers[0] annotations has 1 value"), + }), + ); +}); + it("diagnoses a manifest whose own mediaType conflicts with its descriptor", async () => { const root = await createTestTempDirectory("rea-oci-manifest-conflict-"); const digest = await rewriteManifestDocument(root, (manifest) => { diff --git a/src/oci/OciImageLayoutParser.ts b/src/oci/OciImageLayoutParser.ts index bd06ec3528..c23a37c653 100644 --- a/src/oci/OciImageLayoutParser.ts +++ b/src/oci/OciImageLayoutParser.ts @@ -81,37 +81,40 @@ const isStringArray = (value: unknown): value is readonly string[] => { return true; }; -interface ParsedAnnotations { - readonly annotations: { name: string; value: string }[]; - readonly dropped: number; -} - -// Each retained annotation is charged as its serialized {name, value} record -// before it is allocated, so many short pairs cannot outgrow their text. -// Non-string values are counted in the same pass so defect reporting does not -// rescan annotations. -const annotationsOf = ( +// Charge each serialized pair before retaining it. Descriptors whose +// annotations are omitted still count malformed values for diagnostics. +const stringRecordsOf = ( value: unknown, - options: { - readonly retain: boolean; - readonly charge?: (bytes: number) => void; - }, -): ParsedAnnotations => { - if (!isRecord(value)) return { annotations: [], dropped: 0 }; - const annotations: { name: string; value: string }[] = []; + field: string, + charge?: (bytes: number) => void, +): { + records: { name: string; value: string }[]; + defect: string | undefined; +} => { + const records: { name: string; value: string }[] = []; + if (value === undefined || value === null) + return { records, defect: undefined }; + if (!isRecord(value)) + return { + records, + defect: `${field} is not a JSON object; it is not modeled`, + }; let dropped = 0; for (const [name, entry] of Object.entries(value)) { if (typeof entry !== "string") { dropped += 1; - continue; + } else if (charge !== undefined) { + chargeArrayItem(charge, records.length, stringRecordBytes(name, entry)); + records.push({ name, value: entry }); } - if (!options.retain) continue; - options.charge?.( - annotationRecordBytes(name, entry) + (annotations.length > 0 ? 1 : 0), - ); - annotations.push({ name, value: entry }); } - return { annotations, dropped }; + return { + records, + defect: + dropped === 0 + ? undefined + : `${field} has ${String(dropped)} value${dropped === 1 ? "" : "s"} that ${dropped === 1 ? "is" : "are"} not a string; ${dropped === 1 ? "it is" : "they are"} omitted`, + }; }; const platformOf = ( @@ -145,15 +148,12 @@ const schemaVersionDefect = (value: unknown): string | undefined => { interface ParsedDescriptor { readonly descriptor: OciImageLayoutIndex["entries"][number]; - readonly droppedAnnotations: number; + readonly annotationDefect: string | undefined; } const descriptorOf = ( value: unknown, - options: { - readonly retainAnnotations?: boolean; - readonly chargeAnnotation?: (bytes: number) => void; - } = {}, + chargeAnnotations?: (bytes: number) => void, ): ParsedDescriptor | undefined => { if (!isRecord(value)) return undefined; const mediaType = nonempty(value.mediaType) ?? nonempty(value.media_type); @@ -165,12 +165,11 @@ const descriptorOf = ( value.size >= 0 ? value.size : null; - const { annotations, dropped } = annotationsOf(value.annotations, { - retain: options.retainAnnotations ?? false, - ...(options.chargeAnnotation === undefined - ? {} - : { charge: options.chargeAnnotation }), - }); + const { records: annotations, defect: annotationDefect } = stringRecordsOf( + value.annotations, + "annotations", + chargeAnnotations, + ); return { descriptor: { media_type: mediaType, @@ -179,7 +178,7 @@ const descriptorOf = ( platform: platformOf(value.platform), annotations, }, - droppedAnnotations: dropped, + annotationDefect, }; }; @@ -220,11 +219,11 @@ const describeSizeValue = (size: unknown): string => { */ const descriptorMetadataDefects = ( value: unknown, - droppedAnnotations: number, + annotationDefect: string | undefined, ): string[] => { if (!isRecord(value)) return []; const defects: string[] = []; - const { platform, annotations } = value; + const { platform } = value; if (platform !== undefined && platform !== null) { if (!isRecord(platform)) defects.push("platform is not a JSON object; it is not modeled"); @@ -241,14 +240,7 @@ const descriptorMetadataDefects = ( ); } } - if (annotations !== undefined && annotations !== null) { - if (!isRecord(annotations)) - defects.push("annotations is not a JSON object; it is not modeled"); - else if (droppedAnnotations > 0) - defects.push( - `annotations has ${String(droppedAnnotations)} value${droppedAnnotations === 1 ? "" : "s"} that ${droppedAnnotations === 1 ? "is" : "are"} not a string; ${droppedAnnotations === 1 ? "it is" : "they are"} omitted`, - ); - } + if (annotationDefect !== undefined) defects.push(annotationDefect); return defects; }; @@ -498,15 +490,12 @@ const envBindingBytes = (name: string, value: string | null): number => (jsonStringBytes(name) - EMPTY_STRING_JSON_BYTES) + (value === null ? 0 : jsonStringBytes(value) - NULL_JSON_BYTES); -const EMPTY_LABEL_BYTES = jsonByteLength({ name: "", value: "" }); -const labelBytes = (name: string, value: string): number => - EMPTY_LABEL_BYTES + +const EMPTY_STRING_RECORD_BYTES = jsonByteLength({ name: "", value: "" }); +const stringRecordBytes = (name: string, value: string): number => + EMPTY_STRING_RECORD_BYTES + (jsonStringBytes(name) - EMPTY_STRING_JSON_BYTES) + (jsonStringBytes(value) - EMPTY_STRING_JSON_BYTES); -// Annotations serialize as the same {name, value} record as labels. -const annotationRecordBytes = labelBytes; - const envBindings = ( value: unknown, includeValues: boolean, @@ -838,25 +827,12 @@ const labelsField = ( defects: string[], charge: ChargeConfigBytes, ): { name: string; value: string }[] => { - if (value === undefined || value === null) return []; - if (!isRecord(value)) { - defects.push("config.Labels is not a JSON object; it is not modeled"); - return []; - } - const labels: { name: string; value: string }[] = []; - let dropped = 0; - for (const [name, entry] of Object.entries(value)) { - if (typeof entry !== "string") { - dropped += 1; - continue; - } - chargeArrayItem(charge, labels.length, labelBytes(name, entry)); - labels.push({ name, value: entry }); - } - if (dropped > 0) - defects.push( - `config.Labels has ${String(dropped)} value${dropped === 1 ? "" : "s"} that ${dropped === 1 ? "is" : "are"} not a string; ${dropped === 1 ? "it is" : "they are"} omitted`, - ); + const { records: labels, defect } = stringRecordsOf( + value, + "config.Labels", + charge, + ); + if (defect !== undefined) defects.push(defect); return labels; }; @@ -1271,7 +1247,7 @@ const inventoryOciImageLayout = async ( where: string, value: unknown, ownerDigest: string, - droppedAnnotations: number, + annotationDefect: string | undefined, ): void => { const defect = descriptorSizeDefect(value); if (defect !== undefined) @@ -1284,7 +1260,7 @@ const inventoryOciImageLayout = async ( // absent; the readable entries stay on the descriptor. for (const metadataDefect of descriptorMetadataDefects( value, - droppedAnnotations, + annotationDefect, )) recordFinding( "malformed_blob_json", @@ -1544,13 +1520,13 @@ const inventoryOciImageLayout = async ( ); return; } - const { descriptor, droppedAnnotations } = parsedSubject; + const { descriptor, annotationDefect } = parsedSubject; chargeAcceptedDescriptorText(descriptor); reportDescriptorDefects( `${owner} ${ownerDigest} subject`, value, ownerDigest, - droppedAnnotations, + annotationDefect, ); const parsed = parseOciDigest(descriptor.digest); const path = @@ -1786,10 +1762,7 @@ const inventoryOciImageLayout = async ( for (const [position, item] of manifests.entries()) { chargeDescriptor(); // Annotations spend max_descriptor_bytes as each record is built. - const parsedEntry = descriptorOf(item, { - retainAnnotations: true, - chargeAnnotation: spendDescriptorBytes, - }); + const parsedEntry = descriptorOf(item, spendDescriptorBytes); if (parsedEntry === undefined) { referenceGraphTruncated = true; chargeRejectedDescriptorText(item); @@ -1800,14 +1773,14 @@ const inventoryOciImageLayout = async ( ); continue; } - const { descriptor, droppedAnnotations } = parsedEntry; + const { descriptor, annotationDefect } = parsedEntry; chargeDescriptorBytes(descriptor); indexEntries.push(descriptor); reportDescriptorDefects( `Index ${digest} manifests[${String(position)}]`, item, digest, - droppedAnnotations, + annotationDefect, ); remember(descriptor.digest, descriptor.media_type, descriptor.size); if (!isOciKnownMediaType(descriptor.media_type)) { @@ -1933,13 +1906,13 @@ const inventoryOciImageLayout = async ( ); continue; } - const { descriptor, droppedAnnotations } = parsedLayer; + const { descriptor, annotationDefect } = parsedLayer; chargeAcceptedDescriptorText(descriptor); reportDescriptorDefects( `Manifest ${digest} layers[${String(position)}]`, item, digest, - droppedAnnotations, + annotationDefect, ); remember(descriptor.digest, descriptor.media_type, descriptor.size); if (!isOciKnownMediaType(descriptor.media_type)) @@ -1971,7 +1944,7 @@ const inventoryOciImageLayout = async ( `Manifest ${digest} config`, document.value.config, digest, - parsedConfigDescriptor.droppedAnnotations, + parsedConfigDescriptor.annotationDefect, ); remember( configDescriptor.digest,