ai-secure-audit 0.0.15 → 0.0.16

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.
package/src/report.js CHANGED
@@ -125,6 +125,7 @@ export function reportMarkdown(result, date) {
125
125
  lines.push('');
126
126
  lines.push('- Whether one user can open or change another user\'s data (for example by changing an id in the URL)');
127
127
  lines.push('- Business rules: prices, coupons, approvals, payments');
128
+ lines.push('- How sign-in, sign-up and password reset work (for example a role the sign-up form can set, a reset question anyone can guess, or a login that skips a check)');
128
129
  lines.push('- Database rules such as Supabase row-level security or Firebase security rules (planned)');
129
130
  lines.push('- Anything about the live server or website');
130
131
  for (const g of result.coverage?.gaps ?? []) lines.push(`- ${g}`);
@@ -154,7 +155,7 @@ export function consoleSummary(result, outRel) {
154
155
  '- A finding marked not_committed can be moved out of the code without rotating, unless the key was shared or pasted somewhere else.',
155
156
  '- Findings of type code come from pattern rules and can be wrong. Read the code at that location and tell the user',
156
157
  ' if a limit or check exists somewhere the rule cannot see (another file, the proxy) before changing anything.',
157
- '- Not checked: whether one user can reach another user\'s data, and business rules. Say so.',
158
+ '- Not checked: whether one user can reach another user\'s data, business rules, and how sign-in, sign-up and password reset work. Say so.',
158
159
  ...(result.coverage?.gaps ?? []).map((g) => `- ${g} Tell the user.`),
159
160
  ...(result.notices ?? []).map((n) => `- ${n.message} Tell the user, and ask before editing the file.`),
160
161
  ...(failed.length ? ['- Some tools did not run (see NOT RUN above). Tell the user the check is incomplete.'] : []),
package/src/scan.js CHANGED
@@ -59,6 +59,7 @@ export async function scan(target, { log = () => {}, version }) {
59
59
  const runs = [];
60
60
  let findings = [];
61
61
  let librariesRead = null; // stays null when the library scanner did not run: the report says that already
62
+ let codeRead = null; // files the code checker could not read fully; null when it did not run
62
63
  for (const s of BASIC_SCANNERS) {
63
64
  const run = { id: s.id, version: tools[s.id].version, license: tools[s.id].license, status: 'ok' };
64
65
  runs.push(run);
@@ -84,6 +85,7 @@ export async function scan(target, { log = () => {}, version }) {
84
85
  json = pass.name ? [...(json ?? []), ...(data ?? []).map((item) => ({ ...item, pass: pass.name }))] : data;
85
86
  }
86
87
  if (s.filesRead) librariesRead = new Set(s.filesRead(json, ctx));
88
+ if (s.codeRead) codeRead = s.codeRead(json, ctx);
87
89
  const found = s.parse(json, ctx).map((f) => ({ ...f, source: { ...f.source, tool_version: run.version } }));
88
90
  run.findings = found.length;
89
91
  findings = findings.concat(found);
@@ -94,6 +96,7 @@ export async function scan(target, { log = () => {}, version }) {
94
96
  }
95
97
 
96
98
  const coverage = projectCoverage(root);
99
+ if (codeRead) coverage.code_read = codeRead;
97
100
  coverage.unread = librariesRead ? coverage.lockfiles.map((l) => l.file).filter((f) => !librariesRead.has(f)) : [];
98
101
  const result = {
99
102
  generator: `ai-secure-audit ${version}`,
@@ -104,7 +107,7 @@ export async function scan(target, { log = () => {}, version }) {
104
107
  // About this tool's own output, not a weakness of the app: kept apart from findings so it is never rated.
105
108
  notices: dockerignoreNotices(root),
106
109
  notes: [
107
- 'Not checked by this tool: whether one user can reach another user\'s data, business rules, and anything running on a server.',
110
+ 'Not checked by this tool: whether one user can reach another user\'s data, business rules, how sign-in, sign-up and password reset work, and anything running on a server.',
108
111
  'Code rules (category code) are this kit\'s own opengrep rules. They can be wrong: check the location before changing code.',
109
112
  'Config files are checked by trivy: Dockerfiles, Kubernetes, Helm, Terraform, CloudFormation, Azure ARM and Ansible files. ' +
110
113
  'trivy does not read Docker Compose files; coverage.gaps names any that were not checked.',
@@ -22,25 +22,68 @@ export default {
22
22
 
23
23
  // Local rules only, no version check: nothing about the project leaves the machine.
24
24
  // --no-git-ignore: code that is not committed yet is still code that will run.
25
+ // --taint-intrafile: follow a request value into a helper function in the same file (sql-built-from-request).
25
26
  args(ctx, out) {
26
27
  return ['scan', '--config', RULES, '--json', '--output', out, '--disable-version-check', '--quiet',
27
- '--no-git-ignore', ...[...EXCLUDE, ...RULE_TESTS].flatMap((d) => ['--exclude', d]), ctx.root];
28
+ '--no-git-ignore', '--taint-intrafile', ...[...EXCLUDE, ...RULE_TESTS].flatMap((d) => ['--exclude', d]), ctx.root];
28
29
  },
29
30
 
30
31
  okExit: [0],
31
32
 
33
+ // Files opengrep could not check fully, from its errors list: a syntax error means the whole file was skipped, a
34
+ // partial parse means some lines were skipped (a finding there can be missing), a timeout means a rule gave up on
35
+ // the file. Errors with no file (about a rule) are left out: those make the run fail instead.
36
+ codeRead(json, ctx) {
37
+ const notRead = new Map();
38
+ const inPart = new Map();
39
+ const stopped = new Map();
40
+ for (const e of json?.errors ?? []) {
41
+ if (!e?.path) continue;
42
+ const file = rel(ctx.root, e.path);
43
+ const kind = Array.isArray(e.type) ? e.type[0] : e.type;
44
+ const lines = (e.spans ?? []).map((s) => s?.start?.line).filter((n) => Number.isInteger(n));
45
+ // A syntax error that stops the whole file has no spans; its message names the line: "...at line <path>:7:".
46
+ const inMessage = Number(String(e.message ?? '').split(`${e.path}:`)[1]?.match(/^(\d+):/)?.[1]);
47
+ if (!lines.length && Number.isInteger(inMessage) && inMessage > 0) lines.push(inMessage);
48
+ if (kind === 'PartialParsing') {
49
+ inPart.set(file, [...new Set([...(inPart.get(file) ?? []), ...lines])].sort((a, b) => a - b));
50
+ } else if (kind === 'Timeout') {
51
+ const rule = String(e.rule_id ?? '').split('.').at(-1);
52
+ stopped.set(file, [...new Set([...(stopped.get(file) ?? []), ...(rule ? [rule] : [])])].sort());
53
+ } else if (!notRead.has(file)) {
54
+ notRead.set(file, lines[0] ?? null);
55
+ }
56
+ }
57
+ const sorted = (m) => [...m].sort((a, b) => (a[0] < b[0] ? -1 : 1));
58
+ return {
59
+ not_read: sorted(notRead).map(([file, line]) => ({ file, line })),
60
+ read_in_part: sorted(inPart).filter(([file]) => !notRead.has(file)).map(([file, lines]) => ({ file, lines })),
61
+ stopped: sorted(stopped).map(([file, rules]) => ({ file, rules })),
62
+ };
63
+ },
64
+
32
65
  parse(json, ctx) {
33
66
  // opengrep prefixes the rule id with the config path, joined by dots; our rule ids have no dots.
34
67
  const results = (json?.results ?? []).map((r) => ({ ...r, ruleId: r.check_id.split('.').at(-1) }));
35
68
  // Internal rules are never shown: they only record that something exists anywhere in the project (for example a
36
69
  // throttle registered for the whole app), and a rule with suppressed-by: <id> is dropped when that rule matched.
37
70
  const seen = new Set(results.filter((r) => r.extra?.metadata?.internal).map((r) => r.ruleId));
71
+ // A rule with suppressed-on-same-line-by: <id> is dropped where that rule reported the same file and line (a
72
+ // medium "check the callers" finding is not needed next to the high one that already sees the request).
73
+ const lineOf = (r) => `${rel(ctx.root, r.path)}:${r.start.line}`;
74
+ const reported = new Set(results.map((r) => `${r.ruleId}@${lineOf(r)}`));
38
75
  const findings = [];
76
+ // A taint rule reports one result per path from a source to the same line (three request values in one query
77
+ // give three results): one line is one finding.
78
+ const done = new Set();
39
79
  for (const r of results) {
40
80
  const meta = r.extra?.metadata ?? {};
41
81
  if (meta.internal || (meta['suppressed-by'] && seen.has(meta['suppressed-by']))) continue;
82
+ if (meta['suppressed-on-same-line-by'] && reported.has(`${meta['suppressed-on-same-line-by']}@${lineOf(r)}`)) continue;
42
83
  const ruleId = r.ruleId;
43
84
  const location = `${rel(ctx.root, r.path)}:${r.start.line}`;
85
+ if (done.has(`${ruleId}:${location}`)) continue;
86
+ done.add(`${ruleId}:${location}`);
44
87
  const [title, ...rest] = (r.extra?.message ?? ruleId).split('. ');
45
88
  findings.push({
46
89
  fingerprint: `opengrep:${ruleId}:${location}`,