|
| 1 | +import { tool } from "ai"; |
| 2 | +import type { Accepts } from "aixyz/accepts"; |
| 3 | +import { z } from "zod"; |
| 4 | + |
| 5 | +const GITLEAKS_BASE_URL = process.env.GITLEAKS_BASE_URL ?? "http://gitleaks.railway.internal:8080"; |
| 6 | + |
| 7 | +interface GitleaksFinding { |
| 8 | + Description: string; |
| 9 | + StartLine: number; |
| 10 | + EndLine: number; |
| 11 | + StartColumn: number; |
| 12 | + EndColumn: number; |
| 13 | + Match: string; |
| 14 | + Secret: string; |
| 15 | + File: string; |
| 16 | + SymlinkFile: string; |
| 17 | + Commit: string; |
| 18 | + Entropy: number; |
| 19 | + Author: string; |
| 20 | + Email: string; |
| 21 | + Date: string; |
| 22 | + Message: string; |
| 23 | + Tags: string[]; |
| 24 | + RuleID: string; |
| 25 | + Fingerprint: string; |
| 26 | +} |
| 27 | + |
| 28 | +function redact(value: string): string { |
| 29 | + if (value.length <= 8) { |
| 30 | + return value.slice(0, 2) + "*".repeat(value.length - 2); |
| 31 | + } |
| 32 | + const visibleStart = Math.min(4, Math.floor(value.length * 0.15)); |
| 33 | + const visibleEnd = Math.min(4, Math.floor(value.length * 0.15)); |
| 34 | + return value.slice(0, visibleStart) + "*".repeat(value.length - visibleStart - visibleEnd) + value.slice(-visibleEnd); |
| 35 | +} |
| 36 | + |
| 37 | +export const accepts: Accepts = { |
| 38 | + scheme: "exact", |
| 39 | + price: "$0.01", |
| 40 | +}; |
| 41 | + |
| 42 | +export default tool({ |
| 43 | + description: |
| 44 | + "Scan text or code for exposed credentials, API keys, tokens, private keys, and other secrets. Returns findings with rule ID, description, redacted secret, line number, and tags.", |
| 45 | + inputSchema: z.object({ |
| 46 | + text: z.string().describe("The code or text content to scan for exposed secrets."), |
| 47 | + }), |
| 48 | + execute: async ({ text }) => { |
| 49 | + const response = await fetch(`${GITLEAKS_BASE_URL}/scan`, { |
| 50 | + method: "POST", |
| 51 | + headers: { "Content-Type": "application/json" }, |
| 52 | + body: JSON.stringify({ content: text }), |
| 53 | + }); |
| 54 | + |
| 55 | + if (!response.ok) { |
| 56 | + const errorText = await response.text(); |
| 57 | + throw new Error(`Scan service error (${response.status}): ${errorText}`); |
| 58 | + } |
| 59 | + |
| 60 | + const raw: GitleaksFinding[] = await response.json(); |
| 61 | + |
| 62 | + const findings = raw.map((f) => ({ |
| 63 | + ruleId: f.RuleID, |
| 64 | + description: f.Description, |
| 65 | + match: redact(f.Secret || f.Match), |
| 66 | + startLine: f.StartLine, |
| 67 | + endLine: f.EndLine, |
| 68 | + tags: f.Tags ?? [], |
| 69 | + })); |
| 70 | + |
| 71 | + return { |
| 72 | + totalFindings: findings.length, |
| 73 | + findings, |
| 74 | + }; |
| 75 | + }, |
| 76 | +}); |
0 commit comments