kryptheon 0.1.57 → 0.1.61

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.
@@ -1,222 +1,222 @@
1
- // What changed in a project since Kryptheon last looked, and what that means
2
- // for which checks have to run.
3
- //
4
- // A snapshot is a content hash of every file that is the project's own - its
5
- // source, its database migrations, its dependency list - kept in the store,
6
- // never in the project. Comparing two snapshots answers the first question a
7
- // person has after an AI tool says "done": what did it actually touch?
8
- //
9
- // Content, not timestamps and not git: an editor that saves a file unchanged
10
- // is not a change, and a landing page with no git repository still has one.
11
- //
12
- // Each changed file is put in a part of the app (frontend, backend, database,
13
- // tests, dependencies, config, docs), and each part names the checks that
14
- // can speak to it. The mapping is written here, once, and deliberately
15
- // plain: a path-based guess that is right for how most projects are laid
16
- // out, and said as a guess ("likely affected") rather than a proof.
17
-
18
- const fs = require('fs');
19
- const path = require('path');
20
- const crypto = require('crypto');
21
-
22
- const SKIP_DIRS = new Set([
23
- 'node_modules', '.git', 'dist', 'build', 'out', '.next', '.nuxt', '.svelte-kit', 'coverage',
24
- '.vercel', '.netlify', '.turbo', '.cache', 'test-results', 'playwright-report', '.kryptheon',
25
- ]);
26
- const MAX_BYTES = 5 * 1024 * 1024;
27
-
28
- // Where a file sits in the app. The first rule that matches wins, so the
29
- // specific ones come first: a .sql file inside backend/ is a database change.
30
- const PARTS = [
31
- { part: 'dependencies', test: (p) => /(^|\/)(package\.json|package-lock\.json|yarn\.lock|pnpm-lock\.yaml|bun\.lockb?|requirements\.txt|poetry\.lock|Gemfile\.lock|go\.sum)$/i.test(p) },
32
- { part: 'database', test: (p) => /\.sql$/i.test(p) || /(^|\/)(migrations|supabase|prisma|drizzle|db\/schema)(\/|$)/i.test(p) || /(^|\/)schema\.prisma$/i.test(p) },
33
- { part: 'tests', test: (p) => /(^|\/)(tests?|__tests__|e2e|cypress)\//i.test(p) || /\.(spec|test)\.[cm]?[jt]sx?$/i.test(p) },
34
- { part: 'docs', test: (p) => /\.(md|mdx|txt|rst)$/i.test(p) || /(^|\/)docs\//i.test(p) },
35
- { part: 'config', test: (p) => /(^|\/)(\.env\.example|\.env\.sample|tsconfig[^/]*\.json|jsconfig\.json|vercel\.json|netlify\.toml|dockerfile|docker-compose[^/]*\.ya?ml)$/i.test(p) || /\.config\.[cm]?[jt]s$/i.test(p) || /(^|\/)\.github\//i.test(p) },
36
- { part: 'backend', test: (p) => /(^|\/)(server|backend|api|routes|controllers|services|middleware|functions|workers|jobs|lib\/server)(\/|\.[cm]?[jt]s$)/i.test(p) || /(^|\/)(server|app|index)\.[cm]?[jt]s$/i.test(p) && !/(^|\/)(src|public|frontend|client|pages|components)\//i.test(p) },
37
- { part: 'frontend', test: (p) => /\.(html?|css|scss|sass|less|jsx|tsx|vue|svelte|astro)$/i.test(p) || /(^|\/)(frontend|client|public|src|pages|components|app|views|static)\//i.test(p) && /\.[cm]?[jt]s$/i.test(p) },
38
- { part: 'assets', test: (p) => /\.(png|jpe?g|gif|svg|webp|ico|woff2?|ttf|otf|mp4|webm|mp3|pdf)$/i.test(p) },
39
- ];
40
-
41
- // Which checks can speak to a change in each part. Ids are the dashboard's.
42
- const CHECKS_FOR = {
43
- frontend: ['frontend', 'regression'],
44
- backend: ['backend', 'api', 'regression'],
45
- database: ['data', 'regression'],
46
- dependencies: ['dependency', 'build', 'regression'],
47
- tests: ['regression'],
48
- config: ['build', 'regression'],
49
- code: ['regression'],
50
- docs: [],
51
- assets: [],
52
- };
53
-
54
- function partOf(rel) {
55
- const p = rel.split(path.sep).join('/');
56
- const hit = PARTS.find((r) => r.test(p));
57
- if (hit) return hit.part;
58
- return /\.[cm]?[jt]sx?$/i.test(p) ? 'code' : 'other';
59
- }
60
-
61
- /** Every file that is the project's own, as relative paths with forward slashes. */
62
- function listFiles(root) {
63
- const out = [];
64
- (function walk(dir) {
65
- let entries;
66
- try {
67
- entries = fs.readdirSync(dir, { withFileTypes: true });
68
- } catch (err) {
69
- return;
70
- }
71
- for (const entry of entries) {
72
- const full = path.join(dir, entry.name);
73
- if (entry.isDirectory()) {
74
- if (!SKIP_DIRS.has(entry.name)) walk(full);
75
- } else if (entry.isFile()) {
76
- // A real .env holds secrets; its content is never hashed or kept.
77
- if (/^\.env(\..+)?$/i.test(entry.name) && !/\.(example|sample)$/i.test(entry.name)) continue;
78
- out.push(path.relative(root, full).split(path.sep).join('/'));
79
- }
80
- }
81
- })(root);
82
- return out.sort();
83
- }
84
-
85
- /** The dependency lists of package.json, kept so a change can say which package moved. */
86
- function dependenciesOf(root) {
87
- try {
88
- const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'));
89
- return Object.assign({}, pkg.dependencies || {}, pkg.devDependencies || {});
90
- } catch (err) {
91
- return null;
92
- }
93
- }
94
-
95
- /**
96
- * The project as it is now. Files whose size and modified time match the
97
- * previous snapshot keep its hash without being read again - on a large
98
- * project that is the difference between milliseconds and seconds, and an
99
- * unchanged size and time is the same bytes for every editor that exists.
100
- */
101
- function takeSnapshot(root, previous) {
102
- const before = (previous && previous.files) || {};
103
- const files = {};
104
- for (const rel of listFiles(root)) {
105
- let stat;
106
- try {
107
- stat = fs.statSync(path.join(root, rel));
108
- } catch (err) {
109
- continue;
110
- }
111
- const old = before[rel];
112
- if (old && old.size === stat.size && old.mtimeMs === stat.mtimeMs) {
113
- files[rel] = old;
114
- continue;
115
- }
116
- let hash;
117
- if (stat.size > MAX_BYTES) {
118
- hash = 'size:' + stat.size + ':' + stat.mtimeMs;
119
- } else {
120
- try {
121
- hash = crypto.createHash('sha1').update(fs.readFileSync(path.join(root, rel))).digest('hex');
122
- } catch (err) {
123
- continue;
124
- }
125
- }
126
- files[rel] = { hash: hash, size: stat.size, mtimeMs: stat.mtimeMs };
127
- }
128
- return { takenAt: new Date().toISOString(), files: files, dependencies: dependenciesOf(root) };
129
- }
130
-
131
- /** Packages added, removed or moved to another version between two snapshots. */
132
- function dependencyChanges(before, after) {
133
- const a = (before && before.dependencies) || {};
134
- const b = (after && after.dependencies) || {};
135
- const out = [];
136
- for (const name of new Set(Object.keys(a).concat(Object.keys(b)))) {
137
- if (!(name in a)) out.push({ name: name, change: 'added', to: b[name] });
138
- else if (!(name in b)) out.push({ name: name, change: 'removed', from: a[name] });
139
- else if (a[name] !== b[name]) out.push({ name: name, change: 'changed', from: a[name], to: b[name] });
140
- }
141
- return out.sort((x, y) => x.name.localeCompare(y.name));
142
- }
143
-
144
- /**
145
- * What changed between two snapshots, by part of the app, and which checks
146
- * that calls for. With no earlier snapshot there is nothing to compare, and
147
- * that is said - it is not reported as "everything changed".
148
- */
149
- function compare(before, after) {
150
- if (!before) return { first: true, files: [], parts: [], checks: [], dependencies: [] };
151
- const files = [];
152
- const a = before.files || {};
153
- const b = after.files || {};
154
- for (const rel of Object.keys(b)) {
155
- if (!a[rel]) files.push({ path: rel, state: 'added' });
156
- else if (a[rel].hash !== b[rel].hash) files.push({ path: rel, state: 'modified' });
157
- }
158
- for (const rel of Object.keys(a)) if (!b[rel]) files.push({ path: rel, state: 'deleted' });
159
- files.sort((x, y) => x.path.localeCompare(y.path));
160
- for (const f of files) f.part = partOf(f.path);
161
-
162
- const parts = [];
163
- for (const f of files) if (!parts.includes(f.part)) parts.push(f.part);
164
- const checks = [];
165
- for (const part of parts) for (const id of CHECKS_FOR[part] || []) if (!checks.includes(id)) checks.push(id);
166
- return {
167
- first: false,
168
- files: files,
169
- parts: parts,
170
- checks: checks,
171
- dependencies: dependencyChanges(before, after),
172
- };
173
- }
174
-
175
- /**
176
- * Findings that were not there last time. Keyed by what they are about rather
177
- * than by line number: a line added above a known finding moves it, and a
178
- * moved finding reported as new would teach a person to ignore this list.
179
- */
180
- //
181
- // The detail is part of what a finding is only for the code read, where it is
182
- // the expression itself. For a recorded flow or a database finding it is the
183
- // account of how it broke this time - "ended up somewhere different", "could
184
- // not be opened" - and a flow that is still broken in a new way is the same
185
- // finding, not a fixed one and a new one.
186
- function findingKey(f) {
187
- return [
188
- f.check || '',
189
- f.where ? String(f.where).replace(/:\d+$/, '') : '',
190
- f.headline ? String(f.headline).replace(/:\d+\b/g, '') : '',
191
- f.check === 'frontend' ? f.detail || '' : '',
192
- ].join('|');
193
- }
194
-
195
- /** A short, stable name for a finding: what a person types to re-check it. */
196
- function findingId(f) {
197
- return crypto.createHash('sha1').update(findingKey(f)).digest('hex').slice(0, 8);
198
- }
199
-
200
- function newFindings(before, after) {
201
- const seen = new Set((before || []).map(findingKey));
202
- return (after || []).filter((f) => !seen.has(findingKey(f)));
203
- }
204
-
205
- function goneFindings(before, after) {
206
- const now = new Set((after || []).map(findingKey));
207
- return (before || []).filter((f) => !now.has(findingKey(f)));
208
- }
209
-
210
- module.exports = {
211
- PARTS: PARTS,
212
- CHECKS_FOR: CHECKS_FOR,
213
- partOf: partOf,
214
- listFiles: listFiles,
215
- takeSnapshot: takeSnapshot,
216
- compare: compare,
217
- dependencyChanges: dependencyChanges,
218
- findingKey: findingKey,
219
- findingId: findingId,
220
- newFindings: newFindings,
221
- goneFindings: goneFindings,
222
- };
1
+ // What changed in a project since Kryptheon last looked, and what that means
2
+ // for which checks have to run.
3
+ //
4
+ // A snapshot is a content hash of every file that is the project's own - its
5
+ // source, its database migrations, its dependency list - kept in the store,
6
+ // never in the project. Comparing two snapshots answers the first question a
7
+ // person has after an AI tool says "done": what did it actually touch?
8
+ //
9
+ // Content, not timestamps and not git: an editor that saves a file unchanged
10
+ // is not a change, and a landing page with no git repository still has one.
11
+ //
12
+ // Each changed file is put in a part of the app (frontend, backend, database,
13
+ // tests, dependencies, config, docs), and each part names the checks that
14
+ // can speak to it. The mapping is written here, once, and deliberately
15
+ // plain: a path-based guess that is right for how most projects are laid
16
+ // out, and said as a guess ("likely affected") rather than a proof.
17
+
18
+ const fs = require('fs');
19
+ const path = require('path');
20
+ const crypto = require('crypto');
21
+
22
+ const SKIP_DIRS = new Set([
23
+ 'node_modules', '.git', 'dist', 'build', 'out', '.next', '.nuxt', '.svelte-kit', 'coverage',
24
+ '.vercel', '.netlify', '.turbo', '.cache', 'test-results', 'playwright-report', '.kryptheon',
25
+ ]);
26
+ const MAX_BYTES = 5 * 1024 * 1024;
27
+
28
+ // Where a file sits in the app. The first rule that matches wins, so the
29
+ // specific ones come first: a .sql file inside backend/ is a database change.
30
+ const PARTS = [
31
+ { part: 'dependencies', test: (p) => /(^|\/)(package\.json|package-lock\.json|yarn\.lock|pnpm-lock\.yaml|bun\.lockb?|requirements\.txt|poetry\.lock|Gemfile\.lock|go\.sum)$/i.test(p) },
32
+ { part: 'database', test: (p) => /\.sql$/i.test(p) || /(^|\/)(migrations|supabase|prisma|drizzle|db\/schema)(\/|$)/i.test(p) || /(^|\/)schema\.prisma$/i.test(p) },
33
+ { part: 'tests', test: (p) => /(^|\/)(tests?|__tests__|e2e|cypress)\//i.test(p) || /\.(spec|test)\.[cm]?[jt]sx?$/i.test(p) },
34
+ { part: 'docs', test: (p) => /\.(md|mdx|txt|rst)$/i.test(p) || /(^|\/)docs\//i.test(p) },
35
+ { part: 'config', test: (p) => /(^|\/)(\.env\.example|\.env\.sample|tsconfig[^/]*\.json|jsconfig\.json|vercel\.json|netlify\.toml|dockerfile|docker-compose[^/]*\.ya?ml)$/i.test(p) || /\.config\.[cm]?[jt]s$/i.test(p) || /(^|\/)\.github\//i.test(p) },
36
+ { part: 'backend', test: (p) => /(^|\/)(server|backend|api|routes|controllers|services|middleware|functions|workers|jobs|lib\/server)(\/|\.[cm]?[jt]s$)/i.test(p) || /(^|\/)(server|app|index)\.[cm]?[jt]s$/i.test(p) && !/(^|\/)(src|public|frontend|client|pages|components)\//i.test(p) },
37
+ { part: 'frontend', test: (p) => /\.(html?|css|scss|sass|less|jsx|tsx|vue|svelte|astro)$/i.test(p) || /(^|\/)(frontend|client|public|src|pages|components|app|views|static)\//i.test(p) && /\.[cm]?[jt]s$/i.test(p) },
38
+ { part: 'assets', test: (p) => /\.(png|jpe?g|gif|svg|webp|ico|woff2?|ttf|otf|mp4|webm|mp3|pdf)$/i.test(p) },
39
+ ];
40
+
41
+ // Which checks can speak to a change in each part. Ids are the dashboard's.
42
+ const CHECKS_FOR = {
43
+ frontend: ['frontend', 'regression'],
44
+ backend: ['backend', 'api', 'regression'],
45
+ database: ['data', 'regression'],
46
+ dependencies: ['dependency', 'build', 'regression'],
47
+ tests: ['regression'],
48
+ config: ['build', 'regression'],
49
+ code: ['regression'],
50
+ docs: [],
51
+ assets: [],
52
+ };
53
+
54
+ function partOf(rel) {
55
+ const p = rel.split(path.sep).join('/');
56
+ const hit = PARTS.find((r) => r.test(p));
57
+ if (hit) return hit.part;
58
+ return /\.[cm]?[jt]sx?$/i.test(p) ? 'code' : 'other';
59
+ }
60
+
61
+ /** Every file that is the project's own, as relative paths with forward slashes. */
62
+ function listFiles(root) {
63
+ const out = [];
64
+ (function walk(dir) {
65
+ let entries;
66
+ try {
67
+ entries = fs.readdirSync(dir, { withFileTypes: true });
68
+ } catch (err) {
69
+ return;
70
+ }
71
+ for (const entry of entries) {
72
+ const full = path.join(dir, entry.name);
73
+ if (entry.isDirectory()) {
74
+ if (!SKIP_DIRS.has(entry.name)) walk(full);
75
+ } else if (entry.isFile()) {
76
+ // A real .env holds secrets; its content is never hashed or kept.
77
+ if (/^\.env(\..+)?$/i.test(entry.name) && !/\.(example|sample)$/i.test(entry.name)) continue;
78
+ out.push(path.relative(root, full).split(path.sep).join('/'));
79
+ }
80
+ }
81
+ })(root);
82
+ return out.sort();
83
+ }
84
+
85
+ /** The dependency lists of package.json, kept so a change can say which package moved. */
86
+ function dependenciesOf(root) {
87
+ try {
88
+ const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'));
89
+ return Object.assign({}, pkg.dependencies || {}, pkg.devDependencies || {});
90
+ } catch (err) {
91
+ return null;
92
+ }
93
+ }
94
+
95
+ /**
96
+ * The project as it is now. Files whose size and modified time match the
97
+ * previous snapshot keep its hash without being read again - on a large
98
+ * project that is the difference between milliseconds and seconds, and an
99
+ * unchanged size and time is the same bytes for every editor that exists.
100
+ */
101
+ function takeSnapshot(root, previous) {
102
+ const before = (previous && previous.files) || {};
103
+ const files = {};
104
+ for (const rel of listFiles(root)) {
105
+ let stat;
106
+ try {
107
+ stat = fs.statSync(path.join(root, rel));
108
+ } catch (err) {
109
+ continue;
110
+ }
111
+ const old = before[rel];
112
+ if (old && old.size === stat.size && old.mtimeMs === stat.mtimeMs) {
113
+ files[rel] = old;
114
+ continue;
115
+ }
116
+ let hash;
117
+ if (stat.size > MAX_BYTES) {
118
+ hash = 'size:' + stat.size + ':' + stat.mtimeMs;
119
+ } else {
120
+ try {
121
+ hash = crypto.createHash('sha1').update(fs.readFileSync(path.join(root, rel))).digest('hex');
122
+ } catch (err) {
123
+ continue;
124
+ }
125
+ }
126
+ files[rel] = { hash: hash, size: stat.size, mtimeMs: stat.mtimeMs };
127
+ }
128
+ return { takenAt: new Date().toISOString(), files: files, dependencies: dependenciesOf(root) };
129
+ }
130
+
131
+ /** Packages added, removed or moved to another version between two snapshots. */
132
+ function dependencyChanges(before, after) {
133
+ const a = (before && before.dependencies) || {};
134
+ const b = (after && after.dependencies) || {};
135
+ const out = [];
136
+ for (const name of new Set(Object.keys(a).concat(Object.keys(b)))) {
137
+ if (!(name in a)) out.push({ name: name, change: 'added', to: b[name] });
138
+ else if (!(name in b)) out.push({ name: name, change: 'removed', from: a[name] });
139
+ else if (a[name] !== b[name]) out.push({ name: name, change: 'changed', from: a[name], to: b[name] });
140
+ }
141
+ return out.sort((x, y) => x.name.localeCompare(y.name));
142
+ }
143
+
144
+ /**
145
+ * What changed between two snapshots, by part of the app, and which checks
146
+ * that calls for. With no earlier snapshot there is nothing to compare, and
147
+ * that is said - it is not reported as "everything changed".
148
+ */
149
+ function compare(before, after) {
150
+ if (!before) return { first: true, files: [], parts: [], checks: [], dependencies: [] };
151
+ const files = [];
152
+ const a = before.files || {};
153
+ const b = after.files || {};
154
+ for (const rel of Object.keys(b)) {
155
+ if (!a[rel]) files.push({ path: rel, state: 'added' });
156
+ else if (a[rel].hash !== b[rel].hash) files.push({ path: rel, state: 'modified' });
157
+ }
158
+ for (const rel of Object.keys(a)) if (!b[rel]) files.push({ path: rel, state: 'deleted' });
159
+ files.sort((x, y) => x.path.localeCompare(y.path));
160
+ for (const f of files) f.part = partOf(f.path);
161
+
162
+ const parts = [];
163
+ for (const f of files) if (!parts.includes(f.part)) parts.push(f.part);
164
+ const checks = [];
165
+ for (const part of parts) for (const id of CHECKS_FOR[part] || []) if (!checks.includes(id)) checks.push(id);
166
+ return {
167
+ first: false,
168
+ files: files,
169
+ parts: parts,
170
+ checks: checks,
171
+ dependencies: dependencyChanges(before, after),
172
+ };
173
+ }
174
+
175
+ /**
176
+ * Findings that were not there last time. Keyed by what they are about rather
177
+ * than by line number: a line added above a known finding moves it, and a
178
+ * moved finding reported as new would teach a person to ignore this list.
179
+ */
180
+ //
181
+ // The detail is part of what a finding is only for the code read, where it is
182
+ // the expression itself. For a recorded flow or a database finding it is the
183
+ // account of how it broke this time - "ended up somewhere different", "could
184
+ // not be opened" - and a flow that is still broken in a new way is the same
185
+ // finding, not a fixed one and a new one.
186
+ function findingKey(f) {
187
+ return [
188
+ f.check || '',
189
+ f.where ? String(f.where).replace(/:\d+$/, '') : '',
190
+ f.headline ? String(f.headline).replace(/:\d+\b/g, '') : '',
191
+ f.check === 'frontend' ? f.detail || '' : '',
192
+ ].join('|');
193
+ }
194
+
195
+ /** A short, stable name for a finding: what a person types to re-check it. */
196
+ function findingId(f) {
197
+ return crypto.createHash('sha1').update(findingKey(f)).digest('hex').slice(0, 8);
198
+ }
199
+
200
+ function newFindings(before, after) {
201
+ const seen = new Set((before || []).map(findingKey));
202
+ return (after || []).filter((f) => !seen.has(findingKey(f)));
203
+ }
204
+
205
+ function goneFindings(before, after) {
206
+ const now = new Set((after || []).map(findingKey));
207
+ return (before || []).filter((f) => !now.has(findingKey(f)));
208
+ }
209
+
210
+ module.exports = {
211
+ PARTS: PARTS,
212
+ CHECKS_FOR: CHECKS_FOR,
213
+ partOf: partOf,
214
+ listFiles: listFiles,
215
+ takeSnapshot: takeSnapshot,
216
+ compare: compare,
217
+ dependencyChanges: dependencyChanges,
218
+ findingKey: findingKey,
219
+ findingId: findingId,
220
+ newFindings: newFindings,
221
+ goneFindings: goneFindings,
222
+ };
package/kryptheon-code.js CHANGED
@@ -83,6 +83,9 @@ function listFiles(root) {
83
83
  // Column types that can hold no tag: numbers, yes/no, ids, dates and times.
84
84
  const TAGLESS_TYPE = /^(smallint|integer|int|int2|int4|int8|bigint|smallserial|serial|bigserial|numeric|decimal|real|float4|float8|double precision|money|boolean|bool|uuid|date|time|timetz|timestamp|timestamptz|interval)\b/i;
85
85
  const NOT_A_COLUMN = /^(constraint|primary|foreign|unique|check|exclude|like)$/i;
86
+ // count(...), sum(...) or avg(...) - with a FILTER (...) and a cast or not - or any number cast, then AS name.
87
+ // min() and max() are left out: of a text column they are text.
88
+ const AGGREGATE_ALIAS = /(?:\b(?:count|sum|avg)\s*\((?:[^()]|\([^()]*\))*\)(?:\s*filter\s*\((?:[^()]|\([^()]*\))*\))?(?:\s*::\s*[a-z][\w ]*?)?|::\s*(?:int|int2|int4|int8|integer|bigint|smallint|numeric|decimal|real|float4|float8|double precision)\b)\s+as\s+"?([A-Za-z_][\w$]*)"?/i;
86
89
 
87
90
  /**
88
91
  * Column names the project's own SQL declares only with a type that cannot
@@ -112,7 +115,9 @@ function taglessColumns(root) {
112
115
  if (!entry.name.startsWith('.') && !SKIP_DIRS.has(entry.name)) walk(full, depth + 1);
113
116
  continue;
114
117
  }
115
- if (!/\.sql$/i.test(entry.name)) continue;
118
+ // and a server file that makes its own tables: CREATE TABLE in a string of server.js (round 25)
119
+ const script = /\.(c|m)?(js|ts)$/i.test(entry.name);
120
+ if (!/\.sql$/i.test(entry.name) && !script) continue;
116
121
  let sql;
117
122
  try {
118
123
  if (fs.statSync(full).size > 4 * MAX_BYTES) continue;
@@ -120,6 +125,7 @@ function taglessColumns(root) {
120
125
  } catch (err) {
121
126
  continue;
122
127
  }
128
+ if (script && !/create\s+table/i.test(sql) && !AGGREGATE_ALIAS.test(sql)) continue;
123
129
  read++;
124
130
  sql = sql.replace(/--[^\n]*/g, ' ').replace(/\/\*[\s\S]*?\*\//g, ' ');
125
131
  const add = (name, type) => {
@@ -150,6 +156,9 @@ function taglessColumns(root) {
150
156
  // ALTER TABLE ... ADD COLUMN name type
151
157
  const added = /add\s+column\s+(?:if\s+not\s+exists\s+)?("[^"]+"|[A-Za-z_][\w$]*)\s+([A-Za-z][\w ]*?)(?=\s*(\(|\[|not\b|null\b|default\b|references\b|unique\b|check\b|generated\b|;|,|$))/gi;
152
158
  while ((m = added.exec(sql)) !== null) add(m[1], m[2]);
159
+ // a name a query gives a count, a sum or a number cast: COUNT(*) FILTER (WHERE ...)::int AS on_road (round 26)
160
+ const agg = new RegExp(AGGREGATE_ALIAS.source, 'gi');
161
+ while ((m = agg.exec(sql)) !== null) add(m[1], 'integer');
153
162
  }
154
163
  })(root, 0);
155
164
  const out = new Set();
@@ -302,6 +311,8 @@ function classify(node, scope, code, depth, aliases) {
302
311
  if (MAKES_SAFE.test(name)) return 'safe';
303
312
  if (readsResponseBody(node)) return 'network';
304
313
  if (NUMERIC_CALLS.test(name)) return 'safe';
314
+ // String(x): as dangerous as x, no more (round 26 harness: String(value) of a date column was "unknown")
315
+ if (name === 'String' && node.callee.type === 'Identifier' && node.arguments.length === 1) return again(node.arguments[0]);
305
316
  // new Intl.NumberFormat(...).format(x) and its date and time kin: digits,
306
317
  // signs and the locale's own words - never a tag.
307
318
  if (name === 'format' && node.callee.type === 'MemberExpression' && node.callee.object.type === 'NewExpression' &&
@@ -349,6 +360,13 @@ function classify(node, scope, code, depth, aliases) {
349
360
  const inner = argumentAliases(local.node, node.arguments, scope, local.scope, code, depth, aliases);
350
361
  if (returns.length) return worst(returns.map((r) => classify(r.node, r.scope, code, depth + 1, inner)));
351
362
  }
363
+ // ({ inventory: renderInventory, activity: renderActivity })[view](): as
364
+ // dangerous as the worst of the functions it can pick (blind round 25).
365
+ const picked = pickedFunctions(node, scope);
366
+ if (picked) {
367
+ const returns = picked.flatMap((fn) => returnsOf(fn).map((r) => ({ r: r, fn: fn })));
368
+ if (returns.length) return worst(returns.map((x) => classify(x.r.node, x.r.scope, code, depth + 1, argumentAliases(x.fn.node, node.arguments, scope, x.fn.scope, code, depth, aliases))));
369
+ }
352
370
  // String methods pass through whatever they were called on, and so do
353
371
  // the array methods that only pick or reorder items: notes.filter(...)
354
372
  // holds the same notes.
@@ -370,6 +388,10 @@ function classify(node, scope, code, depth, aliases) {
370
388
  // adds nothing that was not already there. textContent is not this - it
371
389
  // is text, and a "<" in it becomes a tag when written as HTML.
372
390
  if (prop === 'innerHTML' || prop === 'outerHTML') return 'safe';
391
+ // ({ delivered: 'green', failed: 'red' }[status]): whatever the key, one of the object's own literals (or
392
+ // undefined) - never a tag. Found on blind round 26: a status colour class was reported in 7 places.
393
+ if (node.computed && node.object.type === 'ObjectExpression' && node.object.properties.length &&
394
+ node.object.properties.every((p) => p.type === 'ObjectProperty' && /^(StringLiteral|NumericLiteral|BooleanLiteral|NullLiteral)$/.test(p.value.type))) return 'safe';
373
395
  if (NUMERIC_PROPS.test(prop || '') || NUMERIC_NAMES.test(prop || '') || TAGLESS_COLUMNS.has(prop || '')) return 'safe';
374
396
  // The value of one of the page's own <select>s: one of its options.
375
397
  if (prop === 'value' && SELECT_IDS.has(pickedId(node.object, scope) || '')) return 'safe';
@@ -443,6 +465,23 @@ function localFunction(name, scope) {
443
465
  return null;
444
466
  }
445
467
 
468
+ /**
469
+ * The functions of this file's own that a call picks by name from an object
470
+ * written out right there - ({ a: renderA, b: renderB })[view]() - or null.
471
+ * Null too when any of them is not a function this file declares.
472
+ */
473
+ function pickedFunctions(call, scope) {
474
+ const callee = call.callee;
475
+ if (!callee || !/MemberExpression$/.test(callee.type) || !callee.computed || callee.object.type !== 'ObjectExpression') return null;
476
+ const fns = [];
477
+ for (const prop of callee.object.properties) {
478
+ const fn = prop.type === 'ObjectProperty' && prop.value.type === 'Identifier' ? localFunction(prop.value.name, scope) : null;
479
+ if (!fn) return null;
480
+ fns.push(fn);
481
+ }
482
+ return fns.length ? fns : null;
483
+ }
484
+
446
485
  /** Every value a function returns, with the scope to judge it in; nested functions excluded. */
447
486
  function returnsOf(fnPath) {
448
487
  if (fnPath.node.body.type !== 'BlockStatement') return [{ node: fnPath.node.body, scope: fnPath.scope }];
@@ -636,12 +675,33 @@ function riskyParts(node, scope, code, aliases, out, depth) {
636
675
  }
637
676
  if (node.type === 'ConditionalExpression') { go(node.consequent); go(node.alternate); return; }
638
677
  if (node.type === 'LogicalExpression') { go(node.left); go(node.right); return; }
678
+ if (node.type === 'AwaitExpression') { go(node.argument); return; }
639
679
  if (/CallExpression$/.test(node.type)) {
640
680
  const mapped = mapCallback(node, scope, code, depth, aliases);
641
681
  if (mapped && mapped.returns.length) {
642
682
  mapped.returns.forEach((r) => go(r.node, r.scope, mapped.inner));
643
683
  return;
644
684
  }
685
+ // a function of this file's own, or one picked from an object of them: the
686
+ // parts are inside what it returns (round 25 named only "html")
687
+ const own = node.callee.type === 'Identifier' ? localFunction(node.callee.name, scope) : null;
688
+ const fns = own ? [own] : pickedFunctions(node, scope);
689
+ const returns = (fns || []).flatMap((fn) => returnsOf(fn).map((r) => ({ r: r, fn: fn })));
690
+ if (returns.length) {
691
+ returns.forEach((x) => go(x.r.node, x.r.scope, argumentAliases(x.fn.node, node.arguments, scope, x.fn.scope, code, depth, aliases)));
692
+ return;
693
+ }
694
+ }
695
+ // const html = await render(): the parts are in what it was set to
696
+ if (node.type === 'Identifier' && !(aliases && Object.prototype.hasOwnProperty.call(aliases, node.name))) {
697
+ const binding = scope && scope.getBinding(node.name);
698
+ const decl = binding && binding.path && binding.path.node;
699
+ if (decl && decl.type === 'VariableDeclarator' && decl.id.type === 'Identifier' && decl.init && !(binding.constantViolations && binding.constantViolations.length) &&
700
+ (/CallExpression$/.test(decl.init.type) || decl.init.type === 'AwaitExpression')) {
701
+ const before = out.length;
702
+ go(decl.init, binding.path.scope);
703
+ if (out.length > before) return;
704
+ }
645
705
  }
646
706
  const text = code.slice(node.start, node.end).replace(/\s+/g, ' ').slice(0, 80);
647
707
  if (!out.some((p) => p.text === text)) out.push({ text: text, origin: origin, line: node.loc ? node.loc.start.line : null });
@@ -629,9 +629,13 @@ function buildState(root, env) {
629
629
  }
630
630
 
631
631
  // The backend map: what it points at, each one to check - a reading of the
632
- // code, so never "confirmed".
632
+ // code, so never "confirmed". One the running app strongly held is not a
633
+ // thing to fix any more: it is kept apart, with what was tried (round 26).
634
+ const checkedHeld = [];
633
635
  if (backendMap && config.enabled.backend) {
634
- for (const t of backendMap.toCheck || []) {
636
+ const { shown, held } = require('./kryptheon-api.js').reconcile(backendMap.toCheck, apiLast && apiLast.verdicts);
637
+ for (const t of held) checkedHeld.push({ where: t.where, route: t.route, kind: t.kind, headline: t.headline, verdict: t.verdict.verdict, when: apiLast.checkedAt });
638
+ for (const t of shown) {
635
639
  findings.push({
636
640
  check: 'backend',
637
641
  severity: t.severity,
@@ -910,6 +914,8 @@ function buildState(root, env) {
910
914
  checks: checks,
911
915
  notChecked: NOT_CHECKED,
912
916
  findings: findings,
917
+ // what the map pointed at and the running app strongly held: checked, not a thing to fix
918
+ checkedHeld: checkedHeld,
913
919
  allFixPrompt: combinedPrompt(forAll),
914
920
  runs: history.slice().reverse().map((r) => ({ runAt: r.runAt, status: r.status, passed: r.passed, failed: r.failed, durationMs: r.durationMs, skipped: (r.skipped || []).slice(0, 20) })),
915
921
  recordings: recordings,