|
| 1 | +import { explainShellCommand } from "../command-explainer/extract.js"; |
| 2 | +import type { CommandExplanation, CommandRisk } from "../command-explainer/types.js"; |
| 3 | +import type { ExecCommandSegment } from "../exec-approvals-analysis.js"; |
| 4 | +import { detectCommandCarrierArgv, detectInlineEvalInSegments } from "./risks.js"; |
| 5 | + |
| 6 | +export type CommandExplanationSummary = { |
| 7 | + commandCount: number; |
| 8 | + nestedCommandCount: number; |
| 9 | + riskKinds: string[]; |
| 10 | + warningLines: string[]; |
| 11 | +}; |
| 12 | + |
| 13 | +function riskLabel(risk: CommandRisk): string { |
| 14 | + switch (risk.kind) { |
| 15 | + case "inline-eval": |
| 16 | + return `${risk.command} ${risk.flag}`; |
| 17 | + case "shell-wrapper": |
| 18 | + return `${risk.executable} ${risk.flag}`; |
| 19 | + case "command-carrier": |
| 20 | + return risk.flag ? `${risk.command} ${risk.flag}` : risk.command; |
| 21 | + case "dynamic-argument": |
| 22 | + return `${risk.command} dynamic argument`; |
| 23 | + case "source": |
| 24 | + return risk.command; |
| 25 | + case "function-definition": |
| 26 | + return risk.name; |
| 27 | + default: |
| 28 | + return risk.kind; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export function summarizeCommandExplanation( |
| 33 | + explanation: CommandExplanation, |
| 34 | +): CommandExplanationSummary { |
| 35 | + const riskKinds = [...new Set(explanation.risks.map((risk) => risk.kind))]; |
| 36 | + const warningLines = explanation.risks.map((risk) => { |
| 37 | + const label = riskLabel(risk); |
| 38 | + return label === risk.kind ? `Contains ${risk.kind}` : `Contains ${risk.kind}: ${label}`; |
| 39 | + }); |
| 40 | + return { |
| 41 | + commandCount: explanation.topLevelCommands.length, |
| 42 | + nestedCommandCount: explanation.nestedCommands.length, |
| 43 | + riskKinds, |
| 44 | + warningLines: [...new Set(warningLines)], |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +function uniqueStrings(values: string[]): string[] { |
| 49 | + return [...new Set(values)]; |
| 50 | +} |
| 51 | + |
| 52 | +export function summarizeCommandSegmentsForDisplay( |
| 53 | + segments: readonly ExecCommandSegment[], |
| 54 | +): CommandExplanationSummary { |
| 55 | + const riskKinds: string[] = []; |
| 56 | + const warningLines: string[] = []; |
| 57 | + const inlineEval = detectInlineEvalInSegments(segments); |
| 58 | + if (inlineEval) { |
| 59 | + riskKinds.push("inline-eval"); |
| 60 | + warningLines.push( |
| 61 | + `Contains inline-eval: ${inlineEval.normalizedExecutable} ${inlineEval.flag}`, |
| 62 | + ); |
| 63 | + } |
| 64 | + for (const segment of segments) { |
| 65 | + const effectiveArgv = segment.resolution?.effectiveArgv ?? segment.argv; |
| 66 | + for (const hit of detectCommandCarrierArgv(effectiveArgv)) { |
| 67 | + riskKinds.push("command-carrier"); |
| 68 | + warningLines.push( |
| 69 | + hit.flag |
| 70 | + ? `Contains command-carrier: ${hit.command} ${hit.flag}` |
| 71 | + : `Contains command-carrier: ${hit.command}`, |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | + return { |
| 76 | + commandCount: segments.length, |
| 77 | + nestedCommandCount: 0, |
| 78 | + riskKinds: uniqueStrings(riskKinds), |
| 79 | + warningLines: uniqueStrings(warningLines), |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +export async function explainCommandForDisplay( |
| 84 | + command: string, |
| 85 | +): Promise<{ explanation: CommandExplanation; summary: CommandExplanationSummary } | null> { |
| 86 | + try { |
| 87 | + const explanation = await explainShellCommand(command); |
| 88 | + return { explanation, summary: summarizeCommandExplanation(explanation) }; |
| 89 | + } catch { |
| 90 | + return null; |
| 91 | + } |
| 92 | +} |
0 commit comments