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,313 @@
|
|
|
1
|
+
import { providerForPackage } from '../providers/registry.js';
|
|
2
|
+
import { normalizePyName } from '../deps/manifests.js';
|
|
3
|
+
import { chainSegments, isStringLiteral, sameNode, siteId, snippet, walk } from './ts-utils.js';
|
|
4
|
+
const HTTP_VERBS = new Set(['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'request']);
|
|
5
|
+
function pyArgs(call) {
|
|
6
|
+
const list = call.childForFieldName('arguments');
|
|
7
|
+
if (!list)
|
|
8
|
+
return [];
|
|
9
|
+
// positional args only (keyword_argument handled separately)
|
|
10
|
+
return list.children.filter((c) => ![
|
|
11
|
+
'(',
|
|
12
|
+
')',
|
|
13
|
+
',',
|
|
14
|
+
'comment',
|
|
15
|
+
'keyword_argument',
|
|
16
|
+
'list_splat_pattern',
|
|
17
|
+
'dictionary_splat_pattern',
|
|
18
|
+
].includes(c.type));
|
|
19
|
+
}
|
|
20
|
+
function collectKwargs(callNodes) {
|
|
21
|
+
const keys = [];
|
|
22
|
+
const lit = new Map();
|
|
23
|
+
for (const call of callNodes) {
|
|
24
|
+
const list = call.childForFieldName('arguments');
|
|
25
|
+
if (list) {
|
|
26
|
+
for (const kw of list.children.filter((c) => c.type === 'keyword_argument')) {
|
|
27
|
+
const name = kw.childForFieldName('name')?.text;
|
|
28
|
+
const value = kw.childForFieldName('value');
|
|
29
|
+
if (!name)
|
|
30
|
+
continue;
|
|
31
|
+
keys.push(name);
|
|
32
|
+
if (value && isStringLiteral(value))
|
|
33
|
+
lit.set(name, [...(lit.get(name) ?? []), cleanPyString(value.text)]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const args = pyArgs(call);
|
|
37
|
+
args.forEach((a, i) => {
|
|
38
|
+
if (isStringLiteral(a))
|
|
39
|
+
lit.set(`$${i}`, [...(lit.get(`$${i}`) ?? []), cleanPyString(a.text)]);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return { keys: [...new Set(keys)], literals: Object.fromEntries(lit) };
|
|
43
|
+
}
|
|
44
|
+
function cleanPyString(raw) {
|
|
45
|
+
let t = raw;
|
|
46
|
+
const m = t.match(/^([rbufRBUF]{0,2})(['"`]{1,3})([\s\S]*)\2$/);
|
|
47
|
+
if (m)
|
|
48
|
+
t = m[3] ?? t;
|
|
49
|
+
else if (t.length >= 2)
|
|
50
|
+
t = t.slice(1, -1);
|
|
51
|
+
return t;
|
|
52
|
+
}
|
|
53
|
+
export function analyzePython(root, code, file, index) {
|
|
54
|
+
const rules = index.rules.python;
|
|
55
|
+
const active = new Set();
|
|
56
|
+
const bindings = new Map();
|
|
57
|
+
const instances = new Map();
|
|
58
|
+
const sites = [];
|
|
59
|
+
const seen = new Set();
|
|
60
|
+
const ruleForModuleRoot = (root0) => {
|
|
61
|
+
const pid = providerForPackage('pypi', normalizePyName(root0));
|
|
62
|
+
if (!pid)
|
|
63
|
+
return undefined;
|
|
64
|
+
active.add(pid);
|
|
65
|
+
return rules.find((r) => r.providerId === pid);
|
|
66
|
+
};
|
|
67
|
+
const bind = (local, rule, factory) => {
|
|
68
|
+
bindings.set(local, {
|
|
69
|
+
providerId: rule.providerId,
|
|
70
|
+
factory: factory || rule.factories.includes(local),
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
const importedName = (n) => {
|
|
74
|
+
if (n.type === 'aliased_import') {
|
|
75
|
+
const name = n.childForFieldName('name');
|
|
76
|
+
const alias = n.childForFieldName('alias');
|
|
77
|
+
if (!name)
|
|
78
|
+
return undefined;
|
|
79
|
+
return { imported: name.text.split('.').pop(), local: alias?.text ?? name.text };
|
|
80
|
+
}
|
|
81
|
+
if (n.type === 'dotted_name' || n.type === 'identifier') {
|
|
82
|
+
return { imported: n.text.split('.').pop(), local: n.text.split('.').pop() };
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
};
|
|
86
|
+
// --- pass 1: imports ---
|
|
87
|
+
for (const node of walk(root)) {
|
|
88
|
+
if (node.type === 'import_statement') {
|
|
89
|
+
for (const child of node.children) {
|
|
90
|
+
if (child.type === 'dotted_name') {
|
|
91
|
+
const root0 = child.text.split('.')[0];
|
|
92
|
+
const rule = ruleForModuleRoot(root0);
|
|
93
|
+
if (rule)
|
|
94
|
+
bind(root0, rule, false);
|
|
95
|
+
}
|
|
96
|
+
else if (child.type === 'aliased_import') {
|
|
97
|
+
const name = child.childForFieldName('name');
|
|
98
|
+
const alias = child.childForFieldName('alias');
|
|
99
|
+
const root0 = name?.text.split('.')[0];
|
|
100
|
+
const rule = root0 ? ruleForModuleRoot(root0) : undefined;
|
|
101
|
+
if (rule && alias)
|
|
102
|
+
bind(alias.text, rule, false);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (node.type === 'import_from_statement') {
|
|
107
|
+
const mod = node.childForFieldName('module_name');
|
|
108
|
+
const root0 = mod?.text.split('.')[0];
|
|
109
|
+
const rule = root0 ? ruleForModuleRoot(root0) : undefined;
|
|
110
|
+
if (!rule)
|
|
111
|
+
continue;
|
|
112
|
+
for (const child of node.children) {
|
|
113
|
+
if (child.type === 'wildcard_import')
|
|
114
|
+
continue;
|
|
115
|
+
const info = importedName(child);
|
|
116
|
+
if (info)
|
|
117
|
+
bind(info.local, rule, rule.factories.includes(info.imported));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// --- pass 2: factory propagation through assignments ---
|
|
122
|
+
const calleeProvider = (fn) => {
|
|
123
|
+
if (fn.type === 'identifier') {
|
|
124
|
+
const b = bindings.get(fn.text);
|
|
125
|
+
return b?.factory ? b.providerId : undefined;
|
|
126
|
+
}
|
|
127
|
+
const segs = chainSegments(fn);
|
|
128
|
+
if (!segs)
|
|
129
|
+
return undefined;
|
|
130
|
+
const [r, ...rest] = segs;
|
|
131
|
+
const b = r ? bindings.get(r) : undefined;
|
|
132
|
+
if (!b)
|
|
133
|
+
return undefined;
|
|
134
|
+
const rule = rules.find((x) => x.providerId === b.providerId);
|
|
135
|
+
if (rest.length === 1 && rule?.factories.includes(rest[0]))
|
|
136
|
+
return b.providerId;
|
|
137
|
+
return undefined;
|
|
138
|
+
};
|
|
139
|
+
for (const node of walk(root)) {
|
|
140
|
+
if (node.type !== 'assignment')
|
|
141
|
+
continue;
|
|
142
|
+
const left = node.childForFieldName('left');
|
|
143
|
+
let right = node.childForFieldName('right');
|
|
144
|
+
if (!left || left.type !== 'identifier' || !right)
|
|
145
|
+
continue;
|
|
146
|
+
if (right.type === 'await_expression')
|
|
147
|
+
right = right.childForFieldName('argument') ?? right;
|
|
148
|
+
if (right.type !== 'call')
|
|
149
|
+
continue;
|
|
150
|
+
const fn = right.childForFieldName('function');
|
|
151
|
+
const pid = fn ? calleeProvider(fn) : undefined;
|
|
152
|
+
if (pid)
|
|
153
|
+
instances.set(left.text, pid);
|
|
154
|
+
}
|
|
155
|
+
const rootOf = (id) => instances.get(id) ?? bindings.get(id)?.providerId;
|
|
156
|
+
const emit = (s) => {
|
|
157
|
+
const key = `${s.file}:${s.line}:${s.path}:${s.receiver}`;
|
|
158
|
+
if (seen.has(key))
|
|
159
|
+
return;
|
|
160
|
+
seen.add(key);
|
|
161
|
+
sites.push({ id: siteId(s.file, s.line - 1, s.path), ...s });
|
|
162
|
+
};
|
|
163
|
+
const pkgOf = (pid) => rules.find((r) => r.providerId === pid)?.packageName ?? pid;
|
|
164
|
+
// --- pass 3: attribute-call chains ---
|
|
165
|
+
for (const node of walk(root)) {
|
|
166
|
+
if (node.type !== 'call')
|
|
167
|
+
continue;
|
|
168
|
+
// intermediate of x.a().b()
|
|
169
|
+
if (node.parent?.type === 'attribute' &&
|
|
170
|
+
sameNode(node.parent.childForFieldName('object'), node) &&
|
|
171
|
+
node.parent.parent?.type === 'call') {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const chain = [];
|
|
175
|
+
const callNodes = [];
|
|
176
|
+
let cur = node;
|
|
177
|
+
while (cur && cur.type === 'call') {
|
|
178
|
+
const fn = cur.childForFieldName('function');
|
|
179
|
+
if (!fn || fn.type !== 'attribute')
|
|
180
|
+
break;
|
|
181
|
+
const attr = fn.childForFieldName('attribute');
|
|
182
|
+
if (attr)
|
|
183
|
+
chain.unshift(attr.text);
|
|
184
|
+
callNodes.unshift(cur);
|
|
185
|
+
let obj = fn.childForFieldName('object');
|
|
186
|
+
// descend pure attribute chain: a.b.c() -> [b, c]
|
|
187
|
+
while (obj && obj.type === 'attribute') {
|
|
188
|
+
const a2 = obj.childForFieldName('attribute');
|
|
189
|
+
if (a2)
|
|
190
|
+
chain.unshift(a2.text);
|
|
191
|
+
obj = obj.childForFieldName('object');
|
|
192
|
+
}
|
|
193
|
+
cur = obj;
|
|
194
|
+
}
|
|
195
|
+
if (cur && cur.type === 'identifier' && chain.length > 0) {
|
|
196
|
+
const pid = rootOf(cur.text);
|
|
197
|
+
if (pid && active.has(pid)) {
|
|
198
|
+
// skip constructor/factory calls bound directly off a package binding
|
|
199
|
+
const binding = bindings.get(cur.text);
|
|
200
|
+
const isFactoryNoise = binding &&
|
|
201
|
+
!binding.factory &&
|
|
202
|
+
chain.length === 1 &&
|
|
203
|
+
rules.find((r) => r.providerId === pid)?.factories.includes(chain[0]);
|
|
204
|
+
if (!isFactoryNoise) {
|
|
205
|
+
const args = collectKwargs(callNodes);
|
|
206
|
+
emit({
|
|
207
|
+
file,
|
|
208
|
+
language: 'python',
|
|
209
|
+
line: node.startPosition.row + 1,
|
|
210
|
+
endLine: node.endPosition.row + 1,
|
|
211
|
+
package: pkgOf(pid),
|
|
212
|
+
providerId: pid,
|
|
213
|
+
path: chain.join('.'),
|
|
214
|
+
receiver: [cur.text, ...chain].join('.'),
|
|
215
|
+
callText: snippet(code, node.startPosition.row),
|
|
216
|
+
argumentKeys: args.keys,
|
|
217
|
+
argumentLiterals: args.literals,
|
|
218
|
+
memberRefs: [],
|
|
219
|
+
});
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// raw HTTP: requests.get/post/... / httpx.get / requests.request
|
|
225
|
+
if (cur &&
|
|
226
|
+
cur.type === 'identifier' &&
|
|
227
|
+
(cur.text === 'requests' || cur.text === 'httpx') &&
|
|
228
|
+
chain.length === 1) {
|
|
229
|
+
const verb = chain[0];
|
|
230
|
+
if (HTTP_VERBS.has(verb)) {
|
|
231
|
+
const args = pyArgs(callNodes[0] ?? node);
|
|
232
|
+
const strArgs = args.filter(isStringLiteral);
|
|
233
|
+
let method = verb.toUpperCase();
|
|
234
|
+
let urlArg = strArgs[0];
|
|
235
|
+
if (verb === 'request' && strArgs.length >= 2) {
|
|
236
|
+
method = cleanPyString(strArgs[0].text).toUpperCase();
|
|
237
|
+
urlArg = strArgs[1];
|
|
238
|
+
}
|
|
239
|
+
if (urlArg) {
|
|
240
|
+
const raw = cleanPyString(urlArg.text);
|
|
241
|
+
const pid = hostProvider(raw, index);
|
|
242
|
+
if (pid) {
|
|
243
|
+
let pathname = '/';
|
|
244
|
+
try {
|
|
245
|
+
pathname = new URL(raw).pathname;
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
/* keep / */
|
|
249
|
+
}
|
|
250
|
+
emit({
|
|
251
|
+
file,
|
|
252
|
+
language: 'python',
|
|
253
|
+
line: node.startPosition.row + 1,
|
|
254
|
+
package: '(http)',
|
|
255
|
+
providerId: pid,
|
|
256
|
+
path: pathname,
|
|
257
|
+
receiver: raw,
|
|
258
|
+
callText: snippet(code, node.startPosition.row),
|
|
259
|
+
argumentKeys: [],
|
|
260
|
+
argumentLiterals: { $0: [raw] },
|
|
261
|
+
memberRefs: [],
|
|
262
|
+
http: { method, url: raw },
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// --- pass 4: enum / constant attribute references ---
|
|
270
|
+
for (const node of walk(root)) {
|
|
271
|
+
if (node.type !== 'attribute')
|
|
272
|
+
continue;
|
|
273
|
+
const parent = node.parent;
|
|
274
|
+
if (parent?.type === 'attribute')
|
|
275
|
+
continue;
|
|
276
|
+
if (parent?.type === 'call' && sameNode(parent.childForFieldName('function'), node))
|
|
277
|
+
continue;
|
|
278
|
+
const segs = chainSegments(node);
|
|
279
|
+
if (!segs)
|
|
280
|
+
continue;
|
|
281
|
+
const [r, ...rest] = segs;
|
|
282
|
+
if (!r || rest.length === 0)
|
|
283
|
+
continue;
|
|
284
|
+
const pid = rootOf(r);
|
|
285
|
+
if (!pid || !active.has(pid))
|
|
286
|
+
continue;
|
|
287
|
+
const ref = rest.join('.');
|
|
288
|
+
emit({
|
|
289
|
+
file,
|
|
290
|
+
language: 'python',
|
|
291
|
+
line: node.startPosition.row + 1,
|
|
292
|
+
package: pkgOf(pid),
|
|
293
|
+
providerId: pid,
|
|
294
|
+
path: ref,
|
|
295
|
+
receiver: segs.join('.'),
|
|
296
|
+
callText: snippet(code, node.startPosition.row),
|
|
297
|
+
argumentKeys: [],
|
|
298
|
+
argumentLiterals: {},
|
|
299
|
+
memberRefs: [ref],
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
return sites;
|
|
303
|
+
}
|
|
304
|
+
function hostProvider(raw, index) {
|
|
305
|
+
let host;
|
|
306
|
+
try {
|
|
307
|
+
host = new URL(raw).hostname.toLowerCase();
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
return index.hosts.find((h) => host === h.suffix || host.endsWith(`.${h.suffix}`))?.providerId;
|
|
313
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export function* walk(node) {
|
|
2
|
+
yield node;
|
|
3
|
+
for (const child of node.children)
|
|
4
|
+
yield* walk(child);
|
|
5
|
+
}
|
|
6
|
+
export function findChildren(node, type) {
|
|
7
|
+
return node.children.filter((c) => c.type === type);
|
|
8
|
+
}
|
|
9
|
+
export function isStringLiteral(node) {
|
|
10
|
+
return (!!node &&
|
|
11
|
+
(node.type === 'string' ||
|
|
12
|
+
node.type === 'string_literal' ||
|
|
13
|
+
node.type === 'interpreted_string_literal'));
|
|
14
|
+
}
|
|
15
|
+
/** Decode the text of a string-literal node (best effort, no escape semantics needed). */
|
|
16
|
+
export function stringValue(node) {
|
|
17
|
+
let t = node.text;
|
|
18
|
+
// python f-strings / js template literals: keep raw but trim wrappers
|
|
19
|
+
if ((t.startsWith('"') && t.endsWith('"')) || (t.startsWith("'") && t.endsWith("'"))) {
|
|
20
|
+
t = t.slice(1, -1);
|
|
21
|
+
}
|
|
22
|
+
if (t.startsWith('`') && t.endsWith('`'))
|
|
23
|
+
t = t.slice(1, -1);
|
|
24
|
+
return t;
|
|
25
|
+
}
|
|
26
|
+
export function snippet(code, row) {
|
|
27
|
+
const line = code.split(/\r?\n/)[row];
|
|
28
|
+
return (line ?? '').trim().slice(0, 220);
|
|
29
|
+
}
|
|
30
|
+
/** Node wrappers from web-tree-sitter are not referentially stable. */
|
|
31
|
+
export function sameNode(a, b) {
|
|
32
|
+
return !!a && !!b && (a.equals(b) ?? false);
|
|
33
|
+
}
|
|
34
|
+
let counter = 0;
|
|
35
|
+
export function siteId(file, row, path) {
|
|
36
|
+
counter += 1;
|
|
37
|
+
return `${file}:${row + 1}:${path}#${counter}`;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Normalize a member/selector chain to identifier segments.
|
|
41
|
+
* e.g. stripe . customers . create -> ["stripe","customers","create"]
|
|
42
|
+
*/
|
|
43
|
+
export function chainSegments(root) {
|
|
44
|
+
if (root.type === 'identifier')
|
|
45
|
+
return [root.text];
|
|
46
|
+
// JS/Python use object/property; Go uses operand/field.
|
|
47
|
+
const obj = root.childForFieldName('object') ?? root.childForFieldName('operand');
|
|
48
|
+
const prop = root.childForFieldName('property') ?? root.childForFieldName('field');
|
|
49
|
+
if (!obj || !prop)
|
|
50
|
+
return undefined;
|
|
51
|
+
const head = chainSegments(obj);
|
|
52
|
+
if (!head)
|
|
53
|
+
return undefined;
|
|
54
|
+
if (prop.type === 'property_identifier' ||
|
|
55
|
+
prop.type === 'identifier' ||
|
|
56
|
+
prop.type === 'field_identifier') {
|
|
57
|
+
return [...head, prop.text];
|
|
58
|
+
}
|
|
59
|
+
if (isStringLiteral(prop))
|
|
60
|
+
return [...head, stringValue(prop)];
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|