ShtukbaseStandard
файл пакета→ .shtukbase/packages/std-owasp-secrets/checks/scr03_env.ts

checks/scr03_env.ts

27 строк, 1.7 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // SCR-R03: environment and key files stay out of the repository: `.env` is ignored by git, and no .env-like file
  2. // (except templates such as .env.example) or private key file is tracked or waiting to be added.
  3. import { existsSync, readFileSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. import { PKG } from './secretlib.ts';
  6. import { finish, git, inGit, norm, projectFiles, ROOT } from './lib/std.ts';
  7. const n = norm(PKG, 'SCR-R03', 'enforce');
  8. const out: string[] = [];
  9. const TEMPLATE = /\.(example|sample|template|dist|defaults|schema|tpl)$/i;
  10. const KEYFILE = /^(id_rsa|id_dsa|id_ecdsa|id_ed25519)$|\.(p12|pfx|jks|keystore)$/i;
  11. if (n.active) {
  12. if (inGit()) {
  13. const r = git(['check-ignore', '-q', '--no-index', '.env'], { okCodes: [1] });
  14. if (r !== null && git(['check-ignore', '-q', '--no-index', '.env']) === null) out.push('.gitignore: the .env file is not ignored → add the line .env (and .env.*, except !.env.example)');
  15. } else {
  16. const gi = join(ROOT, '.gitignore');
  17. const text = existsSync(gi) ? readFileSync(gi, 'utf8') : '';
  18. if (!text.split('\n').some((l) => /^\/?(\.env|\.env\*|\*\.env|\.env\.\*)\s*$/.test(l.trim()))) out.push('.gitignore: no .env line → add .env and .env.*, except !.env.example');
  19. }
  20. for (const f of projectFiles()) {
  21. const base = f.split('/').pop() ?? '';
  22. if (/^\.env(\..+)?$/.test(base) && !TEMPLATE.test(base)) out.push(`${f}: an environment file in the repository → remove from git (git rm --cached), revoke the secrets, keep a .env.example template without values`);
  23. else if (KEYFILE.test(base)) out.push(`${f}: a private key file in the repository → remove from git, revoke the key`);
  24. }
  25. }
  26. finish(n, out);