|
| 1 | +import type { PatchManifest, PatchRecord } from '../schema/manifest-schema.js' |
| 2 | +import { PatchManifestSchema, PatchRecordSchema } from '../schema/manifest-schema.js' |
| 3 | + |
| 4 | +/** |
| 5 | + * Result of manifest recovery operation |
| 6 | + */ |
| 7 | +export interface RecoveryResult { |
| 8 | + manifest: PatchManifest |
| 9 | + repairNeeded: boolean |
| 10 | + invalidPatches: string[] |
| 11 | + recoveredPatches: string[] |
| 12 | + discardedPatches: string[] |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Options for manifest recovery |
| 17 | + */ |
| 18 | +export interface RecoveryOptions { |
| 19 | + /** |
| 20 | + * Optional function to refetch patch data from external source (e.g., database) |
| 21 | + * Should return patch data or null if not found |
| 22 | + * @param uuid - The patch UUID |
| 23 | + * @param purl - The package URL (for context/validation) |
| 24 | + */ |
| 25 | + refetchPatch?: (uuid: string, purl?: string) => Promise<PatchData | null> |
| 26 | + |
| 27 | + /** |
| 28 | + * Optional callback for logging recovery events |
| 29 | + */ |
| 30 | + onRecoveryEvent?: (event: RecoveryEvent) => void |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Patch data returned from external source |
| 35 | + */ |
| 36 | +export interface PatchData { |
| 37 | + uuid: string |
| 38 | + purl: string |
| 39 | + publishedAt: string |
| 40 | + files: Record< |
| 41 | + string, |
| 42 | + { |
| 43 | + beforeHash?: string |
| 44 | + afterHash?: string |
| 45 | + } |
| 46 | + > |
| 47 | + vulnerabilities: Record< |
| 48 | + string, |
| 49 | + { |
| 50 | + cves: string[] |
| 51 | + summary: string |
| 52 | + severity: string |
| 53 | + description: string |
| 54 | + } |
| 55 | + > |
| 56 | + description: string |
| 57 | + license: string |
| 58 | + tier: string |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Events emitted during recovery |
| 63 | + */ |
| 64 | +export type RecoveryEvent = |
| 65 | + | { type: 'corrupted_manifest' } |
| 66 | + | { type: 'invalid_patch'; purl: string; uuid: string | null } |
| 67 | + | { type: 'recovered_patch'; purl: string; uuid: string } |
| 68 | + | { type: 'discarded_patch_not_found'; purl: string; uuid: string } |
| 69 | + | { type: 'discarded_patch_purl_mismatch'; purl: string; uuid: string; dbPurl: string } |
| 70 | + | { type: 'discarded_patch_no_uuid'; purl: string } |
| 71 | + | { type: 'recovery_error'; purl: string; uuid: string; error: string } |
| 72 | + |
| 73 | +/** |
| 74 | + * Recover and validate manifest with automatic repair of invalid patches |
| 75 | + * |
| 76 | + * This function attempts to parse and validate a manifest. If the manifest |
| 77 | + * contains invalid patches, it will attempt to recover them using the provided |
| 78 | + * refetch function. Patches that cannot be recovered are discarded. |
| 79 | + * |
| 80 | + * @param parsed - The parsed manifest object (may be invalid) |
| 81 | + * @param options - Recovery options including refetch function and event callback |
| 82 | + * @returns Recovery result with repaired manifest and statistics |
| 83 | + */ |
| 84 | +export async function recoverManifest( |
| 85 | + parsed: unknown, |
| 86 | + options: RecoveryOptions = {}, |
| 87 | +): Promise<RecoveryResult> { |
| 88 | + const { refetchPatch, onRecoveryEvent } = options |
| 89 | + |
| 90 | + // Try strict parse first (fast path for valid manifests) |
| 91 | + const strictResult = PatchManifestSchema.safeParse(parsed) |
| 92 | + if (strictResult.success) { |
| 93 | + return { |
| 94 | + manifest: strictResult.data, |
| 95 | + repairNeeded: false, |
| 96 | + invalidPatches: [], |
| 97 | + recoveredPatches: [], |
| 98 | + discardedPatches: [], |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Extract patches object with safety checks |
| 103 | + const patchesObj = |
| 104 | + parsed && |
| 105 | + typeof parsed === 'object' && |
| 106 | + 'patches' in parsed && |
| 107 | + parsed.patches && |
| 108 | + typeof parsed.patches === 'object' |
| 109 | + ? (parsed.patches as Record<string, unknown>) |
| 110 | + : null |
| 111 | + |
| 112 | + if (!patchesObj) { |
| 113 | + // Completely corrupted manifest |
| 114 | + onRecoveryEvent?.({ type: 'corrupted_manifest' }) |
| 115 | + return { |
| 116 | + manifest: { patches: {} }, |
| 117 | + repairNeeded: true, |
| 118 | + invalidPatches: [], |
| 119 | + recoveredPatches: [], |
| 120 | + discardedPatches: [], |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Try to recover individual patches |
| 125 | + const recoveredPatchesMap: Record<string, PatchRecord> = {} |
| 126 | + const invalidPatches: string[] = [] |
| 127 | + const recoveredPatches: string[] = [] |
| 128 | + const discardedPatches: string[] = [] |
| 129 | + |
| 130 | + for (const [purl, patchData] of Object.entries(patchesObj)) { |
| 131 | + // Try to parse this individual patch |
| 132 | + const patchResult = PatchRecordSchema.safeParse(patchData) |
| 133 | + |
| 134 | + if (patchResult.success) { |
| 135 | + // Valid patch, keep it as-is |
| 136 | + recoveredPatchesMap[purl] = patchResult.data |
| 137 | + } else { |
| 138 | + // Invalid patch, try to recover from external source |
| 139 | + const uuid = |
| 140 | + patchData && |
| 141 | + typeof patchData === 'object' && |
| 142 | + 'uuid' in patchData && |
| 143 | + typeof patchData.uuid === 'string' |
| 144 | + ? patchData.uuid |
| 145 | + : null |
| 146 | + |
| 147 | + invalidPatches.push(purl) |
| 148 | + onRecoveryEvent?.({ type: 'invalid_patch', purl, uuid }) |
| 149 | + |
| 150 | + if (uuid && refetchPatch) { |
| 151 | + try { |
| 152 | + // Try to refetch from external source |
| 153 | + const patchFromSource = await refetchPatch(uuid, purl) |
| 154 | + |
| 155 | + if (patchFromSource && patchFromSource.purl === purl) { |
| 156 | + // Successfully recovered, reconstruct patch record |
| 157 | + const manifestFiles: Record< |
| 158 | + string, |
| 159 | + { beforeHash: string; afterHash: string } |
| 160 | + > = {} |
| 161 | + for (const [filePath, fileInfo] of Object.entries( |
| 162 | + patchFromSource.files, |
| 163 | + )) { |
| 164 | + if (fileInfo.beforeHash && fileInfo.afterHash) { |
| 165 | + manifestFiles[filePath] = { |
| 166 | + beforeHash: fileInfo.beforeHash, |
| 167 | + afterHash: fileInfo.afterHash, |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + recoveredPatchesMap[purl] = { |
| 173 | + uuid: patchFromSource.uuid, |
| 174 | + exportedAt: patchFromSource.publishedAt, |
| 175 | + files: manifestFiles, |
| 176 | + vulnerabilities: patchFromSource.vulnerabilities, |
| 177 | + description: patchFromSource.description, |
| 178 | + license: patchFromSource.license, |
| 179 | + tier: patchFromSource.tier, |
| 180 | + } |
| 181 | + |
| 182 | + recoveredPatches.push(purl) |
| 183 | + onRecoveryEvent?.({ type: 'recovered_patch', purl, uuid }) |
| 184 | + } else if (patchFromSource && patchFromSource.purl !== purl) { |
| 185 | + // PURL mismatch - wrong package! |
| 186 | + discardedPatches.push(purl) |
| 187 | + onRecoveryEvent?.({ |
| 188 | + type: 'discarded_patch_purl_mismatch', |
| 189 | + purl, |
| 190 | + uuid, |
| 191 | + dbPurl: patchFromSource.purl, |
| 192 | + }) |
| 193 | + } else { |
| 194 | + // Not found in external source (might be unpublished) |
| 195 | + discardedPatches.push(purl) |
| 196 | + onRecoveryEvent?.({ |
| 197 | + type: 'discarded_patch_not_found', |
| 198 | + purl, |
| 199 | + uuid, |
| 200 | + }) |
| 201 | + } |
| 202 | + } catch (error: unknown) { |
| 203 | + // Error during recovery |
| 204 | + discardedPatches.push(purl) |
| 205 | + const errorMessage = error instanceof Error ? error.message : String(error) |
| 206 | + onRecoveryEvent?.({ |
| 207 | + type: 'recovery_error', |
| 208 | + purl, |
| 209 | + uuid, |
| 210 | + error: errorMessage, |
| 211 | + }) |
| 212 | + } |
| 213 | + } else { |
| 214 | + // No UUID or no refetch function, can't recover |
| 215 | + discardedPatches.push(purl) |
| 216 | + if (!uuid) { |
| 217 | + onRecoveryEvent?.({ type: 'discarded_patch_no_uuid', purl }) |
| 218 | + } else { |
| 219 | + onRecoveryEvent?.({ |
| 220 | + type: 'discarded_patch_not_found', |
| 221 | + purl, |
| 222 | + uuid, |
| 223 | + }) |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + const repairNeeded = invalidPatches.length > 0 |
| 230 | + |
| 231 | + return { |
| 232 | + manifest: { patches: recoveredPatchesMap }, |
| 233 | + repairNeeded, |
| 234 | + invalidPatches, |
| 235 | + recoveredPatches, |
| 236 | + discardedPatches, |
| 237 | + } |
| 238 | +} |
0 commit comments