@crombolo/server 0.43.1 → 0.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +20 -14
- package/src/adapters/health.ts +48 -0
- package/src/bundle-admin.ts +235 -0
- package/src/bundle-build.ts +6 -2
- package/src/bundle-code-install.ts +168 -0
- package/src/bundle-install.ts +80 -2
- package/src/config.ts +61 -2
- package/src/core.ts +5 -1
- package/src/form-submit.ts +32 -5
- package/src/form-workflows.ts +7 -1
- package/src/index.ts +4 -0
- package/src/marketplace.ts +277 -65
- package/src/no-nodes.ts +1 -1
- package/src/provenance.ts +466 -0
- package/src/remote-ci.ts +334 -0
- package/src/remote.ts +679 -14
- package/src/restart-pending.ts +52 -0
- package/src/schema.ts +7 -0
- package/src/seed/compile.ts +17 -15
- package/src/seed/export.ts +16 -9
- package/src/seed/read.ts +43 -13
- package/src/server.ts +113 -67
- package/src/settings.ts +29 -4
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a bundle was built (story 060): a Sigstore signature over the package,
|
|
3
|
+
* made in GitHub Actions from the commit or tag it was built at, kept in the
|
|
4
|
+
* package as `provenance.json` with links to the commit, the tag, the run and
|
|
5
|
+
* the transparency log. Optional: an unsigned bundle is still a bundle. One
|
|
6
|
+
* whose signature does not check out is always refused. `docs/bundles.md`,
|
|
7
|
+
* "Signing a bundle".
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
|
|
10
|
+
import { homedir } from 'node:os'
|
|
11
|
+
import { join, relative, sep } from 'node:path'
|
|
12
|
+
import { bundleFromJSON, type SerializedBundle } from '@sigstore/bundle'
|
|
13
|
+
import { X509Certificate } from '@sigstore/core'
|
|
14
|
+
import { TrustedRoot } from '@sigstore/protobuf-specs'
|
|
15
|
+
import { toSignedEntity, toTrustMaterial, Verifier } from '@sigstore/verify'
|
|
16
|
+
|
|
17
|
+
/** The file in a package that holds its signature, and the one file the digest leaves out. */
|
|
18
|
+
export const PROVENANCE_FILE = 'provenance.json'
|
|
19
|
+
|
|
20
|
+
/** What the signed statement says it is about. */
|
|
21
|
+
export const PROVENANCE_PREDICATE = 'https://crombolo.dev/bundle-provenance/v1'
|
|
22
|
+
|
|
23
|
+
const GITHUB_ISSUER = 'https://token.actions.githubusercontent.com'
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Loaded by name: their type declarations need packages a site does not
|
|
27
|
+
* install, so a site that type-checks its own components would fail on them.
|
|
28
|
+
*/
|
|
29
|
+
const SIGSTORE = 'sigstore'
|
|
30
|
+
const SIGSTORE_TUF = '@sigstore/tuf'
|
|
31
|
+
interface SigstoreModule {
|
|
32
|
+
attest(payload: Buffer, payloadType: string): Promise<SerializedBundle>
|
|
33
|
+
}
|
|
34
|
+
interface TufModule {
|
|
35
|
+
getTrustedRoot(options: { cachePath: string; forceCache?: boolean }): Promise<TrustedRoot>
|
|
36
|
+
}
|
|
37
|
+
const IN_TOTO = 'application/vnd.in-toto+json'
|
|
38
|
+
|
|
39
|
+
/** Fulcio's certificate extensions: what GitHub said about the job that signed. */
|
|
40
|
+
const OID = {
|
|
41
|
+
issuer: '1.3.6.1.4.1.57264.1.8',
|
|
42
|
+
workflow: '1.3.6.1.4.1.57264.1.9',
|
|
43
|
+
repository: '1.3.6.1.4.1.57264.1.12',
|
|
44
|
+
commit: '1.3.6.1.4.1.57264.1.13',
|
|
45
|
+
ref: '1.3.6.1.4.1.57264.1.14',
|
|
46
|
+
run: '1.3.6.1.4.1.57264.1.21',
|
|
47
|
+
} as const
|
|
48
|
+
|
|
49
|
+
/** Where a signed bundle was built, as its certificate says. */
|
|
50
|
+
export interface ProvenanceSource {
|
|
51
|
+
issuer: string
|
|
52
|
+
/** `owner/repo`. */
|
|
53
|
+
repository: string
|
|
54
|
+
/** `refs/tags/v1.2.0` or `refs/heads/main`, as it was when the bundle was signed. */
|
|
55
|
+
ref: string
|
|
56
|
+
commit: string
|
|
57
|
+
/** The workflow file and ref that signed it. */
|
|
58
|
+
workflow: string
|
|
59
|
+
run: string | null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface ProvenanceLinks {
|
|
63
|
+
commit: string
|
|
64
|
+
/** The tag's page, when the bundle was signed from a tag. */
|
|
65
|
+
tag: string | null
|
|
66
|
+
run: string | null
|
|
67
|
+
log: string | null
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** `provenance.json`. */
|
|
71
|
+
export interface ProvenanceFile {
|
|
72
|
+
format: 1
|
|
73
|
+
digest: string
|
|
74
|
+
signature: SerializedBundle
|
|
75
|
+
source: ProvenanceSource
|
|
76
|
+
links: ProvenanceLinks
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type ProvenanceState = 'signed' | 'unsigned' | 'invalid' | 'local'
|
|
80
|
+
|
|
81
|
+
export interface ProvenanceResult {
|
|
82
|
+
state: ProvenanceState
|
|
83
|
+
digest?: string
|
|
84
|
+
source?: ProvenanceSource
|
|
85
|
+
links?: ProvenanceLinks
|
|
86
|
+
/** Why a signature was not accepted, in words a person can act on. */
|
|
87
|
+
reason?: string
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Every file a package's digest covers: all of it but its signature, installs and history. */
|
|
91
|
+
function packageFiles(dir: string): string[] {
|
|
92
|
+
const out: string[] = []
|
|
93
|
+
const walk = (at: string) => {
|
|
94
|
+
for (const name of readdirSync(at)) {
|
|
95
|
+
if (name === 'node_modules' || name === '.git') continue
|
|
96
|
+
const path = join(at, name)
|
|
97
|
+
if (statSync(path).isDirectory()) walk(path)
|
|
98
|
+
else out.push(relative(dir, path).split(sep).join('/'))
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
walk(dir)
|
|
102
|
+
// By code unit, not locale: the digest has to be the same on every machine.
|
|
103
|
+
return out.filter((path) => path !== PROVENANCE_FILE).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The package digest: SHA-256 over every file but `provenance.json`, in path
|
|
108
|
+
* order, as `bundleIntegrity` hashes a payload. It covers what a bundle's own
|
|
109
|
+
* integrity does not: `package.json` and an extension bundle's code.
|
|
110
|
+
*/
|
|
111
|
+
export function packageDigest(dir: string): string {
|
|
112
|
+
const hasher = new Bun.CryptoHasher('sha256')
|
|
113
|
+
for (const path of packageFiles(dir)) {
|
|
114
|
+
hasher.update(path)
|
|
115
|
+
hasher.update('\0')
|
|
116
|
+
hasher.update(readFileSync(join(dir, path)))
|
|
117
|
+
hasher.update('\n')
|
|
118
|
+
}
|
|
119
|
+
return `sha256-${hasher.digest('hex')}`
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const hexOf = (digest: string) => digest.replace(/^sha256-/, '')
|
|
123
|
+
|
|
124
|
+
/** The statement signed: the digest alone, so the public log learns nothing of the bundle but it. */
|
|
125
|
+
function statement(digest: string): Buffer {
|
|
126
|
+
return Buffer.from(
|
|
127
|
+
JSON.stringify({
|
|
128
|
+
_type: 'https://in-toto.io/Statement/v1',
|
|
129
|
+
subject: [{ name: 'crombolo-bundle', digest: { sha256: hexOf(digest) } }],
|
|
130
|
+
predicateType: PROVENANCE_PREDICATE,
|
|
131
|
+
predicate: { format: 1 },
|
|
132
|
+
}),
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function extensionText(cert: X509Certificate, oid: string): string | undefined {
|
|
137
|
+
const extension = cert.extension(oid)
|
|
138
|
+
return extension?.valueObj.subs?.[0]?.value.toString('utf8')
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const leafOf = (bundle: SerializedBundle): X509Certificate | undefined => {
|
|
142
|
+
const material = bundle.verificationMaterial as Record<string, unknown>
|
|
143
|
+
const raw =
|
|
144
|
+
(material.certificate as { rawBytes?: string } | undefined)?.rawBytes ??
|
|
145
|
+
(material.x509CertificateChain as { certificates?: Array<{ rawBytes: string }> } | undefined)
|
|
146
|
+
?.certificates?.[0]?.rawBytes
|
|
147
|
+
return raw ? X509Certificate.parse(Buffer.from(raw, 'base64')) : undefined
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** What the certificate says about the job that signed; undefined when it is not one. */
|
|
151
|
+
export function sourceOf(bundle: SerializedBundle): ProvenanceSource | undefined {
|
|
152
|
+
const cert = leafOf(bundle)
|
|
153
|
+
if (!cert) return undefined
|
|
154
|
+
const repositoryUri = extensionText(cert, OID.repository)
|
|
155
|
+
const commit = extensionText(cert, OID.commit)
|
|
156
|
+
const ref = extensionText(cert, OID.ref)
|
|
157
|
+
if (!repositoryUri || !commit || !ref) return undefined
|
|
158
|
+
return {
|
|
159
|
+
issuer: extensionText(cert, OID.issuer) ?? '',
|
|
160
|
+
repository: repositoryUri.replace(/^https:\/\/github\.com\//, ''),
|
|
161
|
+
ref,
|
|
162
|
+
commit,
|
|
163
|
+
workflow: extensionText(cert, OID.workflow) ?? '',
|
|
164
|
+
run: extensionText(cert, OID.run) ?? null,
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function linksFor(source: ProvenanceSource, bundle: SerializedBundle): ProvenanceLinks {
|
|
169
|
+
const repo = `https://github.com/${source.repository}`
|
|
170
|
+
const tag = source.ref.startsWith('refs/tags/') ? source.ref.slice('refs/tags/'.length) : null
|
|
171
|
+
const logIndex = (bundle.verificationMaterial as { tlogEntries?: Array<{ logIndex?: string }> })
|
|
172
|
+
.tlogEntries?.[0]?.logIndex
|
|
173
|
+
return {
|
|
174
|
+
commit: `${repo}/commit/${source.commit}`,
|
|
175
|
+
tag: tag ? `${repo}/releases/tag/${encodeURIComponent(tag)}` : null,
|
|
176
|
+
run: source.run,
|
|
177
|
+
log: logIndex ? `https://search.sigstore.dev/?logIndex=${logIndex}` : null,
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The ref as a person names it: `v1.2.0`, or the branch. */
|
|
182
|
+
export const refName = (ref: string): string => ref.replace(/^refs\/(tags|heads)\//, '')
|
|
183
|
+
|
|
184
|
+
/** One line for a person: where it was built, or that it was not signed. */
|
|
185
|
+
export function describeProvenance(result: ProvenanceResult): string {
|
|
186
|
+
switch (result.state) {
|
|
187
|
+
case 'signed': {
|
|
188
|
+
const source = result.source as ProvenanceSource
|
|
189
|
+
return `Signed: built from github.com/${source.repository} at ${refName(source.ref)} (${source.commit.slice(0, 7)})`
|
|
190
|
+
}
|
|
191
|
+
case 'unsigned':
|
|
192
|
+
return 'Not signed'
|
|
193
|
+
case 'local':
|
|
194
|
+
return 'Local: from this machine, not a registry'
|
|
195
|
+
case 'invalid':
|
|
196
|
+
return `Signature not accepted: ${result.reason ?? 'it does not check out'}`
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export interface SignOptions {
|
|
201
|
+
/**
|
|
202
|
+
* Sign from a private or internal repository anyway. Sigstore's public log
|
|
203
|
+
* then records the repository's name, workflow, ref and commit for good.
|
|
204
|
+
*/
|
|
205
|
+
publicLog?: boolean
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** The job's repository visibility, from the event GitHub Actions gives it; undefined when it does not say. */
|
|
209
|
+
export function repositoryVisibility(): 'public' | 'private' | 'internal' | undefined {
|
|
210
|
+
const path = Bun.env.GITHUB_EVENT_PATH
|
|
211
|
+
if (!path || !existsSync(path)) return undefined
|
|
212
|
+
try {
|
|
213
|
+
const repo = (
|
|
214
|
+
JSON.parse(readFileSync(path, 'utf8')) as {
|
|
215
|
+
repository?: { visibility?: unknown; private?: unknown }
|
|
216
|
+
}
|
|
217
|
+
).repository
|
|
218
|
+
if (
|
|
219
|
+
repo?.visibility === 'public' ||
|
|
220
|
+
repo?.visibility === 'private' ||
|
|
221
|
+
repo?.visibility === 'internal'
|
|
222
|
+
)
|
|
223
|
+
return repo.visibility
|
|
224
|
+
if (typeof repo?.private === 'boolean') return repo.private ? 'private' : 'public'
|
|
225
|
+
} catch {}
|
|
226
|
+
return undefined
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Signs a package directory in GitHub Actions, writing `provenance.json`. The
|
|
231
|
+
* job needs `id-token: write`; Sigstore turns its identity token into a
|
|
232
|
+
* short-lived certificate naming the repository, ref, commit and workflow.
|
|
233
|
+
*/
|
|
234
|
+
export async function signPackage(dir: string, options: SignOptions = {}): Promise<ProvenanceFile> {
|
|
235
|
+
if (Bun.env.GITHUB_ACTIONS !== 'true' || !Bun.env.ACTIONS_ID_TOKEN_REQUEST_URL)
|
|
236
|
+
throw new Error(
|
|
237
|
+
'A bundle is signed in GitHub Actions, by a job with `permissions: id-token: write`: run `crombolo bundle sign` there.',
|
|
238
|
+
)
|
|
239
|
+
const visibility = repositoryVisibility()
|
|
240
|
+
if (visibility !== 'public' && !options.publicLog)
|
|
241
|
+
throw new Error(
|
|
242
|
+
`${visibility === undefined ? 'GitHub Actions did not say whether this repository is public' : `This repository is ${visibility}`}: signing records its name, the workflow, the tag or branch and the commit in Sigstore's public log, for good. Pass --public-log to sign anyway.`,
|
|
243
|
+
)
|
|
244
|
+
const { attest } = (await import(SIGSTORE)) as SigstoreModule
|
|
245
|
+
const digest = packageDigest(dir)
|
|
246
|
+
const signature = await attest(statement(digest), IN_TOTO)
|
|
247
|
+
const source = sourceOf(signature)
|
|
248
|
+
if (!source) throw new Error('The signature Sigstore returned names no GitHub repository.')
|
|
249
|
+
const file: ProvenanceFile = {
|
|
250
|
+
format: 1,
|
|
251
|
+
digest,
|
|
252
|
+
signature,
|
|
253
|
+
source,
|
|
254
|
+
links: linksFor(source, signature),
|
|
255
|
+
}
|
|
256
|
+
writeFileSync(join(dir, PROVENANCE_FILE), `${JSON.stringify(file, null, 2)}\n`)
|
|
257
|
+
return file
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export interface VerifyProvenanceOptions {
|
|
261
|
+
/** Sigstore's trusted root as JSON; the public one, refreshed through TUF, by default. */
|
|
262
|
+
trustedRoot?: unknown
|
|
263
|
+
/**
|
|
264
|
+
* Checks the Sigstore bundle itself, throwing when it does not verify; the
|
|
265
|
+
* Sigstore verifier by default. Only a test passes one: a signature in this
|
|
266
|
+
* format is made in GitHub Actions, not offline.
|
|
267
|
+
*/
|
|
268
|
+
verifySignature?: (signature: SerializedBundle) => void | Promise<void>
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
let publicRoot: { at: number; root: Promise<TrustedRoot> } | undefined
|
|
272
|
+
const ROOT_FOR_MS = 60 * 60_000
|
|
273
|
+
|
|
274
|
+
/** Sigstore's public trusted root, fetched through TUF at most hourly, falling back to its cache. */
|
|
275
|
+
function publicTrustedRoot(): Promise<TrustedRoot> {
|
|
276
|
+
if (!publicRoot || Date.now() - publicRoot.at > ROOT_FOR_MS) {
|
|
277
|
+
const cachePath = join(
|
|
278
|
+
Bun.env.XDG_CACHE_HOME ?? join(homedir(), '.cache'),
|
|
279
|
+
'crombolo',
|
|
280
|
+
'sigstore',
|
|
281
|
+
)
|
|
282
|
+
const root = (import(SIGSTORE_TUF) as Promise<TufModule>).then(({ getTrustedRoot }) =>
|
|
283
|
+
getTrustedRoot({ cachePath }).catch(() => getTrustedRoot({ cachePath, forceCache: true })),
|
|
284
|
+
)
|
|
285
|
+
publicRoot = { at: Date.now(), root }
|
|
286
|
+
root.catch(() => {
|
|
287
|
+
publicRoot = undefined
|
|
288
|
+
})
|
|
289
|
+
}
|
|
290
|
+
return publicRoot.root
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const invalid = (reason: string, digest?: string): ProvenanceResult => ({
|
|
294
|
+
state: 'invalid',
|
|
295
|
+
reason,
|
|
296
|
+
...(digest ? { digest } : {}),
|
|
297
|
+
})
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Checks a package directory's signature: its digest, Sigstore's signature and
|
|
301
|
+
* certificate against the trusted root, that GitHub Actions signed it, and that
|
|
302
|
+
* `provenance.json` claims what the certificate says. Unsigned when there is no
|
|
303
|
+
* `provenance.json`; invalid, with the reason, when anything disagrees.
|
|
304
|
+
*/
|
|
305
|
+
export async function verifyProvenance(
|
|
306
|
+
dir: string,
|
|
307
|
+
options: VerifyProvenanceOptions = {},
|
|
308
|
+
): Promise<ProvenanceResult> {
|
|
309
|
+
const file = join(dir, PROVENANCE_FILE)
|
|
310
|
+
if (!existsSync(file)) return { state: 'unsigned' }
|
|
311
|
+
let provenance: ProvenanceFile
|
|
312
|
+
try {
|
|
313
|
+
provenance = JSON.parse(readFileSync(file, 'utf8')) as ProvenanceFile
|
|
314
|
+
} catch {
|
|
315
|
+
return invalid(`${PROVENANCE_FILE} is not JSON`)
|
|
316
|
+
}
|
|
317
|
+
if (provenance.format !== 1 || !provenance.signature)
|
|
318
|
+
return invalid(`${PROVENANCE_FILE} is not a format this release reads`)
|
|
319
|
+
const digest = packageDigest(dir)
|
|
320
|
+
if (provenance.digest !== digest)
|
|
321
|
+
return invalid('the bundle has changed since it was signed', digest)
|
|
322
|
+
|
|
323
|
+
let check = options.verifySignature
|
|
324
|
+
if (!check) {
|
|
325
|
+
let root: TrustedRoot
|
|
326
|
+
try {
|
|
327
|
+
root =
|
|
328
|
+
options.trustedRoot !== undefined
|
|
329
|
+
? TrustedRoot.fromJSON(options.trustedRoot)
|
|
330
|
+
: await publicTrustedRoot()
|
|
331
|
+
} catch (error) {
|
|
332
|
+
return invalid(
|
|
333
|
+
`Sigstore's trusted root could not be fetched, so the signature cannot be checked: ${error instanceof Error ? error.message : String(error)}`,
|
|
334
|
+
digest,
|
|
335
|
+
)
|
|
336
|
+
}
|
|
337
|
+
check = (signature) => {
|
|
338
|
+
new Verifier(toTrustMaterial(root)).verify(toSignedEntity(bundleFromJSON(signature)), {
|
|
339
|
+
extensions: { issuer: GITHUB_ISSUER },
|
|
340
|
+
})
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
let signed: Buffer
|
|
344
|
+
try {
|
|
345
|
+
await check(provenance.signature)
|
|
346
|
+
const envelope = (provenance.signature as { dsseEnvelope?: { payload: string } }).dsseEnvelope
|
|
347
|
+
if (!envelope) return invalid('the signature is not over a statement', digest)
|
|
348
|
+
signed = Buffer.from(envelope.payload, 'base64')
|
|
349
|
+
} catch (error) {
|
|
350
|
+
return invalid(
|
|
351
|
+
`the signature does not check out: ${error instanceof Error ? error.message : String(error)}`,
|
|
352
|
+
digest,
|
|
353
|
+
)
|
|
354
|
+
}
|
|
355
|
+
let subject: { digest?: { sha256?: string } } | undefined
|
|
356
|
+
let predicateType: unknown
|
|
357
|
+
try {
|
|
358
|
+
const parsed = JSON.parse(signed.toString('utf8')) as {
|
|
359
|
+
subject?: Array<{ digest?: { sha256?: string } }>
|
|
360
|
+
predicateType?: unknown
|
|
361
|
+
}
|
|
362
|
+
subject = parsed.subject?.[0]
|
|
363
|
+
predicateType = parsed.predicateType
|
|
364
|
+
} catch {
|
|
365
|
+
return invalid('the signed statement is not JSON', digest)
|
|
366
|
+
}
|
|
367
|
+
if (predicateType !== PROVENANCE_PREDICATE)
|
|
368
|
+
return invalid('the signature is not a Crombolo bundle signature', digest)
|
|
369
|
+
if (subject?.digest?.sha256 !== hexOf(digest))
|
|
370
|
+
return invalid('the signature is for a different bundle', digest)
|
|
371
|
+
|
|
372
|
+
const source = sourceOf(provenance.signature)
|
|
373
|
+
if (!source) return invalid('the certificate names no GitHub repository', digest)
|
|
374
|
+
const claimed = provenance.source
|
|
375
|
+
for (const key of ['repository', 'ref', 'commit'] as const)
|
|
376
|
+
if (claimed?.[key] !== source[key])
|
|
377
|
+
return invalid(
|
|
378
|
+
`${PROVENANCE_FILE} says ${key} ${claimed?.[key] ?? '(none)'}, and the certificate says ${source[key]}`,
|
|
379
|
+
digest,
|
|
380
|
+
)
|
|
381
|
+
return { state: 'signed', digest, source, links: linksFor(source, provenance.signature) }
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export type SignaturePolicy = 'optional' | 'required'
|
|
385
|
+
|
|
386
|
+
export interface ProvenancePolicy {
|
|
387
|
+
signatures: SignaturePolicy
|
|
388
|
+
/** `org/*`, `org/repo` or `org/repo@<ref pattern>`; empty takes any source. */
|
|
389
|
+
sources: readonly string[]
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** Whether a signed bundle's source matches one entry of `CROMBOLO_BUNDLE_SOURCES`. */
|
|
393
|
+
export function sourceAllowed(source: ProvenanceSource, entry: string): boolean {
|
|
394
|
+
const [repoPattern, refPattern] = entry.split('@') as [string, string | undefined]
|
|
395
|
+
if (!new Bun.Glob(repoPattern.toLowerCase()).match(source.repository.toLowerCase())) return false
|
|
396
|
+
return refPattern === undefined || new Bun.Glob(refPattern).match(refName(source.ref))
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Why the site will not take this bundle, or undefined when it will. An
|
|
401
|
+
* invalid signature is refused whatever the policy; `local` is the site's own.
|
|
402
|
+
*/
|
|
403
|
+
export function provenanceProblem(
|
|
404
|
+
result: ProvenanceResult,
|
|
405
|
+
policy: ProvenancePolicy,
|
|
406
|
+
): string | undefined {
|
|
407
|
+
if (result.state === 'invalid')
|
|
408
|
+
return `its signature was not accepted: ${result.reason ?? 'it does not check out'}`
|
|
409
|
+
if (result.state === 'local') return undefined
|
|
410
|
+
if (result.state === 'unsigned')
|
|
411
|
+
return policy.signatures === 'required'
|
|
412
|
+
? 'this site takes only signed bundles (CROMBOLO_BUNDLE_SIGNATURES=required); the producer signs it with `crombolo bundle sign` in GitHub Actions'
|
|
413
|
+
: undefined
|
|
414
|
+
const source = result.source as ProvenanceSource
|
|
415
|
+
if (policy.sources.length > 0 && !policy.sources.some((entry) => sourceAllowed(source, entry)))
|
|
416
|
+
return `it was built from github.com/${source.repository} at ${refName(source.ref)}, and this site takes signed bundles only from ${policy.sources.join(', ')} (CROMBOLO_BUNDLE_SOURCES)`
|
|
417
|
+
return undefined
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** A `.tgz` unpacked into a temporary directory for `work`, which is removed afterwards. */
|
|
421
|
+
async function withUnpacked<T>(file: string, work: (dir: string) => Promise<T>): Promise<T> {
|
|
422
|
+
const { mkdtemp, rm } = await import('node:fs/promises')
|
|
423
|
+
const { tmpdir } = await import('node:os')
|
|
424
|
+
const { extractTarball } = await import('./tar.ts')
|
|
425
|
+
const dir = await mkdtemp(join(tmpdir(), 'crombolo-provenance-'))
|
|
426
|
+
try {
|
|
427
|
+
await extractTarball(file, dir)
|
|
428
|
+
return await work(dir)
|
|
429
|
+
} finally {
|
|
430
|
+
await rm(dir, { recursive: true, force: true })
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
export const isTarball = (path: string): boolean => /\.(tgz|tar\.gz)$/i.test(path)
|
|
435
|
+
|
|
436
|
+
/** The digest of a package directory or `.tgz`. */
|
|
437
|
+
export async function digestOf(path: string): Promise<string> {
|
|
438
|
+
return isTarball(path)
|
|
439
|
+
? withUnpacked(path, async (dir) => packageDigest(dir))
|
|
440
|
+
: packageDigest(path)
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** Checks a package directory or `.tgz`. */
|
|
444
|
+
export async function verifyPackage(
|
|
445
|
+
path: string,
|
|
446
|
+
options: VerifyProvenanceOptions = {},
|
|
447
|
+
): Promise<ProvenanceResult> {
|
|
448
|
+
return isTarball(path)
|
|
449
|
+
? withUnpacked(path, (dir) => verifyProvenance(dir, options))
|
|
450
|
+
: verifyProvenance(path, options)
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** Signs a package directory, or a `.tgz` in place with `provenance.json` added to it. */
|
|
454
|
+
export async function signPath(path: string, options: SignOptions = {}): Promise<ProvenanceFile> {
|
|
455
|
+
if (!isTarball(path)) return signPackage(path, options)
|
|
456
|
+
const { writeNpmTarball } = await import('./tar.ts')
|
|
457
|
+
return withUnpacked(path, async (dir) => {
|
|
458
|
+
const signed = await signPackage(dir, options)
|
|
459
|
+
const entries = [...packageFiles(dir), PROVENANCE_FILE].map((file) => ({
|
|
460
|
+
path: file,
|
|
461
|
+
bytes: new Uint8Array(readFileSync(join(dir, file))),
|
|
462
|
+
}))
|
|
463
|
+
writeFileSync(path, writeNpmTarball(entries))
|
|
464
|
+
return signed
|
|
465
|
+
})
|
|
466
|
+
}
|