@nac3/runtime 2.5.0-alpha.5 → 2.5.0
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/CHANGELOG.md +1017 -887
- package/README.md +145 -145
- package/SPEC.md +1761 -1442
- package/contract-head.json +16 -0
- package/dist/cli-levels.json +214 -0
- package/dist/cli-url.mjs +751 -0
- package/dist/cli.js +232 -38
- package/dist/nac-chat-client.d.ts +5 -75
- package/dist/nac-v2-extensions.d.ts +71 -68
- package/dist/nac.browser.js +118 -31
- package/dist/nac.cjs +118 -31
- package/dist/nac.d.ts +140 -34
- package/dist/nac.mjs +118 -31
- package/package.json +19 -19
package/dist/cli-url.mjs
ADDED
|
@@ -0,0 +1,751 @@
|
|
|
1
|
+
/* =====================================================================
|
|
2
|
+
cli-url.mjs -- URL mode of the NAC CLI validator, and the only place
|
|
3
|
+
that decides a conformance level.
|
|
4
|
+
|
|
5
|
+
Audit N-18, second half. Before 2.5.0 `nac validate <url>` aborted
|
|
6
|
+
with exit 2 and a message saying URL validation required Playwright,
|
|
7
|
+
and NO mode of the CLI ever printed a conformance level -- while
|
|
8
|
+
SPEC.md section 8 says, in those words, that the package's CLI
|
|
9
|
+
"reports the highest level a page reaches". A spec that promises an
|
|
10
|
+
instrument nobody can run is worse than one that promises nothing:
|
|
11
|
+
the reader believes the claim is checkable.
|
|
12
|
+
|
|
13
|
+
THE LEVEL DEFINITIONS ARE NOT IN THIS FILE. They are read from
|
|
14
|
+
dist/cli-levels.json, which runtime/build.mjs generates out of
|
|
15
|
+
SPEC.md section 8 through scripts/spec-levels.mjs -- the same module
|
|
16
|
+
that produces the published api/v/<version>/levels.json. Two copies
|
|
17
|
+
of section 8 would let this validator certify pages against a spec
|
|
18
|
+
nobody is reading. What lives here is the DECIDERS: one function per
|
|
19
|
+
requirement, registered under the name the contract publishes for
|
|
20
|
+
it, so a report can be audited against the check that produced it.
|
|
21
|
+
|
|
22
|
+
Three verdicts, and the difference between the last two is the whole
|
|
23
|
+
point:
|
|
24
|
+
pass measured, and it holds.
|
|
25
|
+
fail measured, and it does not hold.
|
|
26
|
+
unverified NOT measured. Never counted as a pass.
|
|
27
|
+
|
|
28
|
+
ASCII only. No dependency beyond node core, except Playwright, which
|
|
29
|
+
is resolved at call time and is optional.
|
|
30
|
+
===================================================================== */
|
|
31
|
+
|
|
32
|
+
import { readFileSync } from 'node:fs';
|
|
33
|
+
import { dirname, join } from 'node:path';
|
|
34
|
+
import { fileURLToPath } from 'node:url';
|
|
35
|
+
|
|
36
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
37
|
+
|
|
38
|
+
export function loadLevels() {
|
|
39
|
+
return JSON.parse(readFileSync(join(HERE, 'cli-levels.json'), 'utf8'));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class PlaywrightMissing extends Error {}
|
|
43
|
+
|
|
44
|
+
/* Playwright is OPTIONAL, resolved at call time, and never a
|
|
45
|
+
dependency of this package: a runtime that pulled a browser into
|
|
46
|
+
every adopter's node_modules to offer a validator would be charging
|
|
47
|
+
every user for a tool most of them never run. The three names are
|
|
48
|
+
the three ways it is actually installed in the wild. */
|
|
49
|
+
async function resolveChromium() {
|
|
50
|
+
const tried = [];
|
|
51
|
+
for (const name of ['playwright', 'playwright-core', '@playwright/test']) {
|
|
52
|
+
try {
|
|
53
|
+
const mod = await import(name);
|
|
54
|
+
const chromium = mod.chromium || (mod.default && mod.default.chromium);
|
|
55
|
+
if (chromium) return { chromium, from: name };
|
|
56
|
+
tried.push(name + ' (resolved, exports no chromium)');
|
|
57
|
+
} catch (e) {
|
|
58
|
+
tried.push(name + ' (not installed)');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const err = new PlaywrightMissing('cannot resolve a Playwright chromium launcher');
|
|
62
|
+
err.tried = tried;
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/* ---- observation -----------------------------------------------------
|
|
67
|
+
One evaluate, one snapshot. Asking the page five separate questions
|
|
68
|
+
invites five different answers, because a page that is still
|
|
69
|
+
registering manifests changes between calls -- and a validator whose
|
|
70
|
+
findings depend on when it looked is not measuring the page. */
|
|
71
|
+
|
|
72
|
+
function pageObservation() {
|
|
73
|
+
/* Runs INSIDE the page. Must be self-contained: no closures over
|
|
74
|
+
anything in this file. */
|
|
75
|
+
const N = window.NAC;
|
|
76
|
+
if (!N) return { nac: false };
|
|
77
|
+
|
|
78
|
+
function labelMaps(m) {
|
|
79
|
+
/* Every user-facing label the manifest declares, canonical shape
|
|
80
|
+
(elements[]) and legacy groups alike, including the per-action
|
|
81
|
+
labels nested under an element. */
|
|
82
|
+
const out = [];
|
|
83
|
+
function take(owner, where) {
|
|
84
|
+
if (owner && owner.label_i18n && typeof owner.label_i18n === 'object') {
|
|
85
|
+
out.push({ where: where, locales: Object.keys(owner.label_i18n) });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const els = Array.isArray(m.elements) ? m.elements : [];
|
|
89
|
+
els.forEach(function (e) {
|
|
90
|
+
take(e, String(e && e.id));
|
|
91
|
+
const acts = Array.isArray(e && e.actions) ? e.actions : [];
|
|
92
|
+
acts.forEach(function (a, i) {
|
|
93
|
+
take(a, String(e && e.id) + '.actions[' + i + ']');
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
['tabs', 'fields', 'actions', 'kpis', 'charts'].forEach(function (g) {
|
|
97
|
+
const arr = Array.isArray(m[g]) ? m[g] : [];
|
|
98
|
+
arr.forEach(function (x, i) {
|
|
99
|
+
take(x, g + '[' + i + ']');
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const manifests = (N.manifest() || []).filter(Boolean).map(function (m) {
|
|
106
|
+
const groupLen = function (g) { return Array.isArray(m[g]) ? m[g].length : null; };
|
|
107
|
+
return {
|
|
108
|
+
plugin_slug: m.plugin_slug || null,
|
|
109
|
+
element_count: Array.isArray(m.elements) ? m.elements.length : null,
|
|
110
|
+
groups: { tabs: groupLen('tabs'), fields: groupLen('fields'), actions: groupLen('actions') },
|
|
111
|
+
/* "Carries HMAC-signed manifests": the only thing a validator
|
|
112
|
+
without the tenant secret can see is whether a provenance
|
|
113
|
+
block is there at all. */
|
|
114
|
+
provenance_block: !!(m.provenance || m.signature
|
|
115
|
+
|| (m.source && m.source.signature)
|
|
116
|
+
|| (m.provenance_source && m.provenance_source.signature)),
|
|
117
|
+
labels: labelMaps(m),
|
|
118
|
+
};
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const described = N.describe();
|
|
122
|
+
|
|
123
|
+
/* ADVERTISED means declared by the contract, not merely present in
|
|
124
|
+
the DOM: describe() can only report what it finds mounted, so
|
|
125
|
+
reading the advertised set off describe() would make "every
|
|
126
|
+
advertised id resolves" true by construction -- a comparison that
|
|
127
|
+
cannot fail. So the advertised set comes from the MANIFESTS and
|
|
128
|
+
resolution is checked against the DOM. */
|
|
129
|
+
const advertised = [];
|
|
130
|
+
const seenId = Object.create(null);
|
|
131
|
+
(N.manifest() || []).filter(Boolean).forEach(function (m) {
|
|
132
|
+
const slug = m.plugin_slug || null;
|
|
133
|
+
function push(id, role) {
|
|
134
|
+
if (!id) return;
|
|
135
|
+
id = String(id);
|
|
136
|
+
if (seenId[id]) return;
|
|
137
|
+
seenId[id] = true;
|
|
138
|
+
advertised.push({ nac_id: id, role: role || null, plugin: slug });
|
|
139
|
+
}
|
|
140
|
+
(Array.isArray(m.elements) ? m.elements : []).forEach(function (e) {
|
|
141
|
+
push(e && e.id, e && e.role);
|
|
142
|
+
});
|
|
143
|
+
[['tabs', 'tab'], ['fields', 'field'], ['actions', 'action'],
|
|
144
|
+
['kpis', null], ['charts', null]].forEach(function (pair) {
|
|
145
|
+
(Array.isArray(m[pair[0]]) ? m[pair[0]] : []).forEach(function (x) {
|
|
146
|
+
push(x && (x.nac_id || x.id), (x && x.role) || pair[1]);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const domIds = [];
|
|
152
|
+
Array.prototype.forEach.call(document.querySelectorAll('[data-nac-id]'), function (el) {
|
|
153
|
+
const id = el.getAttribute('data-nac-id');
|
|
154
|
+
if (id) domIds.push(id);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const g = N.validate_global();
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
nac: true,
|
|
161
|
+
version: N.version || null,
|
|
162
|
+
spec_version: N.spec_version || null,
|
|
163
|
+
conformance_tier: N.conformance_tier || null,
|
|
164
|
+
strict_versioning: !!N.STRICT_VERSIONING,
|
|
165
|
+
url: location.href,
|
|
166
|
+
tree_version: described.tree_version || null,
|
|
167
|
+
plugin_count: g.plugin_count,
|
|
168
|
+
total_ids: g.total_ids,
|
|
169
|
+
dom_ids: domIds.length,
|
|
170
|
+
dom_id_list: domIds,
|
|
171
|
+
advertised: advertised,
|
|
172
|
+
manifests: manifests,
|
|
173
|
+
/* The buckets, flattened with their bucket name kept, because the
|
|
174
|
+
severity threshold is what two different level requirements
|
|
175
|
+
disagree about. `tolerated` is reported and NOT counted: the
|
|
176
|
+
host registered it as tracked debt, which is a different claim
|
|
177
|
+
than "clean". */
|
|
178
|
+
findings: ['duplicates', 'orphans', 'unmounted', 'convention_violations',
|
|
179
|
+
'plugin_instance_violations'].reduce(function (acc, b) {
|
|
180
|
+
(g[b] || []).forEach(function (f) {
|
|
181
|
+
/* severity 'unknown' rather than a guess: a runtime older than
|
|
182
|
+
2.5.0 attaches neither code nor severity to any bucket item
|
|
183
|
+
(audit N-14), and defaulting those to 'error' would make the
|
|
184
|
+
validator invent a severity the page never stated. Unknown
|
|
185
|
+
counts as failing -- fail closed -- and the report says why
|
|
186
|
+
instead of printing a number it made up. */
|
|
187
|
+
acc.push({
|
|
188
|
+
bucket: b,
|
|
189
|
+
code: f.code || null,
|
|
190
|
+
severity: f.severity || 'unknown',
|
|
191
|
+
nac_id: f.nac_id || null,
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
return acc;
|
|
195
|
+
}, []),
|
|
196
|
+
tolerated_count: (g.tolerated || []).length,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/* The page may register its manifests inside a readiness callback, so
|
|
201
|
+
the honest moment to look is when the registry stops moving -- not
|
|
202
|
+
after a fixed sleep, which measures the machine's mood. */
|
|
203
|
+
async function settle(page, { settleMs, timeoutMs }) {
|
|
204
|
+
const deadline = Date.now() + timeoutMs;
|
|
205
|
+
let last = -1;
|
|
206
|
+
let stableSince = 0;
|
|
207
|
+
for (;;) {
|
|
208
|
+
const n = await page.evaluate(() => {
|
|
209
|
+
const N = window.NAC;
|
|
210
|
+
if (!N) return -1;
|
|
211
|
+
try { return N.list_registered_plugins().length; } catch (_) { return -1; }
|
|
212
|
+
});
|
|
213
|
+
if (n >= 0 && n === last) {
|
|
214
|
+
if (!stableSince) stableSince = Date.now();
|
|
215
|
+
if (Date.now() - stableSince >= settleMs) return { plugins: n, settled: true };
|
|
216
|
+
} else {
|
|
217
|
+
stableSince = 0;
|
|
218
|
+
last = n;
|
|
219
|
+
}
|
|
220
|
+
if (Date.now() > deadline) return { plugins: last, settled: false };
|
|
221
|
+
await new Promise((r) => setTimeout(r, 60));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/* ---- driving ---------------------------------------------------------
|
|
226
|
+
Two of the four level-1 requirements are behavioural: they are about
|
|
227
|
+
what the page DOES when an agent clicks, and no amount of reading
|
|
228
|
+
decides them. So the validator can click -- but only when asked,
|
|
229
|
+
because clicking every advertised control on somebody's production
|
|
230
|
+
page is not a read-only operation and must never be the default. */
|
|
231
|
+
|
|
232
|
+
async function driveOne(page, id, { perActionMs }) {
|
|
233
|
+
try {
|
|
234
|
+
return await driveOneInPage(page, id, { perActionMs });
|
|
235
|
+
} catch (e) {
|
|
236
|
+
const msg = String((e && e.message) || e);
|
|
237
|
+
/* A click that navigates away tears down the context the evaluate
|
|
238
|
+
was running in. That is not a failed action -- navigating IS the
|
|
239
|
+
side effect of a link -- but the completion event becomes
|
|
240
|
+
unobservable, and the difference between "did not fire" and
|
|
241
|
+
"could not be watched" is the difference between a verdict and a
|
|
242
|
+
guess. So it is recorded as its own outcome and the caller
|
|
243
|
+
restores the page before the next id. */
|
|
244
|
+
if (/Execution context was destroyed|navigation|detached/i.test(msg)) {
|
|
245
|
+
return { id, resolved: null, click_error: null, succeeded: false, failed: null, navigated: true };
|
|
246
|
+
}
|
|
247
|
+
return { id, resolved: null, click_error: msg.split('\n')[0], succeeded: false, failed: null };
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async function driveOneInPage(page, id, { perActionMs }) {
|
|
252
|
+
return page.evaluate(async (payload) => {
|
|
253
|
+
const N = window.NAC;
|
|
254
|
+
const id = payload.id;
|
|
255
|
+
const seen = { succeeded: false, failed: null };
|
|
256
|
+
const onOk = function (e) {
|
|
257
|
+
const d = (e && e.detail) || {};
|
|
258
|
+
if (d.action_id === id || d.nac_id === id) seen.succeeded = true;
|
|
259
|
+
};
|
|
260
|
+
const onBad = function (e) {
|
|
261
|
+
const d = (e && e.detail) || {};
|
|
262
|
+
if (d.action_id === id || d.nac_id === id) seen.failed = d.error || d.code || 'failed';
|
|
263
|
+
};
|
|
264
|
+
document.addEventListener('nac:action:succeeded', onOk);
|
|
265
|
+
document.addEventListener('nac:action:failed', onBad);
|
|
266
|
+
|
|
267
|
+
const resolved = !!N.find(id);
|
|
268
|
+
/* Is this action's side effect a NAVIGATION? It changes what a
|
|
269
|
+
timeout means. A plain anchor cannot emit a completion event
|
|
270
|
+
after its own unload, so "no event" says nothing about the page
|
|
271
|
+
-- while for a scripted button it says everything. Recorded
|
|
272
|
+
here, decided later, because the validator must not collapse two
|
|
273
|
+
different facts into one verdict. */
|
|
274
|
+
const el = document.querySelector('[data-nac-id="' + id + '"]');
|
|
275
|
+
const nav_link = !!(el && el.tagName === 'A' && el.getAttribute('href')
|
|
276
|
+
&& !/^#/.test(el.getAttribute('href')));
|
|
277
|
+
let click_error = null;
|
|
278
|
+
try {
|
|
279
|
+
/* STRICT_VERSIONING defaults to true in 2.5, so a click with no
|
|
280
|
+
expected_* is refused with version_required. Passing the tree
|
|
281
|
+
version the page just reported is the conformant way to ask,
|
|
282
|
+
and it exercises the versioned tier while it is at it. */
|
|
283
|
+
const tv = N.describe().tree_version;
|
|
284
|
+
await N.click(id, tv ? { expected_tree_version: tv } : undefined);
|
|
285
|
+
} catch (e) {
|
|
286
|
+
click_error = (e && (e.code || e.message)) || 'error';
|
|
287
|
+
}
|
|
288
|
+
await new Promise((r) => setTimeout(r, payload.perActionMs));
|
|
289
|
+
document.removeEventListener('nac:action:succeeded', onOk);
|
|
290
|
+
document.removeEventListener('nac:action:failed', onBad);
|
|
291
|
+
return {
|
|
292
|
+
id: id, resolved: resolved, nav_link: nav_link, click_error: click_error,
|
|
293
|
+
succeeded: seen.succeeded, failed: seen.failed,
|
|
294
|
+
};
|
|
295
|
+
}, { id, perActionMs });
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/* ---- deciders --------------------------------------------------------
|
|
299
|
+
One per published `decider` name. The name is the contract: the
|
|
300
|
+
build refuses to ship a CLI whose table does not match the names the
|
|
301
|
+
sidecar reviewed against each sentence, so a report always names a
|
|
302
|
+
check that exists. */
|
|
303
|
+
|
|
304
|
+
function pass(detail) { return { verdict: 'pass', detail }; }
|
|
305
|
+
function failV(detail) { return { verdict: 'fail', detail }; }
|
|
306
|
+
function unverified(detail) { return { verdict: 'unverified', detail }; }
|
|
307
|
+
|
|
308
|
+
const DECIDERS = {
|
|
309
|
+
manifest_registered(ctx) {
|
|
310
|
+
const n = ctx.obs.plugin_count;
|
|
311
|
+
return n >= 1
|
|
312
|
+
? pass(n + ' manifest(s) registered')
|
|
313
|
+
: failV('no manifest is registered (finding code no_manifest)');
|
|
314
|
+
},
|
|
315
|
+
|
|
316
|
+
manifest_declares_groups(ctx) {
|
|
317
|
+
/* Read LITERALLY: the sentence names tabs[], fields[] and
|
|
318
|
+
actions[]. That is the LEGACY manifest shape; section 4
|
|
319
|
+
documents elements[] as the canonical one, so a page written the
|
|
320
|
+
documented way fails this as worded. The validator does not
|
|
321
|
+
rewrite the requirement to what it probably meant -- that is
|
|
322
|
+
what the hash-keyed review in the sidecar is for, and the gap is
|
|
323
|
+
audit N-37. */
|
|
324
|
+
const missing = [];
|
|
325
|
+
for (const m of ctx.obs.manifests) {
|
|
326
|
+
const gaps = ['tabs', 'fields', 'actions'].filter((g) => !(m.groups[g] > 0));
|
|
327
|
+
if (gaps.length) missing.push((m.plugin_slug || '(unnamed)') + ' lacks ' + gaps.join('/'));
|
|
328
|
+
}
|
|
329
|
+
if (!ctx.obs.manifests.length) return failV('no manifest to read');
|
|
330
|
+
return missing.length === 0
|
|
331
|
+
? pass('every manifest declares tabs[], fields[] and actions[]')
|
|
332
|
+
: failV(missing.length + ' of ' + ctx.obs.manifests.length + ' manifest(s) do not declare the three arrays: '
|
|
333
|
+
+ missing.slice(0, 4).join('; ') + (missing.length > 4 ? '; ...' : '')
|
|
334
|
+
+ ' -- note: these manifests use the canonical elements[] shape instead (audit N-37)');
|
|
335
|
+
},
|
|
336
|
+
|
|
337
|
+
label_i18n_covers_locales(ctx) {
|
|
338
|
+
const required = ctx.levels.locales_required;
|
|
339
|
+
let total = 0;
|
|
340
|
+
const offenders = [];
|
|
341
|
+
for (const m of ctx.obs.manifests) {
|
|
342
|
+
for (const l of m.labels) {
|
|
343
|
+
total += 1;
|
|
344
|
+
const miss = required.filter((loc) => l.locales.indexOf(loc) < 0);
|
|
345
|
+
if (miss.length) offenders.push(l.where + ' misses ' + miss.join(','));
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (total === 0) return failV('no label_i18n is declared anywhere in the registered manifests');
|
|
349
|
+
return offenders.length === 0
|
|
350
|
+
? pass(total + ' label(s), all covering the ' + required.length + ' required locales')
|
|
351
|
+
: failV(offenders.length + ' of ' + total + ' label(s) do not cover all ' + required.length
|
|
352
|
+
+ ' locales: ' + offenders.slice(0, 3).join('; ') + (offenders.length > 3 ? '; ...' : ''));
|
|
353
|
+
},
|
|
354
|
+
|
|
355
|
+
validate_global_no_errors(ctx) {
|
|
356
|
+
/* 'unknown' counts against the page: a finding whose severity the
|
|
357
|
+
page never stated cannot be assumed harmless. The caveat names
|
|
358
|
+
why, so nobody reads a pre-2.5.0 runtime as a defective page. */
|
|
359
|
+
const errs = ctx.obs.findings.filter((f) => f.severity === 'error' || f.severity === 'unknown');
|
|
360
|
+
const unknown = ctx.obs.findings.filter((f) => f.severity === 'unknown').length;
|
|
361
|
+
const caveat = unknown
|
|
362
|
+
? ' -- ' + unknown + ' of them carry no severity at all, because the runtime on this page '
|
|
363
|
+
+ 'predates coded findings (audit N-14); counted as failing rather than assumed harmless'
|
|
364
|
+
: '';
|
|
365
|
+
return errs.length === 0
|
|
366
|
+
? pass('validate_global reports 0 error-severity findings'
|
|
367
|
+
+ (ctx.obs.findings.length ? ' (' + ctx.obs.findings.length + ' of lower severity)' : ''))
|
|
368
|
+
: failV(errs.length + ' error-severity finding(s): '
|
|
369
|
+
+ errs.slice(0, 4).map((f) => (f.code || f.bucket) + (f.nac_id ? '(' + f.nac_id + ')' : '')).join(', ')
|
|
370
|
+
+ (errs.length > 4 ? ', ...' : '') + caveat);
|
|
371
|
+
},
|
|
372
|
+
|
|
373
|
+
validate_global_no_findings(ctx) {
|
|
374
|
+
const n = ctx.obs.findings.length;
|
|
375
|
+
/* The sentence asks for {probe: true}. validate_global() takes no
|
|
376
|
+
arguments and never reads `probe`, although SPEC.md 10.1
|
|
377
|
+
publishes the option -- audit N-35. So this is the level-2 check
|
|
378
|
+
with a stricter threshold, not the deeper probing the sentence
|
|
379
|
+
implies, and the report says so rather than quietly passing. */
|
|
380
|
+
const caveat = ' -- note: the runtime ignores the {probe: true} this requirement asks for (audit N-35), so this is the same call with a stricter threshold';
|
|
381
|
+
return n === 0
|
|
382
|
+
? pass('validate_global reports 0 findings of any severity' + caveat)
|
|
383
|
+
: failV(n + ' finding(s) of some severity' + caveat);
|
|
384
|
+
},
|
|
385
|
+
|
|
386
|
+
manifest_hmac_block_present(ctx) {
|
|
387
|
+
const withBlock = ctx.obs.manifests.filter((m) => m.provenance_block);
|
|
388
|
+
if (!ctx.obs.manifests.length) return failV('no manifest to read');
|
|
389
|
+
if (withBlock.length === 0) {
|
|
390
|
+
return failV('no registered manifest carries a provenance block, so none can be HMAC-signed');
|
|
391
|
+
}
|
|
392
|
+
/* Present, and that is as far as any tool gets. The validator has
|
|
393
|
+
no tenant secret, and the runtime exposes no manifest
|
|
394
|
+
verification to ask -- sign_provenance / verify_provenance cover
|
|
395
|
+
an event's detail.source, never a manifest (audit N-36). */
|
|
396
|
+
return unverified(withBlock.length + ' of ' + ctx.obs.manifests.length
|
|
397
|
+
+ ' manifest(s) carry a provenance block, and no tool can confirm it is authentic: '
|
|
398
|
+
+ 'the validator holds no tenant secret and the runtime exposes no manifest verification (audit N-36)');
|
|
399
|
+
},
|
|
400
|
+
|
|
401
|
+
action_completion_events(ctx) {
|
|
402
|
+
if (!ctx.drive) {
|
|
403
|
+
return unverified('behavioural: needs --drive. ' + ctx.actionIds.length
|
|
404
|
+
+ ' action element(s) advertised and none were clicked, so whether they fire nac:action:succeeded is unmeasured');
|
|
405
|
+
}
|
|
406
|
+
const driven = ctx.driveResults.filter((r) => ctx.actionIds.indexOf(r.id) >= 0);
|
|
407
|
+
if (!driven.length) return failV('no data-nac-role="action" element is advertised, so the page fires nothing');
|
|
408
|
+
const navigated = driven.filter((r) => r.navigated);
|
|
409
|
+
/* A navigating anchor that emits nothing is NOT a measured failure.
|
|
410
|
+
Its side effect is leaving the page, and nothing can emit a
|
|
411
|
+
completion event after its own unload -- so the requirement as
|
|
412
|
+
written has no outcome a tool can record for it either way. The
|
|
413
|
+
validator says that, instead of counting it as a defect of the
|
|
414
|
+
page or quietly as a pass. Audit N-38. */
|
|
415
|
+
const navSilent = driven.filter((r) => !r.succeeded && !r.navigated && r.nav_link);
|
|
416
|
+
const silent = driven.filter((r) => !r.succeeded && !r.navigated && !r.nav_link);
|
|
417
|
+
if (silent.length) {
|
|
418
|
+
return failV(silent.length + ' of ' + driven.length + ' scripted action(s) did not fire nac:action:succeeded: '
|
|
419
|
+
+ silent.slice(0, 4).map((r) => r.id).join(', ') + (silent.length > 4 ? ', ...' : ''));
|
|
420
|
+
}
|
|
421
|
+
if (navSilent.length) {
|
|
422
|
+
return unverified((driven.length - navSilent.length - navigated.length) + ' of ' + driven.length
|
|
423
|
+
+ ' action(s) fired nac:action:succeeded. The other ' + navSilent.length
|
|
424
|
+
+ ' are plain links whose side effect is a navigation, and no element can emit a completion'
|
|
425
|
+
+ ' event after its own unload, so this requirement has no outcome a tool can record for them'
|
|
426
|
+
+ ' (audit N-38): ' + navSilent.slice(0, 4).map((r) => r.id).join(', ')
|
|
427
|
+
+ (navSilent.length > 4 ? ', ...' : ''));
|
|
428
|
+
}
|
|
429
|
+
if (navigated.length) {
|
|
430
|
+
/* Not a pass. The event may well have fired; the page went away
|
|
431
|
+
before anything could watch it, and "probably fired" is not a
|
|
432
|
+
measurement. */
|
|
433
|
+
return unverified((driven.length - navigated.length) + ' of ' + driven.length
|
|
434
|
+
+ ' action(s) fired nac:action:succeeded, and ' + navigated.length
|
|
435
|
+
+ ' navigated the page away before the event could be observed: '
|
|
436
|
+
+ navigated.slice(0, 4).map((r) => r.id).join(', ') + (navigated.length > 4 ? ', ...' : ''));
|
|
437
|
+
}
|
|
438
|
+
return pass('all ' + driven.length + ' advertised action(s) fired nac:action:succeeded');
|
|
439
|
+
},
|
|
440
|
+
|
|
441
|
+
click_resolves_and_lands(ctx) {
|
|
442
|
+
/* Resolution is decidable by reading; that a click LANDS is not.
|
|
443
|
+
So the unresolved half is reported even without --drive, because
|
|
444
|
+
an advertised id that does not resolve is already a definite
|
|
445
|
+
no -- a validator that stayed silent about it until someone
|
|
446
|
+
passed a flag would be hiding a measured failure. */
|
|
447
|
+
const unresolved = ctx.obs.advertised.filter((a) => !ctx.domIds.has(a.nac_id));
|
|
448
|
+
if (!ctx.obs.advertised.length) return failV('the page advertises no id at all');
|
|
449
|
+
if (unresolved.length) {
|
|
450
|
+
return failV(unresolved.length + ' of ' + ctx.obs.advertised.length
|
|
451
|
+
+ ' advertised id(s) do not resolve in the DOM: '
|
|
452
|
+
+ unresolved.slice(0, 4).map((a) => a.nac_id).join(', ') + (unresolved.length > 4 ? ', ...' : '')
|
|
453
|
+
+ ' (finding codes missing_in_dom / plugin_not_mounted)');
|
|
454
|
+
}
|
|
455
|
+
if (!ctx.drive) {
|
|
456
|
+
return unverified('all ' + ctx.obs.advertised.length
|
|
457
|
+
+ ' advertised id(s) resolve in the DOM; whether a click LANDS is behavioural and needs --drive');
|
|
458
|
+
}
|
|
459
|
+
/* The same distinction as above: a click on a plain link LANDED --
|
|
460
|
+
it would have navigated -- and timed out only because nothing
|
|
461
|
+
can report a completion after unload. Counting that as "the
|
|
462
|
+
click did not work" would blame the page for a hole in the
|
|
463
|
+
requirement (audit N-38). */
|
|
464
|
+
const navTimeout = ctx.driveResults.filter((r) => r.click_error && r.nav_link);
|
|
465
|
+
const bad = ctx.driveResults.filter((r) => r.click_error && !r.nav_link);
|
|
466
|
+
const navigated = ctx.driveResults.filter((r) => r.navigated).length;
|
|
467
|
+
const scope = ctx.driveResults.length + ' of ' + ctx.obs.advertised.length
|
|
468
|
+
+ ' advertised id(s) clicked (the rest carry roles with no completion event, so a click on them cannot be observed)'
|
|
469
|
+
+ (navigated ? '; ' + navigated + ' of those navigated the page, which is a side effect landing, not a failure' : '');
|
|
470
|
+
if (bad.length) {
|
|
471
|
+
return failV(bad.length + ' click(s) did not land: '
|
|
472
|
+
+ bad.slice(0, 4).map((r) => r.id + ' -> ' + r.click_error).join(', ')
|
|
473
|
+
+ (bad.length > 4 ? ', ...' : '') + '; ' + scope);
|
|
474
|
+
}
|
|
475
|
+
if (navTimeout.length) {
|
|
476
|
+
return unverified('every advertised id resolves, and ' + scope
|
|
477
|
+
+ '. ' + navTimeout.length + ' of the clicks timed out waiting for a completion event because'
|
|
478
|
+
+ ' the element is a plain link: the click landed, and nothing can report it after unload'
|
|
479
|
+
+ ' (audit N-38)');
|
|
480
|
+
}
|
|
481
|
+
return pass('every advertised id resolves, and ' + scope);
|
|
482
|
+
},
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
export const IMPLEMENTED_DECIDERS = Object.keys(DECIDERS).sort();
|
|
486
|
+
|
|
487
|
+
/* ---- the report ------------------------------------------------------ */
|
|
488
|
+
|
|
489
|
+
function decideAll(levels, ctx) {
|
|
490
|
+
const out = [];
|
|
491
|
+
for (const lv of levels.levels) {
|
|
492
|
+
for (const r of lv.requirements) {
|
|
493
|
+
let v;
|
|
494
|
+
if (!r.machine_checkable) {
|
|
495
|
+
v = unverified('not decidable by a tool: ' + (r.note || 'no tool can decide this'));
|
|
496
|
+
} else if (!DECIDERS[r.decider]) {
|
|
497
|
+
/* Cannot happen through the build gate; kept because a
|
|
498
|
+
published contract outlives the binary that read it, and a
|
|
499
|
+
validator that silently skipped a requirement would report a
|
|
500
|
+
level it never measured. */
|
|
501
|
+
v = unverified('this build implements no decider named "' + r.decider + '"');
|
|
502
|
+
} else {
|
|
503
|
+
v = DECIDERS[r.decider](ctx);
|
|
504
|
+
}
|
|
505
|
+
out.push({
|
|
506
|
+
level: lv.level,
|
|
507
|
+
level_id: lv.id,
|
|
508
|
+
requirement_id: r.id,
|
|
509
|
+
text: r.text,
|
|
510
|
+
machine_checkable: r.machine_checkable,
|
|
511
|
+
check: r.check,
|
|
512
|
+
decider: r.decider,
|
|
513
|
+
verdict: v.verdict,
|
|
514
|
+
detail: v.detail,
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return out;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/* The published rule (levels.json .reporting): a level may be reported
|
|
522
|
+
only when every machine_checkable requirement of that level AND of
|
|
523
|
+
every lower level passes; the rest are reported unverified, never
|
|
524
|
+
assumed. The coverage ratio travels with it because the rule alone
|
|
525
|
+
lets a report say "level 1" without saying that three of its four
|
|
526
|
+
requirements were never measured. A level is a measurement, not a
|
|
527
|
+
certificate. */
|
|
528
|
+
function reportLevel(levels, verdicts) {
|
|
529
|
+
let reached = null;
|
|
530
|
+
for (const lv of levels.levels) {
|
|
531
|
+
const upto = verdicts.filter((v) => v.level <= lv.level && v.machine_checkable);
|
|
532
|
+
if (upto.length && upto.every((v) => v.verdict === 'pass')) reached = lv;
|
|
533
|
+
else break;
|
|
534
|
+
}
|
|
535
|
+
const at = reached ? verdicts.filter((v) => v.level === reached.level) : [];
|
|
536
|
+
return {
|
|
537
|
+
level: reached ? reached.level : null,
|
|
538
|
+
level_id: reached ? reached.id : null,
|
|
539
|
+
display_name: reached ? reached.display_name : null,
|
|
540
|
+
legacy_display_name: reached ? reached.legacy_display_name : null,
|
|
541
|
+
decided: at.filter((v) => v.verdict !== 'unverified').length,
|
|
542
|
+
total: at.length,
|
|
543
|
+
undecided: at.filter((v) => v.verdict === 'unverified').map((v) => v.requirement_id),
|
|
544
|
+
/* Why no level, when there is none: the first machine-checkable
|
|
545
|
+
requirement that failed. Pointing at it is the difference
|
|
546
|
+
between a verdict and a shrug. */
|
|
547
|
+
blocked_by: reached ? [] : verdicts.filter((v) => v.machine_checkable && v.verdict === 'fail')
|
|
548
|
+
.map((v) => v.requirement_id),
|
|
549
|
+
blocked_by_unverified: reached ? [] : verdicts.filter((v) => v.machine_checkable && v.verdict === 'unverified')
|
|
550
|
+
.map((v) => v.requirement_id),
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
export async function validateUrl(url, opts = {}) {
|
|
555
|
+
const {
|
|
556
|
+
drive = false,
|
|
557
|
+
driveRoles = ['action'],
|
|
558
|
+
timeoutMs = 30000,
|
|
559
|
+
settleMs = 400,
|
|
560
|
+
perActionMs = 250,
|
|
561
|
+
driveLimit = 0,
|
|
562
|
+
onProgress = () => {},
|
|
563
|
+
} = opts;
|
|
564
|
+
|
|
565
|
+
const levels = loadLevels();
|
|
566
|
+
const { chromium, from } = await resolveChromium();
|
|
567
|
+
|
|
568
|
+
const browser = await chromium.launch({ headless: true });
|
|
569
|
+
const consoleErrors = [];
|
|
570
|
+
let obs = null;
|
|
571
|
+
let driveResults = [];
|
|
572
|
+
let settled = null;
|
|
573
|
+
|
|
574
|
+
try {
|
|
575
|
+
const page = await browser.newPage();
|
|
576
|
+
page.on('console', (m) => { if (m.type() === 'error') consoleErrors.push(m.text()); });
|
|
577
|
+
page.on('pageerror', (e) => consoleErrors.push(String((e && e.message) || e)));
|
|
578
|
+
|
|
579
|
+
onProgress('loading ' + url);
|
|
580
|
+
/* domcontentloaded, not load: a real page with web fonts, an
|
|
581
|
+
analytics beacon or a lazy iframe can leave the load event
|
|
582
|
+
pending for the whole budget, and a validator that timed out on
|
|
583
|
+
a page the browser had already rendered would be reporting about
|
|
584
|
+
the network. The registry settle below is the signal that
|
|
585
|
+
matters -- it waits for the thing being measured. */
|
|
586
|
+
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: timeoutMs });
|
|
587
|
+
|
|
588
|
+
try {
|
|
589
|
+
await page.waitForFunction(() => !!window.NAC, null, { timeout: timeoutMs });
|
|
590
|
+
} catch (_) {
|
|
591
|
+
return {
|
|
592
|
+
url, nac: false, playwright_from: from, console_errors: consoleErrors,
|
|
593
|
+
error: 'the page loaded and never exposed window.NAC, so there is no NAC3 contract to validate',
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
settled = await settle(page, { settleMs, timeoutMs });
|
|
598
|
+
onProgress('registry settled at ' + settled.plugins + ' plugin(s)'
|
|
599
|
+
+ (settled.settled ? '' : ' (still moving when the clock ran out)'));
|
|
600
|
+
obs = await page.evaluate(pageObservation);
|
|
601
|
+
|
|
602
|
+
if (drive) {
|
|
603
|
+
const roles = driveRoles.indexOf('all') >= 0
|
|
604
|
+
? Object.keys(levels.click_observable_roles)
|
|
605
|
+
: driveRoles;
|
|
606
|
+
let ids = obs.advertised.filter((a) => roles.indexOf(a.role) >= 0).map((a) => a.nac_id);
|
|
607
|
+
if (driveLimit > 0) ids = ids.slice(0, driveLimit);
|
|
608
|
+
|
|
609
|
+
/* A popup does not destroy the page, but it does leave a browser
|
|
610
|
+
window nobody closes and it hides the side effect from the
|
|
611
|
+
listener. Neutralised and treated as the click having landed.
|
|
612
|
+
|
|
613
|
+
Aborting the NAVIGATIONS was the first thing tried here and it
|
|
614
|
+
is the wrong tool: Chromium commits an error page, the
|
|
615
|
+
execution context dies anyway, and the abort also catches the
|
|
616
|
+
validator's own recovery navigation. So navigations are
|
|
617
|
+
allowed to happen and the loop restores the page after each
|
|
618
|
+
one -- the honest shape, since a link that navigates has
|
|
619
|
+
performed its side effect. */
|
|
620
|
+
/* Keep the page under the validator's feet.
|
|
621
|
+
|
|
622
|
+
A NAC action is very often a plain anchor with an href, and
|
|
623
|
+
clicking one navigates: the execution context dies, the
|
|
624
|
+
completion event becomes unobservable, and every id after it
|
|
625
|
+
would be measured on a different page.
|
|
626
|
+
|
|
627
|
+
What works is cancelling the DEFAULT action in the bubble
|
|
628
|
+
phase at window: by then every handler the page installed has
|
|
629
|
+
already run -- so the side effect under test happens and the
|
|
630
|
+
completion event fires -- and the browser has not yet followed
|
|
631
|
+
the link. What does NOT work, tried and measured here, is
|
|
632
|
+
aborting the navigation REQUEST: Chromium commits an error
|
|
633
|
+
page, the context dies anyway, and the abort also catches the
|
|
634
|
+
validator's own recovery navigation, so the run died on the
|
|
635
|
+
first CTA. The restore loop below stays as the backstop for
|
|
636
|
+
the programmatic navigations this cannot reach. */
|
|
637
|
+
await page.addInitScript(() => {
|
|
638
|
+
window.open = function () { return null; };
|
|
639
|
+
window.addEventListener('click', function (e) {
|
|
640
|
+
const t = e.target;
|
|
641
|
+
const a = t && t.closest ? t.closest('a[href]') : null;
|
|
642
|
+
if (a) e.preventDefault();
|
|
643
|
+
}, false);
|
|
644
|
+
window.addEventListener('submit', function (e) { e.preventDefault(); }, false);
|
|
645
|
+
});
|
|
646
|
+
/* The init script only applies to documents loaded AFTER it is
|
|
647
|
+
installed, and this page is already loaded, so the same guard
|
|
648
|
+
is installed once directly for the current document. */
|
|
649
|
+
await page.evaluate(() => {
|
|
650
|
+
window.open = function () { return null; };
|
|
651
|
+
window.addEventListener('click', function (e) {
|
|
652
|
+
const t = e.target;
|
|
653
|
+
const a = t && t.closest ? t.closest('a[href]') : null;
|
|
654
|
+
if (a) e.preventDefault();
|
|
655
|
+
}, false);
|
|
656
|
+
window.addEventListener('submit', function (e) { e.preventDefault(); }, false);
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
onProgress('driving ' + ids.length + ' id(s) of role ' + roles.join('/'));
|
|
660
|
+
const startUrl = obs.url || url;
|
|
661
|
+
for (const id of ids) {
|
|
662
|
+
const r = await driveOne(page, id, { perActionMs });
|
|
663
|
+
driveResults.push(r);
|
|
664
|
+
if (r.navigated || page.url() !== startUrl) {
|
|
665
|
+
/* Put the page back before the next id. Measuring id number
|
|
666
|
+
two on whatever page id number one navigated to would be
|
|
667
|
+
reporting about a page nobody asked about. Retried,
|
|
668
|
+
because a navigation already in flight can interrupt the
|
|
669
|
+
restore -- and a validator that gave up there would blame
|
|
670
|
+
the page for the browser being mid-redirect. */
|
|
671
|
+
onProgress('the page navigated on ' + id + '; restoring it');
|
|
672
|
+
let restored = false;
|
|
673
|
+
for (let attempt = 0; attempt < 3 && !restored; attempt += 1) {
|
|
674
|
+
try {
|
|
675
|
+
await page.goto(startUrl, { waitUntil: 'domcontentloaded', timeout: timeoutMs });
|
|
676
|
+
restored = true;
|
|
677
|
+
} catch (e) {
|
|
678
|
+
await new Promise((res) => setTimeout(res, 200));
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
if (!restored) {
|
|
682
|
+
r.restore_failed = true;
|
|
683
|
+
onProgress('could not restore the page after ' + id + '; stopping the drive here');
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
await settle(page, { settleMs, timeoutMs });
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
} finally {
|
|
691
|
+
await browser.close();
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
const ctx = {
|
|
695
|
+
obs,
|
|
696
|
+
levels,
|
|
697
|
+
drive,
|
|
698
|
+
driveResults,
|
|
699
|
+
domIds: new Set(obs.dom_id_list),
|
|
700
|
+
actionIds: obs.advertised.filter((a) => a.role === 'action').map((a) => a.nac_id),
|
|
701
|
+
};
|
|
702
|
+
|
|
703
|
+
const verdicts = decideAll(levels, ctx);
|
|
704
|
+
|
|
705
|
+
/* A warning ABOUT THE INSTRUMENT ON THE PAGE, not about the page.
|
|
706
|
+
Audit N-33: before 2.5.0, validate_global() read only the legacy
|
|
707
|
+
manifest groups, so against a manifest written the documented way
|
|
708
|
+
it reported total_ids = 0 and called every declared element an
|
|
709
|
+
orphan -- and then answered ok = true. A validator that relayed
|
|
710
|
+
that flood of phantom findings without naming the cause would
|
|
711
|
+
hand somebody a page that looks broken and is not. The signature
|
|
712
|
+
is unmistakable: manifests registered, ids in the DOM, and the
|
|
713
|
+
runtime claiming it knows of no declared id. */
|
|
714
|
+
const warnings = [];
|
|
715
|
+
if (obs.plugin_count >= 1 && obs.total_ids === 0 && obs.dom_ids > 0) {
|
|
716
|
+
warnings.push('THE RUNTIME ON THIS PAGE CANNOT SEE ITS OWN MANIFESTS. It reports '
|
|
717
|
+
+ obs.plugin_count + ' registered manifest(s), ' + obs.dom_ids + ' ids in the DOM and 0 declared ids, '
|
|
718
|
+
+ 'which is the signature of audit N-33 -- a validator blind to the canonical elements[] manifest shape. '
|
|
719
|
+
+ 'Every orphan finding below is a false positive produced by that blindness, and the level verdicts that '
|
|
720
|
+
+ 'depend on validate_global are measuring the instrument, not the page. Serve a runtime at 2.5.0 or later.');
|
|
721
|
+
}
|
|
722
|
+
if (obs.version && levels.version && obs.version !== levels.version) {
|
|
723
|
+
warnings.push('the page serves runtime ' + obs.version + ' and this validator was built for '
|
|
724
|
+
+ levels.version + '; where they disagree, the page is what an adopter actually gets');
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
return {
|
|
728
|
+
url: obs.url || url,
|
|
729
|
+
warnings,
|
|
730
|
+
nac: true,
|
|
731
|
+
playwright_from: from,
|
|
732
|
+
runtime_version: obs.version,
|
|
733
|
+
spec_version: obs.spec_version,
|
|
734
|
+
conformance_tier: obs.conformance_tier,
|
|
735
|
+
strict_versioning: obs.strict_versioning,
|
|
736
|
+
validator_version: levels.version,
|
|
737
|
+
validator_spec_version: levels.spec_version,
|
|
738
|
+
settled,
|
|
739
|
+
plugin_count: obs.plugin_count,
|
|
740
|
+
declared_ids: obs.total_ids,
|
|
741
|
+
dom_ids: obs.dom_ids,
|
|
742
|
+
findings: obs.findings,
|
|
743
|
+
tolerated_count: obs.tolerated_count,
|
|
744
|
+
console_errors: consoleErrors,
|
|
745
|
+
drive,
|
|
746
|
+
drive_results: driveResults,
|
|
747
|
+
verdicts,
|
|
748
|
+
report: reportLevel(levels, verdicts),
|
|
749
|
+
reporting_rule: levels.reporting,
|
|
750
|
+
};
|
|
751
|
+
}
|