-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrity.ts
More file actions
299 lines (281 loc) · 9.59 KB
/
integrity.ts
File metadata and controls
299 lines (281 loc) · 9.59 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* @file Integrity + checksum helpers. The fleet uses two named hash flavors:
*
* - **integrity** — W3C Subresource Integrity string:
* `sha(256|384|512)-<base64>`. The same shape the npm registry returns and
* the `<script integrity>` HTML attribute accepts. Algorithm is embedded.
* - **checksum** — sha256 hex digest, exactly 64 lowercase chars. The shape
* produced by `shasum -a 256` and used in GitHub-release SHA256SUMS files.
* Conversion is direction-specific so the names read in English:
* `checksumToIntegrity(hex)` and `integrityToChecksum(sri)`. Both are
* idempotent on the destination format (pass an SRI to
* `checksumToIntegrity`, get the same SRI back) so callers can apply them
* without first sniffing the input shape. "SSRI" is just another name for
* Subresource Integrity — this module is the only one that should mention
* it, and only inside `parseIntegrity` to extract the embedded algorithm +
* body.
*/
import crypto from 'node:crypto'
import { hash as computeHash } from './crypto/hash'
import { BufferFrom, BufferPrototypeToString } from './primordials/buffer'
import { ErrorCtor, TypeErrorCtor } from './primordials/error'
/**
* Tagged union representing an expected hash.
*
* @example
* // Bare SRI (sniffed as integrity):
* 'sha256-NiCg/K+B7NOq7M1ZZZGdkNvJE/TQepbhHnyvwseFBUs='
*
* @example
* // Bare sha256 hex (sniffed as checksum):
* 'a1b2c3...'
*
* @example
* // Explicit:
* { type: 'integrity', value: 'sha512-...' }
* { type: 'checksum', value: 'a1b2c3...' }
*/
export type HashSpec =
| string
| { type: 'integrity'; value: string }
| { type: 'checksum'; value: string }
/**
* Normalized internal form. Always an object.
*/
export interface NormalizedHash {
type: 'integrity' | 'checksum'
value: string
}
/**
* Both hash formats for the same bytes. Returned from downloads so callers can
* record whichever format their config uses.
*/
export interface ComputedHashes {
/**
* SRI integrity: `sha512-<base64>`. Matches what the npm registry returns.
*/
integrity: string
/**
* Sha256 hex (64 chars). Matches `shasum -a 256`.
*/
checksum: string
}
/**
* Parsed components of an integrity string.
*/
export interface ParsedIntegrity {
/**
* SRI algorithm: `'sha256' | 'sha384' | 'sha512'`.
*/
algorithm: string
/**
* Base64-encoded digest body (everything after the `-`).
*/
body: string
}
// SRI accepts sha256 / sha384 / sha512 by spec — the algorithm prefix is
// part of the wire format, not a fleet convention. Restrict to those three
// (the W3C-blessed set) rather than parsing arbitrary `<algo>-<base64>`.
const INTEGRITY_RE = /^(sha(?:256|384|512))-([A-Za-z0-9+/]+=*)$/
const CHECKSUM_RE = /^[a-f0-9]{64}$/i
/**
* Convert a sha256 hex checksum to its SRI integrity form (`sha256-<base64>`).
* Idempotent on integrity input — call this on user-supplied data without first
* sniffing the format.
*
* The default algorithm is `'sha256'` because that's the fleet's checksum
* convention; pass an explicit algorithm if you have a hex digest from `sha384`
* or `sha512` (the function does not verify hex length against the algorithm —
* caller's responsibility).
*
* @example
* ;```typescript
* checksumToIntegrity(
* '3620a0fcaf81ecd3aaeccd5965919d90dbc913f4d07a96e11e7cafc2c785054b',
* )
* // 'sha256-NiCg/K+B7NOq7M1ZZZGdkNvJE/TQepbhHnyvwseFBUs='
*
* checksumToIntegrity('sha256-NiCg/K+B7NOq7M1ZZZGdkNvJE/TQepbhHnyvwseFBUs=')
* // 'sha256-NiCg/K+B7NOq7M1ZZZGdkNvJE/TQepbhHnyvwseFBUs=' (idempotent)
* ```
*
* @throws TypeError when the input is neither a recognized SRI nor a hex
* digest.
*/
export function checksumToIntegrity(
input: string,
algorithm = 'sha256',
): string {
if (isIntegrity(input)) {
return input
}
if (!/^[a-f0-9]+$/i.test(input)) {
throw new TypeErrorCtor(
`checksumToIntegrity: expected a hex digest or SRI string, got: ${input}`,
)
}
const body = BufferPrototypeToString!(BufferFrom!(input, 'hex'), 'base64')
return `${algorithm}-${body}`
}
/**
* Compute both integrity (sha512 SRI) and checksum (sha256 hex) for a buffer of
* bytes.
*/
export function computeHashes(bytes: Buffer): ComputedHashes {
const integrity = `sha512-${computeHash('sha512', bytes, 'base64')}`
const checksum = computeHash('sha256', bytes, 'hex')
return { integrity, checksum }
}
/**
* Convert a sha256 SRI integrity string to its hex checksum form (64 lowercase
* chars). Idempotent on checksum input.
*
* Throws on `sha384` and `sha512` SRI — checksums are sha256-only by fleet
* convention. Callers that need a hex digest for those algorithms can call
* `parseIntegrity(sri)` and decode `.body` manually.
*
* @example
* ;```typescript
* integrityToChecksum('sha256-NiCg/K+B7NOq7M1ZZZGdkNvJE/TQepbhHnyvwseFBUs=')
* // '3620a0fcaf81ecd3aaeccd5965919d90dbc913f4d07a96e11e7cafc2c785054b'
*
* integrityToChecksum(
* '3620a0fcaf81ecd3aaeccd5965919d90dbc913f4d07a96e11e7cafc2c785054b',
* )
* // '3620a0fcaf81ecd3aaeccd5965919d90dbc913f4d07a96e11e7cafc2c785054b' (idempotent)
* ```
*
* @throws TypeError when the input is neither a recognized SRI nor a hex
* checksum, or when the input is a non-sha256 SRI.
*/
export function integrityToChecksum(input: string): string {
if (isChecksum(input)) {
return input
}
const parsed = parseIntegrity(input)
if (parsed.algorithm !== 'sha256') {
throw new TypeErrorCtor(
`integrityToChecksum: ${parsed.algorithm} integrity has no 64-hex-char checksum form — checksums are sha256-only by fleet convention.`,
)
}
return BufferPrototypeToString!(BufferFrom!(parsed.body, 'base64'), 'hex')
}
/**
* True when `s` is a sha256 hex checksum (exactly 64 hex chars).
*/
export function isChecksum(s: string): boolean {
return CHECKSUM_RE.test(s)
}
/**
* True when `s` is a W3C SRI integrity string: `sha(256|384|512)-<base64>`.
*/
export function isIntegrity(s: string): boolean {
return INTEGRITY_RE.test(s)
}
/**
* Normalize a {@link HashSpec} to its canonical `{ type, value }` form.
*
* - Object form is trusted (its `value` is validated for shape).
* - Bare string matching SRI → integrity.
* - Bare string of 64 hex chars → checksum.
* - Anything else throws TypeError.
*
* @throws TypeError if the string is not a recognized format, or if an explicit
* object's value doesn't match its declared type.
*/
export function normalizeHash(spec: HashSpec): NormalizedHash {
if (typeof spec === 'object' && spec !== null) {
if (spec.type === 'integrity') {
if (!isIntegrity(spec.value)) {
throw new TypeErrorCtor(
`normalizeHash: expected SRI integrity "sha(256|384|512)-<base64>", got: ${spec.value}`,
)
}
return { type: 'integrity', value: spec.value }
}
if (spec.type === 'checksum') {
if (!isChecksum(spec.value)) {
throw new TypeErrorCtor(
`normalizeHash: expected sha256 hex checksum (64 hex chars), got: ${spec.value}`,
)
}
return { type: 'checksum', value: spec.value }
}
throw new TypeErrorCtor(
`normalizeHash: unknown hash type: ${(spec as { type: unknown }).type}`,
)
}
if (typeof spec !== 'string') {
throw new TypeErrorCtor(
`normalizeHash: expected string or { type, value }, got: ${typeof spec}`,
)
}
if (isIntegrity(spec)) {
return { type: 'integrity', value: spec }
}
if (isChecksum(spec)) {
return { type: 'checksum', value: spec }
}
throw new TypeErrorCtor(
`normalizeHash: unrecognized hash format. Expected SRI integrity ("sha(256|384|512)-<base64>") or sha256 hex checksum (64 hex chars), got: ${spec}`,
)
}
/**
* Split an integrity string into its `{ algorithm, body }` components. `body`
* is the base64-encoded digest (everything after the algorithm + dash).
*
* @example
* ;```typescript
* parseIntegrity('sha256-NiCg/K+B7NOq7M1ZZZGdkNvJE/TQepbhHnyvwseFBUs=')
* // { algorithm: 'sha256', body: 'NiCg/K+B7NOq7M1ZZZGdkNvJE/TQepbhHnyvwseFBUs=' }
* ```
*
* @throws Error when the input is not a valid SRI integrity string.
*/
export function parseIntegrity(sri: string): ParsedIntegrity {
const m = INTEGRITY_RE.exec(sri)
if (!m) {
throw new ErrorCtor(`parseIntegrity: invalid SRI format: ${sri}`)
}
return { algorithm: m[1]!, body: m[2]! }
}
/**
* Verify computed hashes against an expected {@link NormalizedHash}. Uses
* `crypto.timingSafeEqual` for constant-time comparison.
*
* @throws DlxHashMismatchError when the hash of the matching type doesn't match
* the expected value.
*/
export function verifyHash(
expected: NormalizedHash,
computed: ComputedHashes,
): void {
const actual =
expected.type === 'integrity' ? computed.integrity : computed.checksum
const expectedBuf = BufferFrom!(expected.value)
const actualBuf = BufferFrom!(actual)
if (
expectedBuf.length !== actualBuf.length ||
!crypto.timingSafeEqual(expectedBuf, actualBuf)
) {
throw new DlxHashMismatchError(expected, computed)
}
}
/**
* Thrown when an expected hash doesn't match the computed hash of the
* downloaded bytes. Carries both sides for diagnostics.
*/
export class DlxHashMismatchError extends Error {
readonly expected: NormalizedHash
readonly actual: ComputedHashes
constructor(expected: NormalizedHash, actual: ComputedHashes) {
const actualValue =
expected.type === 'integrity' ? actual.integrity : actual.checksum
super(
`Hash mismatch (${expected.type}): expected ${expected.value}, got ${actualValue}`,
)
this.name = 'DlxHashMismatchError'
this.expected = expected
this.actual = actual
}
}