|
| 1 | +import { canonicalOriginAllowed } from './source.mts' |
| 2 | +import crypto from 'node:crypto' |
| 3 | +import { readFileSync, statSync } from 'node:fs' |
| 4 | +import path from 'node:path' |
| 5 | +import process from 'node:process' |
| 6 | +import { spawnSync } from '@socketsecurity/lib-stable/process/spawn/child' |
| 7 | +import { canonicalPathIsSafe } from './git.mts' |
| 8 | +import type { CanonicalIndexEntry } from './git.mts' |
| 9 | + |
| 10 | +function record(value: unknown): Record<string, unknown> | undefined { |
| 11 | + return value !== null && typeof value === 'object' && !Array.isArray(value) |
| 12 | + ? Object.fromEntries(Object.entries(value)) |
| 13 | + : undefined |
| 14 | +} |
| 15 | + |
| 16 | +function unscopedTarget( |
| 17 | + manifest: Record<string, unknown>, |
| 18 | + file: string, |
| 19 | +): boolean { |
| 20 | + for (const field of [ |
| 21 | + 'capabilityScopedFiles', |
| 22 | + 'conditionalScopedFiles', |
| 23 | + 'shapeScopedFiles', |
| 24 | + ]) { |
| 25 | + const groups = manifest[field] |
| 26 | + if (groups === undefined) { |
| 27 | + continue |
| 28 | + } |
| 29 | + if (!Array.isArray(groups)) { |
| 30 | + return false |
| 31 | + } |
| 32 | + for (const value of groups) { |
| 33 | + const files = record(value)?.['files'] |
| 34 | + if (!Array.isArray(files) || files.includes(file)) { |
| 35 | + return false |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return true |
| 40 | +} |
| 41 | + |
| 42 | +export function canonicalBundleBytesMatch( |
| 43 | + ref: string, |
| 44 | + file: string, |
| 45 | + entry: CanonicalIndexEntry, |
| 46 | + manifest: unknown, |
| 47 | +): boolean { |
| 48 | + const parsed = record(manifest) |
| 49 | + const files = record(parsed?.['files']) |
| 50 | + const expected = files?.[file] |
| 51 | + return ( |
| 52 | + /^fleet-pack-[0-9a-f]{40}$/u.test(ref) && |
| 53 | + parsed?.['templateSha'] === ref.slice('fleet-pack-'.length) && |
| 54 | + unscopedTarget(parsed, file) && |
| 55 | + canonicalPathIsSafe(file) && |
| 56 | + entry.mode === '100644' && |
| 57 | + typeof expected === 'string' && |
| 58 | + /^[0-9a-f]{64}$/u.test(expected) && |
| 59 | + crypto.hash('sha256', entry.content, 'hex') === expected |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +const manifests = new Map<string, unknown>() |
| 64 | +function fetchManifest(member: string, ref: string): unknown { |
| 65 | + const key = `${member}:${ref}` |
| 66 | + if (!manifests.has(key)) { |
| 67 | + const result = spawnSync( |
| 68 | + process.execPath, |
| 69 | + [path.join(member, 'scripts/fleet/pack/canonical-proof.mts'), ref], |
| 70 | + { |
| 71 | + cwd: member, |
| 72 | + encoding: 'utf8', |
| 73 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 74 | + timeout: 30_000, |
| 75 | + maxBuffer: 4_194_304, |
| 76 | + }, |
| 77 | + ) |
| 78 | + if (result.status !== 0) { |
| 79 | + return undefined |
| 80 | + } |
| 81 | + const lines = result.stdout.trim().split(/\r?\n/u) |
| 82 | + const manifest: unknown = JSON.parse(lines.at(-1) ?? '') |
| 83 | + if (record(manifest)?.['templateSha'] !== ref.slice('fleet-pack-'.length)) { |
| 84 | + return undefined |
| 85 | + } |
| 86 | + manifests.set(key, manifest) |
| 87 | + } |
| 88 | + return manifests.get(key) |
| 89 | +} |
| 90 | + |
| 91 | +export function canonicalBundleCopyMatches( |
| 92 | + member: string, |
| 93 | + file: string, |
| 94 | + entry: CanonicalIndexEntry, |
| 95 | + options: { |
| 96 | + fetchManifest?: ((member: string, ref: string) => unknown) | undefined |
| 97 | + } = {}, |
| 98 | +): boolean { |
| 99 | + if (!canonicalOriginAllowed(member)) { |
| 100 | + return false |
| 101 | + } |
| 102 | + try { |
| 103 | + const appliedPath = path.join( |
| 104 | + member, |
| 105 | + '.cache/fleet/socket-wheelhouse/bundle-applied', |
| 106 | + ) |
| 107 | + if (statSync(appliedPath).size > 128) { |
| 108 | + return false |
| 109 | + } |
| 110 | + const ref = readFileSync(appliedPath, 'utf8').trim() |
| 111 | + if (!/^fleet-pack-[0-9a-f]{40}$/u.test(ref)) { |
| 112 | + return false |
| 113 | + } |
| 114 | + return canonicalBundleBytesMatch( |
| 115 | + ref, |
| 116 | + file, |
| 117 | + entry, |
| 118 | + (options.fetchManifest ?? fetchManifest)(member, ref), |
| 119 | + ) |
| 120 | + } catch { |
| 121 | + return false |
| 122 | + } |
| 123 | +} |
0 commit comments