@jini-ai/registry 0.5.1 → 0.5.3
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 +8 -0
- package/dist/github-backend.js +17 -10
- package/dist/github-client.d.ts +6 -0
- package/dist/github-client.js +98 -77
- package/dist/index.d.ts +11 -62
- package/dist/static-backend.js +106 -106
- package/dist/tool-catalog/sqlite.d.ts +1 -1
- package/dist/tool-catalog-builder/ports.d.ts +7 -0
- package/dist/tool-catalog-builder/query.js +7 -3
- package/dist/tool-catalog-builder/search-enricher.d.ts +4 -2
- package/dist/tool-catalog-builder/search-enricher.js +13 -5
- package/dist/trust-certificates.d.ts +50 -0
- package/dist/trust-certificates.js +84 -0
- package/dist/trust.js +68 -118
- package/dist/versioning.js +78 -69
- package/package.json +18 -6
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { Clock } from '@jini-ai/core/primitives';
|
|
2
|
+
import type { AgentToolSideEffect, ToolMetadata } from '@jini-ai/core';
|
|
2
3
|
/** Structural contracts only: callers choose their registry and search backend. */
|
|
3
4
|
export interface ToolDescriptor {
|
|
5
|
+
readonly metadata?: ToolMetadata;
|
|
6
|
+
readonly readOnly?: boolean;
|
|
7
|
+
readonly sideEffects?: AgentToolSideEffect;
|
|
4
8
|
readonly id: string;
|
|
5
9
|
readonly description?: string | undefined;
|
|
6
10
|
readonly inputSchema?: unknown;
|
|
@@ -9,6 +13,8 @@ export interface ToolDescriptorSource {
|
|
|
9
13
|
list(): readonly ToolDescriptor[];
|
|
10
14
|
}
|
|
11
15
|
export interface ToolCatalogEntry {
|
|
16
|
+
readonly readOnly?: boolean;
|
|
17
|
+
readonly sideEffects?: AgentToolSideEffect | 'unknown';
|
|
12
18
|
readonly id: string;
|
|
13
19
|
readonly source: string;
|
|
14
20
|
readonly description: string;
|
|
@@ -38,6 +44,7 @@ export interface SearchEnricher {
|
|
|
38
44
|
indexedDescription(required: {
|
|
39
45
|
id: string;
|
|
40
46
|
description: string;
|
|
47
|
+
metadata?: ToolMetadata;
|
|
41
48
|
}, optional: {
|
|
42
49
|
includeDoc2query: boolean;
|
|
43
50
|
}): string;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { toolDiscoveryMetadata } from '@jini-ai/core';
|
|
1
2
|
import { nowIso } from '@jini-ai/core/primitives';
|
|
2
3
|
/** Live overview, preserving registration order; vocabulary and source policy are host-owned. */
|
|
3
4
|
export function listToolCatalogEntries(required) {
|
|
@@ -23,6 +24,7 @@ export function buildToolCatalogQuery(required, optional = {}) {
|
|
|
23
24
|
throw new Error('catalog tool IDs must be unique');
|
|
24
25
|
}
|
|
25
26
|
const authored = new Map();
|
|
27
|
+
const effects = new Map(descriptors.map(descriptor => [descriptor.id, toolDiscoveryMetadata({ descriptor })]));
|
|
26
28
|
const entries = descriptors.map(descriptor => {
|
|
27
29
|
const description = descriptor.description ?? '';
|
|
28
30
|
authored.set(descriptor.id, required.enricher.authoredDescription({ description }));
|
|
@@ -30,7 +32,9 @@ export function buildToolCatalogQuery(required, optional = {}) {
|
|
|
30
32
|
id: descriptor.id,
|
|
31
33
|
source: required.classifier.classify({ id: descriptor.id }),
|
|
32
34
|
description: (optional.includeSearchKeywords ?? true)
|
|
33
|
-
? required.enricher.indexedDescription({ id: descriptor.id, description
|
|
35
|
+
? required.enricher.indexedDescription({ id: descriptor.id, description,
|
|
36
|
+
...(descriptor.metadata ? { metadata: descriptor.metadata } : {}),
|
|
37
|
+
}, {
|
|
34
38
|
includeDoc2query: optional.includeDoc2query ?? true,
|
|
35
39
|
}) : description,
|
|
36
40
|
...(descriptor.inputSchema === undefined ? {} : { inputSchema: descriptor.inputSchema }),
|
|
@@ -43,11 +47,11 @@ export function buildToolCatalogQuery(required, optional = {}) {
|
|
|
43
47
|
search({ query }, { limit = 10 } = {}) {
|
|
44
48
|
if (!Number.isSafeInteger(limit) || limit < 0)
|
|
45
49
|
throw new RangeError('catalog limit must be a nonnegative integer');
|
|
46
|
-
return store.search({ query }, { limit }).map(hit => ({ ...hit, description: descriptionFor(hit) }));
|
|
50
|
+
return store.search({ query }, { limit }).map(hit => ({ ...hit, ...effects.get(hit.id), description: descriptionFor(hit) }));
|
|
47
51
|
},
|
|
48
52
|
describe({ id }) {
|
|
49
53
|
const entry = store.describe({ id });
|
|
50
|
-
return entry === null ? null : { ...entry, description: descriptionFor(entry) };
|
|
54
|
+
return entry === null ? null : { ...entry, ...effects.get(entry.id), description: descriptionFor(entry) };
|
|
51
55
|
},
|
|
52
56
|
};
|
|
53
57
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type { SearchEnricher, SourceClassifier } from './ports.js';
|
|
2
|
-
/** Host vocabulary and boundary marker are required; no product dictionary ships here.
|
|
2
|
+
/** Host vocabulary and boundary marker are required; no product dictionary ships here.
|
|
3
|
+
* Registered search metadata takes precedence over legacy dictionaries. The marker and question
|
|
4
|
+
* switch remain host-controlled so an index migration cannot silently change authored copy. */
|
|
3
5
|
export declare function createSearchEnricher(required: {
|
|
4
6
|
marker: string;
|
|
5
7
|
keywords: Readonly<Record<string, string>>;
|
|
6
8
|
questions: Readonly<Record<string, readonly string[]>>;
|
|
7
|
-
}): SearchEnricher;
|
|
9
|
+
}, _optional?: Record<string, never>): SearchEnricher;
|
|
8
10
|
/** Source naming conventions are supplied by the host, including the orphan-ID fallback. */
|
|
9
11
|
export declare function createPrefixSourceClassifier(required: {
|
|
10
12
|
separator: string;
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
/** Host vocabulary and boundary marker are required; no product dictionary ships here.
|
|
2
|
-
|
|
1
|
+
/** Host vocabulary and boundary marker are required; no product dictionary ships here.
|
|
2
|
+
* Registered search metadata takes precedence over legacy dictionaries. The marker and question
|
|
3
|
+
* switch remain host-controlled so an index migration cannot silently change authored copy. */
|
|
4
|
+
export function createSearchEnricher(required, _optional = {}) {
|
|
3
5
|
const { marker, keywords, questions } = required;
|
|
4
6
|
if (!marker)
|
|
5
7
|
throw new Error('search vocabulary marker must not be empty');
|
|
6
8
|
return {
|
|
7
|
-
indexedDescription({ id, description }, { includeDoc2query }) {
|
|
8
|
-
const
|
|
9
|
-
|
|
9
|
+
indexedDescription({ id, description, metadata }, { includeDoc2query }) {
|
|
10
|
+
const search = metadata?.search;
|
|
11
|
+
const vocabulary = search?.keywords ?? ownValue(keywords, id);
|
|
12
|
+
const queries = includeDoc2query ? (search?.queries ?? ownValue(questions, id))?.join(' ') : undefined;
|
|
13
|
+
const tail = [vocabulary, queries].filter(Boolean).join(' ');
|
|
10
14
|
if (!tail)
|
|
11
15
|
return description;
|
|
12
16
|
return description ? `${description}${marker}${tail}` : tail;
|
|
@@ -17,6 +21,10 @@ export function createSearchEnricher(required) {
|
|
|
17
21
|
},
|
|
18
22
|
};
|
|
19
23
|
}
|
|
24
|
+
/** Own-property lookup: inherited object keys (`toString`, `__proto__`) never count as host vocabulary. */
|
|
25
|
+
function ownValue(record, id) {
|
|
26
|
+
return Object.hasOwn(record, id) ? record[id] : undefined;
|
|
27
|
+
}
|
|
20
28
|
/** Source naming conventions are supplied by the host, including the orphan-ID fallback. */
|
|
21
29
|
export function createPrefixSourceClassifier(required) {
|
|
22
30
|
if (!required.separator || !required.fallbackSource)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module trust-certificates
|
|
3
|
+
*
|
|
4
|
+
* X.509 helpers behind `trust.ts`'s `github-oidc` verifier: PEM bundle
|
|
5
|
+
* parsing, the cryptographic chain walk to a host-configured CA, and SAN URI
|
|
6
|
+
* extraction. Internal to this package (not re-exported from the barrel).
|
|
7
|
+
*/
|
|
8
|
+
import { X509Certificate } from 'node:crypto';
|
|
9
|
+
/** @internal Split a PEM string that may contain a leaf certificate followed by one or more intermediate certificates into individual parsed certificates, leaf first. */
|
|
10
|
+
export declare function parseCertificateBundle(pem: string): X509Certificate[];
|
|
11
|
+
/**
|
|
12
|
+
* @internal Walk from `chain[0]` (the leaf) through any bundled
|
|
13
|
+
* intermediates toward a certificate in `trustedCas`, checking a genuine
|
|
14
|
+
* cryptographic signature at every hop (`X509Certificate#verify`, not just
|
|
15
|
+
* `checkIssued`'s DN-string comparison). Bounded by `chain.length +
|
|
16
|
+
* trustedCas.length + 1` hops so a malformed/cyclic bundle cannot loop
|
|
17
|
+
* forever.
|
|
18
|
+
*/
|
|
19
|
+
export declare function chainToTrustedRoot(chain: X509Certificate[], trustedCas: X509Certificate[]): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* @internal `checkIssued` (DN linkage) AND `verify` (the actual cryptographic
|
|
22
|
+
* signature check) — either alone is insufficient: DN match alone proves
|
|
23
|
+
* nothing cryptographically. The `try`/`catch` is defense-in-depth for this
|
|
24
|
+
* function's own "never throw" contract, not a path this module's test
|
|
25
|
+
* suite demonstrates reachable: empirically, `X509Certificate#verify()`
|
|
26
|
+
* (unlike the standalone `crypto.verify()` function used later in this
|
|
27
|
+
* file, which genuinely does throw for an incompatible key/digest pairing —
|
|
28
|
+
* see `verifyRegistrySignature`'s doc comment on that call) was checked
|
|
29
|
+
* across EC/RSA/Ed25519 issuer-key combinations during this module's
|
|
30
|
+
* construction and consistently returned `false` rather than throwing.
|
|
31
|
+
* Kept anyway in case a future Node/OpenSSL version or an exotic key type
|
|
32
|
+
* behaves differently.
|
|
33
|
+
*
|
|
34
|
+
* **Re-verified 2026-07-22** (audit fix — re-derived independently rather than
|
|
35
|
+
* trusted from this comment's own prior claim): all 9 cross-combinations of
|
|
36
|
+
* {RSA, P-256, Ed25519} signer × tester, plus same-key self-verification for
|
|
37
|
+
* each, plus a P-384 cross-check, all returned a clean `boolean` — zero
|
|
38
|
+
* throws — against this repo's actual Node/OpenSSL (v22.22.2 / OpenSSL
|
|
39
|
+
* 3.0.13). `X509Certificate#verify(publicKey)` *can* throw a `TypeError`,
|
|
40
|
+
* but only for a malformed `publicKey` argument (a non-`KeyObject`, or a
|
|
41
|
+
* private key passed where a public key is required) — neither is reachable
|
|
42
|
+
* through this function's only call site (`chainToTrustedRoot`, which always
|
|
43
|
+
* passes `issuer.publicKey`, a real `X509Certificate`'s own getter, never
|
|
44
|
+
* arbitrary user input). The `catch` here is genuinely unreachable through
|
|
45
|
+
* this module's real call graph, confirmed empirically, not merely asserted.
|
|
46
|
+
*/
|
|
47
|
+
export declare function certIssuedAndSignedBy(cert: Pick<X509Certificate, 'checkIssued' | 'verify'>, issuer: X509Certificate): boolean;
|
|
48
|
+
/** @internal Extract `URI:...` entries from an `X509Certificate#subjectAltName` string (e.g. `"URI:https://github.com/acme/example/.github/workflows/build.yml@refs/heads/main"`, comma-separated when multiple SAN entries are present). */
|
|
49
|
+
export declare function parseSanUris(subjectAltName: string | null | undefined): string[];
|
|
50
|
+
//# sourceMappingURL=trust-certificates.d.ts.map
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module trust-certificates
|
|
3
|
+
*
|
|
4
|
+
* X.509 helpers behind `trust.ts`'s `github-oidc` verifier: PEM bundle
|
|
5
|
+
* parsing, the cryptographic chain walk to a host-configured CA, and SAN URI
|
|
6
|
+
* extraction. Internal to this package (not re-exported from the barrel).
|
|
7
|
+
*/
|
|
8
|
+
import { X509Certificate } from 'node:crypto';
|
|
9
|
+
/** @internal Split a PEM string that may contain a leaf certificate followed by one or more intermediate certificates into individual parsed certificates, leaf first. */
|
|
10
|
+
export function parseCertificateBundle(pem) {
|
|
11
|
+
const blocks = pem.match(/-----BEGIN CERTIFICATE-----[\s\S]+?-----END CERTIFICATE-----/g) ?? [];
|
|
12
|
+
return blocks.map((block) => new X509Certificate(block));
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* @internal Walk from `chain[0]` (the leaf) through any bundled
|
|
16
|
+
* intermediates toward a certificate in `trustedCas`, checking a genuine
|
|
17
|
+
* cryptographic signature at every hop (`X509Certificate#verify`, not just
|
|
18
|
+
* `checkIssued`'s DN-string comparison). Bounded by `chain.length +
|
|
19
|
+
* trustedCas.length + 1` hops so a malformed/cyclic bundle cannot loop
|
|
20
|
+
* forever.
|
|
21
|
+
*/
|
|
22
|
+
export function chainToTrustedRoot(chain, trustedCas) {
|
|
23
|
+
let current = chain[0];
|
|
24
|
+
if (!current)
|
|
25
|
+
return false;
|
|
26
|
+
const remaining = chain.slice(1);
|
|
27
|
+
const maxHops = chain.length + trustedCas.length + 1;
|
|
28
|
+
for (let hop = 0; hop < maxHops; hop += 1) {
|
|
29
|
+
if (trustedCas.some((ca) => certIssuedAndSignedBy(current, ca)))
|
|
30
|
+
return true;
|
|
31
|
+
const nextIndex = remaining.findIndex((candidate) => certIssuedAndSignedBy(current, candidate));
|
|
32
|
+
if (nextIndex === -1)
|
|
33
|
+
return false;
|
|
34
|
+
current = remaining[nextIndex];
|
|
35
|
+
remaining.splice(nextIndex, 1);
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @internal `checkIssued` (DN linkage) AND `verify` (the actual cryptographic
|
|
41
|
+
* signature check) — either alone is insufficient: DN match alone proves
|
|
42
|
+
* nothing cryptographically. The `try`/`catch` is defense-in-depth for this
|
|
43
|
+
* function's own "never throw" contract, not a path this module's test
|
|
44
|
+
* suite demonstrates reachable: empirically, `X509Certificate#verify()`
|
|
45
|
+
* (unlike the standalone `crypto.verify()` function used later in this
|
|
46
|
+
* file, which genuinely does throw for an incompatible key/digest pairing —
|
|
47
|
+
* see `verifyRegistrySignature`'s doc comment on that call) was checked
|
|
48
|
+
* across EC/RSA/Ed25519 issuer-key combinations during this module's
|
|
49
|
+
* construction and consistently returned `false` rather than throwing.
|
|
50
|
+
* Kept anyway in case a future Node/OpenSSL version or an exotic key type
|
|
51
|
+
* behaves differently.
|
|
52
|
+
*
|
|
53
|
+
* **Re-verified 2026-07-22** (audit fix — re-derived independently rather than
|
|
54
|
+
* trusted from this comment's own prior claim): all 9 cross-combinations of
|
|
55
|
+
* {RSA, P-256, Ed25519} signer × tester, plus same-key self-verification for
|
|
56
|
+
* each, plus a P-384 cross-check, all returned a clean `boolean` — zero
|
|
57
|
+
* throws — against this repo's actual Node/OpenSSL (v22.22.2 / OpenSSL
|
|
58
|
+
* 3.0.13). `X509Certificate#verify(publicKey)` *can* throw a `TypeError`,
|
|
59
|
+
* but only for a malformed `publicKey` argument (a non-`KeyObject`, or a
|
|
60
|
+
* private key passed where a public key is required) — neither is reachable
|
|
61
|
+
* through this function's only call site (`chainToTrustedRoot`, which always
|
|
62
|
+
* passes `issuer.publicKey`, a real `X509Certificate`'s own getter, never
|
|
63
|
+
* arbitrary user input). The `catch` here is genuinely unreachable through
|
|
64
|
+
* this module's real call graph, confirmed empirically, not merely asserted.
|
|
65
|
+
*/
|
|
66
|
+
export function certIssuedAndSignedBy(cert, issuer) {
|
|
67
|
+
try {
|
|
68
|
+
return cert.checkIssued(issuer) && cert.verify(issuer.publicKey);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** @internal Extract `URI:...` entries from an `X509Certificate#subjectAltName` string (e.g. `"URI:https://github.com/acme/example/.github/workflows/build.yml@refs/heads/main"`, comma-separated when multiple SAN entries are present). */
|
|
75
|
+
export function parseSanUris(subjectAltName) {
|
|
76
|
+
if (!subjectAltName)
|
|
77
|
+
return [];
|
|
78
|
+
return subjectAltName
|
|
79
|
+
.split(',')
|
|
80
|
+
.map((part) => part.trim())
|
|
81
|
+
.filter((part) => part.startsWith('URI:'))
|
|
82
|
+
.map((part) => part.slice('URI:'.length));
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=trust-certificates.js.map
|
package/dist/trust.js
CHANGED
|
@@ -77,6 +77,7 @@
|
|
|
77
77
|
* configured.
|
|
78
78
|
*/
|
|
79
79
|
import { X509Certificate, verify as nodeVerify } from 'node:crypto';
|
|
80
|
+
import { chainToTrustedRoot, parseCertificateBundle, parseSanUris } from './trust-certificates.js';
|
|
80
81
|
/** GitHub Actions' own OIDC token issuer — the default {@link GithubOidcTrustRoot.allowedIssuers} entry. */
|
|
81
82
|
export const GITHUB_ACTIONS_OIDC_ISSUER = 'https://token.actions.githubusercontent.com';
|
|
82
83
|
/**
|
|
@@ -114,68 +115,96 @@ export function verifyRegistrySignature({ entry, signature }, { trustRoot } = {}
|
|
|
114
115
|
// report "unsupported", never silently pass and never throw.
|
|
115
116
|
return { verified: false, kind: signature.kind, reason: `unsupported signature kind: ${signature.kind}` };
|
|
116
117
|
}
|
|
117
|
-
const
|
|
118
|
+
const outcome = verifyGithubOidcSignature(entry, signature, trustRoot?.githubOidc);
|
|
119
|
+
if ('reason' in outcome)
|
|
120
|
+
return { verified: false, kind: signature.kind, reason: outcome.reason };
|
|
121
|
+
return { verified: true, kind: signature.kind, issuer: outcome.issuer, ...(outcome.subject !== undefined ? { subject: outcome.subject } : {}) };
|
|
122
|
+
}
|
|
123
|
+
const fail = (reason) => ({ reason });
|
|
124
|
+
/**
|
|
125
|
+
* The `github-oidc` checks, in their fixed order — the first failing check's
|
|
126
|
+
* reason is the one reported: trust root, signature fields, certificate
|
|
127
|
+
* chain + validity window, identity allowlist, signature bytes.
|
|
128
|
+
*/
|
|
129
|
+
function verifyGithubOidcSignature(entry, signature, root) {
|
|
118
130
|
if (!root || root.caCertificates.length === 0) {
|
|
119
131
|
// Decision 4: a backend with no configured trust root gets no
|
|
120
132
|
// verification at all — current (pre-this-task) behavior, unchanged.
|
|
121
|
-
return
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
133
|
+
return fail('no github-oidc trust root configured');
|
|
134
|
+
}
|
|
135
|
+
const fields = checkSignatureFields(signature, root);
|
|
136
|
+
if ('reason' in fields)
|
|
137
|
+
return fields;
|
|
138
|
+
const leaf = checkCertificate(fields.certificate, root, fields.signedAt);
|
|
139
|
+
if ('reason' in leaf)
|
|
140
|
+
return leaf;
|
|
141
|
+
const identities = parseSanUris(leaf.subjectAltName);
|
|
142
|
+
if (!identityAllowed(identities, root.allowedIdentities)) {
|
|
143
|
+
return fail('signer identity is not in the configured allowlist');
|
|
128
144
|
}
|
|
145
|
+
const signatureFailure = checkSignatureBytes(entry, signature.signature, leaf);
|
|
146
|
+
if (signatureFailure)
|
|
147
|
+
return signatureFailure;
|
|
148
|
+
return { issuer: fields.issuer, subject: signature.subject ?? identities[0] };
|
|
149
|
+
}
|
|
150
|
+
/** Presence of certificate/issuer/signedAt, the issuer allowlist, and a parseable `signedAt`. */
|
|
151
|
+
function checkSignatureFields(signature, root) {
|
|
152
|
+
if (!signature.certificate)
|
|
153
|
+
return fail('github-oidc signature is missing a certificate');
|
|
154
|
+
if (!signature.issuer)
|
|
155
|
+
return fail('github-oidc signature is missing an issuer');
|
|
129
156
|
const allowedIssuers = root.allowedIssuers ?? [GITHUB_ACTIONS_OIDC_ISSUER];
|
|
130
157
|
if (!allowedIssuers.includes(signature.issuer)) {
|
|
131
|
-
return
|
|
132
|
-
}
|
|
133
|
-
if (!signature.signedAt) {
|
|
134
|
-
return { verified: false, kind: signature.kind, reason: 'github-oidc signature is missing signedAt' };
|
|
158
|
+
return fail(`issuer ${JSON.stringify(signature.issuer)} is not in the configured allowlist`);
|
|
135
159
|
}
|
|
160
|
+
if (!signature.signedAt)
|
|
161
|
+
return fail('github-oidc signature is missing signedAt');
|
|
136
162
|
const signedAt = new Date(signature.signedAt);
|
|
137
|
-
if (Number.isNaN(signedAt.getTime()))
|
|
138
|
-
return
|
|
139
|
-
}
|
|
163
|
+
if (Number.isNaN(signedAt.getTime()))
|
|
164
|
+
return fail('github-oidc signature has an invalid signedAt timestamp');
|
|
165
|
+
return { certificate: signature.certificate, issuer: signature.issuer, signedAt };
|
|
166
|
+
}
|
|
167
|
+
/** Parse the bundled certificate(s) and the configured CAs, require a chain to a CA, and require `signedAt` inside the leaf's validity window. Returns the leaf. */
|
|
168
|
+
function checkCertificate(pem, root, signedAt) {
|
|
140
169
|
let chain;
|
|
141
170
|
try {
|
|
142
|
-
chain = parseCertificateBundle(
|
|
171
|
+
chain = parseCertificateBundle(pem);
|
|
143
172
|
}
|
|
144
173
|
catch (cause) {
|
|
145
|
-
return
|
|
146
|
-
}
|
|
147
|
-
if (chain.length === 0) {
|
|
148
|
-
return { verified: false, kind: signature.kind, reason: 'no PEM certificate found in signature.certificate' };
|
|
174
|
+
return fail(`could not parse certificate: ${cause.message}`);
|
|
149
175
|
}
|
|
150
176
|
const leaf = chain[0];
|
|
177
|
+
if (!leaf)
|
|
178
|
+
return fail('no PEM certificate found in signature.certificate');
|
|
151
179
|
let trustedCas;
|
|
152
180
|
try {
|
|
153
|
-
trustedCas = root.caCertificates.map((
|
|
181
|
+
trustedCas = root.caCertificates.map((caPem) => new X509Certificate(caPem));
|
|
154
182
|
}
|
|
155
183
|
catch (cause) {
|
|
156
|
-
return
|
|
157
|
-
}
|
|
158
|
-
if (!chainToTrustedRoot(chain, trustedCas)) {
|
|
159
|
-
return { verified: false, kind: signature.kind, reason: 'certificate does not chain to a configured trust root' };
|
|
184
|
+
return fail(`configured trust root certificate is invalid: ${cause.message}`);
|
|
160
185
|
}
|
|
186
|
+
if (!chainToTrustedRoot(chain, trustedCas))
|
|
187
|
+
return fail('certificate does not chain to a configured trust root');
|
|
161
188
|
if (signedAt < leaf.validFromDate || signedAt > leaf.validToDate) {
|
|
162
|
-
return
|
|
163
|
-
}
|
|
164
|
-
const identities = parseSanUris(leaf.subjectAltName);
|
|
165
|
-
if (root.allowedIdentities && root.allowedIdentities.length > 0) {
|
|
166
|
-
const matched = identities.some((identity) => root.allowedIdentities.some((pattern) => (typeof pattern === 'string' ? pattern === identity : pattern.test(identity))));
|
|
167
|
-
if (!matched) {
|
|
168
|
-
return { verified: false, kind: signature.kind, reason: 'signer identity is not in the configured allowlist' };
|
|
169
|
-
}
|
|
189
|
+
return fail('signedAt falls outside the certificate validity window');
|
|
170
190
|
}
|
|
191
|
+
return leaf;
|
|
192
|
+
}
|
|
193
|
+
/** No (or an empty) allowlist accepts any identity; otherwise one SAN URI must match one pattern — exact string, or `RegExp#test`. */
|
|
194
|
+
function identityAllowed(identities, allowedIdentities) {
|
|
195
|
+
if (!allowedIdentities || allowedIdentities.length === 0)
|
|
196
|
+
return true;
|
|
197
|
+
return identities.some((identity) => allowedIdentities.some((pattern) => (typeof pattern === 'string' ? pattern === identity : pattern.test(identity))));
|
|
198
|
+
}
|
|
199
|
+
/** Verify `signature` (base64) over {@link canonicalRegistrySigningPayload} with the leaf's public key; `undefined` when it verifies. */
|
|
200
|
+
function checkSignatureBytes(entry, signature, leaf) {
|
|
171
201
|
// `Buffer.from(str, 'base64')` never throws (Node's base64 decoder is
|
|
172
202
|
// lenient about invalid input, silently dropping bad characters) — a
|
|
173
203
|
// malformed encoding surfaces as an empty/garbage buffer that then fails
|
|
174
204
|
// the signature check below, not as an exception here.
|
|
175
|
-
const signatureBytes = Buffer.from(signature
|
|
176
|
-
if (signatureBytes.length === 0)
|
|
177
|
-
return
|
|
178
|
-
}
|
|
205
|
+
const signatureBytes = Buffer.from(signature, 'base64');
|
|
206
|
+
if (signatureBytes.length === 0)
|
|
207
|
+
return fail('signature is empty after base64 decoding');
|
|
179
208
|
const payload = Buffer.from(canonicalRegistrySigningPayload({ entry }), 'utf8');
|
|
180
209
|
let signatureValid;
|
|
181
210
|
try {
|
|
@@ -188,13 +217,9 @@ export function verifyRegistrySignature({ entry, signature }, { trustRoot } = {}
|
|
|
188
217
|
signatureValid = nodeVerify('sha256', payload, leaf.publicKey, signatureBytes);
|
|
189
218
|
}
|
|
190
219
|
catch (cause) {
|
|
191
|
-
return
|
|
192
|
-
}
|
|
193
|
-
if (!signatureValid) {
|
|
194
|
-
return { verified: false, kind: signature.kind, reason: 'signature does not match the certificate public key' };
|
|
220
|
+
return fail(`signature verification failed: ${cause.message}`);
|
|
195
221
|
}
|
|
196
|
-
|
|
197
|
-
return { verified: true, kind: signature.kind, issuer: signature.issuer, ...(subject !== undefined ? { subject } : {}) };
|
|
222
|
+
return signatureValid ? undefined : fail('signature does not match the certificate public key');
|
|
198
223
|
}
|
|
199
224
|
/**
|
|
200
225
|
* Verify every signature on `entry` against `trustRoot`, returning the first
|
|
@@ -217,79 +242,4 @@ export function verifyRegistryEntrySignatures({ entry }, { trustRoot } = {}) {
|
|
|
217
242
|
}
|
|
218
243
|
return last;
|
|
219
244
|
}
|
|
220
|
-
/** @internal Split a PEM string that may contain a leaf certificate followed by one or more intermediate certificates into individual parsed certificates, leaf first. */
|
|
221
|
-
function parseCertificateBundle(pem) {
|
|
222
|
-
const blocks = pem.match(/-----BEGIN CERTIFICATE-----[\s\S]+?-----END CERTIFICATE-----/g) ?? [];
|
|
223
|
-
return blocks.map((block) => new X509Certificate(block));
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* @internal Walk from `chain[0]` (the leaf) through any bundled
|
|
227
|
-
* intermediates toward a certificate in `trustedCas`, checking a genuine
|
|
228
|
-
* cryptographic signature at every hop (`X509Certificate#verify`, not just
|
|
229
|
-
* `checkIssued`'s DN-string comparison). Bounded by `chain.length +
|
|
230
|
-
* trustedCas.length + 1` hops so a malformed/cyclic bundle cannot loop
|
|
231
|
-
* forever.
|
|
232
|
-
*/
|
|
233
|
-
function chainToTrustedRoot(chain, trustedCas) {
|
|
234
|
-
let current = chain[0];
|
|
235
|
-
if (!current)
|
|
236
|
-
return false;
|
|
237
|
-
const remaining = chain.slice(1);
|
|
238
|
-
const maxHops = chain.length + trustedCas.length + 1;
|
|
239
|
-
for (let hop = 0; hop < maxHops; hop += 1) {
|
|
240
|
-
if (trustedCas.some((ca) => certIssuedAndSignedBy(current, ca)))
|
|
241
|
-
return true;
|
|
242
|
-
const nextIndex = remaining.findIndex((candidate) => certIssuedAndSignedBy(current, candidate));
|
|
243
|
-
if (nextIndex === -1)
|
|
244
|
-
return false;
|
|
245
|
-
current = remaining[nextIndex];
|
|
246
|
-
remaining.splice(nextIndex, 1);
|
|
247
|
-
}
|
|
248
|
-
return false;
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* @internal `checkIssued` (DN linkage) AND `verify` (the actual cryptographic
|
|
252
|
-
* signature check) — either alone is insufficient: DN match alone proves
|
|
253
|
-
* nothing cryptographically. The `try`/`catch` is defense-in-depth for this
|
|
254
|
-
* function's own "never throw" contract, not a path this module's test
|
|
255
|
-
* suite demonstrates reachable: empirically, `X509Certificate#verify()`
|
|
256
|
-
* (unlike the standalone `crypto.verify()` function used later in this
|
|
257
|
-
* file, which genuinely does throw for an incompatible key/digest pairing —
|
|
258
|
-
* see `verifyRegistrySignature`'s doc comment on that call) was checked
|
|
259
|
-
* across EC/RSA/Ed25519 issuer-key combinations during this module's
|
|
260
|
-
* construction and consistently returned `false` rather than throwing.
|
|
261
|
-
* Kept anyway in case a future Node/OpenSSL version or an exotic key type
|
|
262
|
-
* behaves differently.
|
|
263
|
-
*
|
|
264
|
-
* **Re-verified 2026-07-22** (audit fix — re-derived independently rather than
|
|
265
|
-
* trusted from this comment's own prior claim): all 9 cross-combinations of
|
|
266
|
-
* {RSA, P-256, Ed25519} signer × tester, plus same-key self-verification for
|
|
267
|
-
* each, plus a P-384 cross-check, all returned a clean `boolean` — zero
|
|
268
|
-
* throws — against this repo's actual Node/OpenSSL (v22.22.2 / OpenSSL
|
|
269
|
-
* 3.0.13). `X509Certificate#verify(publicKey)` *can* throw a `TypeError`,
|
|
270
|
-
* but only for a malformed `publicKey` argument (a non-`KeyObject`, or a
|
|
271
|
-
* private key passed where a public key is required) — neither is reachable
|
|
272
|
-
* through this function's only call site (`chainToTrustedRoot`, which always
|
|
273
|
-
* passes `issuer.publicKey`, a real `X509Certificate`'s own getter, never
|
|
274
|
-
* arbitrary user input). The `catch` here is genuinely unreachable through
|
|
275
|
-
* this module's real call graph, confirmed empirically, not merely asserted.
|
|
276
|
-
*/
|
|
277
|
-
function certIssuedAndSignedBy(cert, issuer) {
|
|
278
|
-
try {
|
|
279
|
-
return cert.checkIssued(issuer) && cert.verify(issuer.publicKey);
|
|
280
|
-
}
|
|
281
|
-
catch {
|
|
282
|
-
return false;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
/** @internal Extract `URI:...` entries from an `X509Certificate#subjectAltName` string (e.g. `"URI:https://github.com/acme/example/.github/workflows/build.yml@refs/heads/main"`, comma-separated when multiple SAN entries are present). */
|
|
286
|
-
function parseSanUris(subjectAltName) {
|
|
287
|
-
if (!subjectAltName)
|
|
288
|
-
return [];
|
|
289
|
-
return subjectAltName
|
|
290
|
-
.split(',')
|
|
291
|
-
.map((part) => part.trim())
|
|
292
|
-
.filter((part) => part.startsWith('URI:'))
|
|
293
|
-
.map((part) => part.slice('URI:'.length));
|
|
294
|
-
}
|
|
295
245
|
//# sourceMappingURL=trust.js.map
|