Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { Data, Effect, Schema } from 'effect'; |
| 2 | import { Digest } from 'effect/crypto'; |
| 3 | import { Base64 } from 'effect/encoding'; |
| 4 | import { FetchHttpClient, HttpClient } from 'effect/http'; |
| 5 | import { SDK_RELEASE_REPOSITORY } from './github'; |
| 6 | import { NPM_REGISTRY } from './lanes'; |
| 7 | import type { Distribution } from './registry'; |
| 8 | import { REGISTRY_WAIT, isPending, pollRegistry, type RegistryDeadline } from './registry-wait'; |
| 9 | import { ensure, fail } from './runtime'; |
| 10 | import { releaseTag } from './source'; |
| 11 | |
| 12 | /** The publisher every released file must name: this repository, workflow and release tag. */ |
| 13 | export const RELEASE_PUBLISHER = { |
| 14 | repository: SDK_RELEASE_REPOSITORY, |
| 15 | workflow: 'sdk.release.yml', |
| 16 | pypiEnvironment: 'pypi-release', |
| 17 | } as const; |
| 18 | |
| 19 | const SLSA_PROVENANCE = 'https://slsa.dev/provenance/v1'; |
| 20 | |
| 21 | const NpmAttestations = Schema.Struct({ |
| 22 | attestations: Schema.Array( |
| 23 | Schema.Struct({ |
| 24 | predicateType: Schema.String, |
| 25 | bundle: Schema.Struct({ dsseEnvelope: Schema.Struct({ payload: Schema.String }) }), |
| 26 | }) |
| 27 | ), |
| 28 | }); |
| 29 | const SlsaStatement = Schema.Struct({ |
| 30 | subject: Schema.Array(Schema.Struct({ digest: Schema.Struct({ sha512: Schema.String }) })), |
| 31 | predicate: Schema.Struct({ |
| 32 | buildDefinition: Schema.Struct({ |
| 33 | externalParameters: Schema.Struct({ |
| 34 | workflow: Schema.Struct({ |
| 35 | ref: Schema.String, |
| 36 | repository: Schema.String, |
| 37 | path: Schema.String, |
| 38 | }), |
| 39 | }), |
| 40 | }), |
| 41 | }), |
| 42 | }); |
| 43 | const PypiProvenance = Schema.Struct({ |
| 44 | attestation_bundles: Schema.Array( |
| 45 | Schema.Struct({ |
| 46 | publisher: Schema.Struct({ |
| 47 | kind: Schema.String, |
| 48 | repository: Schema.String, |
| 49 | workflow: Schema.String, |
| 50 | environment: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 51 | }), |
| 52 | }) |
| 53 | ), |
| 54 | }); |
| 55 | |
| 56 | /** |
| 57 | * Provenance trails an upload by up to npm's publish-time scan, so a missing document is polled |
| 58 | * again; so is a rate limit or a server error. |
| 59 | */ |
| 60 | class ProvenancePending extends Data.TaggedError('ProvenancePending')<{ |
| 61 | readonly url: string; |
| 62 | readonly status: number | 'timeout'; |
| 63 | }> {} |
| 64 | |
| 65 | const provenanceJson = <S extends Schema.Top>( |
| 66 | url: string, |
| 67 | schema: S, |
| 68 | deadline?: RegistryDeadline |
| 69 | ) => |
| 70 | pollRegistry( |
| 71 | Effect.gen(function* () { |
| 72 | const client = yield* HttpClient.HttpClient; |
| 73 | const response = yield* client.get(url); |
| 74 | if (isPending(response.status)) |
| 75 | return yield* new ProvenancePending({ url, status: response.status }); |
| 76 | yield* ensure( |
| 77 | response.status === 200, |
| 78 | `Provenance lookup failed: ${url} (${response.status})` |
| 79 | ); |
| 80 | return yield* Schema.decodeUnknownEffect(Schema.fromJsonString(schema))(yield* response.text); |
| 81 | }).pipe( |
| 82 | Effect.provideService(FetchHttpClient.RequestInit, { redirect: 'error' }), |
| 83 | // A stalled lookup is retried like an answer that is not served yet. |
| 84 | Effect.timeoutOrElse({ |
| 85 | duration: REGISTRY_WAIT.requestTimeout, |
| 86 | orElse: () => Effect.fail(new ProvenancePending({ url, status: 'timeout' })), |
| 87 | }) |
| 88 | ), |
| 89 | error => error instanceof ProvenancePending, |
| 90 | deadline |
| 91 | ).pipe( |
| 92 | Effect.catchTag('ProvenancePending', error => |
| 93 | fail( |
| 94 | `No provenance is published at ${error.url} when the registry wait (${REGISTRY_WAIT.deadline}) ended; its last answer was ${error.status}` |
| 95 | ) |
| 96 | ) |
| 97 | ); |
| 98 | |
| 99 | /** |
| 100 | * The npm SLSA provenance of a published tarball must name this repository, the release |
| 101 | * workflow and this batch's tag, and its subject must be the reviewed bytes. |
| 102 | */ |
| 103 | export const verifyNpmProvenance = (file: Distribution, id: string, deadline?: RegistryDeadline) => |
| 104 | Effect.gen(function* () { |
| 105 | const url = `${NPM_REGISTRY}/-/npm/v1/attestations/${encodeURIComponent(file.name)}@${file.version}`; |
| 106 | const { attestations } = yield* provenanceJson(url, NpmAttestations, deadline); |
| 107 | const slsa = attestations.find(attestation => attestation.predicateType === SLSA_PROVENANCE); |
| 108 | if (!slsa) return yield* fail(`${file.name}@${file.version} has no SLSA provenance`); |
| 109 | const payload = yield* Effect.fromResult(Base64.decodeString(slsa.bundle.dsseEnvelope.payload)); |
| 110 | const statement = yield* Schema.decodeUnknownEffect(Schema.fromJsonString(SlsaStatement))( |
| 111 | payload |
| 112 | ); |
| 113 | const { workflow } = statement.predicate.buildDefinition.externalParameters; |
| 114 | yield* ensure( |
| 115 | workflow.repository === `https://github.com/${RELEASE_PUBLISHER.repository}` && |
| 116 | workflow.path === `.github/workflows/${RELEASE_PUBLISHER.workflow}` && |
| 117 | workflow.ref === `refs/tags/${releaseTag(id)}`, |
| 118 | `${file.name}@${file.version} was built by ${workflow.repository} ${workflow.path} at ${workflow.ref}, not by this release` |
| 119 | ); |
| 120 | const digest = yield* Digest.Digest; |
| 121 | const sha512 = yield* digest.digestHex(Digest.sha512, file.bytes); |
| 122 | yield* ensure( |
| 123 | statement.subject.some(subject => subject.digest.sha512 === sha512), |
| 124 | `${file.name}@${file.version} provenance describes other bytes than the reviewed tarball` |
| 125 | ); |
| 126 | }); |
| 127 | |
| 128 | /** Every PyPI file must carry a PEP 740 attestation from this repository's release workflow. */ |
| 129 | export const verifyPypiProvenance = (file: Distribution, deadline?: RegistryDeadline) => |
| 130 | Effect.gen(function* () { |
| 131 | const url = `https://pypi.org/integrity/${file.name}/${file.version}/${file.file}/provenance`; |
| 132 | const { attestation_bundles } = yield* provenanceJson(url, PypiProvenance, deadline); |
| 133 | yield* ensure( |
| 134 | attestation_bundles.some( |
| 135 | ({ publisher }) => |
| 136 | publisher.kind === 'GitHub' && |
| 137 | publisher.repository === RELEASE_PUBLISHER.repository && |
| 138 | publisher.workflow === RELEASE_PUBLISHER.workflow && |
| 139 | publisher.environment === RELEASE_PUBLISHER.pypiEnvironment |
| 140 | ), |
| 141 | `${file.file} is not attested by ${RELEASE_PUBLISHER.repository} ${RELEASE_PUBLISHER.workflow} in ${RELEASE_PUBLISHER.pypiEnvironment}` |
| 142 | ); |
| 143 | }); |