// The checks every pushed file must pass before a commit lands ("tested, then merged"). // Kept short and public so anyone can see exactly what is checked. import { td } from './git/core'; export const MAX_FILE = 10 * 1024 * 1024; const SECRETS: [RegExp, string][] = [ [/-----BEGIN (?:RSA |EC |OPENSSH |DSA |PGP )?PRIVATE KEY-----/, 'a private key'], [/\bAKIA[0-9A-Z]{16}\b/, 'an AWS access key'], [/\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{32,}/, 'an API secret key'], [/\bgh[pousr]_[A-Za-z0-9]{36,}\b/, 'a GitHub token'], [/\bxox[abprs]-[A-Za-z0-9-]{10,}/, 'a Slack token'], [/\bAIza[0-9A-Za-z_-]{35}\b/, 'a Google API key'], [/\bbw_push_[A-Za-z0-9]{20,}/, 'a Branchwell push key'], ]; /** Returns why a file fails, or null when it passes. */ export function checkBlob(path: string, data: Uint8Array): string | null { if (data.length > MAX_FILE) return `file is over 10 MB (${(data.length / 1048576).toFixed(1)} MB)`; let bin = false; for (let i = 0; i < Math.min(data.length, 8000); i++) if (data[i] === 0) { bin = true; break; } if (bin) return null; const text = td.decode(data); for (const [re, what] of SECRETS) if (re.test(text)) return `looks like it contains ${what}. Never push secrets`; if (/\.json$/i.test(path)) { try { JSON.parse(text); } catch (e) { return `is not valid JSON (${(e as Error).message.slice(0, 80)})`; } } return null; } export const CHECKS_TEXT = [ 'No file over 10 MB.', 'No secrets: private keys, API keys and access tokens are refused.', 'Every .json file must be valid JSON.', 'History is never rewritten: pushes must build on the branch as it is (no force-push).', 'Branches are only removed by votes, never by push.', ];