-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathprovenance.ts
More file actions
142 lines (123 loc) · 5.13 KB
/
provenance.ts
File metadata and controls
142 lines (123 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import type { ProvenanceDetails } from '#shared/types'
const SLSA_PROVENANCE_V1 = 'https://slsa.dev/provenance/v1'
const SLSA_PROVENANCE_V0_2 = 'https://slsa.dev/provenance/v0.2'
const PROVIDER_IDS: Record<string, { provider: string; providerLabel: string }> = {
'https://github.com/actions/runner/github-hosted': {
provider: 'github',
providerLabel: 'GitHub Actions',
},
'https://github.com/actions/runner': { provider: 'github', providerLabel: 'GitHub Actions' },
}
/** GitLab uses project-specific builder IDs: https://gitlab.com/<path>/-/runners/<id> */
function getProviderInfo(builderId: string): { provider: string; providerLabel: string } {
const exact = PROVIDER_IDS[builderId]
if (exact) return exact
if (builderId.includes('gitlab.com') && builderId.includes('/runners/'))
return { provider: 'gitlab', providerLabel: 'GitLab CI' }
return { provider: 'unknown', providerLabel: builderId ? 'CI' : 'Unknown' }
}
const SIGSTORE_SEARCH_BASE = 'https://search.sigstore.dev'
/** SLSA provenance v1 predicate; optional v0.2 fields for fallback */
interface SlsaPredicate {
buildDefinition?: {
externalParameters?: {
workflow?: {
repository?: string
path?: string
ref?: string
}
}
resolvedDependencies?: Array<{
uri?: string
digest?: { gitCommit?: string }
}>
}
runDetails?: {
builder?: { id?: string }
metadata?: { invocationId?: string }
}
/** v0.2 */
builder?: { id?: string }
/** v0.2 */
metadata?: { buildInvocationId?: string }
}
interface AttestationItem {
predicateType?: string
bundle?: {
dsseEnvelope?: { payload?: string }
verificationMaterial?: {
tlogEntries?: Array<{ logIndex?: string }>
}
}
}
export interface NpmAttestationsResponse {
attestations?: AttestationItem[]
}
function decodePayload(
payloadBase64: string | undefined,
): { predicateType?: string; predicate?: SlsaPredicate } | null {
if (!payloadBase64 || typeof payloadBase64 !== 'string') return null
try {
const decoded = Buffer.from(payloadBase64, 'base64').toString('utf-8')
return JSON.parse(decoded) as { predicateType?: string; predicate?: SlsaPredicate }
} catch {
return null
}
}
function repoUrlToCommitUrl(repository: string, sha: string): string {
const normalized = repository.replace(/\/$/, '').replace(/\.git$/, '')
if (normalized.includes('github.com')) return `${normalized}/commit/${sha}`
if (normalized.includes('gitlab.com')) return `${normalized}/-/commit/${sha}`
return `${normalized}/commit/${sha}`
}
function repoUrlToBlobUrl(repository: string, path: string, ref = 'main'): string {
const normalized = repository.replace(/\/$/, '').replace(/\.git$/, '')
if (normalized.includes('github.com')) return `${normalized}/blob/${ref}/${path}`
if (normalized.includes('gitlab.com')) return `${normalized}/-/blob/${ref}/${path}`
return `${normalized}/blob/${ref}/${path}`
}
/**
* Parse npm attestations API response into ProvenanceDetails.
* Prefers SLSA provenance v1; falls back to v0.2 for provider label and ledger only (no source commit/build file from v0.2).
*/
export function parseAttestationToProvenanceDetails(response: unknown): ProvenanceDetails | null {
const body = response as NpmAttestationsResponse
const list = body?.attestations
if (!Array.isArray(list)) return null
const slsaAttestation =
list.find(a => a.predicateType === SLSA_PROVENANCE_V1) ??
list.find(a => a.predicateType === SLSA_PROVENANCE_V0_2)
if (!slsaAttestation?.bundle?.dsseEnvelope) return null
const payload = decodePayload(slsaAttestation.bundle.dsseEnvelope.payload)
if (!payload?.predicate) return null
const pred = payload.predicate as SlsaPredicate
const builderId = pred.runDetails?.builder?.id ?? pred.builder?.id ?? ''
const providerInfo = getProviderInfo(builderId)
const workflow = pred.buildDefinition?.externalParameters?.workflow
const repo = workflow?.repository?.replace(/\/$/, '').replace(/\.git$/, '') ?? ''
const workflowPath = workflow?.path ?? ''
const ref = workflow?.ref?.replace(/^refs\/heads\//, '').replace(/^refs\/tags\//, '') ?? 'main'
const resolved = pred.buildDefinition?.resolvedDependencies?.[0]
const commitSha = resolved?.digest?.gitCommit ?? ''
const rawInvocationId =
pred.runDetails?.metadata?.invocationId ?? pred.metadata?.buildInvocationId
const buildSummaryUrl =
rawInvocationId?.startsWith('http://') || rawInvocationId?.startsWith('https://')
? rawInvocationId
: undefined
const sourceCommitUrl = repo && commitSha ? repoUrlToCommitUrl(repo, commitSha) : undefined
const buildFileUrl = repo && workflowPath ? repoUrlToBlobUrl(repo, workflowPath, ref) : undefined
const tlogEntries = slsaAttestation.bundle.verificationMaterial?.tlogEntries
const logIndex = tlogEntries?.[0]?.logIndex
const publicLedgerUrl = logIndex ? `${SIGSTORE_SEARCH_BASE}/?logIndex=${logIndex}` : undefined
return {
provider: providerInfo.provider,
providerLabel: providerInfo.providerLabel,
buildSummaryUrl,
sourceCommitUrl,
sourceCommitSha: commitSha || undefined,
buildFileUrl,
buildFilePath: workflowPath || undefined,
publicLedgerUrl,
}
}