Branchwell

Branchwell / source / src / checks.ts

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