@acemarke/diffloupe 0.4.1 → 0.5.1

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.
@@ -231,7 +231,9 @@ const splitSegments = (cmd: string): { sep: string; text: string }[] => {
231
231
  continue;
232
232
  }
233
233
  const two = cmd.slice(i, i + 2);
234
- const found = two === "&&" || two === "||" ? two : c === ";" || c === "|" ? c : "";
234
+ const background = c === "&" && cmd[i - 1] !== ">" && cmd[i - 1] !== "|" && cmd[i + 1] !== ">";
235
+ const found =
236
+ two === "&&" || two === "||" ? two : c === ";" || c === "|" || c === "\n" || background ? c : "";
235
237
  if (!found) continue;
236
238
  out.push({ sep, text: cmd.slice(start, i) });
237
239
  sep = found;
@@ -247,7 +249,8 @@ const commandCore = (segment: string): string =>
247
249
  segment
248
250
  .trim()
249
251
  .replace(/^(?:[A-Za-z_][A-Za-z0-9_]*=(?:'[^']*'|"[^"]*"|\S*)\s+)+/, "")
250
- .replace(/^(?:command|timeout\s+\S+|env|nohup|xargs)\s+/, "")
252
+ .replace(/^(?:timeout|env|xargs)(?:\s+(?:-\S+|\d[\w.]*|[A-Z][A-Z0-9_]*|[A-Za-z_]\w*=\S*|\{\}))*\s+/, "")
253
+ .replace(/^(?:command|nohup)\s+/, "")
251
254
  .trim();
252
255
 
253
256
  /**
@@ -280,8 +283,68 @@ const fileArgs = (core: string): string[] =>
280
283
  (tok.includes("/") || /\.[A-Za-z0-9]+$/.test(tok)),
281
284
  );
282
285
 
286
+ /**
287
+ * Commands that reach the network, refused on sight wherever they appear in a
288
+ * command line, pipe targets included.
289
+ *
290
+ * Nothing a review needs is on the network: the pipeline gathers the PR's
291
+ * description and prior comments before any agent starts. What is out there is
292
+ * the rest of the PR thread, which carries the author's arguments and is how a
293
+ * reviewer gets argued out of a finding without judging the code, and on a CI
294
+ * runner the job's token is in the agent's environment. This catches the
295
+ * commands, not every way to open a socket, so CI should restrict outbound
296
+ * network as well. The web tools are denied in the agent config.
297
+ */
298
+ const NETWORK_COMMAND = new RegExp(
299
+ [
300
+ /^(?:curl|wget|nc|ncat|netcat|telnet|ssh|scp|sftp|ftp|http|https|xh)\b/.source,
301
+ /^gh\s+(?:api|pr|issue|repo|run|search|browse|release|gist|workflow|auth)\b/.source,
302
+ /^git\s+(?:-[Cc]\s+\S+\s+)*(?:fetch|pull|push|clone|ls-remote|remote\s+update|submodule\s+update)\b/.source,
303
+ ].join("|"),
304
+ );
305
+ /** Interpreters whose inline code is checked for network calls. */
306
+ const INTERPRETER = /^(?:node|bun|deno|python[0-9.]*|ruby|perl|php)\b/;
307
+ const NETWORK_CODE =
308
+ /\bfetch\s*\(|\bhttps?\.(?:get|request)\b|['"](?:node:)?https?['"]|\burllib\b|\brequests\.|\bhttp\.client\b|\bsocket\b|Net::HTTP|LWP::|XMLHttpRequest|WebSocket|\b(?:Bun|Deno)\.connect\b/;
309
+
310
+ /** Commands nested inside a segment: `$(...)`, backticks, `sh -c '...'` and `eval '...'`. */
311
+ const nestedCommands = (text: string): string[] =>
312
+ [
313
+ /\$\((?<inner>[^()]*)\)/g,
314
+ /`(?<inner>[^`]*)`/g,
315
+ /\b(?:ba|z|da)?sh\s+(?:-[A-Za-z]+\s+)*-[A-Za-z]*c\s+(['"])(?<inner>.*?)\1/g,
316
+ /\beval\s+(['"])(?<inner>.*?)\1/g,
317
+ ].flatMap((re) => [...text.matchAll(re)].map((m) => m.groups?.inner ?? ""));
318
+
319
+ const reachesNetwork = (cmd: string, depth = 0): boolean => {
320
+ if (depth > 3) return false;
321
+ for (const segment of splitSegments(cmd)) {
322
+ let core = commandCore(segment.text);
323
+ for (let prev = ""; prev !== core; ) {
324
+ prev = core;
325
+ core = commandCore(
326
+ core
327
+ .replace(/^[({!&]\s*/, "")
328
+ .replace(/^(?:sudo|exec|nice)\s+/, "")
329
+ .replace(/^[^\s'"]*\//, ""),
330
+ );
331
+ }
332
+ if (NETWORK_COMMAND.test(core)) return true;
333
+ if (INTERPRETER.test(core) && NETWORK_CODE.test(core)) return true;
334
+ if (nestedCommands(segment.text).some((inner) => reachesNetwork(inner, depth + 1))) return true;
335
+ }
336
+ return false;
337
+ };
338
+
283
339
  /** Classify a shell command: refused outright, warned, or fine. */
284
340
  const checkShellCommand = (cmd: string): { rule: string; tip: string; deny: boolean } | null => {
341
+ if (reachesNetwork(cmd))
342
+ return {
343
+ rule: "network",
344
+ deny: true,
345
+ tip: "reach the network during a review. Everything the review needs is in the checkout and in your instructions; judge the code as it stands.",
346
+ };
347
+
285
348
  for (const segment of splitSegments(cmd)) {
286
349
  if (segment.sep === "|") continue;
287
350
  const core = commandCore(segment.text);