@llblab/pi-telegram 0.24.6 → 0.24.7

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.
@@ -1,300 +0,0 @@
1
- /**
2
- * Fail-closed dependency audit policy
3
- * Zones: repository validation, dependency security
4
- * Validates the exact expiring Pi-shrinkwrap exception and installed package evidence
5
- */
6
-
7
- const EXCEPTION_EXPIRES_AT = Date.parse("2026-08-22T00:00:00Z");
8
-
9
- interface AuditAdvisory {
10
- source: number;
11
- name: string;
12
- url: string;
13
- severity: string;
14
- }
15
-
16
- interface AuditVulnerability {
17
- name: string;
18
- severity: string;
19
- via: Array<string | AuditAdvisory>;
20
- nodes: string[];
21
- }
22
-
23
- export interface AuditReport {
24
- error?: unknown;
25
- metadata?: {
26
- vulnerabilities?: {
27
- info?: number;
28
- low?: number;
29
- moderate?: number;
30
- high?: number;
31
- critical?: number;
32
- total?: number;
33
- };
34
- };
35
- vulnerabilities?: Record<string, AuditVulnerability>;
36
- }
37
-
38
- interface AllowedAdvisory {
39
- source: number;
40
- packageName: string;
41
- version: string;
42
- severity: string;
43
- url: string;
44
- nodes: readonly string[];
45
- }
46
-
47
- const ALLOWED_ADVISORIES = new Map<number, AllowedAdvisory>([
48
- [
49
- 1123898,
50
- {
51
- source: 1123898,
52
- packageName: "brace-expansion",
53
- version: "5.0.6",
54
- severity: "high",
55
- url: "https://github.com/advisories/GHSA-3jxr-9vmj-r5cp",
56
- nodes: [
57
- "node_modules/@earendil-works/pi-coding-agent/node_modules/brace-expansion",
58
- ],
59
- },
60
- ],
61
- [
62
- 1123964,
63
- {
64
- source: 1123964,
65
- packageName: "protobufjs",
66
- version: "7.6.4",
67
- severity: "moderate",
68
- url: "https://github.com/advisories/GHSA-j3f2-48v5-ccww",
69
- nodes: [
70
- "node_modules/@earendil-works/pi-coding-agent/node_modules/protobufjs",
71
- ],
72
- },
73
- ],
74
- ]);
75
-
76
- const ALLOWED_GRAPH: Readonly<Record<string, readonly string[]>> = {
77
- "brace-expansion": [],
78
- protobufjs: [],
79
- "@google/genai": ["protobufjs"],
80
- "@earendil-works/pi-ai": ["@google/genai"],
81
- "@earendil-works/pi-agent-core": ["@earendil-works/pi-ai"],
82
- "@earendil-works/pi-coding-agent": [
83
- "@earendil-works/pi-agent-core",
84
- "@earendil-works/pi-ai",
85
- ],
86
- };
87
-
88
- const ALLOWED_GRAPH_SEVERITIES: Readonly<Record<string, string>> = {
89
- "brace-expansion": "high",
90
- protobufjs: "moderate",
91
- "@google/genai": "moderate",
92
- "@earendil-works/pi-ai": "moderate",
93
- "@earendil-works/pi-agent-core": "moderate",
94
- "@earendil-works/pi-coding-agent": "moderate",
95
- };
96
-
97
- const ALLOWED_GRAPH_NODES: Readonly<Record<string, readonly string[]>> = {
98
- "brace-expansion": [
99
- "node_modules/@earendil-works/pi-coding-agent/node_modules/brace-expansion",
100
- ],
101
- protobufjs: [
102
- "node_modules/@earendil-works/pi-coding-agent/node_modules/protobufjs",
103
- ],
104
- "@google/genai": [
105
- "node_modules/@google/genai",
106
- "node_modules/@earendil-works/pi-coding-agent/node_modules/@google/genai",
107
- ],
108
- "@earendil-works/pi-ai": [
109
- "node_modules/@earendil-works/pi-ai",
110
- "node_modules/@earendil-works/pi-coding-agent/node_modules/@earendil-works/pi-ai",
111
- ],
112
- "@earendil-works/pi-agent-core": [
113
- "node_modules/@earendil-works/pi-agent-core",
114
- "node_modules/@earendil-works/pi-coding-agent/node_modules/@earendil-works/pi-agent-core",
115
- ],
116
- "@earendil-works/pi-coding-agent": [
117
- "node_modules/@earendil-works/pi-coding-agent",
118
- ],
119
- };
120
-
121
- export interface AuditEvaluation {
122
- acceptedAdvisorySources: number[];
123
- vulnerabilityCount: number;
124
- }
125
-
126
- function hasExactMembers(actual: readonly string[], expected: readonly string[]): boolean {
127
- return (
128
- actual.length === expected.length &&
129
- new Set(actual).size === actual.length &&
130
- expected.every((value) => actual.includes(value))
131
- );
132
- }
133
-
134
- export function evaluateDependencyAudit(
135
- report: AuditReport,
136
- readInstalledVersion: (nodePath: string) => string,
137
- nowMs = Date.now(),
138
- ): AuditEvaluation {
139
- if (report.error !== undefined) {
140
- throw new Error("npm audit returned an error payload");
141
- }
142
- const vulnerabilities = report.vulnerabilities;
143
- if (!vulnerabilities || typeof vulnerabilities !== "object") {
144
- throw new Error("npm audit output is missing vulnerabilities");
145
- }
146
-
147
- const entries = Object.entries(vulnerabilities);
148
- const counts = report.metadata?.vulnerabilities;
149
- if (!counts) {
150
- throw new Error("npm audit output is missing vulnerability metadata");
151
- }
152
- const countKeys = [
153
- "info",
154
- "low",
155
- "moderate",
156
- "high",
157
- "critical",
158
- "total",
159
- ] as const;
160
- for (const key of countKeys) {
161
- if (!Number.isInteger(counts[key]) || (counts[key] ?? -1) < 0) {
162
- throw new Error(`npm audit metadata has invalid ${key} count`);
163
- }
164
- }
165
- const severityTotal =
166
- (counts.info ?? 0) +
167
- (counts.low ?? 0) +
168
- (counts.moderate ?? 0) +
169
- (counts.high ?? 0) +
170
- (counts.critical ?? 0);
171
- if (severityTotal !== counts.total || counts.total !== entries.length) {
172
- throw new Error(
173
- `npm audit vulnerability total mismatch: metadata=${String(counts.total)}, severities=${severityTotal}, graph=${entries.length}`,
174
- );
175
- }
176
- if (entries.length === 0) {
177
- return { acceptedAdvisorySources: [], vulnerabilityCount: 0 };
178
- }
179
- if (nowMs >= EXCEPTION_EXPIRES_AT) {
180
- throw new Error(
181
- "approved dependency audit exception expired at 2026-08-22T00:00:00Z",
182
- );
183
- }
184
-
185
- const acceptedSources = new Set<number>();
186
- for (const [name, vulnerability] of entries) {
187
- if (vulnerability.name !== name) {
188
- throw new Error(`npm audit graph key/name mismatch for ${name}`);
189
- }
190
- const allowedParents = ALLOWED_GRAPH[name];
191
- const allowedNodes = ALLOWED_GRAPH_NODES[name];
192
- const allowedSeverity = ALLOWED_GRAPH_SEVERITIES[name];
193
- if (!allowedParents || !allowedNodes || !allowedSeverity) {
194
- throw new Error(`unapproved vulnerable package: ${name}`);
195
- }
196
- if (vulnerability.severity !== allowedSeverity) {
197
- throw new Error(
198
- `unapproved severity for ${name}: expected ${allowedSeverity}, got ${vulnerability.severity}`,
199
- );
200
- }
201
- if (!Array.isArray(vulnerability.via) || !Array.isArray(vulnerability.nodes)) {
202
- throw new Error(`malformed npm audit graph entry for ${name}`);
203
- }
204
- if (!hasExactMembers(vulnerability.nodes, allowedNodes)) {
205
- throw new Error(
206
- `audit graph paths differ for ${name}: expected ${allowedNodes.join(",")}, got ${vulnerability.nodes.join(",")}`,
207
- );
208
- }
209
-
210
- const parentEdges = vulnerability.via.filter(
211
- (via): via is string => typeof via === "string",
212
- );
213
- const advisories = vulnerability.via.filter(
214
- (via): via is AuditAdvisory => typeof via !== "string",
215
- );
216
- if (allowedParents.length > 0) {
217
- if (advisories.length > 0 || !hasExactMembers(parentEdges, allowedParents)) {
218
- throw new Error(
219
- `audit graph edges differ for ${name}: expected ${allowedParents.join(",")}, got ${parentEdges.join(",")}`,
220
- );
221
- }
222
- for (const parent of parentEdges) {
223
- if (!vulnerabilities[parent]) {
224
- throw new Error(`missing npm audit graph node: ${parent}`);
225
- }
226
- }
227
- continue;
228
- }
229
- if (parentEdges.length > 0 || advisories.length !== 1) {
230
- throw new Error(`audit leaf shape differs for ${name}`);
231
- }
232
- const advisory = advisories[0];
233
- const allowed = ALLOWED_ADVISORIES.get(advisory.source);
234
- if (
235
- !allowed ||
236
- advisory.name !== allowed.packageName ||
237
- advisory.url !== allowed.url ||
238
- advisory.severity !== allowed.severity ||
239
- name !== allowed.packageName ||
240
- vulnerability.severity !== allowed.severity
241
- ) {
242
- throw new Error(
243
- `unapproved advisory for ${name}: source=${String(advisory.source)} url=${advisory.url}`,
244
- );
245
- }
246
- acceptedSources.add(advisory.source);
247
- }
248
-
249
- const rootsByPackage = new Map<string, Set<number>>();
250
- const resolveRoots = (name: string, stack: Set<string>): Set<number> => {
251
- const cached = rootsByPackage.get(name);
252
- if (cached) return cached;
253
- if (stack.has(name)) throw new Error(`cycle in npm audit graph at ${name}`);
254
- const vulnerability = vulnerabilities[name];
255
- if (!vulnerability) throw new Error(`missing npm audit graph node: ${name}`);
256
- const nextStack = new Set(stack).add(name);
257
- const roots = new Set<number>();
258
- for (const via of vulnerability.via) {
259
- if (typeof via === "string") {
260
- for (const source of resolveRoots(via, nextStack)) roots.add(source);
261
- } else {
262
- roots.add(via.source);
263
- }
264
- }
265
- if (roots.size === 0) {
266
- throw new Error(`npm audit graph node has no approved advisory root: ${name}`);
267
- }
268
- rootsByPackage.set(name, roots);
269
- return roots;
270
- };
271
-
272
- for (const name of Object.keys(vulnerabilities)) resolveRoots(name, new Set());
273
-
274
- for (const source of acceptedSources) {
275
- const allowed = ALLOWED_ADVISORIES.get(source);
276
- if (!allowed) throw new Error(`missing policy for advisory source ${source}`);
277
- const vulnerability = vulnerabilities[allowed.packageName];
278
- if (!vulnerability) {
279
- throw new Error(`missing leaf package for advisory source ${source}`);
280
- }
281
- for (const nodePath of vulnerability.nodes) {
282
- if (!allowed.nodes.includes(nodePath)) {
283
- throw new Error(
284
- `unapproved installed path for advisory source ${source}: ${nodePath}`,
285
- );
286
- }
287
- const version = readInstalledVersion(nodePath);
288
- if (version !== allowed.version) {
289
- throw new Error(
290
- `unapproved installed version at ${nodePath}: expected ${allowed.version}, got ${version}`,
291
- );
292
- }
293
- }
294
- }
295
-
296
- return {
297
- acceptedAdvisorySources: [...acceptedSources].sort((a, b) => a - b),
298
- vulnerabilityCount: entries.length,
299
- };
300
- }