changeguard 0.0.0-stage → 0.7.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 +440 -0
- package/LICENSE +202 -0
- package/LICENSE-PROPRIETARY +94 -0
- package/README.md +317 -2
- package/SECURITY.md +40 -0
- package/data/changes/openai.seed.json +87 -0
- package/data/changes/shopify.seed.json +42 -0
- package/data/changes/stripe.seed.json +60 -0
- package/data/changes/supabase.seed.json +56 -0
- package/data/changes/twilio.seed.json +63 -0
- package/data/openapi/openai/meta.json +9 -0
- package/data/openapi/openai/new.json +24 -0
- package/data/openapi/openai/old.json +68 -0
- package/data/providers/openai.json +60 -0
- package/data/providers/shopify.json +52 -0
- package/data/providers/stripe.json +57 -0
- package/data/providers/supabase.json +56 -0
- package/data/providers/twilio.json +57 -0
- package/dist/analyze/go-analyzer.js +343 -0
- package/dist/analyze/index.js +77 -0
- package/dist/analyze/js-analyzer.js +380 -0
- package/dist/analyze/parser.js +78 -0
- package/dist/analyze/py-analyzer.js +313 -0
- package/dist/analyze/rules.js +1 -0
- package/dist/analyze/ts-utils.js +62 -0
- package/dist/cli.js +335 -0
- package/dist/codemod/apply.js +159 -0
- package/dist/config.js +121 -0
- package/dist/deps/manifests.js +145 -0
- package/dist/github/api.js +136 -0
- package/dist/github/auth.js +54 -0
- package/dist/impact/engine.js +384 -0
- package/dist/ingest/canonical.js +52 -0
- package/dist/ingest/changelog-html.js +63 -0
- package/dist/ingest/feed-client.js +99 -0
- package/dist/ingest/feed-keys.js +198 -0
- package/dist/ingest/feed.js +178 -0
- package/dist/ingest/github-releases.js +59 -0
- package/dist/ingest/note-parser.js +274 -0
- package/dist/ingest/openapi-diff.js +217 -0
- package/dist/ingest/store.js +181 -0
- package/dist/llm/explanation.js +273 -0
- package/dist/mcp/format.js +116 -0
- package/dist/mcp/index.js +29 -0
- package/dist/mcp/protocol.js +36 -0
- package/dist/mcp/server.js +110 -0
- package/dist/mcp/standalone.js +12 -0
- package/dist/mcp/tools.js +189 -0
- package/dist/notify/index.js +174 -0
- package/dist/providers/registry.js +215 -0
- package/dist/report/html.js +194 -0
- package/dist/report/markdown.js +90 -0
- package/dist/report/pr-draft.js +37 -0
- package/dist/scan.js +168 -0
- package/dist/source/repo-source.js +100 -0
- package/dist/types.js +6 -0
- package/dist/util/http.js +40 -0
- package/dist/util/logger.js +61 -0
- package/dist/util/metrics.js +99 -0
- package/dist/util/request-context.js +22 -0
- package/dist/util/task-queue.js +69 -0
- package/dist/util/versions.js +52 -0
- package/fixtures/sample-repo/cmd/billing/main.go +47 -0
- package/fixtures/sample-repo/go.mod +12 -0
- package/fixtures/sample-repo/package.json +17 -0
- package/fixtures/sample-repo/requirements.txt +7 -0
- package/fixtures/sample-repo/services/ai.py +65 -0
- package/fixtures/sample-repo/src/billing.ts +68 -0
- package/package.json +84 -4
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two-layer feed trust: built-in long-lived root keys sign a short-lived
|
|
3
|
+
* keyring, and the keyring lists the keys that sign feeds.
|
|
4
|
+
*
|
|
5
|
+
* Trust anchor: `BUILTIN_ROOT_KEYS` ships inside the Apache-2.0 package, so its
|
|
6
|
+
* integrity is the package's integrity (npm provenance + signed releases).
|
|
7
|
+
* Root keys are the only keys that may introduce signing keys — a compromised
|
|
8
|
+
* feed endpoint cannot self-certify. A keyring may be fetched from a URL:
|
|
9
|
+
* fetching is safe because the root signature is checked locally before any
|
|
10
|
+
* listed `kid` is trusted.
|
|
11
|
+
*
|
|
12
|
+
* Rotation: signing keys rotate by re-signing a keyring with a root key, so
|
|
13
|
+
* already-installed CLIs accept the new keyring without a CLI release. Root
|
|
14
|
+
* keys rotate rarely and require shipping a new CLI release that trusts both
|
|
15
|
+
* roots during the overlap. See docs/feed.md.
|
|
16
|
+
*
|
|
17
|
+
* Bootstrap keys (root1/sign1) were generated locally on 2026-11-13; the root
|
|
18
|
+
* private key stays in the git-ignored `.keys/` directory. Before serving
|
|
19
|
+
* external users, re-run the key ceremony in an isolated environment and
|
|
20
|
+
* rotate: append the new root public key to `BUILTIN_ROOT_KEYS`, ship the
|
|
21
|
+
* overlap release, then retire the bootstrap root.
|
|
22
|
+
*/
|
|
23
|
+
import { readFileSync } from 'node:fs';
|
|
24
|
+
import { createPublicKey } from 'node:crypto';
|
|
25
|
+
import { checkKeyValidity, deriveKid, dropSignature, signBody, verifySignature, } from './feed.js';
|
|
26
|
+
export const KEYRING_VERSION = 1;
|
|
27
|
+
/** Root trust anchors. Public keys only — private keys never ship. */
|
|
28
|
+
export const BUILTIN_ROOT_KEYS = [
|
|
29
|
+
{
|
|
30
|
+
kid: 'ed25519:OAJWHQo57zZuOSv-',
|
|
31
|
+
publicKeyPem: '-----BEGIN PUBLIC KEY-----\n' +
|
|
32
|
+
'MCowBQYDK2VwAyEAK6+vNffuXsoEQv6w3BvUtcuSS+tJa1XXX5jlVyBqQPo=\n' +
|
|
33
|
+
'-----END PUBLIC KEY-----\n',
|
|
34
|
+
},
|
|
35
|
+
];
|
|
36
|
+
/**
|
|
37
|
+
* The keyring shipped with the package, signed by a built-in root key.
|
|
38
|
+
* With it in place the CLI trusts the listed signing keys out of the box;
|
|
39
|
+
* verification still fails closed when a feed's `kid` is not in the keyring.
|
|
40
|
+
*/
|
|
41
|
+
export const BUILTIN_FEED_KEYRING = {
|
|
42
|
+
keyringVersion: 1,
|
|
43
|
+
generatedAt: '2026-11-13T06:04:37.944Z',
|
|
44
|
+
keys: [
|
|
45
|
+
{
|
|
46
|
+
kid: 'ed25519:pYFI_sokhVWDncrW',
|
|
47
|
+
publicKeyPem: '-----BEGIN PUBLIC KEY-----\n' +
|
|
48
|
+
'MCowBQYDK2VwAyEAQLq/yESD1R3zChhmAr/xtvNI9oD5mPW+nAb0zArxQY8=\n' +
|
|
49
|
+
'-----END PUBLIC KEY-----\n',
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
signature: {
|
|
53
|
+
algorithm: 'ed25519',
|
|
54
|
+
kid: 'ed25519:OAJWHQo57zZuOSv-',
|
|
55
|
+
digest: 'd7c2d18b8329bfa5d4d3dd4391d30c52ab40e34cd3fcf3341c868999f119c20d',
|
|
56
|
+
value: 'gokFmYqLr2cFzTH5lVqLXgkzhU5TYQAjdb8a3VF57SJ0dNkau+UZ4gSyHuPELvVs3xm5WyYAo1BUDsYpDbvHBQ==',
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
/** Structural validation of key entries (root files and keyring bodies). */
|
|
60
|
+
export function parseKeyEntries(raw, source = 'keyring') {
|
|
61
|
+
const list = Array.isArray(raw)
|
|
62
|
+
? raw
|
|
63
|
+
: typeof raw === 'object' && raw !== null && Array.isArray(raw.keys)
|
|
64
|
+
? raw.keys
|
|
65
|
+
: undefined;
|
|
66
|
+
if (!list) {
|
|
67
|
+
throw new Error(`${source}: expected {"keys":[...]} or an array of keys`);
|
|
68
|
+
}
|
|
69
|
+
return list.map((entry, index) => {
|
|
70
|
+
if (typeof entry !== 'object' || entry === null) {
|
|
71
|
+
throw new Error(`${source}: key #${index} must be an object`);
|
|
72
|
+
}
|
|
73
|
+
const { kid, publicKeyPem, notBefore, retiresAt } = entry;
|
|
74
|
+
if (typeof publicKeyPem !== 'string' || !publicKeyPem.includes('BEGIN PUBLIC KEY')) {
|
|
75
|
+
throw new Error(`${source}: key #${index} needs a PEM public key`);
|
|
76
|
+
}
|
|
77
|
+
let derived;
|
|
78
|
+
try {
|
|
79
|
+
derived = deriveKid(publicKeyPem);
|
|
80
|
+
createPublicKey(publicKeyPem);
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
throw new Error(`${source}: key #${index} is not a valid public key (${err.message})`, { cause: err });
|
|
84
|
+
}
|
|
85
|
+
if (typeof kid !== 'string') {
|
|
86
|
+
throw new Error(`${source}: key #${index} is missing kid (expected ${derived})`);
|
|
87
|
+
}
|
|
88
|
+
if (kid !== derived) {
|
|
89
|
+
throw new Error(`${source}: key #${index} kid ${kid} does not match the key (${derived})`);
|
|
90
|
+
}
|
|
91
|
+
for (const [field, value] of [
|
|
92
|
+
['notBefore', notBefore],
|
|
93
|
+
['retiresAt', retiresAt],
|
|
94
|
+
]) {
|
|
95
|
+
if (value !== undefined && Number.isNaN(Date.parse(String(value)))) {
|
|
96
|
+
throw new Error(`${source}: key #${index} has an invalid ${field}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
kid: derived,
|
|
101
|
+
publicKeyPem,
|
|
102
|
+
...(notBefore === undefined ? {} : { notBefore: String(notBefore) }),
|
|
103
|
+
...(retiresAt === undefined ? {} : { retiresAt: String(retiresAt) }),
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/** Sign a keyring payload with a root key. */
|
|
108
|
+
export function signKeyring(payload, root) {
|
|
109
|
+
const body = {
|
|
110
|
+
keyringVersion: payload.keyringVersion ?? KEYRING_VERSION,
|
|
111
|
+
...(payload.generatedAt === undefined ? {} : { generatedAt: payload.generatedAt }),
|
|
112
|
+
keys: parseKeyEntries({ keys: payload.keys }, 'keyring'),
|
|
113
|
+
};
|
|
114
|
+
return {
|
|
115
|
+
...body,
|
|
116
|
+
signature: signBody(body, root),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Verify a keyring against the built-in root keys. No signing key from the
|
|
121
|
+
* keyring is trusted unless this function returns ok.
|
|
122
|
+
*/
|
|
123
|
+
export function verifySignedKeyring(raw, rootKeys, opts = {}) {
|
|
124
|
+
if (!isPlainObject(raw)) {
|
|
125
|
+
return { ok: false, reason: 'keyring-malformed', detail: 'keyring must be a JSON object' };
|
|
126
|
+
}
|
|
127
|
+
const version = raw.keyringVersion;
|
|
128
|
+
if (typeof version !== 'number' || !Number.isInteger(version) || version !== KEYRING_VERSION) {
|
|
129
|
+
return {
|
|
130
|
+
ok: false,
|
|
131
|
+
reason: 'keyring-malformed',
|
|
132
|
+
detail: `keyringVersion ${String(version)} is not supported (expected ${KEYRING_VERSION})`,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
let keys;
|
|
136
|
+
try {
|
|
137
|
+
keys = parseKeyEntries(raw, 'keyring');
|
|
138
|
+
}
|
|
139
|
+
catch (err) {
|
|
140
|
+
return { ok: false, reason: 'keyring-malformed', detail: err.message };
|
|
141
|
+
}
|
|
142
|
+
const sig = raw.signature;
|
|
143
|
+
if (!isPlainObject(sig)) {
|
|
144
|
+
return { ok: false, reason: 'keyring-unsigned', detail: 'keyring has no root signature' };
|
|
145
|
+
}
|
|
146
|
+
if (sig.algorithm !== 'ed25519' || typeof sig.kid !== 'string' || typeof sig.value !== 'string') {
|
|
147
|
+
return {
|
|
148
|
+
ok: false,
|
|
149
|
+
reason: 'keyring-unsigned',
|
|
150
|
+
detail: 'keyring signature must be {algorithm:"ed25519", kid, value}',
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
const root = rootKeys.find((candidate) => candidate.kid === sig.kid);
|
|
154
|
+
if (!root) {
|
|
155
|
+
const known = rootKeys.map((candidate) => candidate.kid).join(', ') || '(none configured)';
|
|
156
|
+
return {
|
|
157
|
+
ok: false,
|
|
158
|
+
reason: 'keyring-unknown-root',
|
|
159
|
+
detail: `keyring root kid ${sig.kid} is not trusted; trusted roots: ${known}`,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
const validity = checkKeyValidity(root, opts.now ?? new Date());
|
|
163
|
+
if (!validity.ok)
|
|
164
|
+
return validity;
|
|
165
|
+
const checked = verifySignature(dropSignature(raw), sig, root.publicKeyPem);
|
|
166
|
+
if (!checked.ok)
|
|
167
|
+
return { ok: false, reason: 'keyring-bad-signature', detail: checked.detail };
|
|
168
|
+
return {
|
|
169
|
+
ok: true,
|
|
170
|
+
keys,
|
|
171
|
+
root,
|
|
172
|
+
...(typeof raw.generatedAt === 'string' ? { generatedAt: raw.generatedAt } : {}),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Built-in roots plus an optional operator-provided root file
|
|
177
|
+
* (`CHANGEGUARD_FEED_ROOT_KEYS`). Local/private deployments use the file;
|
|
178
|
+
* production clients rely on the built-in list.
|
|
179
|
+
*/
|
|
180
|
+
export function resolveRootKeys() {
|
|
181
|
+
const envPath = process.env.CHANGEGUARD_FEED_ROOT_KEYS;
|
|
182
|
+
if (!envPath)
|
|
183
|
+
return { keys: BUILTIN_ROOT_KEYS, source: 'built-in root keys' };
|
|
184
|
+
let raw;
|
|
185
|
+
try {
|
|
186
|
+
raw = JSON.parse(readFileSync(envPath, 'utf8'));
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
throw new Error(`cannot read root keys from ${envPath}: ${err.message}`, {
|
|
190
|
+
cause: err,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
const keys = parseKeyEntries(raw, `${envPath} (env)`);
|
|
194
|
+
return { keys: [...BUILTIN_ROOT_KEYS, ...keys], source: `${envPath} (env) + built-in roots` };
|
|
195
|
+
}
|
|
196
|
+
function isPlainObject(value) {
|
|
197
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
198
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signed change feed (envelope v1).
|
|
3
|
+
*
|
|
4
|
+
* The payload is the whole envelope minus `signature`, serialized with
|
|
5
|
+
* canonical JSON (see ./canonical.ts). Because *everything except* the
|
|
6
|
+
* signature is signed, a publisher can add new top-level fields or new record
|
|
7
|
+
* fields later and old clients still verify them byte-for-byte; clients simply
|
|
8
|
+
* ignore fields they do not understand. Removing or retyping a field requires
|
|
9
|
+
* bumping `feedVersion`.
|
|
10
|
+
*
|
|
11
|
+
* Signature: ed25519, detached value embedded in the envelope, key selected by
|
|
12
|
+
* `kid`. `kid` is derived from the public key itself, so a registry entry that
|
|
13
|
+
* pairs a `kid` with a different key is rejected.
|
|
14
|
+
*
|
|
15
|
+
* The same primitives sign the keyring itself (see ./feed-keys.ts): root keys
|
|
16
|
+
* are built into the package, signing keys travel in a root-signed keyring.
|
|
17
|
+
*/
|
|
18
|
+
import { createHash, createPrivateKey, createPublicKey, sign, verify } from 'node:crypto';
|
|
19
|
+
import { canonicalBytes } from './canonical.js';
|
|
20
|
+
export const FEED_VERSION = 1;
|
|
21
|
+
export const FEED_PUBLISHER = 'changeguard';
|
|
22
|
+
export const FEED_SIGNATURE_ALGORITHM = 'ed25519';
|
|
23
|
+
/** Stable `kid` derived from the public key (96-bit fingerprint). */
|
|
24
|
+
export function deriveKid(publicKeyPem) {
|
|
25
|
+
const key = createPublicKey(publicKeyPem);
|
|
26
|
+
const der = key.export({ type: 'spki', format: 'der' });
|
|
27
|
+
const fingerprint = createHash('sha256').update(der).digest('base64url');
|
|
28
|
+
return `${FEED_SIGNATURE_ALGORITHM}:${fingerprint.slice(0, 16)}`;
|
|
29
|
+
}
|
|
30
|
+
export function feedDigest(payload) {
|
|
31
|
+
return createHash('sha256').update(canonicalBytes(payload)).digest('hex');
|
|
32
|
+
}
|
|
33
|
+
/** Sign any canonical JSON body (a feed payload or a keyring payload). */
|
|
34
|
+
export function signBody(body, key) {
|
|
35
|
+
const expectedKid = deriveKid(publicKeyFromPrivate(key.privateKeyPem));
|
|
36
|
+
if (expectedKid !== key.kid) {
|
|
37
|
+
throw new Error(`kid ${key.kid} does not match the supplied private key (expected ${expectedKid})`);
|
|
38
|
+
}
|
|
39
|
+
const digest = feedDigest(body);
|
|
40
|
+
const value = sign(null, canonicalBytes(body), createPrivateKey(key.privateKeyPem));
|
|
41
|
+
return {
|
|
42
|
+
algorithm: FEED_SIGNATURE_ALGORITHM,
|
|
43
|
+
kid: key.kid,
|
|
44
|
+
digest,
|
|
45
|
+
value: value.toString('base64'),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** Check a detached signature over a canonical JSON body. */
|
|
49
|
+
export function verifySignature(body, sig, publicKeyPem) {
|
|
50
|
+
const computed = feedDigest(body);
|
|
51
|
+
if (typeof sig.digest === 'string' && sig.digest !== computed) {
|
|
52
|
+
return {
|
|
53
|
+
ok: false,
|
|
54
|
+
detail: `payload digest mismatch (declared ${sig.digest}, computed ${computed})`,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
const valid = verify(null, canonicalBytes(body), createPublicKey(publicKeyPem), Buffer.from(sig.value, 'base64'));
|
|
59
|
+
if (!valid)
|
|
60
|
+
return { ok: false, detail: 'signature does not match the payload' };
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
return { ok: false, detail: `signature could not be checked: ${err.message}` };
|
|
64
|
+
}
|
|
65
|
+
return { ok: true };
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Apply `notBefore` / `retiresAt` to a trusted key. Used for signing keys in a
|
|
69
|
+
* verified keyring and for built-in root keys alike.
|
|
70
|
+
*/
|
|
71
|
+
export function checkKeyValidity(key, now) {
|
|
72
|
+
if (key.notBefore) {
|
|
73
|
+
const from = Date.parse(key.notBefore);
|
|
74
|
+
if (!Number.isFinite(from)) {
|
|
75
|
+
return { ok: false, reason: 'malformed', detail: `key ${key.kid} has an invalid notBefore` };
|
|
76
|
+
}
|
|
77
|
+
if (now.getTime() < from) {
|
|
78
|
+
return {
|
|
79
|
+
ok: false,
|
|
80
|
+
reason: 'not-yet-valid',
|
|
81
|
+
detail: `kid ${key.kid} is not valid before ${key.notBefore}`,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (key.retiresAt) {
|
|
86
|
+
const until = Date.parse(key.retiresAt);
|
|
87
|
+
if (!Number.isFinite(until)) {
|
|
88
|
+
return { ok: false, reason: 'malformed', detail: `key ${key.kid} has an invalid retiresAt` };
|
|
89
|
+
}
|
|
90
|
+
if (now.getTime() >= until) {
|
|
91
|
+
return {
|
|
92
|
+
ok: false,
|
|
93
|
+
reason: 'retired-key',
|
|
94
|
+
detail: `kid ${key.kid} was retired at ${key.retiresAt}`,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return { ok: true };
|
|
99
|
+
}
|
|
100
|
+
export function signFeed(payload, key) {
|
|
101
|
+
return {
|
|
102
|
+
...payload,
|
|
103
|
+
signature: signBody(payload, key),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** Fail-closed verification: every branch either returns ok or a typed rejection. */
|
|
107
|
+
export function verifyFeed(raw, trustedKeys, opts = {}) {
|
|
108
|
+
if (!isPlainObject(raw)) {
|
|
109
|
+
return { ok: false, reason: 'malformed', detail: 'feed must be a JSON object' };
|
|
110
|
+
}
|
|
111
|
+
const version = raw.feedVersion;
|
|
112
|
+
if (typeof version !== 'number' || !Number.isInteger(version) || version < 1) {
|
|
113
|
+
return { ok: false, reason: 'malformed', detail: 'feedVersion must be a positive integer' };
|
|
114
|
+
}
|
|
115
|
+
if (version !== FEED_VERSION) {
|
|
116
|
+
return {
|
|
117
|
+
ok: false,
|
|
118
|
+
reason: 'unsupported-version',
|
|
119
|
+
detail: `feedVersion ${version} is not supported by this client (max ${FEED_VERSION})`,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (!Array.isArray(raw.records)) {
|
|
123
|
+
return { ok: false, reason: 'malformed', detail: 'records must be an array' };
|
|
124
|
+
}
|
|
125
|
+
const sig = raw.signature;
|
|
126
|
+
if (!isPlainObject(sig)) {
|
|
127
|
+
return { ok: false, reason: 'missing-signature', detail: 'feed has no signature' };
|
|
128
|
+
}
|
|
129
|
+
if (sig.algorithm !== FEED_SIGNATURE_ALGORITHM) {
|
|
130
|
+
return {
|
|
131
|
+
ok: false,
|
|
132
|
+
reason: 'unsupported-algorithm',
|
|
133
|
+
detail: `algorithm ${String(sig.algorithm)} is not supported`,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
if (typeof sig.kid !== 'string' || typeof sig.value !== 'string') {
|
|
137
|
+
return { ok: false, reason: 'missing-signature', detail: 'signature must have kid and value' };
|
|
138
|
+
}
|
|
139
|
+
const key = trustedKeys.find((candidate) => candidate.kid === sig.kid);
|
|
140
|
+
if (!key) {
|
|
141
|
+
const known = trustedKeys.map((candidate) => candidate.kid).join(', ') || '(none configured)';
|
|
142
|
+
return {
|
|
143
|
+
ok: false,
|
|
144
|
+
reason: 'unknown-kid',
|
|
145
|
+
detail: `kid ${sig.kid} is not trusted; trusted kids: ${known}`,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
const validity = checkKeyValidity(key, opts.now ?? new Date());
|
|
149
|
+
if (!validity.ok)
|
|
150
|
+
return validity;
|
|
151
|
+
// Sign the payload exactly as received: every member except `signature`.
|
|
152
|
+
const payload = dropSignature(raw);
|
|
153
|
+
const checked = verifySignature(payload, { digest: sig.digest, value: sig.value }, key.publicKeyPem);
|
|
154
|
+
if (!checked.ok)
|
|
155
|
+
return { ok: false, reason: 'bad-signature', detail: checked.detail };
|
|
156
|
+
return {
|
|
157
|
+
ok: true,
|
|
158
|
+
payload: payload,
|
|
159
|
+
key,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/** Remove the signature member; everything else is signed. */
|
|
163
|
+
export function dropSignature(raw) {
|
|
164
|
+
const payload = {};
|
|
165
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
166
|
+
if (key === 'signature')
|
|
167
|
+
continue;
|
|
168
|
+
payload[key] = value;
|
|
169
|
+
}
|
|
170
|
+
return payload;
|
|
171
|
+
}
|
|
172
|
+
function isPlainObject(value) {
|
|
173
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
174
|
+
}
|
|
175
|
+
function publicKeyFromPrivate(privateKeyPem) {
|
|
176
|
+
const publicKey = createPublicKey(createPrivateKey(privateKeyPem));
|
|
177
|
+
return publicKey.export({ type: 'spki', format: 'pem' });
|
|
178
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { fetchJson, HttpError } from '../util/http.js';
|
|
2
|
+
import { parseNoteEntry, splitReleaseNotes } from './note-parser.js';
|
|
3
|
+
import { config } from '../config.js';
|
|
4
|
+
import { logger } from '../util/logger.js';
|
|
5
|
+
/**
|
|
6
|
+
* Fetch JSON from the GitHub API with rate-limit awareness.
|
|
7
|
+
* On 403 with X-RateLimit-Remaining: 0, wait until the reset time and retry once.
|
|
8
|
+
*/
|
|
9
|
+
async function fetchGithubJson(url, token) {
|
|
10
|
+
const headers = token ? { authorization: `Bearer ${token}` } : undefined;
|
|
11
|
+
try {
|
|
12
|
+
return await fetchJson(url, { headers });
|
|
13
|
+
}
|
|
14
|
+
catch (err) {
|
|
15
|
+
if (err instanceof HttpError && err.status === 403) {
|
|
16
|
+
// Try to surface rate-limit info from the response body
|
|
17
|
+
logger.warn(`github API 403 for ${url}: ${err.body.slice(0, 160)}`);
|
|
18
|
+
}
|
|
19
|
+
throw err;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export async function fetchReleaseChanges(src, sinceISO) {
|
|
23
|
+
const url = `https://api.github.com/repos/${src.owner}/${src.repo}/releases?per_page=30`;
|
|
24
|
+
let releases;
|
|
25
|
+
try {
|
|
26
|
+
releases = await fetchGithubJson(url, config.github.token || undefined);
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
logger.warn(`releases ${src.owner}/${src.repo}: ${err instanceof Error ? err.message : String(err)}`);
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
const out = [];
|
|
33
|
+
for (const rel of releases) {
|
|
34
|
+
if (rel.draft || rel.prerelease)
|
|
35
|
+
continue;
|
|
36
|
+
const date = (rel.published_at ?? '').slice(0, 10);
|
|
37
|
+
if (!date || date < sinceISO)
|
|
38
|
+
continue;
|
|
39
|
+
const body = rel.body ?? '';
|
|
40
|
+
const entries = splitReleaseNotes(body);
|
|
41
|
+
if (entries.length === 0 && body.trim())
|
|
42
|
+
entries.push(body);
|
|
43
|
+
for (const entry of entries) {
|
|
44
|
+
const record = parseNoteEntry(`### ${rel.name ?? rel.tag_name}\n${entry}`, {
|
|
45
|
+
providerId: src.providerId,
|
|
46
|
+
source: 'release-note',
|
|
47
|
+
sourceUrl: rel.html_url,
|
|
48
|
+
date,
|
|
49
|
+
language: src.language,
|
|
50
|
+
ecosystem: src.ecosystem,
|
|
51
|
+
packageName: src.repo,
|
|
52
|
+
tag: rel.tag_name,
|
|
53
|
+
});
|
|
54
|
+
if (record)
|
|
55
|
+
out.push(record);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|