|
| 1 | +import { execFile } from 'child_process'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { promisify } from 'util'; |
| 4 | +import { readJsonFile } from './fileutils'; |
| 5 | + |
| 6 | +/* |
| 7 | + * Verifies that the given npm package has provenance attestations |
| 8 | + * generated by the GitHub Actions workflow at .github/workflows/publish.yml |
| 9 | + * in the nrwl/nx repository. |
| 10 | + * |
| 11 | + * Will throw if the package does not have valid provenance. |
| 12 | + */ |
| 13 | +export async function ensurePackageHasProvenance( |
| 14 | + packageName: string, |
| 15 | + packageVersion: string |
| 16 | +): Promise<void> { |
| 17 | + // this is used for locally released versions without provenance |
| 18 | + // do not set this for other reasons or you might be exposed to security risks |
| 19 | + if (process.env.NX_SKIP_PROVENANCE_CHECK) { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + const execFileAsync = promisify(execFile); |
| 24 | + |
| 25 | + const npmViewResult = JSON.parse( |
| 26 | + ( |
| 27 | + await execFileAsync( |
| 28 | + 'npm', |
| 29 | + ['view', `${packageName}@${packageVersion}`, '--json', '--silent'], |
| 30 | + { |
| 31 | + timeout: 20000, |
| 32 | + } |
| 33 | + ) |
| 34 | + ).stdout.trim() |
| 35 | + ); |
| 36 | + |
| 37 | + const attURL = npmViewResult.dist?.attestations?.url; |
| 38 | + |
| 39 | + if (!attURL) |
| 40 | + throw noProvenanceError( |
| 41 | + packageName, |
| 42 | + packageVersion, |
| 43 | + 'No attestation URL found' |
| 44 | + ); |
| 45 | + |
| 46 | + const attestations = (await (await fetch(attURL)).json()) as { |
| 47 | + attestations: Attestation[]; |
| 48 | + }; |
| 49 | + |
| 50 | + const provenanceAttestation = attestations?.attestations?.find( |
| 51 | + (a) => a.predicateType === 'https://slsa.dev/provenance/v1' |
| 52 | + ); |
| 53 | + if (!provenanceAttestation) |
| 54 | + throw noProvenanceError( |
| 55 | + packageName, |
| 56 | + packageVersion, |
| 57 | + 'No provenance attestation found' |
| 58 | + ); |
| 59 | + |
| 60 | + const dsseEnvelopePayload = JSON.parse( |
| 61 | + Buffer.from( |
| 62 | + provenanceAttestation.bundle.dsseEnvelope.payload, |
| 63 | + 'base64' |
| 64 | + ).toString() |
| 65 | + ); |
| 66 | + |
| 67 | + const workflowParameters = |
| 68 | + dsseEnvelopePayload?.predicate?.buildDefinition?.externalParameters |
| 69 | + ?.workflow; |
| 70 | + |
| 71 | + // verify that provenance was actually generated from the right publishing workflow |
| 72 | + if (workflowParameters?.repository !== 'https://github.com/nrwl/nx') { |
| 73 | + throw noProvenanceError( |
| 74 | + packageName, |
| 75 | + packageVersion, |
| 76 | + 'Repository does not match nrwl/nx' |
| 77 | + ); |
| 78 | + } |
| 79 | + if (workflowParameters?.path !== '.github/workflows/publish.yml') { |
| 80 | + throw noProvenanceError( |
| 81 | + packageName, |
| 82 | + packageVersion, |
| 83 | + 'Publishing workflow does not match .github/workflows/publish.yml' |
| 84 | + ); |
| 85 | + } |
| 86 | + if (workflowParameters?.ref !== `refs/tags/${npmViewResult.version}`) { |
| 87 | + throw noProvenanceError( |
| 88 | + packageName, |
| 89 | + packageVersion, |
| 90 | + `Version ref does not match refs/tags/${npmViewResult.version}` |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + // verify that provenance was generated from the exact same artifact as the one we are installing |
| 95 | + const distSha = Buffer.from( |
| 96 | + npmViewResult.dist.integrity.replace('sha512-', ''), |
| 97 | + 'base64' |
| 98 | + ).toString('hex'); |
| 99 | + const attestationSha = dsseEnvelopePayload?.subject[0]?.digest.sha512; |
| 100 | + if (distSha !== attestationSha) { |
| 101 | + throw noProvenanceError( |
| 102 | + packageName, |
| 103 | + packageVersion, |
| 104 | + 'Integrity hash does not match attestation hash' |
| 105 | + ); |
| 106 | + } |
| 107 | + return; |
| 108 | +} |
| 109 | + |
| 110 | +export const noProvenanceError = ( |
| 111 | + packageName: string, |
| 112 | + packageVersion: string, |
| 113 | + error?: string |
| 114 | +) => |
| 115 | + `An error occurred while checking the provenance of ${packageName}@${packageVersion}. This could indicate a security risk. Please double check https://www.npmjs.com/package/${packageName} to see if the package is published correctly or file an issue at https://github.com/nrwl/nx/issues \n Error: ${ |
| 116 | + error ?? '' |
| 117 | + }`; |
| 118 | + |
| 119 | +export function getNxPackageGroup(): string[] { |
| 120 | + const packageJsonPath = join(__dirname, '../../package.json'); |
| 121 | + const packageJson = readJsonFile(packageJsonPath); |
| 122 | + const packages = packageJson['nx-migrations'].packageGroup.filter( |
| 123 | + (dep) => typeof dep === 'string' && dep.startsWith('@nx/') |
| 124 | + ); |
| 125 | + packages.push('nx'); |
| 126 | + return packages; |
| 127 | +} |
| 128 | + |
| 129 | +type Attestation = { |
| 130 | + predicateType: string; |
| 131 | + bundle: { |
| 132 | + dsseEnvelope: { |
| 133 | + payload: string; // base64 encoded JSON |
| 134 | + payloadType: string; |
| 135 | + signatures: { |
| 136 | + keyid: string; |
| 137 | + sig: string; |
| 138 | + }[]; |
| 139 | + }; |
| 140 | + mediaType: string; |
| 141 | + [x: string]: unknown; |
| 142 | + }; |
| 143 | + [x: string]: unknown; |
| 144 | +}; |
| 145 | + |
| 146 | +// referh to https://slsa.dev/spec/v1.1/provenance#schema |
| 147 | +export type DecodedAttestationPayload = { |
| 148 | + _type: 'https://in-toto.io/Statement/v1'; |
| 149 | + subject: unknown[]; |
| 150 | + predicateType: 'https://slsa.dev/provenance/v1'; |
| 151 | + predicate: { |
| 152 | + buildDefinition: { |
| 153 | + buildType: string; |
| 154 | + externalParameters: Record<string, any>; |
| 155 | + internalParameters?: Record<string, any>; |
| 156 | + resolvedDependencies?: ResourceDescriptor[]; |
| 157 | + }; |
| 158 | + runDetails: { |
| 159 | + builder: { |
| 160 | + id: string; |
| 161 | + builderDependencies?: ResourceDescriptor[]; |
| 162 | + version?: Record<string, string>; |
| 163 | + }; |
| 164 | + metadata?: { |
| 165 | + invocationId?: string; |
| 166 | + startedOn?: string; // <YYYY>-<MM>-<DD>T<hh>:<mm>:<ss>Z |
| 167 | + finishedOn?: string; // <YYYY>-<MM>-<DD>T<hh>:<mm>:<ss>Z |
| 168 | + }; |
| 169 | + byproducts?: ResourceDescriptor[]; |
| 170 | + }; |
| 171 | + }; |
| 172 | +}; |
| 173 | + |
| 174 | +export interface ResourceDescriptor { |
| 175 | + uri?: string; |
| 176 | + digest?: { |
| 177 | + sha256?: string; |
| 178 | + sha512?: string; |
| 179 | + gitCommit?: string; |
| 180 | + [key: string]: string | undefined; |
| 181 | + }; |
| 182 | + name?: string; |
| 183 | + downloadLocation?: string; |
| 184 | + mediaType?: string; |
| 185 | + content?: string; |
| 186 | + annotations?: { |
| 187 | + [key: string]: any; |
| 188 | + }; |
| 189 | +} |
0 commit comments