包文件→ .shtukbase/packages/std-owasp-secrets/checks/history.ts
checks/history.ts
39 行, 1.6 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // Action `history`: looks for known-format secrets in lines added by past commits (git log -p, read only).
- // `--last N` commits (default 200) or `--all`. Not a check: a secret in history needs revoking, not a commit.
- import { config, KNOWN, placeholder } from './secretlib.ts';
- import { flags, git } from './lib/std.ts';
- const f = flags();
- const cfg = config();
- const args = ['log', '-p', '--no-color', '--no-ext-diff', '--format=commit %h'];
- if (!f.all) args.push(`-n${typeof f.last === 'string' ? Number(f.last) : 200}`);
- const log = git(args);
- if (log === null) {
- console.log('not a git repository, or git does not answer');
- process.exit(0);
- }
- let commit = '';
- let file = '';
- let found = 0;
- let pem = false; // an added PEM header waits for its body on the next added line
- for (const l of log.split('\n')) {
- if (pem) {
- if (/^\+[A-Za-z0-9+/=]{40,}\s*$/.test(l)) {
- console.log(`commit ${commit} ${file}: ${KNOWN[0].kind}`);
- found++;
- }
- pem = false;
- }
- if (l.startsWith('commit ')) commit = l.slice(7);
- else if (l.startsWith('+++ ')) file = l.slice(4).replace(/^b\//, '');
- else if (l.startsWith('+') && !cfg.allowMarkers.some((m) => l.includes(m)))
- for (const k of KNOWN) {
- const m = k.re.exec(l);
- if (m && k === KNOWN[0]) pem = true;
- else if (m && !placeholder(m[0])) {
- console.log(`commit ${commit} ${file}: ${k.kind}`);
- found++;
- }
- }
- }
- console.log(found ? `findings: ${found}. Revoke the secret from the history and issue a new one; rewriting history is for the owner to decide.` : 'no known secrets found in the history');