@coffre/core 0.14.5 → 0.15.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/dist/{access-BrdZKp1d.js → access-CgQTlUMz.js} +88 -1
- package/dist/{access-qkMZa-6Z.d.ts → access-DqLJjnLM.d.ts} +62 -1
- package/dist/access.d.ts +2 -2
- package/dist/access.js +2 -2
- package/dist/identity.d.ts +102 -2
- package/dist/identity.js +410 -161
- package/dist/mcp.d.ts +3 -2
- package/dist/mcp.js +6 -2
- package/dist/merkle.d.ts +456 -0
- package/dist/merkle.js +964 -0
- package/dist/schemas.d.ts +11 -3
- package/dist/schemas.js +12 -2
- package/dist/vault.d.ts +1151 -2
- package/dist/vault.js +274 -1
- package/dist/{workloads-CW2dK4AW.d.ts → workloads-D049JH0M.d.ts} +9 -1
- package/dist/workloads.d.ts +2 -2
- package/dist/workloads.js +9 -1
- package/package.json +8 -3
- package/src/access.ts +87 -0
- package/src/answers.ts +209 -0
- package/src/audit/leaf.ts +81 -0
- package/src/audit/merkle.ts +13 -0
- package/src/audit/note.ts +187 -0
- package/src/audit/pair.ts +86 -0
- package/src/audit/tiles.ts +197 -0
- package/src/audit/tree.ts +163 -0
- package/src/audit/verify.ts +393 -0
- package/src/audit/witness.ts +50 -0
- package/src/identity/index.ts +2 -0
- package/src/identity/profile-rules.ts +147 -0
- package/src/identity/run-proof.ts +159 -0
- package/src/identity/workloads.ts +11 -0
- package/src/mcp.ts +6 -2
- package/src/schemas.ts +11 -0
- package/src/vault.ts +931 -0
package/dist/identity.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BEARER_PROFILES, BindingInvalid, CREDENTIAL_NAME, EVENT_EXPOSURE, GITHUB_EVENTS, GITHUB_ISSUER, GITLAB_ISSUER, GITLAB_PIPELINE_SOURCES, GOOGLE_ISSUER, KEY_NAMING_PROFILES, MAX_BINDINGS, MAX_CLAIMS, MAX_CLAIM_BYTES, MAX_ISSUER_BYTES, MAX_READ_KEYS, MAX_READ_PLACES, MAX_SOURCES, OIDC_PROFILES, POLICY_DEFAULTS, POLICY_FIELDS, PROFILE_URNS, REPEATING_PROFILES, REQUIRED_CLAIMS, WORKLOAD_PROFILES, addressWithin, canonicalClaims, checkBinding, checkFetchUrl, checkPolicy, claimAt, claimValues, defaultIssuer, differingClaims, freezeTarget, fullRef, githubEventsAt, githubReusable, githubWorkflow, gitlabProject, isBearerProfile, parseAddress, parseCidr, policyText, workflowRef } from "./workloads.js";
|
|
1
|
+
import { BEARER_PROFILES, BindingInvalid, CREDENTIAL_NAME, EVENT_EXPOSURE, EXCHANGED_CREDENTIAL_MS, EXCHANGE_SKEW_SECONDS, GITHUB_EVENTS, GITHUB_ISSUER, GITLAB_ISSUER, GITLAB_PIPELINE_SOURCES, GOOGLE_ISSUER, KEY_NAMING_PROFILES, MAX_BINDINGS, MAX_CLAIMS, MAX_CLAIM_BYTES, MAX_ISSUER_BYTES, MAX_READ_KEYS, MAX_READ_PLACES, MAX_SOURCES, OIDC_PROFILES, POLICY_DEFAULTS, POLICY_FIELDS, PROFILE_URNS, REPEATING_PROFILES, REQUIRED_CLAIMS, SESSION_MS, WORKLOAD_PROFILES, WORKLOAD_SIGN_IN_PATH, addressWithin, canonicalClaims, checkBinding, checkFetchUrl, checkPolicy, claimAt, claimValues, defaultIssuer, differingClaims, freezeTarget, fullRef, githubEventsAt, githubReusable, githubWorkflow, gitlabProject, isBearerProfile, parseAddress, parseCidr, policyText, workflowRef } from "./workloads.js";
|
|
2
2
|
import { createCipheriv, createDecipheriv, createHash, hkdfSync, randomBytes } from "node:crypto";
|
|
3
3
|
import * as oauth from "oauth4webapi";
|
|
4
4
|
import { MAX_PROOF_LENGTH, MAX_PROOF_LENGTH as MAX_PROOF_LENGTH$1, MAX_SIGNED_HEADERS, MAX_SIGNED_HEADERS as MAX_SIGNED_HEADERS$1, METHOD_TOKEN, UNSIGNED_HEADERS, canonicalHtu, canonicalHtu as canonicalHtu$1, canonicalRequest, fetchMethod, fetchMethod as fetchMethod$1, fromBase64url, requestDigest, sentTarget, sentTarget as sentTarget$1, thumbprint, thumbprint as thumbprint$1, tokenDigest } from "@coffre/client/dpop";
|
|
@@ -1259,171 +1259,106 @@ async function verifyProof(proof, expect) {
|
|
|
1259
1259
|
};
|
|
1260
1260
|
}
|
|
1261
1261
|
//#endregion
|
|
1262
|
-
//#region src/identity/
|
|
1263
|
-
const
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
* others.
|
|
1272
|
-
*/
|
|
1273
|
-
function deriveKey(secret, purpose) {
|
|
1274
|
-
return Buffer.from(hkdfSync("sha256", secret, Buffer.alloc(0), `coffre/${purpose}`, 32));
|
|
1275
|
-
}
|
|
1276
|
-
/**
|
|
1277
|
-
* Encrypt a small JSON value for a round trip through the browser.
|
|
1278
|
-
*
|
|
1279
|
-
* AES-256-GCM authenticates as well as hides, so a tampered or forged value
|
|
1280
|
-
* fails to open rather than decoding into something else. The expiry sits
|
|
1281
|
-
* inside the ciphertext, which keeps an old cookie from being replayed past
|
|
1282
|
-
* it even by a client that ignores Max-Age.
|
|
1283
|
-
*/
|
|
1284
|
-
function seal(key, value, ttlSeconds, now = Date.now()) {
|
|
1285
|
-
const iv = randomBytes(IV_BYTES);
|
|
1286
|
-
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
1287
|
-
cipher.setAAD(Buffer.from(VERSION));
|
|
1288
|
-
const plaintext = Buffer.from(JSON.stringify({
|
|
1289
|
-
exp: now + ttlSeconds * 1e3,
|
|
1290
|
-
value
|
|
1291
|
-
}));
|
|
1292
|
-
const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
1293
|
-
return `${VERSION}.${Buffer.concat([
|
|
1294
|
-
iv,
|
|
1295
|
-
cipher.getAuthTag(),
|
|
1296
|
-
ciphertext
|
|
1297
|
-
]).toString("base64url")}`;
|
|
1298
|
-
}
|
|
1299
|
-
/** The sealed value, or null when it is malformed, forged, tampered with or expired. */
|
|
1300
|
-
function unseal(key, sealed, now = Date.now()) {
|
|
1301
|
-
if (!sealed?.startsWith(`${VERSION}.`)) return null;
|
|
1302
|
-
const body = sealed.slice(3);
|
|
1303
|
-
const raw = Buffer.from(body, "base64url");
|
|
1304
|
-
if (raw.length <= 28 || raw.toString("base64url") !== body) return null;
|
|
1305
|
-
try {
|
|
1306
|
-
const decipher = createDecipheriv("aes-256-gcm", key, raw.subarray(0, IV_BYTES));
|
|
1307
|
-
decipher.setAAD(Buffer.from(VERSION));
|
|
1308
|
-
decipher.setAuthTag(raw.subarray(IV_BYTES, 28));
|
|
1309
|
-
const plaintext = Buffer.concat([decipher.update(raw.subarray(28)), decipher.final()]);
|
|
1310
|
-
const parsed = JSON.parse(plaintext.toString("utf8"));
|
|
1311
|
-
if (typeof parsed.exp !== "number" || parsed.exp <= now) return null;
|
|
1312
|
-
return parsed.value;
|
|
1313
|
-
} catch {
|
|
1314
|
-
return null;
|
|
1315
|
-
}
|
|
1316
|
-
}
|
|
1317
|
-
//#endregion
|
|
1318
|
-
//#region src/identity/tokens.ts
|
|
1319
|
-
const PREFIX = {
|
|
1320
|
-
browser: "coffre_web_",
|
|
1321
|
-
cli: "coffre_cli_",
|
|
1322
|
-
service: "coffre_svc_",
|
|
1323
|
-
personal: "coffre_pat_",
|
|
1324
|
-
handle: "coffre_use_",
|
|
1325
|
-
workload: "coffre_wl_",
|
|
1326
|
-
enrollment: "coffre_enr_"
|
|
1327
|
-
};
|
|
1328
|
-
const TOKEN = /^coffre_(web|cli|svc|pat|use|wl|enr)_[A-Za-z0-9_-]{43}$/;
|
|
1329
|
-
function generateToken(kind) {
|
|
1330
|
-
return PREFIX[kind] + randomBytes(32).toString("base64url");
|
|
1331
|
-
}
|
|
1332
|
-
/** Whether a string has the shape of a coffre token, before any lookup. */
|
|
1333
|
-
function isCoffreToken(value) {
|
|
1334
|
-
return TOKEN.test(value);
|
|
1335
|
-
}
|
|
1336
|
-
/**
|
|
1337
|
-
* The kind a token's prefix says, before any lookup. Its hash covers the
|
|
1338
|
-
* prefix, so a token found under its hash is the kind it says.
|
|
1339
|
-
*/
|
|
1340
|
-
function tokenKind(token) {
|
|
1341
|
-
if (!TOKEN.test(token)) return null;
|
|
1342
|
-
return Object.keys(PREFIX).find((kind) => token.startsWith(PREFIX[kind])) ?? null;
|
|
1343
|
-
}
|
|
1344
|
-
function hashToken(token) {
|
|
1345
|
-
return createHash("sha256").update(token, "utf8").digest();
|
|
1346
|
-
}
|
|
1347
|
-
/**
|
|
1348
|
-
* Enough of a token to recognize it in a list, never enough to use it: the
|
|
1349
|
-
* kind prefix and the last four characters. The prefix is matched rather than
|
|
1350
|
-
* cut at the last underscore, because base64url uses `_` too.
|
|
1351
|
-
*/
|
|
1352
|
-
function tokenHint(token) {
|
|
1353
|
-
return `${Object.values(PREFIX).find((candidate) => token.startsWith(candidate)) ?? ""}…${token.slice(-4)}`;
|
|
1354
|
-
}
|
|
1355
|
-
//#endregion
|
|
1356
|
-
//#region src/identity/types.ts
|
|
1357
|
-
/** Access delivers the JWT to the origin in this header. */
|
|
1358
|
-
const ACCESS_JWT_HEADER = "cf-access-jwt-assertion";
|
|
1359
|
-
//#endregion
|
|
1360
|
-
//#region src/identity/verifier.ts
|
|
1262
|
+
//#region src/identity/profile-rules.ts
|
|
1263
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
1264
|
+
/** A Kubernetes namespace (a DNS label) and service account (a DNS subdomain), as its tokens' `sub` names them. */
|
|
1265
|
+
const SERVICE_ACCOUNT_SUB = /^system:serviceaccount:([a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?):([a-z0-9]([-a-z0-9.]{0,251}[a-z0-9])?)$/;
|
|
1266
|
+
/** A Google service account's unique ID: digits, 21 of them today. */
|
|
1267
|
+
const GOOGLE_ID = /^[1-9]\d{0,31}$/;
|
|
1268
|
+
const AZURE_V1 = /^https:\/\/sts\.windows\.net\/([0-9a-f-]{36})\/$/;
|
|
1269
|
+
const AZURE_V2 = /^https:\/\/login\.microsoftonline\.com\/([0-9a-f-]{36})\/v2\.0$/;
|
|
1270
|
+
const FLY_ISSUER = /^https:\/\/oidc\.fly\.io\/[a-z0-9][a-z0-9-]{0,62}$/;
|
|
1361
1271
|
/**
|
|
1362
|
-
*
|
|
1363
|
-
*
|
|
1364
|
-
*
|
|
1365
|
-
* header, which is how `alg: none` and RS256->HS256 confusion work. jose
|
|
1366
|
-
* defaults to permitting whatever the key supports; we do not rely on that.
|
|
1272
|
+
* Fly's IDs are digits in its documentation's tokens, unchecked live: kept
|
|
1273
|
+
* loose, which fails closed, since a token's `org_id` and `app_id` are IDs
|
|
1274
|
+
* and a binding naming a name matches none.
|
|
1367
1275
|
*/
|
|
1368
|
-
const
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
}
|
|
1372
|
-
/**
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
}
|
|
1396
|
-
|
|
1276
|
+
const FLY_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
1277
|
+
const VERCEL_ISSUER = /^https:\/\/oidc\.vercel\.com(\/[a-z0-9][a-z0-9-]{0,62})?$/;
|
|
1278
|
+
const VERCEL_OWNER = /^(team_)?[A-Za-z0-9]{8,64}$/;
|
|
1279
|
+
const VERCEL_PROJECT = /^prj_[A-Za-z0-9]{8,64}$/;
|
|
1280
|
+
/** `production`, `preview`, `development`, or a custom environment's slug. */
|
|
1281
|
+
const VERCEL_ENVIRONMENT = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
1282
|
+
const AWS_ACCOUNT = /^\d{12}$/;
|
|
1283
|
+
/** A role's unique ID: `AROA`, then its own characters. */
|
|
1284
|
+
const AWS_ROLE_ID = /^AROA[A-Z0-9]{8,124}$/;
|
|
1285
|
+
function shaped(claims, name, pattern, what) {
|
|
1286
|
+
if (!pattern.test(claims[name])) throw new BindingInvalid(`${name} is ${what}`);
|
|
1287
|
+
}
|
|
1288
|
+
const none = () => null;
|
|
1289
|
+
const PROFILE_RULES = {
|
|
1290
|
+
kubernetes: {
|
|
1291
|
+
binding({ claims }) {
|
|
1292
|
+
const sub = SERVICE_ACCOUNT_SUB.exec(claims.sub);
|
|
1293
|
+
if (sub === null) throw new BindingInvalid("sub is the service account's, system:serviceaccount:<namespace>:<name>");
|
|
1294
|
+
shaped(claims, "kubernetes.io.serviceaccount.uid", UUID, "the service account's UID, which a service account made again under the same name does not keep: kubectl get serviceaccount <name> -o jsonpath='{.metadata.uid}'");
|
|
1295
|
+
for (const [name, value] of [["kubernetes.io.namespace", sub[1]], ["kubernetes.io.serviceaccount.name", sub[3]]]) if (claims[name] !== void 0 && claims[name] !== value) throw new BindingInvalid(`${name} is ${value}, as its sub says`);
|
|
1296
|
+
},
|
|
1297
|
+
token(claims) {
|
|
1298
|
+
const sub = SERVICE_ACCOUNT_SUB.exec(String(claims.sub));
|
|
1299
|
+
if (sub === null) return "the token's sub is no service account's";
|
|
1300
|
+
const namespace = claimAt(claims, "kubernetes.io.namespace");
|
|
1301
|
+
const name = claimAt(claims, "kubernetes.io.serviceaccount.name");
|
|
1302
|
+
return namespace === sub[1] && name === sub[3] ? null : "the token names another service account under kubernetes.io than its sub";
|
|
1303
|
+
}
|
|
1304
|
+
},
|
|
1305
|
+
gcp: {
|
|
1306
|
+
binding({ claims }) {
|
|
1307
|
+
shaped(claims, "sub", GOOGLE_ID, "the service account's unique ID, its digits, which a service account made again under the same email does not keep: gcloud iam service-accounts describe <email> --format='value(uniqueId)'");
|
|
1308
|
+
},
|
|
1309
|
+
token: none
|
|
1310
|
+
},
|
|
1311
|
+
azure: {
|
|
1312
|
+
binding({ issuer, claims }, policy) {
|
|
1313
|
+
const tenant = AZURE_V1.exec(issuer)?.[1] ?? AZURE_V2.exec(issuer)?.[1];
|
|
1314
|
+
if (tenant === void 0 || !UUID.test(tenant)) throw new BindingInvalid("an azure binding's issuer is https://sts.windows.net/<tenant>/ (v1 tokens) or https://login.microsoftonline.com/<tenant>/v2.0 (v2), by the tenant's ID");
|
|
1315
|
+
shaped(claims, "tid", UUID, "the tenant's ID");
|
|
1316
|
+
if (claims.tid !== tenant) throw new BindingInvalid("tid is the tenant its issuer names");
|
|
1317
|
+
shaped(claims, "oid", UUID, "the managed identity's object ID (its principal ID): az identity show --query principalId");
|
|
1318
|
+
if (AZURE_V2.test(issuer) && !UUID.test(policy.audience ?? "")) throw new BindingInvalid("a v2 token's audience is the API's application (client) ID, a GUID, never its App ID URI: Entra puts no other in a v2 token");
|
|
1319
|
+
},
|
|
1320
|
+
token(claims, binding) {
|
|
1321
|
+
const version = AZURE_V1.test(binding.issuer) ? "1.0" : "2.0";
|
|
1322
|
+
return claims.ver === version ? null : `the token is of version ${JSON.stringify(claims.ver ?? null)}, and its binding's issuer issues ${version}`;
|
|
1323
|
+
}
|
|
1324
|
+
},
|
|
1325
|
+
fly: {
|
|
1326
|
+
binding({ issuer, claims }) {
|
|
1327
|
+
if (!FLY_ISSUER.test(issuer)) throw new BindingInvalid("a fly binding's issuer is https://oidc.fly.io/<organization>, by its slug");
|
|
1328
|
+
shaped(claims, "org_id", FLY_ID, "the organization's ID, as its tokens' org_id says it");
|
|
1329
|
+
shaped(claims, "app_id", FLY_ID, "the app's ID, as its tokens' app_id says it");
|
|
1330
|
+
},
|
|
1331
|
+
token: none
|
|
1332
|
+
},
|
|
1333
|
+
vercel: {
|
|
1334
|
+
binding({ issuer, claims }) {
|
|
1335
|
+
if (!VERCEL_ISSUER.test(issuer)) throw new BindingInvalid("a vercel binding's issuer is https://oidc.vercel.com/<team> (team mode) or https://oidc.vercel.com (global mode)");
|
|
1336
|
+
shaped(claims, "owner_id", VERCEL_OWNER, "the team's ID, team_…, as .vercel/project.json's orgId says it");
|
|
1337
|
+
shaped(claims, "project_id", VERCEL_PROJECT, "the project's ID, prj_…, as .vercel/project.json's projectId says it");
|
|
1338
|
+
shaped(claims, "environment", VERCEL_ENVIRONMENT, "production, preview, development or a custom environment's slug");
|
|
1339
|
+
},
|
|
1340
|
+
token: none
|
|
1341
|
+
},
|
|
1342
|
+
aws: {
|
|
1343
|
+
binding({ claims }) {
|
|
1344
|
+
const extra = Object.keys(claims).filter((name) => name !== "account" && name !== "role_id");
|
|
1345
|
+
if (extra.length > 0) throw new BindingInvalid(`an aws binding names its role's account and role_id alone, not ${extra.join(", ")}`);
|
|
1346
|
+
shaped(claims, "account", AWS_ACCOUNT, "the account's 12 digits");
|
|
1347
|
+
shaped(claims, "role_id", AWS_ROLE_ID, "the role's unique ID, AROA…, which a role made again under the same name does not keep: aws iam get-role --role-name <name> --query Role.RoleId");
|
|
1348
|
+
},
|
|
1349
|
+
token: none
|
|
1397
1350
|
}
|
|
1398
1351
|
};
|
|
1399
1352
|
/**
|
|
1400
|
-
*
|
|
1401
|
-
*
|
|
1402
|
-
*
|
|
1403
|
-
*
|
|
1404
|
-
*
|
|
1405
|
-
* with no name -- an audit row with a null actor answers no question worth
|
|
1406
|
-
* asking.
|
|
1353
|
+
* The audiences a token of a binding may name, the first the one its SDK
|
|
1354
|
+
* asks for: the session's key, `<publicUrl>#jkt=<thumbprint>`, for the
|
|
1355
|
+
* platforms whose caller picks the audience, and `?jkt=` for one that
|
|
1356
|
+
* refuses a `#` in it (section 4.2); the exact audience of a platform that
|
|
1357
|
+
* fixes it; this instance's URL for every other.
|
|
1407
1358
|
*/
|
|
1408
|
-
function
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
type: "service",
|
|
1412
|
-
id: commonName,
|
|
1413
|
-
commonName
|
|
1414
|
-
};
|
|
1415
|
-
const email = asNonEmptyString(claims.email);
|
|
1416
|
-
const subject = asNonEmptyString(claims.sub);
|
|
1417
|
-
if (email !== null && subject !== null) {
|
|
1418
|
-
const id = email.toLowerCase();
|
|
1419
|
-
return {
|
|
1420
|
-
type: "user",
|
|
1421
|
-
id,
|
|
1422
|
-
email: id,
|
|
1423
|
-
subject
|
|
1424
|
-
};
|
|
1425
|
-
}
|
|
1426
|
-
throw new Error("token verified but carries no usable identity: expected common_name (service) or sub+email (user)");
|
|
1359
|
+
function expectedAudiences(profile, policy, expected) {
|
|
1360
|
+
if (KEY_NAMING_PROFILES.includes(profile) || policy.keyAudience === true) return [`${expected.publicUrl}#jkt=${expected.jkt}`, `${expected.publicUrl}?jkt=${expected.jkt}`];
|
|
1361
|
+
return [policy.audience ?? expected.publicUrl];
|
|
1427
1362
|
}
|
|
1428
1363
|
//#endregion
|
|
1429
1364
|
//#region src/identity/workload-token.ts
|
|
@@ -1685,4 +1620,318 @@ function kidAndUse(jwk) {
|
|
|
1685
1620
|
};
|
|
1686
1621
|
}
|
|
1687
1622
|
//#endregion
|
|
1688
|
-
|
|
1623
|
+
//#region src/identity/run-proof.ts
|
|
1624
|
+
/**
|
|
1625
|
+
* The claims of the run a credential was issued for, as the issuer
|
|
1626
|
+
* asserted them: in the sign-in's record, so that a read leads back to its
|
|
1627
|
+
* run. What the issuer says, not proof of which run sent the request.
|
|
1628
|
+
*/
|
|
1629
|
+
const RUN_CLAIMS = [
|
|
1630
|
+
"sub",
|
|
1631
|
+
"jti",
|
|
1632
|
+
"actor",
|
|
1633
|
+
"event_name",
|
|
1634
|
+
"ref",
|
|
1635
|
+
"base_ref",
|
|
1636
|
+
"head_ref",
|
|
1637
|
+
"sha",
|
|
1638
|
+
"repository",
|
|
1639
|
+
"repository_id",
|
|
1640
|
+
"run_id",
|
|
1641
|
+
"run_attempt",
|
|
1642
|
+
"workflow_ref",
|
|
1643
|
+
"job_workflow_ref",
|
|
1644
|
+
"job_workflow_sha",
|
|
1645
|
+
"environment",
|
|
1646
|
+
"project_path",
|
|
1647
|
+
"project_id",
|
|
1648
|
+
"namespace_id",
|
|
1649
|
+
"pipeline_id",
|
|
1650
|
+
"pipeline_source",
|
|
1651
|
+
"job_id",
|
|
1652
|
+
"kubernetes.io.pod.name"
|
|
1653
|
+
];
|
|
1654
|
+
const MAX_RUN_CLAIM = 256;
|
|
1655
|
+
/** A binding's `jwks_uri` when it carries its issuer's keys: the SHA-256 of those keys (`UploadedKeys`), where a fetched set's URL would be. */
|
|
1656
|
+
const UPLOADED_KEYS = "sha256:";
|
|
1657
|
+
/** The run's claims, as the issuer stated them: those that say which run, each bounded. */
|
|
1658
|
+
function runClaims(claims) {
|
|
1659
|
+
const run = {};
|
|
1660
|
+
for (const name of RUN_CLAIMS) {
|
|
1661
|
+
const value = claimAt(claims, name);
|
|
1662
|
+
if (typeof value === "string") run[name] = value.slice(0, MAX_RUN_CLAIM);
|
|
1663
|
+
else if (typeof value === "number" && Number.isFinite(value)) run[name] = value;
|
|
1664
|
+
}
|
|
1665
|
+
return run;
|
|
1666
|
+
}
|
|
1667
|
+
/** A token's one audience, or null for none or several: a token for several parties is no single party's. */
|
|
1668
|
+
function soleAudience(aud) {
|
|
1669
|
+
if (typeof aud === "string") return aud;
|
|
1670
|
+
return Array.isArray(aud) && aud.length === 1 && typeof aud[0] === "string" ? aud[0] : null;
|
|
1671
|
+
}
|
|
1672
|
+
/** Why a run's proof buys nothing, as the app's refusals say it (`ExchangeReason`), with the key a refused token named. */
|
|
1673
|
+
var RunProofRefused = class extends Error {
|
|
1674
|
+
reason;
|
|
1675
|
+
kid;
|
|
1676
|
+
constructor(reason, message, kid = null) {
|
|
1677
|
+
super(message);
|
|
1678
|
+
this.name = "RunProofRefused";
|
|
1679
|
+
this.reason = reason;
|
|
1680
|
+
this.kid = kid;
|
|
1681
|
+
}
|
|
1682
|
+
};
|
|
1683
|
+
/** A token's `kid`, at most 64 characters, for a refusal and the alert it raises; the token is unverified. */
|
|
1684
|
+
function shownKid(header) {
|
|
1685
|
+
return typeof header.kid === "string" ? header.kid.slice(0, 64) : null;
|
|
1686
|
+
}
|
|
1687
|
+
/**
|
|
1688
|
+
* Verify a CI run's or a workload's ID token against one binding, sealed
|
|
1689
|
+
* (D148), whose keys were uploaded with it: the vault's own check, which
|
|
1690
|
+
* no app can steer, and which asks no one. The binding's keys, held to the
|
|
1691
|
+
* digest its seal covers (a key they lack is refused, never looked for),
|
|
1692
|
+
* its issuer, its audience (the instance it was made on, or, for a
|
|
1693
|
+
* platform whose caller names the session's key, that key: `jkt`, from the
|
|
1694
|
+
* DPoP proof the vault verified, never from the app), the token's times,
|
|
1695
|
+
* the binding's claims and its profile's rules. Throws `RunProofRefused`.
|
|
1696
|
+
*/
|
|
1697
|
+
async function verifyRunToken(token, binding, context) {
|
|
1698
|
+
let decoded;
|
|
1699
|
+
try {
|
|
1700
|
+
decoded = decodeWorkloadToken(token);
|
|
1701
|
+
} catch (error) {
|
|
1702
|
+
throw refusedBy(error);
|
|
1703
|
+
}
|
|
1704
|
+
const { claims: unverified, header } = decoded;
|
|
1705
|
+
if (unverified.iss !== binding.issuer) throw new RunProofRefused("no_match", `the token is from ${unverified.iss}, which this binding does not trust`);
|
|
1706
|
+
const policy = binding.policy === null ? null : JSON.parse(binding.policy);
|
|
1707
|
+
if (context.workload === null !== (policy === null)) throw new RunProofRefused("no_match", "a CI run signs in through a binding with no policy, a workload through one with");
|
|
1708
|
+
const accepted = context.workload === null ? [binding.origin] : expectedAudiences(binding.profile, policy, {
|
|
1709
|
+
publicUrl: binding.origin,
|
|
1710
|
+
jkt: context.workload.jkt
|
|
1711
|
+
});
|
|
1712
|
+
const named = soleAudience(unverified.aud);
|
|
1713
|
+
const audience = named !== null && accepted.includes(named) ? named : accepted[0];
|
|
1714
|
+
const repeating = context.workload !== null && REPEATING_PROFILES.includes(binding.profile);
|
|
1715
|
+
const expected = {
|
|
1716
|
+
issuer: binding.issuer,
|
|
1717
|
+
audience,
|
|
1718
|
+
now: context.now,
|
|
1719
|
+
...repeating ? { maxAge: null } : {}
|
|
1720
|
+
};
|
|
1721
|
+
let claims;
|
|
1722
|
+
try {
|
|
1723
|
+
const uploaded = await uploadedKeys(JSON.parse(binding.jwks));
|
|
1724
|
+
if (`sha256:${uploaded.digest}` !== binding.jwksUri) throw new RunProofRefused("keys", "the keys are not those the binding was made with");
|
|
1725
|
+
claims = await verifyWorkloadToken(token, JSON.parse(uploaded.jwks), {
|
|
1726
|
+
...expected,
|
|
1727
|
+
maxLifetime: MAX_TOKEN_AGE_SECONDS
|
|
1728
|
+
});
|
|
1729
|
+
} catch (error) {
|
|
1730
|
+
if (error instanceof RunProofRefused) throw error;
|
|
1731
|
+
if (error instanceof WorkloadTokenRefused && error.reason === "unknown_key") throw new RunProofRefused("unknown_key", error.message, shownKid(header));
|
|
1732
|
+
throw refusedBy(error);
|
|
1733
|
+
}
|
|
1734
|
+
const differ = differingClaims(binding.profile, JSON.parse(binding.claims), claims);
|
|
1735
|
+
if (differ.length > 0) throw new RunProofRefused("no_match", `the binding does not trust these claims: ${differ.join(", ")} differ`);
|
|
1736
|
+
if (context.workload !== null) {
|
|
1737
|
+
const ruled = PROFILE_RULES[binding.profile]?.token(claims, {
|
|
1738
|
+
issuer: binding.issuer,
|
|
1739
|
+
claims: JSON.parse(binding.claims),
|
|
1740
|
+
policy
|
|
1741
|
+
}) ?? null;
|
|
1742
|
+
if (ruled !== null) throw new RunProofRefused("no_match", ruled);
|
|
1743
|
+
}
|
|
1744
|
+
const run = runClaims(claims);
|
|
1745
|
+
if (context.workload === null) {
|
|
1746
|
+
const validUntil = Math.floor(Math.min((decoded.claims.exp + 30) * 1e3, (decoded.claims.iat + MAX_TOKEN_AGE_SECONDS) * 1e3));
|
|
1747
|
+
return {
|
|
1748
|
+
claims,
|
|
1749
|
+
run,
|
|
1750
|
+
spent: decoded.signingInputHash,
|
|
1751
|
+
validUntil,
|
|
1752
|
+
notAfter: null
|
|
1753
|
+
};
|
|
1754
|
+
}
|
|
1755
|
+
const namesKey = policy.keyAudience === true;
|
|
1756
|
+
return {
|
|
1757
|
+
claims,
|
|
1758
|
+
run,
|
|
1759
|
+
spent: namesKey || repeating ? null : decoded.signingInputHash,
|
|
1760
|
+
validUntil: null,
|
|
1761
|
+
notAfter: namesKey ? null : decoded.claims.exp * 1e3
|
|
1762
|
+
};
|
|
1763
|
+
}
|
|
1764
|
+
function refusedBy(error) {
|
|
1765
|
+
if (error instanceof WorkloadTokenRefused) return new RunProofRefused(error.reason, error.message);
|
|
1766
|
+
if (error instanceof RunProofRefused) return error;
|
|
1767
|
+
return new RunProofRefused("malformed", error?.message ?? "the token is not one coffre reads");
|
|
1768
|
+
}
|
|
1769
|
+
//#endregion
|
|
1770
|
+
//#region src/identity/signin/sealed.ts
|
|
1771
|
+
const IV_BYTES = 12;
|
|
1772
|
+
const VERSION = "v1";
|
|
1773
|
+
/**
|
|
1774
|
+
* Derive a purpose-bound key from one of coffre's existing 32-byte secrets.
|
|
1775
|
+
*
|
|
1776
|
+
* HKDF with a distinct `info` string gives a key that is independent of the
|
|
1777
|
+
* source for every practical purpose, so the sign-in cookie needs no new
|
|
1778
|
+
* secret to provision and a leak of one derived key says nothing about the
|
|
1779
|
+
* others.
|
|
1780
|
+
*/
|
|
1781
|
+
function deriveKey(secret, purpose) {
|
|
1782
|
+
return Buffer.from(hkdfSync("sha256", secret, Buffer.alloc(0), `coffre/${purpose}`, 32));
|
|
1783
|
+
}
|
|
1784
|
+
/**
|
|
1785
|
+
* Encrypt a small JSON value for a round trip through the browser.
|
|
1786
|
+
*
|
|
1787
|
+
* AES-256-GCM authenticates as well as hides, so a tampered or forged value
|
|
1788
|
+
* fails to open rather than decoding into something else. The expiry sits
|
|
1789
|
+
* inside the ciphertext, which keeps an old cookie from being replayed past
|
|
1790
|
+
* it even by a client that ignores Max-Age.
|
|
1791
|
+
*/
|
|
1792
|
+
function seal(key, value, ttlSeconds, now = Date.now()) {
|
|
1793
|
+
const iv = randomBytes(IV_BYTES);
|
|
1794
|
+
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
1795
|
+
cipher.setAAD(Buffer.from(VERSION));
|
|
1796
|
+
const plaintext = Buffer.from(JSON.stringify({
|
|
1797
|
+
exp: now + ttlSeconds * 1e3,
|
|
1798
|
+
value
|
|
1799
|
+
}));
|
|
1800
|
+
const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
1801
|
+
return `${VERSION}.${Buffer.concat([
|
|
1802
|
+
iv,
|
|
1803
|
+
cipher.getAuthTag(),
|
|
1804
|
+
ciphertext
|
|
1805
|
+
]).toString("base64url")}`;
|
|
1806
|
+
}
|
|
1807
|
+
/** The sealed value, or null when it is malformed, forged, tampered with or expired. */
|
|
1808
|
+
function unseal(key, sealed, now = Date.now()) {
|
|
1809
|
+
if (!sealed?.startsWith(`${VERSION}.`)) return null;
|
|
1810
|
+
const body = sealed.slice(3);
|
|
1811
|
+
const raw = Buffer.from(body, "base64url");
|
|
1812
|
+
if (raw.length <= 28 || raw.toString("base64url") !== body) return null;
|
|
1813
|
+
try {
|
|
1814
|
+
const decipher = createDecipheriv("aes-256-gcm", key, raw.subarray(0, IV_BYTES));
|
|
1815
|
+
decipher.setAAD(Buffer.from(VERSION));
|
|
1816
|
+
decipher.setAuthTag(raw.subarray(IV_BYTES, 28));
|
|
1817
|
+
const plaintext = Buffer.concat([decipher.update(raw.subarray(28)), decipher.final()]);
|
|
1818
|
+
const parsed = JSON.parse(plaintext.toString("utf8"));
|
|
1819
|
+
if (typeof parsed.exp !== "number" || parsed.exp <= now) return null;
|
|
1820
|
+
return parsed.value;
|
|
1821
|
+
} catch {
|
|
1822
|
+
return null;
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
//#endregion
|
|
1826
|
+
//#region src/identity/tokens.ts
|
|
1827
|
+
const PREFIX = {
|
|
1828
|
+
browser: "coffre_web_",
|
|
1829
|
+
cli: "coffre_cli_",
|
|
1830
|
+
service: "coffre_svc_",
|
|
1831
|
+
personal: "coffre_pat_",
|
|
1832
|
+
handle: "coffre_use_",
|
|
1833
|
+
workload: "coffre_wl_",
|
|
1834
|
+
enrollment: "coffre_enr_"
|
|
1835
|
+
};
|
|
1836
|
+
const TOKEN = /^coffre_(web|cli|svc|pat|use|wl|enr)_[A-Za-z0-9_-]{43}$/;
|
|
1837
|
+
function generateToken(kind) {
|
|
1838
|
+
return PREFIX[kind] + randomBytes(32).toString("base64url");
|
|
1839
|
+
}
|
|
1840
|
+
/** Whether a string has the shape of a coffre token, before any lookup. */
|
|
1841
|
+
function isCoffreToken(value) {
|
|
1842
|
+
return TOKEN.test(value);
|
|
1843
|
+
}
|
|
1844
|
+
/**
|
|
1845
|
+
* The kind a token's prefix says, before any lookup. Its hash covers the
|
|
1846
|
+
* prefix, so a token found under its hash is the kind it says.
|
|
1847
|
+
*/
|
|
1848
|
+
function tokenKind(token) {
|
|
1849
|
+
if (!TOKEN.test(token)) return null;
|
|
1850
|
+
return Object.keys(PREFIX).find((kind) => token.startsWith(PREFIX[kind])) ?? null;
|
|
1851
|
+
}
|
|
1852
|
+
function hashToken(token) {
|
|
1853
|
+
return createHash("sha256").update(token, "utf8").digest();
|
|
1854
|
+
}
|
|
1855
|
+
/**
|
|
1856
|
+
* Enough of a token to recognize it in a list, never enough to use it: the
|
|
1857
|
+
* kind prefix and the last four characters. The prefix is matched rather than
|
|
1858
|
+
* cut at the last underscore, because base64url uses `_` too.
|
|
1859
|
+
*/
|
|
1860
|
+
function tokenHint(token) {
|
|
1861
|
+
return `${Object.values(PREFIX).find((candidate) => token.startsWith(candidate)) ?? ""}…${token.slice(-4)}`;
|
|
1862
|
+
}
|
|
1863
|
+
//#endregion
|
|
1864
|
+
//#region src/identity/types.ts
|
|
1865
|
+
/** Access delivers the JWT to the origin in this header. */
|
|
1866
|
+
const ACCESS_JWT_HEADER = "cf-access-jwt-assertion";
|
|
1867
|
+
//#endregion
|
|
1868
|
+
//#region src/identity/verifier.ts
|
|
1869
|
+
/**
|
|
1870
|
+
* Signature algorithms we accept, as an explicit allowlist.
|
|
1871
|
+
*
|
|
1872
|
+
* Without this, the algorithm is taken from the attacker-controlled token
|
|
1873
|
+
* header, which is how `alg: none` and RS256->HS256 confusion work. jose
|
|
1874
|
+
* defaults to permitting whatever the key supports; we do not rely on that.
|
|
1875
|
+
*/
|
|
1876
|
+
const ALLOWED_ALGORITHMS = ["RS256", "ES256"];
|
|
1877
|
+
function asNonEmptyString(value) {
|
|
1878
|
+
return typeof value === "string" && value.length > 0 ? value : null;
|
|
1879
|
+
}
|
|
1880
|
+
/**
|
|
1881
|
+
* Verifies Cloudflare Access JWTs.
|
|
1882
|
+
*
|
|
1883
|
+
* Signature and claim verification of the token Access sends with each
|
|
1884
|
+
* request.
|
|
1885
|
+
*/
|
|
1886
|
+
var AccessIdentityVerifier = class {
|
|
1887
|
+
#config;
|
|
1888
|
+
#jwks;
|
|
1889
|
+
constructor(config) {
|
|
1890
|
+
this.#config = config;
|
|
1891
|
+
this.#jwks = createRemoteJWKSet(new URL(config.jwksUrl), {
|
|
1892
|
+
cooldownDuration: 3e4,
|
|
1893
|
+
cacheMaxAge: 6e5
|
|
1894
|
+
});
|
|
1895
|
+
}
|
|
1896
|
+
async verify(token) {
|
|
1897
|
+
if (typeof token !== "string" || token.length === 0) throw new Error("missing or empty Access token");
|
|
1898
|
+
const { payload } = await jwtVerify(token, this.#jwks, {
|
|
1899
|
+
issuer: this.#config.issuer,
|
|
1900
|
+
audience: this.#config.audience,
|
|
1901
|
+
algorithms: [...ALLOWED_ALGORITHMS],
|
|
1902
|
+
clockTolerance: this.#config.clockToleranceSeconds ?? 0
|
|
1903
|
+
});
|
|
1904
|
+
return toPrincipal(payload);
|
|
1905
|
+
}
|
|
1906
|
+
};
|
|
1907
|
+
/**
|
|
1908
|
+
* Derive the caller from verified claims.
|
|
1909
|
+
*
|
|
1910
|
+
* Order matters. A service token is identified by `common_name` and carries an
|
|
1911
|
+
* empty `sub`; an identity token carries both `sub` and `email`. Anything else
|
|
1912
|
+
* is rejected rather than being allowed through as an authenticated caller
|
|
1913
|
+
* with no name -- an audit row with a null actor answers no question worth
|
|
1914
|
+
* asking.
|
|
1915
|
+
*/
|
|
1916
|
+
function toPrincipal(claims) {
|
|
1917
|
+
const commonName = asNonEmptyString(claims.common_name);
|
|
1918
|
+
if (commonName !== null) return {
|
|
1919
|
+
type: "service",
|
|
1920
|
+
id: commonName,
|
|
1921
|
+
commonName
|
|
1922
|
+
};
|
|
1923
|
+
const email = asNonEmptyString(claims.email);
|
|
1924
|
+
const subject = asNonEmptyString(claims.sub);
|
|
1925
|
+
if (email !== null && subject !== null) {
|
|
1926
|
+
const id = email.toLowerCase();
|
|
1927
|
+
return {
|
|
1928
|
+
type: "user",
|
|
1929
|
+
id,
|
|
1930
|
+
email: id,
|
|
1931
|
+
subject
|
|
1932
|
+
};
|
|
1933
|
+
}
|
|
1934
|
+
throw new Error("token verified but carries no usable identity: expected common_name (service) or sub+email (user)");
|
|
1935
|
+
}
|
|
1936
|
+
//#endregion
|
|
1937
|
+
export { ACCESS_JWT_HEADER, AccessIdentityVerifier, BEARER_PROFILES, BindingInvalid, CLOCK_TOLERANCE_SECONDS, CREDENTIAL_NAME, ConstraintsInvalid, EVENT_EXPOSURE, EXCHANGED_CREDENTIAL_MS, EXCHANGE_SKEW_SECONDS, GITHUB_EVENTS, GITHUB_ISSUER, GITLAB_ISSUER, GITLAB_PIPELINE_SOURCES, GOOGLE_ISSUER, GitHubApp, GitHubAppUnavailable, GitHubSigninProvider, KEY_NAMING_PROFILES, KeysInvalid, MAX_BINDINGS, MAX_CLAIMS, MAX_CLAIM_BYTES, MAX_CONSTRAINTS, MAX_ISSUER_BYTES, MAX_KEYS, MAX_LISTING_REQUESTS, MAX_PROOF_LENGTH, MAX_READ_KEYS, MAX_READ_PLACES, MAX_SIGNED_HEADERS, MAX_SOURCES, MAX_TOKEN_AGE_SECONDS, MAX_TOKEN_BYTES, OIDC_PROFILES, OidcSigninProvider, POLICY_DEFAULTS, POLICY_FIELDS, PROFILE_RULES, PROFILE_URNS, PROOF_SKEW_SECONDS, REPEATING_PROFILES, REQUIRED_CLAIMS, RUN_CLAIMS, RunProofRefused, SESSION_MS, SigninError, TOKEN_MARGIN_MS, UPLOADED_KEYS, WORKLOAD_ALGORITHMS, WORKLOAD_PROFILES, WORKLOAD_SIGN_IN_PATH, WorkloadTokenRefused, addressWithin, canonicalClaims, canonicalHtu, canonicalRequest, checkBinding, checkConstraints, checkFetchUrl, checkPolicy, claimAt, claimValues, cloudflareAccess, constraintsAllow, constraintsText, decodeWorkloadToken, defaultIssuer, defineSignin, deriveKey, differingClaims, expectedAudiences, fetchMethod, freezeTarget, fullRef, generateToken, github, githubApp, githubEventsAt, githubReusable, githubWorkflow, gitlabProject, google, hashToken, intersect, isBearerProfile, isCoffreToken, issuedRefusal, keySummaries, microsoft, oidc, parseAddress, parseCidr, parseProof, pkcs8Of, policyText, publicOrigin, runClaims, seal, sentTarget, signin, soleAudience, thumbprint, toPrincipal, tokenHint, tokenKind, unseal, uploadedKeys, verifyProof, verifyRunToken, verifyWorkloadToken, workflowRef };
|
package/dist/mcp.d.ts
CHANGED
|
@@ -71,8 +71,9 @@ export type ClientMetadata = {
|
|
|
71
71
|
};
|
|
72
72
|
/**
|
|
73
73
|
* Whether `value` is a Client ID Metadata Document's URL: HTTPS, with a
|
|
74
|
-
* path, and nothing a document's address has no use for
|
|
75
|
-
* instance, a loopback one too, for
|
|
74
|
+
* path, and nothing a document's address has no use for, no longer than
|
|
75
|
+
* `MAX_CLIENT_ID`. On a loopback instance, a loopback one too, for
|
|
76
|
+
* development and conformance.
|
|
76
77
|
*/
|
|
77
78
|
export declare function isClientIdUrl(value: string, options: {
|
|
78
79
|
allowLoopback: boolean;
|
package/dist/mcp.js
CHANGED
|
@@ -128,12 +128,16 @@ function redirectHost(value) {
|
|
|
128
128
|
}
|
|
129
129
|
const MAX_REDIRECTS = 10;
|
|
130
130
|
const MAX_NAME = 100;
|
|
131
|
+
/** The longest client ID URL: a token's issue or revoke through its connection names it in the vault's record, which takes no longer. */
|
|
132
|
+
const MAX_CLIENT_ID = 2048;
|
|
131
133
|
/**
|
|
132
134
|
* Whether `value` is a Client ID Metadata Document's URL: HTTPS, with a
|
|
133
|
-
* path, and nothing a document's address has no use for
|
|
134
|
-
* instance, a loopback one too, for
|
|
135
|
+
* path, and nothing a document's address has no use for, no longer than
|
|
136
|
+
* `MAX_CLIENT_ID`. On a loopback instance, a loopback one too, for
|
|
137
|
+
* development and conformance.
|
|
135
138
|
*/
|
|
136
139
|
function isClientIdUrl(value, options) {
|
|
140
|
+
if (value.length > MAX_CLIENT_ID) return false;
|
|
137
141
|
if (!value.startsWith("https://") && !(options.allowLoopback && value.startsWith("http://"))) return false;
|
|
138
142
|
let url;
|
|
139
143
|
try {
|