tewip-ai 0.18.4 → 0.18.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@
5
5
  import { existsSync } from 'node:fs';
6
6
  import { join } from 'node:path';
7
7
  import { spawnSync } from 'node:child_process';
8
+ const NEWLINE = String.fromCharCode(10);
8
9
  const run = (command, cwd, timeoutMs = 5 * 60_000) => {
9
10
  const r = spawnSync(command, { cwd, shell: true, encoding: 'utf8', timeout: timeoutMs, maxBuffer: 40 * 1024 * 1024 });
10
11
  return { code: r.status ?? 1, out: `${r.stdout ?? ''}`, err: `${r.stderr ?? ''}` };
@@ -15,14 +16,26 @@ export const bySeverity = (a, b) => SEVERITY_ORDER.indexOf(a.severity.toLowerCas
15
16
  /** `npm audit --json`, which needs no install and no account: every Node project already has it. */
16
17
  export function npmAudit(root) {
17
18
  if (!existsSync(join(root, 'package.json')))
18
- return [];
19
- const { out } = run('npm audit --json', root);
19
+ return { findings: [], problem: null };
20
+ const { out, err } = run('npm audit --json', root);
20
21
  let report;
21
22
  try {
22
23
  report = JSON.parse(out);
23
24
  }
24
25
  catch {
25
- return [];
26
+ return { findings: [], problem: `npm audit did not return a report${err.trim() ? `: ${err.trim().split(NEWLINE)[0].slice(0, 120)}` : ''}` };
27
+ }
28
+ // npm reports a refusal inside the JSON and still exits 0: no lockfile, a registry that will not
29
+ // answer, a private registry needing credentials. None of those is "nothing found".
30
+ // Two shapes, both exiting 0. A refusal names itself - {"error":{"code":"ENOLOCK","summary":...}}
31
+ // - while a registry that cannot be reached leaves those fields empty and puts the reason in
32
+ // `message`. Reading summary with ?? kept the empty string, so an unreachable registry passed for
33
+ // a clean audit on a project with three known advisories.
34
+ const failure = report.error || (report.message && !report.vulnerabilities);
35
+ if (failure) {
36
+ const reason = [report.error?.summary, report.error?.code, report.message, 'npm audit could not run'].find((x) => typeof x === 'string' && x.trim());
37
+ const hint = report.error?.code === 'ENOLOCK' ? ' (run `npm i --package-lock-only` first)' : '';
38
+ return { findings: [], problem: `${String(reason).slice(0, 160)}${hint}` };
26
39
  }
27
40
  const findings = [];
28
41
  for (const [name, entry] of Object.entries(report.vulnerabilities ?? {})) {
@@ -40,7 +53,7 @@ export function npmAudit(root) {
40
53
  dev: Boolean(entry.isDirect === false ? false : entry.dev ?? false) || Boolean(entry.effects?.length === 0 && entry.dev),
41
54
  });
42
55
  }
43
- return findings;
56
+ return { findings, problem: null };
44
57
  }
45
58
  /** `pip-audit`, when the project is Python and the tool is installed. */
46
59
  export function pipAudit(root) {
@@ -105,8 +118,12 @@ export function auditDependencies(root) {
105
118
  const missing = [];
106
119
  const findings = [];
107
120
  if (existsSync(join(root, 'package.json'))) {
108
- ran.push('npm audit');
109
- findings.push(...npmAudit(root));
121
+ const npm = npmAudit(root);
122
+ if (npm.problem)
123
+ missing.push(`npm audit could not run: ${npm.problem}`);
124
+ else
125
+ ran.push('npm audit');
126
+ findings.push(...npm.findings);
110
127
  }
111
128
  const python = ['requirements.txt', 'pyproject.toml', 'setup.py'].some((f) => existsSync(join(root, f)));
112
129
  if (python) {
@@ -0,0 +1,42 @@
1
+ /** The path a web finding says is being served, e.g. "/.env" from "GET http://host/.env answered". */
2
+ function servedPath(item) {
3
+ if (item.kind !== 'web')
4
+ return null;
5
+ // Take the whole address and ask the URL parser for the path. Pulling the path out with a regular
6
+ // expression captured "//127.0.0.1:3211/.env", host and all, and then matched nothing.
7
+ const found = /GET\s+(\S+)\s+answered/.exec(item.why);
8
+ if (!found)
9
+ return null;
10
+ try {
11
+ return new URL(found[1]).pathname;
12
+ }
13
+ catch {
14
+ return found[1].startsWith('/') ? found[1] : null;
15
+ }
16
+ }
17
+ /** The file a secret finding is in, from "a database URL with a password in it in .env:1". */
18
+ const fileOf = (item) => (item.kind === 'secret' ? (item.where.split(':')[0] ?? '') : '');
19
+ /**
20
+ * Where one check changes what another means. Today that is one case, and it is the one that
21
+ * matters most: a secret sitting in a file the application serves.
22
+ */
23
+ export function reconcile(items) {
24
+ const served = new Set(items
25
+ .map(servedPath)
26
+ .filter((p) => Boolean(p))
27
+ .map((p) => p.replace(/^\//, '').toLowerCase()));
28
+ if (served.size === 0)
29
+ return items;
30
+ return items.map((item) => {
31
+ const file = fileOf(item).replace(/\\/g, '/').toLowerCase();
32
+ if (!file || !served.has(file) || item.verdict === 'accepted')
33
+ return item;
34
+ return {
35
+ ...item,
36
+ verdict: 'real-risk',
37
+ severity: 'critical',
38
+ why: `${item.why} - but the running application serves /${file} to anyone who asks, so this credential is public`,
39
+ next: `treat this credential as leaked and rotate it now, then stop serving /${file}`,
40
+ };
41
+ });
42
+ }
@@ -9,6 +9,7 @@ import { auditDependencies } from './deps.js';
9
9
  import { scanSecrets, sourceFiles } from './secrets.js';
10
10
  import { readAccepted, triageDependency, triageSecret, triageWeb, worstFirst } from './triage.js';
11
11
  import { isLocal, scanWeb } from './web.js';
12
+ import { reconcile } from './reconcile.js';
12
13
  import { loadVerdicts, saidByAPerson, verdictFor } from '../learn/verdicts.js';
13
14
  import { readZapReport, zapAvailable, zapInstructions } from './zap.js';
14
15
  const CODE = /\.(?:[cm]?[jt]sx?|py|rb|go|java|kt|cs|php|rs|vue|svelte)$/;
@@ -98,6 +99,11 @@ export async function runSecure(cmd) {
98
99
  else if (cmd.url) {
99
100
  notChecked.push(`a full web scan. Tewip's own check is passive; for the deep one: ${zapInstructions(cmd.url, zapAvailable(root))}`);
100
101
  }
102
+ // One check can change what another means: a secret in a file the application serves is not
103
+ // "never left the machine", whatever git says about it.
104
+ const joined = reconcile(items);
105
+ items.length = 0;
106
+ items.push(...joined);
101
107
  // A person's decision about a finding wins over Tewip's, and says whose it was.
102
108
  const corrections = loadVerdicts(root);
103
109
  const decided = items.map((item) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tewip-ai",
3
- "version": "0.18.4",
3
+ "version": "0.18.6",
4
4
  "description": "Tewip finds why each failing test failed - real bug, flaky test, environment, test data or changed selector - for any stack, from the command line, CI, or your editor.",
5
5
  "keywords": [
6
6
  "test-failures",