ShtukbaseStandard
包文件→ .shtukbase/packages/std-owasp-secrets/checks/history.ts

checks/history.ts

39 行, 1.6 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // Action `history`: looks for known-format secrets in lines added by past commits (git log -p, read only).
  2. // `--last N` commits (default 200) or `--all`. Not a check: a secret in history needs revoking, not a commit.
  3. import { config, KNOWN, placeholder } from './secretlib.ts';
  4. import { flags, git } from './lib/std.ts';
  5. const f = flags();
  6. const cfg = config();
  7. const args = ['log', '-p', '--no-color', '--no-ext-diff', '--format=commit %h'];
  8. if (!f.all) args.push(`-n${typeof f.last === 'string' ? Number(f.last) : 200}`);
  9. const log = git(args);
  10. if (log === null) {
  11. console.log('not a git repository, or git does not answer');
  12. process.exit(0);
  13. }
  14. let commit = '';
  15. let file = '';
  16. let found = 0;
  17. let pem = false; // an added PEM header waits for its body on the next added line
  18. for (const l of log.split('\n')) {
  19. if (pem) {
  20. if (/^\+[A-Za-z0-9+/=]{40,}\s*$/.test(l)) {
  21. console.log(`commit ${commit} ${file}: ${KNOWN[0].kind}`);
  22. found++;
  23. }
  24. pem = false;
  25. }
  26. if (l.startsWith('commit ')) commit = l.slice(7);
  27. else if (l.startsWith('+++ ')) file = l.slice(4).replace(/^b\//, '');
  28. else if (l.startsWith('+') && !cfg.allowMarkers.some((m) => l.includes(m)))
  29. for (const k of KNOWN) {
  30. const m = k.re.exec(l);
  31. if (m && k === KNOWN[0]) pem = true;
  32. else if (m && !placeholder(m[0])) {
  33. console.log(`commit ${commit} ${file}: ${k.kind}`);
  34. found++;
  35. }
  36. }
  37. }
  38. 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');