import type { SecretSpan, SpanLabel } from "./types.ts"; export type BuiltinRule = { id: string; tag: string; family: string; regex: RegExp; group: number; kind: SecretSpan["kind"]; wholeSpan: boolean; validate?: (match: RegExpMatchArray) => boolean; }; export const builtinRules: BuiltinRule[] = [ rule( "openrouter", "OPENROUTER_TOKEN", "ai", /\bsk-or-v1-[A-Za-z0-9]{32,}\b/dg, ), rule( "openai", "OPENAI_TOKEN", "ai", /(? validAgeSecret(match[0]), ), rule( "minisign-secret", "MINISIGN_SECRET_KEY", "private-key", /^untrusted comment:\s*minisign (?:encrypted )?secret key\r?\n([A-Za-z0-9+/]+={0,2})(?=\r?\n|$|['"\s])/dgim, 0, "private-key", true, (match) => validMinisignSecret(match[1]), ), ]; export const builtinRegexFuzzTargets = builtinRules.map((item) => ({ name: `builtin:${item.id}`, regex: item.regex, })); export function findBuiltinSpans(text: string): SecretSpan[] { const spans: SecretSpan[] = findPuttySpans(text); for (const item of builtinRules) { item.regex.lastIndex = 0; for (const match of text.matchAll(item.regex)) { if (item.validate && !item.validate(match)) continue; const value = match[item.group] as string; const range = match.indices?.[item.group] as [number, number]; spans.push({ start: range[0], end: range[1], value, labels: [label(item.tag)], kind: item.kind, confidence: item.wholeSpan ? 100 : 90, wholeSpan: item.wholeSpan, }); } } return spans; } function findPuttySpans(text: string): SecretSpan[] { const header = /^PuTTY-User-Key-File-[^\r\n]*\r?\n(?:[^\r\n]*\r?\n)*?Private-Lines:\s*(\d+)\r?\n/gm; const spans: SecretSpan[] = []; for (const match of text.matchAll(header)) { const count = Number(match[1]); if (!Number.isSafeInteger(count) || count < 1 || count > 4096) continue; let end = (match.index as number) + match[0].length; let valid = true; for (let index = 0; index < count; index++) { const newline = text.indexOf("\n", end); const lineEnd = newline === -1 ? text.length : newline; const line = text.slice(end, lineEnd).replace(/\r$/, ""); if (!validPuttyLine(line) || (newline === -1 && index !== count - 1)) { valid = false; break; } end = newline === -1 ? text.length : newline + 1; } if (!valid) continue; spans.push({ start: match.index as number, end, value: text.slice(match.index, end), labels: [label("PUTTY_PRIVATE_KEY")], kind: "private-key", confidence: 100, wholeSpan: true, }); } return spans; } function validAgeSecret(value: string) { const normalized = value.toLowerCase(); const separator = normalized.lastIndexOf("1"); const hrp = normalized.slice(0, separator); const charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; const data = [...normalized.slice(separator + 1)].map((char) => charset.indexOf(char), ); if (hrp !== "age-secret-key-" || data.some((item) => item < 0)) return false; const values = [ ...[...hrp].map((char) => char.charCodeAt(0) >> 5), 0, ...[...hrp].map((char) => char.charCodeAt(0) & 31), ...data, ]; let checksum = 1; for (const item of values) { const top = checksum >>> 25; checksum = (((checksum & 0x1ffffff) << 5) ^ item) >>> 0; for (let bit = 0; bit < 5; bit++) if ((top >>> bit) & 1) checksum = (checksum ^ [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3][ bit ]) >>> 0; } return checksum === 1; } function validMinisignSecret(value: string) { if (!/^[A-Za-z0-9+/]+={0,2}$/.test(value)) return false; const bytes = Buffer.from(value, "base64"); if (bytes.toString("base64") !== value || bytes.length !== 158) return false; if (bytes.subarray(0, 6).toString("ascii") !== "EdScB2") return false; const limits = bytes.subarray(38, 54); return limits.some((byte) => byte !== 0); } function validPuttyLine(value: string) { if (!/^[A-Za-z0-9+/]+={0,2}$/.test(value)) return false; return Buffer.from(value, "base64").toString("base64") === value; } function rule( id: string, tag: string, family: string, regex: RegExp, group = 0, kind: SecretSpan["kind"] = "builtin", wholeSpan = false, validate?: (match: RegExpMatchArray) => boolean, ): BuiltinRule { return { id, tag, family, regex, group, kind, wholeSpan, validate }; } function label(tag: string): SpanLabel { return { tag, priority: 40, source: "builtin" }; } function armorRegex(prefix: string, name: string) { const marker = `${prefix} ${name}`; return new RegExp( `-----BEGIN ${marker}-----[\\s\\S]*?-----END ${marker}-----`, "dg", ); }