@rathnasgala2/adapter-github-pages 0.1.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/CHANGELOG.md +125 -0
- package/README.md +202 -0
- package/package.json +39 -0
- package/src/artifact-projection.js +78 -0
- package/src/capability.js +229 -0
- package/src/carrier.js +216 -0
- package/src/constants.js +156 -0
- package/src/deployment.js +333 -0
- package/src/index.d.ts +246 -0
- package/src/index.js +977 -0
- package/src/marker.js +73 -0
- package/src/oidc.js +515 -0
- package/src/public-read.js +61 -0
- package/src/recovery.js +784 -0
- package/src/redaction.js +112 -0
- package/src/request-catalog.js +417 -0
- package/src/rest.js +360 -0
- package/src/store.js +103 -0
package/src/marker.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `public-generation-marker:2.0.0` construction and validation for the
|
|
3
|
+
* managed Pages adapter. The marker root carries only the universal
|
|
4
|
+
* `schemaId`/`schemaVersion` plus `artifactId`, `artifactDigest` and
|
|
5
|
+
* `generationId` — no publication, repository, provider, timestamp,
|
|
6
|
+
* manifest digest, credential or self-digest (brief section 5) — and is
|
|
7
|
+
* validated against `@rathnasgala2/schemas`, the authoritative validator,
|
|
8
|
+
* rather than checked by hand.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { validateGalaDocument } from '@rathnasgala2/schemas';
|
|
14
|
+
|
|
15
|
+
/** The exact schema identity every marker is validated against. */
|
|
16
|
+
export const MARKER_SCHEMA_ID =
|
|
17
|
+
'urn:gala:schema:public-generation-marker:2.0.0';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Build one schema-valid marker, throwing rather than returning an invalid
|
|
21
|
+
* document: an invalid marker must never reach a carrier.
|
|
22
|
+
*
|
|
23
|
+
* @param {{artifactId: string, artifactDigest: string, generationId: string}} identity
|
|
24
|
+
* the marker's three identity fields
|
|
25
|
+
* @returns {Readonly<Record<string, unknown>>} the validated marker
|
|
26
|
+
*/
|
|
27
|
+
export function buildValidatedMarker(identity) {
|
|
28
|
+
const marker = Object.freeze({
|
|
29
|
+
schemaId: MARKER_SCHEMA_ID,
|
|
30
|
+
schemaVersion: '2.0.0',
|
|
31
|
+
artifactId: identity.artifactId,
|
|
32
|
+
artifactDigest: identity.artifactDigest,
|
|
33
|
+
generationId: identity.generationId,
|
|
34
|
+
});
|
|
35
|
+
const validation = validateGalaDocument(MARKER_SCHEMA_ID, marker);
|
|
36
|
+
if (!validation.valid) {
|
|
37
|
+
throw new Error(
|
|
38
|
+
`PAGES_MARKER_INVALID: ${JSON.stringify(validation.diagnostics)}`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
return marker;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Parse and validate marker bytes read back from the public origin.
|
|
46
|
+
*
|
|
47
|
+
* @param {Buffer} bytes the raw marker bytes
|
|
48
|
+
* @returns {{marker: Record<string, unknown> | null, findings: string[]}} the
|
|
49
|
+
* parsed marker, or `null` with one finding explaining why it was refused
|
|
50
|
+
*/
|
|
51
|
+
export function parsePublicMarker(bytes) {
|
|
52
|
+
/** @type {string[]} */
|
|
53
|
+
const findings = [];
|
|
54
|
+
/** @type {unknown} */
|
|
55
|
+
let parsed;
|
|
56
|
+
try {
|
|
57
|
+
parsed = JSON.parse(bytes.toString('utf8'));
|
|
58
|
+
} catch {
|
|
59
|
+
findings.push('public generation marker is not parseable JSON');
|
|
60
|
+
return { marker: null, findings };
|
|
61
|
+
}
|
|
62
|
+
const validation = validateGalaDocument(MARKER_SCHEMA_ID, parsed);
|
|
63
|
+
if (!validation.valid) {
|
|
64
|
+
findings.push(
|
|
65
|
+
`public generation marker failed schema validation: ${JSON.stringify(validation.diagnostics)}`,
|
|
66
|
+
);
|
|
67
|
+
return { marker: null, findings };
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
marker: /** @type {Record<string, unknown>} */ (parsed),
|
|
71
|
+
findings,
|
|
72
|
+
};
|
|
73
|
+
}
|
package/src/oidc.js
ADDED
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `gala-pages-oidc-v2` credential-source profile: the local, structural
|
|
3
|
+
* binding checks this adapter performs on the Pages OIDC JWT immediately
|
|
4
|
+
* before placing it — and only it — in the create-deployment request body.
|
|
5
|
+
*
|
|
6
|
+
* ## Trust boundary (read this before changing anything here)
|
|
7
|
+
*
|
|
8
|
+
* This adapter **never mints a credential**. The JWT is caller-supplied
|
|
9
|
+
* input: DEC-097 section 7 places the one token acquisition in the Pages
|
|
10
|
+
* environment job, which calls the GitHub-hosted runner URL from
|
|
11
|
+
* `ACTIONS_ID_TOKEN_REQUEST_URL` exactly once with
|
|
12
|
+
* `ACTIONS_ID_TOKEN_REQUEST_TOKEN` (that is what the job's `id-token: write`
|
|
13
|
+
* permission is for), against an origin that must byte-equal one member of
|
|
14
|
+
* the capability-authorized `githubActionsOidcOriginCatalog`.
|
|
15
|
+
*
|
|
16
|
+
* For this in-job, single-use credential the trust boundary is therefore
|
|
17
|
+
* that exact catalog-authorized, DNS/TLS-authenticated runner token endpoint
|
|
18
|
+
* and bearer exchange — **not** anything this module does. GitHub Pages is
|
|
19
|
+
* the relying party that cryptographically validates the JWT when it
|
|
20
|
+
* processes create-deployment. Accordingly this module **deliberately
|
|
21
|
+
* performs no issuer-signature or JWKS verification and makes no discovery,
|
|
22
|
+
* issuer-key or JWKS network call of any kind**; no such unbudgeted request,
|
|
23
|
+
* cache or key-set digest may be inferred from it. The checks below prevent
|
|
24
|
+
* a token minted for another bound context from being forwarded; they are
|
|
25
|
+
* expressly not an independent issuer-authenticity proof, and a stale or
|
|
26
|
+
* signature-substituted token is not a locally authenticated finding — it
|
|
27
|
+
* reaches the disposable Pages relying party, which must reject it before
|
|
28
|
+
* any candidate activation can be derived.
|
|
29
|
+
*
|
|
30
|
+
* ## What is checked
|
|
31
|
+
*
|
|
32
|
+
* Three compact-JWT segments in canonical unpadded base64url; duplicate-key-
|
|
33
|
+
* free JSON object header and payload; and the exact binding claims: issuer,
|
|
34
|
+
* audience `https://github.com/<repository_owner>`, one of the two supported
|
|
35
|
+
* default-environment subject forms with every inserted component recomputed
|
|
36
|
+
* from the separately verified `repository`, `repository_owner`,
|
|
37
|
+
* `repository_id` and `repository_owner_id` claims, `environment`, and the
|
|
38
|
+
* caller-supplied ref/SHA/run id/run attempt/`job_workflow_ref`/
|
|
39
|
+
* `job_workflow_sha` expectations.
|
|
40
|
+
*
|
|
41
|
+
* No error raised here ever contains a token byte.
|
|
42
|
+
*
|
|
43
|
+
* @module
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/** The closed credential-source profile this module implements. */
|
|
47
|
+
export const PAGES_OIDC_PROFILE = 'gala-pages-oidc-v2';
|
|
48
|
+
|
|
49
|
+
/** The exact required `iss` claim. */
|
|
50
|
+
export const PAGES_OIDC_ISSUER = 'https://token.actions.githubusercontent.com';
|
|
51
|
+
|
|
52
|
+
/** The exact required `environment` claim and subject environment segment. */
|
|
53
|
+
export const PAGES_OIDC_ENVIRONMENT = 'github-pages';
|
|
54
|
+
|
|
55
|
+
/** Inclusive byte bounds on the compact JWT (DEC-097 section 7). */
|
|
56
|
+
export const PAGES_OIDC_MINIMUM_BYTES = 1;
|
|
57
|
+
/** Inclusive upper byte bound on the compact JWT. */
|
|
58
|
+
export const PAGES_OIDC_MAXIMUM_BYTES = 8000;
|
|
59
|
+
|
|
60
|
+
const BASE64URL_SEGMENT = /^[A-Za-z0-9_-]+$/u;
|
|
61
|
+
const CANONICAL_DECIMAL = /^(?:0|[1-9][0-9]*)$/u;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @typedef {Readonly<{
|
|
65
|
+
* owner: string,
|
|
66
|
+
* repository: string,
|
|
67
|
+
* repositoryId: string | number,
|
|
68
|
+
* repositoryOwnerId: string | number,
|
|
69
|
+
* ref: string,
|
|
70
|
+
* sha: string,
|
|
71
|
+
* runId: string | number,
|
|
72
|
+
* runAttempt: string | number,
|
|
73
|
+
* jobWorkflowRef: string,
|
|
74
|
+
* jobWorkflowSha: string
|
|
75
|
+
* }>} PagesOidcExpectation
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Raise a typed, token-free failure.
|
|
80
|
+
*
|
|
81
|
+
* @param {string} code the stable error code
|
|
82
|
+
* @param {string} detail a detail that contains no token byte
|
|
83
|
+
* @returns {never} never returns
|
|
84
|
+
*/
|
|
85
|
+
function refuse(code, detail) {
|
|
86
|
+
throw new Error(`${code}: ${detail}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Decode one canonical unpadded base64url segment.
|
|
91
|
+
*
|
|
92
|
+
* @param {string} segment the segment text
|
|
93
|
+
* @param {string} label the segment name, for the diagnostic
|
|
94
|
+
* @returns {Buffer} the decoded bytes
|
|
95
|
+
*/
|
|
96
|
+
export function decodeCanonicalBase64Url(segment, label) {
|
|
97
|
+
if (!BASE64URL_SEGMENT.test(segment)) {
|
|
98
|
+
refuse(
|
|
99
|
+
'PAGES_OIDC_TOKEN_MALFORMED',
|
|
100
|
+
`the ${label} segment is not unpadded base64url`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
const bytes = Buffer.from(segment, 'base64url');
|
|
104
|
+
if (bytes.toString('base64url') !== segment) {
|
|
105
|
+
refuse(
|
|
106
|
+
'PAGES_OIDC_TOKEN_MALFORMED',
|
|
107
|
+
`the ${label} segment is not canonical unpadded base64url`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
return bytes;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Parse one JSON object, refusing any duplicate member name at any depth.
|
|
115
|
+
*
|
|
116
|
+
* `JSON.parse` silently keeps the last of two identical member names, which
|
|
117
|
+
* would let a crafted token present one `sub` to a scanner and another to a
|
|
118
|
+
* parser. This scanner rejects that before the value is used.
|
|
119
|
+
*
|
|
120
|
+
* @param {string} text the JSON text
|
|
121
|
+
* @param {string} label the value's name, for the diagnostic
|
|
122
|
+
* @returns {Record<string, unknown>} the parsed object
|
|
123
|
+
*/
|
|
124
|
+
export function parseDuplicateKeyFreeJsonObject(text, label) {
|
|
125
|
+
/** @type {unknown} */
|
|
126
|
+
let parsed;
|
|
127
|
+
try {
|
|
128
|
+
parsed = JSON.parse(text);
|
|
129
|
+
} catch {
|
|
130
|
+
refuse('PAGES_OIDC_TOKEN_MALFORMED', `the ${label} is not JSON`);
|
|
131
|
+
}
|
|
132
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
133
|
+
refuse('PAGES_OIDC_TOKEN_MALFORMED', `the ${label} is not a JSON object`);
|
|
134
|
+
}
|
|
135
|
+
assertNoDuplicateKeys(text, label);
|
|
136
|
+
return /** @type {Record<string, unknown>} */ (parsed);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Scan JSON text and refuse a repeated member name within one object.
|
|
141
|
+
*
|
|
142
|
+
* @param {string} text the JSON text, already known to parse
|
|
143
|
+
* @param {string} label the value's name, for the diagnostic
|
|
144
|
+
* @returns {void}
|
|
145
|
+
*/
|
|
146
|
+
function assertNoDuplicateKeys(text, label) {
|
|
147
|
+
/** @type {Set<string>[]} */
|
|
148
|
+
const scopes = [];
|
|
149
|
+
/** @type {('object' | 'array')[]} */
|
|
150
|
+
const kinds = [];
|
|
151
|
+
let index = 0;
|
|
152
|
+
let expectingKey = false;
|
|
153
|
+
while (index < text.length) {
|
|
154
|
+
const character = text[index];
|
|
155
|
+
if (character === '{') {
|
|
156
|
+
scopes.push(new Set());
|
|
157
|
+
kinds.push('object');
|
|
158
|
+
expectingKey = true;
|
|
159
|
+
index += 1;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (character === '[') {
|
|
163
|
+
kinds.push('array');
|
|
164
|
+
expectingKey = false;
|
|
165
|
+
index += 1;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (character === '}' || character === ']') {
|
|
169
|
+
if (kinds.pop() === 'object') {
|
|
170
|
+
scopes.pop();
|
|
171
|
+
}
|
|
172
|
+
expectingKey = false;
|
|
173
|
+
index += 1;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (character === ',') {
|
|
177
|
+
expectingKey = kinds[kinds.length - 1] === 'object';
|
|
178
|
+
index += 1;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (character === '"') {
|
|
182
|
+
const { value, next } = readJsonString(text, index);
|
|
183
|
+
if (expectingKey) {
|
|
184
|
+
const scope = scopes[scopes.length - 1];
|
|
185
|
+
if (scope !== undefined && scope.has(value)) {
|
|
186
|
+
refuse(
|
|
187
|
+
'PAGES_OIDC_TOKEN_MALFORMED',
|
|
188
|
+
`the ${label} repeats the member name ${JSON.stringify(value)}`,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
scope?.add(value);
|
|
192
|
+
expectingKey = false;
|
|
193
|
+
}
|
|
194
|
+
index = next;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
index += 1;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Read one JSON string literal starting at an opening quote.
|
|
203
|
+
*
|
|
204
|
+
* @param {string} text the JSON text
|
|
205
|
+
* @param {number} start the index of the opening quote
|
|
206
|
+
* @returns {{value: string, next: number}} the decoded value and next index
|
|
207
|
+
*/
|
|
208
|
+
function readJsonString(text, start) {
|
|
209
|
+
let index = start + 1;
|
|
210
|
+
let raw = '';
|
|
211
|
+
while (index < text.length) {
|
|
212
|
+
const character = text[index];
|
|
213
|
+
if (character === '\\') {
|
|
214
|
+
raw += text.slice(index, index + 2);
|
|
215
|
+
index += 2;
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (character === '"') {
|
|
219
|
+
return {
|
|
220
|
+
value: /** @type {string} */ (JSON.parse(`"${raw}"`)),
|
|
221
|
+
next: index + 1,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
raw += character;
|
|
225
|
+
index += 1;
|
|
226
|
+
}
|
|
227
|
+
return refuse(
|
|
228
|
+
'PAGES_OIDC_TOKEN_MALFORMED',
|
|
229
|
+
'an unterminated JSON string literal',
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Read one required string claim.
|
|
235
|
+
*
|
|
236
|
+
* @param {Record<string, unknown>} payload the parsed payload
|
|
237
|
+
* @param {string} name the claim name
|
|
238
|
+
* @returns {string} the claim value
|
|
239
|
+
*/
|
|
240
|
+
function requireStringClaim(payload, name) {
|
|
241
|
+
const value = payload[name];
|
|
242
|
+
if (typeof value !== 'string' || value === '') {
|
|
243
|
+
refuse(
|
|
244
|
+
'PAGES_OIDC_CLAIM_MISSING',
|
|
245
|
+
`the ${JSON.stringify(name)} claim is absent or not a non-empty string`,
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
return /** @type {string} */ (value);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Normalize a claim or expectation that must be a canonical decimal string.
|
|
253
|
+
*
|
|
254
|
+
* @param {unknown} value the claim or expectation value
|
|
255
|
+
* @param {string} name the claim name, for the diagnostic
|
|
256
|
+
* @returns {string} the canonical decimal string
|
|
257
|
+
*/
|
|
258
|
+
function requireCanonicalDecimal(value, name) {
|
|
259
|
+
const text =
|
|
260
|
+
typeof value === 'number' && Number.isSafeInteger(value) && value >= 0
|
|
261
|
+
? String(value)
|
|
262
|
+
: value;
|
|
263
|
+
if (typeof text !== 'string' || !CANONICAL_DECIMAL.test(text)) {
|
|
264
|
+
refuse(
|
|
265
|
+
'PAGES_OIDC_CLAIM_INVALID',
|
|
266
|
+
`${JSON.stringify(name)} is not a canonical decimal value`,
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
return /** @type {string} */ (text);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Compare one claim against its expectation without echoing either value's
|
|
274
|
+
* observed bytes where the claim could carry caller content.
|
|
275
|
+
*
|
|
276
|
+
* @param {string} observed the claim value
|
|
277
|
+
* @param {string} expected the expected value
|
|
278
|
+
* @param {string} name the claim name
|
|
279
|
+
* @param {string} code the stable code to raise on disagreement
|
|
280
|
+
* @returns {void}
|
|
281
|
+
*/
|
|
282
|
+
function requireClaimEquals(observed, expected, name, code) {
|
|
283
|
+
if (observed !== expected) {
|
|
284
|
+
refuse(
|
|
285
|
+
code,
|
|
286
|
+
`the ${JSON.stringify(name)} claim does not byte-equal the expected ${JSON.stringify(expected)}`,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* The two GitHub-supported default-environment subject forms, recomputed
|
|
293
|
+
* from the separately verified identity claims.
|
|
294
|
+
*
|
|
295
|
+
* @param {{
|
|
296
|
+
* owner: string,
|
|
297
|
+
* repository: string,
|
|
298
|
+
* repositoryId: string,
|
|
299
|
+
* repositoryOwnerId: string
|
|
300
|
+
* }} identity the verified identity claims
|
|
301
|
+
* @returns {{name: string, id: string}} the two admitted subject forms
|
|
302
|
+
*/
|
|
303
|
+
export function buildSubjectForms(identity) {
|
|
304
|
+
return {
|
|
305
|
+
name: `repo:${identity.owner}/${identity.repository}:environment:${PAGES_OIDC_ENVIRONMENT}`,
|
|
306
|
+
id: `repo:${identity.owner}@${identity.repositoryOwnerId}/${identity.repository}@${identity.repositoryId}:environment:${PAGES_OIDC_ENVIRONMENT}`,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Verify one caller-supplied Pages OIDC JWT against its expected bindings.
|
|
312
|
+
*
|
|
313
|
+
* Performs no network call and no signature verification; see this module's
|
|
314
|
+
* header for why that is correct and where the real trust boundary is.
|
|
315
|
+
*
|
|
316
|
+
* @param {unknown} token the caller-supplied compact JWT
|
|
317
|
+
* @param {PagesOidcExpectation} expected the expected bindings
|
|
318
|
+
* @returns {Readonly<{
|
|
319
|
+
* profile: string,
|
|
320
|
+
* subjectForm: 'name' | 'identifier',
|
|
321
|
+
* issuerSignatureVerified: false,
|
|
322
|
+
* tokenByteCount: number
|
|
323
|
+
* }>} non-secret evidence about the accepted token
|
|
324
|
+
*/
|
|
325
|
+
export function verifyPagesOidcToken(token, expected) {
|
|
326
|
+
if (typeof token !== 'string' || token === '') {
|
|
327
|
+
refuse(
|
|
328
|
+
'PAGES_OIDC_TOKEN_REQUIRED',
|
|
329
|
+
'the Pages OIDC token is mandatory: DEC-097 section 7 makes oidc_token a required create-body member, and this adapter never mints one',
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
const tokenText = /** @type {string} */ (token);
|
|
333
|
+
const tokenByteCount = Buffer.byteLength(tokenText, 'utf8');
|
|
334
|
+
if (
|
|
335
|
+
tokenByteCount < PAGES_OIDC_MINIMUM_BYTES ||
|
|
336
|
+
tokenByteCount > PAGES_OIDC_MAXIMUM_BYTES
|
|
337
|
+
) {
|
|
338
|
+
refuse(
|
|
339
|
+
'PAGES_OIDC_TOKEN_MALFORMED',
|
|
340
|
+
`the token is ${tokenByteCount} bytes, outside the admitted ${PAGES_OIDC_MINIMUM_BYTES}..${PAGES_OIDC_MAXIMUM_BYTES}`,
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
const segments = tokenText.split('.');
|
|
344
|
+
if (segments.length !== 3) {
|
|
345
|
+
refuse(
|
|
346
|
+
'PAGES_OIDC_TOKEN_MALFORMED',
|
|
347
|
+
`the token has ${segments.length} compact segments, not 3`,
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
const headerBytes = decodeCanonicalBase64Url(
|
|
351
|
+
/** @type {string} */ (segments[0]),
|
|
352
|
+
'header',
|
|
353
|
+
);
|
|
354
|
+
// The signature segment must be canonical unpadded base64url, but it is
|
|
355
|
+
// deliberately never verified here: Pages is the relying party.
|
|
356
|
+
decodeCanonicalBase64Url(/** @type {string} */ (segments[2]), 'signature');
|
|
357
|
+
parseDuplicateKeyFreeJsonObject(headerBytes.toString('utf8'), 'JWT header');
|
|
358
|
+
const payload = parseDuplicateKeyFreeJsonObject(
|
|
359
|
+
decodeCanonicalBase64Url(
|
|
360
|
+
/** @type {string} */ (segments[1]),
|
|
361
|
+
'payload',
|
|
362
|
+
).toString('utf8'),
|
|
363
|
+
'JWT payload',
|
|
364
|
+
);
|
|
365
|
+
|
|
366
|
+
const repositoryOwner = requireStringClaim(payload, 'repository_owner');
|
|
367
|
+
const repositoryClaim = requireStringClaim(payload, 'repository');
|
|
368
|
+
const repositoryId = requireCanonicalDecimal(
|
|
369
|
+
payload.repository_id,
|
|
370
|
+
'repository_id',
|
|
371
|
+
);
|
|
372
|
+
const repositoryOwnerId = requireCanonicalDecimal(
|
|
373
|
+
payload.repository_owner_id,
|
|
374
|
+
'repository_owner_id',
|
|
375
|
+
);
|
|
376
|
+
requireClaimEquals(
|
|
377
|
+
repositoryOwner,
|
|
378
|
+
expected.owner,
|
|
379
|
+
'repository_owner',
|
|
380
|
+
'PAGES_OIDC_REPOSITORY_MISMATCH',
|
|
381
|
+
);
|
|
382
|
+
requireClaimEquals(
|
|
383
|
+
repositoryClaim,
|
|
384
|
+
`${expected.owner}/${expected.repository}`,
|
|
385
|
+
'repository',
|
|
386
|
+
'PAGES_OIDC_REPOSITORY_MISMATCH',
|
|
387
|
+
);
|
|
388
|
+
requireClaimEquals(
|
|
389
|
+
repositoryId,
|
|
390
|
+
requireCanonicalDecimal(expected.repositoryId, 'expected repositoryId'),
|
|
391
|
+
'repository_id',
|
|
392
|
+
'PAGES_OIDC_REPOSITORY_MISMATCH',
|
|
393
|
+
);
|
|
394
|
+
requireClaimEquals(
|
|
395
|
+
repositoryOwnerId,
|
|
396
|
+
requireCanonicalDecimal(
|
|
397
|
+
expected.repositoryOwnerId,
|
|
398
|
+
'expected repositoryOwnerId',
|
|
399
|
+
),
|
|
400
|
+
'repository_owner_id',
|
|
401
|
+
'PAGES_OIDC_REPOSITORY_MISMATCH',
|
|
402
|
+
);
|
|
403
|
+
|
|
404
|
+
requireClaimEquals(
|
|
405
|
+
requireStringClaim(payload, 'iss'),
|
|
406
|
+
PAGES_OIDC_ISSUER,
|
|
407
|
+
'iss',
|
|
408
|
+
'PAGES_OIDC_ISSUER_MISMATCH',
|
|
409
|
+
);
|
|
410
|
+
requireClaimEquals(
|
|
411
|
+
requireStringClaim(payload, 'aud'),
|
|
412
|
+
`https://github.com/${repositoryOwner}`,
|
|
413
|
+
'aud',
|
|
414
|
+
'PAGES_OIDC_AUDIENCE_MISMATCH',
|
|
415
|
+
);
|
|
416
|
+
requireClaimEquals(
|
|
417
|
+
requireStringClaim(payload, 'environment'),
|
|
418
|
+
PAGES_OIDC_ENVIRONMENT,
|
|
419
|
+
'environment',
|
|
420
|
+
'PAGES_OIDC_ENVIRONMENT_MISMATCH',
|
|
421
|
+
);
|
|
422
|
+
|
|
423
|
+
const forms = buildSubjectForms({
|
|
424
|
+
owner: repositoryOwner,
|
|
425
|
+
repository: repositoryClaim.slice(repositoryOwner.length + 1),
|
|
426
|
+
repositoryId,
|
|
427
|
+
repositoryOwnerId,
|
|
428
|
+
});
|
|
429
|
+
const subject = requireStringClaim(payload, 'sub');
|
|
430
|
+
/** @type {'name' | 'identifier' | undefined} */
|
|
431
|
+
let subjectForm;
|
|
432
|
+
if (subject === forms.name) {
|
|
433
|
+
subjectForm = 'name';
|
|
434
|
+
} else if (subject === forms.id) {
|
|
435
|
+
subjectForm = 'identifier';
|
|
436
|
+
} else {
|
|
437
|
+
refuse(
|
|
438
|
+
'PAGES_OIDC_SUBJECT_MISMATCH',
|
|
439
|
+
'the "sub" claim is neither of the two supported default-environment forms recomputed from the verified repository/owner/id claims; a hybrid, omitted, substituted or reordered name/ID form is refused',
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
requireClaimEquals(
|
|
444
|
+
requireStringClaim(payload, 'ref'),
|
|
445
|
+
expected.ref,
|
|
446
|
+
'ref',
|
|
447
|
+
'PAGES_OIDC_BINDING_MISMATCH',
|
|
448
|
+
);
|
|
449
|
+
requireClaimEquals(
|
|
450
|
+
requireStringClaim(payload, 'sha'),
|
|
451
|
+
expected.sha,
|
|
452
|
+
'sha',
|
|
453
|
+
'PAGES_OIDC_BINDING_MISMATCH',
|
|
454
|
+
);
|
|
455
|
+
requireClaimEquals(
|
|
456
|
+
requireCanonicalDecimal(payload.run_id, 'run_id'),
|
|
457
|
+
requireCanonicalDecimal(expected.runId, 'expected runId'),
|
|
458
|
+
'run_id',
|
|
459
|
+
'PAGES_OIDC_BINDING_MISMATCH',
|
|
460
|
+
);
|
|
461
|
+
requireClaimEquals(
|
|
462
|
+
requireCanonicalDecimal(payload.run_attempt, 'run_attempt'),
|
|
463
|
+
requireCanonicalDecimal(expected.runAttempt, 'expected runAttempt'),
|
|
464
|
+
'run_attempt',
|
|
465
|
+
'PAGES_OIDC_BINDING_MISMATCH',
|
|
466
|
+
);
|
|
467
|
+
requireClaimEquals(
|
|
468
|
+
requireStringClaim(payload, 'job_workflow_ref'),
|
|
469
|
+
expected.jobWorkflowRef,
|
|
470
|
+
'job_workflow_ref',
|
|
471
|
+
'PAGES_OIDC_BINDING_MISMATCH',
|
|
472
|
+
);
|
|
473
|
+
requireClaimEquals(
|
|
474
|
+
requireStringClaim(payload, 'job_workflow_sha'),
|
|
475
|
+
expected.jobWorkflowSha,
|
|
476
|
+
'job_workflow_sha',
|
|
477
|
+
'PAGES_OIDC_BINDING_MISMATCH',
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
return Object.freeze({
|
|
481
|
+
profile: PAGES_OIDC_PROFILE,
|
|
482
|
+
subjectForm: /** @type {'name' | 'identifier'} */ (subjectForm),
|
|
483
|
+
issuerSignatureVerified: /** @type {false} */ (false),
|
|
484
|
+
tokenByteCount,
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Validate one caller-supplied expectation record before it is used.
|
|
490
|
+
*
|
|
491
|
+
* @param {unknown} expected the caller-supplied expectation
|
|
492
|
+
* @returns {PagesOidcExpectation} the validated expectation
|
|
493
|
+
*/
|
|
494
|
+
export function requireOidcExpectation(expected) {
|
|
495
|
+
if (expected === null || typeof expected !== 'object') {
|
|
496
|
+
refuse(
|
|
497
|
+
'PAGES_OIDC_EXPECTATION_INVALID',
|
|
498
|
+
'the expected OIDC bindings record is required; this adapter cannot invent the ref, SHA, run identity or reusable-workflow identity a token must be bound to',
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
const record = /** @type {Record<string, unknown>} */ (expected);
|
|
502
|
+
for (const field of ['ref', 'sha', 'jobWorkflowRef', 'jobWorkflowSha']) {
|
|
503
|
+
if (typeof record[field] !== 'string' || record[field] === '') {
|
|
504
|
+
refuse(
|
|
505
|
+
'PAGES_OIDC_EXPECTATION_INVALID',
|
|
506
|
+
`the expected OIDC binding ${JSON.stringify(field)} (non-empty string) is required`,
|
|
507
|
+
);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
requireCanonicalDecimal(record.runId, 'expected runId');
|
|
511
|
+
requireCanonicalDecimal(record.runAttempt, 'expected runAttempt');
|
|
512
|
+
return /** @type {PagesOidcExpectation} */ (
|
|
513
|
+
/** @type {unknown} */ (Object.freeze({ ...record }))
|
|
514
|
+
);
|
|
515
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credential-free public reads of the activated Pages origin.
|
|
3
|
+
*
|
|
4
|
+
* These are the adapter's `public-http` and `generation-marker` verification
|
|
5
|
+
* capabilities. They carry no `authorization` header (the credential-egress
|
|
6
|
+
* profile declares zero credentials on the public origin), follow no
|
|
7
|
+
* redirect, and read a bounded body.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Maximum bytes any single public read will accept. */
|
|
13
|
+
export const MAXIMUM_PUBLIC_BODY_BYTES = 33554432;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {Readonly<{
|
|
17
|
+
* publicBaseUrl: string,
|
|
18
|
+
* fetch?: typeof globalThis.fetch
|
|
19
|
+
* }>} PublicContext
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Join the canonical base path to an artifact-relative route exactly once.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} publicBaseUrl the destination's public base URL
|
|
26
|
+
* @param {string} artifactRelativePath the artifact-relative POSIX path
|
|
27
|
+
* @returns {string} the absolute public URL
|
|
28
|
+
*/
|
|
29
|
+
export function publicUrlFor(publicBaseUrl, artifactRelativePath) {
|
|
30
|
+
const base = publicBaseUrl.endsWith('/')
|
|
31
|
+
? publicBaseUrl.slice(0, -1)
|
|
32
|
+
: publicBaseUrl;
|
|
33
|
+
return `${base}/${artifactRelativePath}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Read one public route.
|
|
38
|
+
*
|
|
39
|
+
* @param {PublicContext} context the public-origin context
|
|
40
|
+
* @param {string} artifactRelativePath the artifact-relative POSIX path
|
|
41
|
+
* @returns {Promise<{status: number, bytes: Buffer | null, etag: string | null}>}
|
|
42
|
+
* the bounded observation; `bytes` is `null` for any non-`200` status
|
|
43
|
+
*/
|
|
44
|
+
export async function readPublic(context, artifactRelativePath) {
|
|
45
|
+
const doFetch = context.fetch ?? globalThis.fetch;
|
|
46
|
+
const response = await doFetch(
|
|
47
|
+
publicUrlFor(context.publicBaseUrl, artifactRelativePath),
|
|
48
|
+
{ method: 'GET', redirect: 'error', cache: 'no-store' },
|
|
49
|
+
);
|
|
50
|
+
const etag = response.headers.get('etag');
|
|
51
|
+
if (response.status !== 200) {
|
|
52
|
+
return { status: response.status, bytes: null, etag };
|
|
53
|
+
}
|
|
54
|
+
const bytes = Buffer.from(await response.arrayBuffer());
|
|
55
|
+
if (bytes.byteLength > MAXIMUM_PUBLIC_BODY_BYTES) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
`PAGES_PUBLIC_BODY_TOO_LARGE: ${artifactRelativePath} returned ${bytes.byteLength} bytes`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return { status: 200, bytes, etag };
|
|
61
|
+
}
|