@tokenoftrust/storefront-runner 6.12.16 → 6.12.17

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.
Files changed (48) hide show
  1. package/apps/storefront/package.json +1 -0
  2. package/package.json +1 -1
  3. package/packages/feedback/fixtures/v1/expected-findings.json +16 -0
  4. package/packages/feedback/fixtures/v1/feedback-action/invalid/carries-transport-credential.json +14 -0
  5. package/packages/feedback/fixtures/v1/feedback-action/invalid/control-char-in-description.json +13 -0
  6. package/packages/feedback/fixtures/v1/feedback-action/invalid/legacy-schema-version.json +13 -0
  7. package/packages/feedback/fixtures/v1/feedback-action/invalid/page-path-with-query.json +14 -0
  8. package/packages/feedback/fixtures/v1/feedback-action/invalid/title-too-short.json +13 -0
  9. package/packages/feedback/fixtures/v1/feedback-action/valid/element-problem.json +33 -0
  10. package/packages/feedback/fixtures/v1/feedback-action/valid/page-comment.json +20 -0
  11. package/packages/feedback/fixtures/v1/feedback-action/valid/region-improvement.json +24 -0
  12. package/packages/feedback/fixtures/v1/feedback-action/valid/text-selection-feature.json +30 -0
  13. package/packages/feedback/fixtures/v1/submission-receipt/invalid/accepted-at-not-utc.json +9 -0
  14. package/packages/feedback/fixtures/v1/submission-receipt/invalid/bad-feedback-id.json +9 -0
  15. package/packages/feedback/fixtures/v1/submission-receipt/invalid/legacy-contract-version.json +9 -0
  16. package/packages/feedback/fixtures/v1/submission-receipt/invalid/missing-replayed.json +8 -0
  17. package/packages/feedback/fixtures/v1/submission-receipt/valid/accepted.json +9 -0
  18. package/packages/feedback/fixtures/v1/submission-receipt/valid/fractional-seconds.json +9 -0
  19. package/packages/feedback/fixtures/v1/submission-receipt/valid/replayed.json +9 -0
  20. package/packages/feedback/fixtures/v1/target-evidence/invalid/page-kind-claims-structure.json +6 -0
  21. package/packages/feedback/fixtures/v1/target-evidence/invalid/path-too-deep.json +17 -0
  22. package/packages/feedback/fixtures/v1/target-evidence/invalid/relative-region-out-of-range.json +11 -0
  23. package/packages/feedback/fixtures/v1/target-evidence/invalid/selector-uses-class.json +7 -0
  24. package/packages/feedback/fixtures/v1/target-evidence/invalid/source-precision-without-anchor.json +6 -0
  25. package/packages/feedback/fixtures/v1/target-evidence/valid/element-structure.json +21 -0
  26. package/packages/feedback/fixtures/v1/target-evidence/valid/host-iframe.json +14 -0
  27. package/packages/feedback/fixtures/v1/target-evidence/valid/source-anchor.json +12 -0
  28. package/packages/feedback/fixtures/v1/target-evidence/valid/text-selection.json +26 -0
  29. package/packages/feedback/package.json +29 -0
  30. package/packages/feedback/src/index.ts +1 -0
  31. package/packages/feedback/src/primitives/composer.ts +639 -0
  32. package/packages/feedback/src/primitives/dialog.ts +252 -0
  33. package/packages/feedback/src/primitives/hostAdapter.ts +116 -0
  34. package/packages/feedback/src/primitives/index.ts +10 -0
  35. package/packages/feedback/src/primitives/launcher.ts +81 -0
  36. package/packages/feedback/src/primitives/picker.ts +663 -0
  37. package/packages/feedback/src/primitives/redaction.ts +204 -0
  38. package/packages/feedback/src/primitives/selection.ts +49 -0
  39. package/packages/feedback/src/primitives/shiftAccelerator.ts +160 -0
  40. package/packages/feedback/src/primitives/styles.ts +64 -0
  41. package/packages/feedback/src/primitives/surface.ts +124 -0
  42. package/packages/feedback/src/primitives/target.ts +293 -0
  43. package/packages/feedback/src/v1/guards.ts +318 -0
  44. package/packages/feedback/src/v1/index.ts +2 -0
  45. package/packages/feedback/src/v1/types.ts +177 -0
  46. package/packages/feedback/tsconfig.json +9 -0
  47. package/packages/feedback/vitest.config.cts +20 -0
  48. package/pnpm-lock.runner.yaml +15 -0
@@ -0,0 +1,293 @@
1
+ /**
2
+ * The selected-thing model. Collects safe structure BY CONSTRUCTION: element tag names, numeric
3
+ * child indexes, finite geometry and allowlisted semantic roles (roles are used only for the
4
+ * on-screen orientation label and never leave the browser).
5
+ *
6
+ * Never read: textContent, innerHTML/outerHTML, input values, labels, alt/title text, class names,
7
+ * ids, href/src, data-* or any other attribute value, page title or URL. (Text the reviewer
8
+ * explicitly selects is the one exception and goes through selection.ts and local redaction.)
9
+ *
10
+ * Honest precision: a canvas, an iframe (same- or cross-origin), or any shadow host is selected as
11
+ * its HOST element (`precision: "host"`); the collector never descends into another document or a
12
+ * shadow tree and never claims an inner control. A region is `precision: "region"`.
13
+ */
14
+ import type {
15
+ BrowserFamily,
16
+ FeedbackTarget,
17
+ PageContext,
18
+ PathSegment,
19
+ Rect,
20
+ StableSelector,
21
+ TargetEvidenceV1,
22
+ TextSelection,
23
+ ViewportSnapshot,
24
+ } from "../v1/types.js";
25
+
26
+ export const MAX_PATH_SEGMENTS = 16;
27
+ const TAG_PATTERN = /^[a-z][a-z0-9-]{0,31}$/;
28
+ const COORD_LIMIT = 1_000_000;
29
+
30
+ /** Elements whose interior the collector must not claim to see. */
31
+ const HOST_TAGS = new Set(["canvas", "iframe", "frame", "object", "embed", "video", "audio"]);
32
+
33
+ const SAFE_ROLES = new Set([
34
+ "button",
35
+ "link",
36
+ "img",
37
+ "navigation",
38
+ "banner",
39
+ "main",
40
+ "contentinfo",
41
+ "form",
42
+ "search",
43
+ "region",
44
+ "dialog",
45
+ "list",
46
+ "listitem",
47
+ "heading",
48
+ "tab",
49
+ "tablist",
50
+ "tabpanel",
51
+ "menu",
52
+ "menuitem",
53
+ "checkbox",
54
+ "radio",
55
+ "textbox",
56
+ "combobox",
57
+ "table",
58
+ "row",
59
+ "cell",
60
+ "article",
61
+ "figure",
62
+ ]);
63
+
64
+ const IMPLICIT_ROLE: Record<string, string> = {
65
+ a: "link",
66
+ button: "button",
67
+ img: "image",
68
+ nav: "navigation",
69
+ header: "header",
70
+ footer: "footer",
71
+ main: "main",
72
+ form: "form",
73
+ input: "input field",
74
+ textarea: "text area",
75
+ select: "menu",
76
+ ul: "list",
77
+ ol: "list",
78
+ li: "list item",
79
+ h1: "heading",
80
+ h2: "heading",
81
+ h3: "heading",
82
+ h4: "heading",
83
+ h5: "heading",
84
+ h6: "heading",
85
+ table: "table",
86
+ section: "section",
87
+ article: "article",
88
+ aside: "sidebar",
89
+ figure: "figure",
90
+ canvas: "canvas",
91
+ iframe: "embedded frame",
92
+ video: "video",
93
+ label: "label",
94
+ };
95
+
96
+ function finite(n: number, min: number, max: number): number {
97
+ if (!Number.isFinite(n)) return 0;
98
+ return Math.min(max, Math.max(min, Math.round(n * 100) / 100));
99
+ }
100
+
101
+ export function safeRect(r: { x?: number; y?: number; left?: number; top?: number; width: number; height: number }): Rect {
102
+ return {
103
+ x: finite(r.x ?? r.left ?? 0, -COORD_LIMIT, COORD_LIMIT),
104
+ y: finite(r.y ?? r.top ?? 0, -COORD_LIMIT, COORD_LIMIT),
105
+ width: finite(r.width, 0, COORD_LIMIT),
106
+ height: finite(r.height, 0, COORD_LIMIT),
107
+ };
108
+ }
109
+
110
+ function tagOf(el: Element): string {
111
+ return (el.localName || el.tagName || "").toLowerCase();
112
+ }
113
+
114
+ /** True when the element is opaque to the collector (selected as host, never descended into). */
115
+ export function isHostBoundary(el: Element): boolean {
116
+ if (HOST_TAGS.has(tagOf(el))) return true;
117
+ // Any shadow host. A closed root is invisible here; an open one is still another tree whose
118
+ // structure a numeric light-DOM path cannot express, so both are treated as hosts.
119
+ if ((el as Element & { shadowRoot?: ShadowRoot | null }).shadowRoot) return true;
120
+ // Custom elements commonly own a closed shadow root that is undetectable from outside.
121
+ if (tagOf(el).includes("-")) return true;
122
+ return false;
123
+ }
124
+
125
+ /**
126
+ * Root-anchored structural path from <html>'s children down to `el`, using element-child indexes.
127
+ * Returns `truncated: true` when the path exceeds 16 segments or meets a tag outside the schema;
128
+ * the path then stops at the deepest representable ancestor.
129
+ */
130
+ export function structuralPathFor(el: Element): { path: PathSegment[]; truncated: boolean } {
131
+ const chain: Element[] = [];
132
+ let node: Element | null = el;
133
+ while (node && node.parentElement) {
134
+ chain.push(node);
135
+ node = node.parentElement;
136
+ }
137
+ chain.reverse();
138
+ const path: PathSegment[] = [];
139
+ let truncated = false;
140
+ for (const item of chain) {
141
+ const tag = tagOf(item);
142
+ if (!TAG_PATTERN.test(tag) || path.length >= MAX_PATH_SEGMENTS) {
143
+ truncated = true;
144
+ break;
145
+ }
146
+ const parent = item.parentElement as Element;
147
+ const index = Array.prototype.indexOf.call(parent.children, item) as number;
148
+ if (index < 0 || index > 9999) {
149
+ truncated = true;
150
+ break;
151
+ }
152
+ path.push({ tag, childIndex: index });
153
+ }
154
+ return { path, truncated };
155
+ }
156
+
157
+ /** Build an element target. Precision is never claimed beyond what the collector verified. */
158
+ export function elementTarget(el: Element): FeedbackTarget {
159
+ const { path, truncated } = structuralPathFor(el);
160
+ const bounds = safeRect(el.getBoundingClientRect());
161
+ let precision: FeedbackTarget["precision"] = isHostBoundary(el) ? "host" : "structure";
162
+ if (truncated) precision = "region";
163
+ const target: FeedbackTarget = { kind: "element", precision, bounds };
164
+ if (path.length > 0) target.structuralPath = path;
165
+ return target;
166
+ }
167
+
168
+ /** Build a region target, optionally relative to a containing element. */
169
+ export function regionTarget(rect: Rect, container: Element | null): FeedbackTarget {
170
+ const bounds = safeRect(rect);
171
+ const target: FeedbackTarget = { kind: "region", precision: "region", bounds };
172
+ if (container) {
173
+ const c = container.getBoundingClientRect();
174
+ if (c.width > 0 && c.height > 0) {
175
+ const clamp01 = (n: number) => finite(Math.min(1, Math.max(0, n)), 0, 1);
176
+ target.relativeRegion = {
177
+ x: clamp01((bounds.x - c.left) / c.width),
178
+ y: clamp01((bounds.y - c.top) / c.height),
179
+ width: clamp01(bounds.width / c.width),
180
+ height: clamp01(bounds.height / c.height),
181
+ };
182
+ const { path, truncated } = structuralPathFor(container);
183
+ if (path.length > 0 && !truncated) target.structuralPath = path;
184
+ }
185
+ }
186
+ return target;
187
+ }
188
+
189
+ export function pageTarget(): FeedbackTarget {
190
+ return { kind: "page", precision: "page" };
191
+ }
192
+
193
+ /** Normalise two corner points into a rect clamped to the visible viewport. */
194
+ export function rectFromCorners(
195
+ a: { x: number; y: number },
196
+ b: { x: number; y: number },
197
+ viewport: { width: number; height: number },
198
+ ): Rect {
199
+ const cx = (n: number) => Math.min(viewport.width, Math.max(0, n));
200
+ const cy = (n: number) => Math.min(viewport.height, Math.max(0, n));
201
+ const x1 = cx(Math.min(a.x, b.x));
202
+ const y1 = cy(Math.min(a.y, b.y));
203
+ const x2 = cx(Math.max(a.x, b.x));
204
+ const y2 = cy(Math.max(a.y, b.y));
205
+ return safeRect({ x: x1, y: y1, width: x2 - x1, height: y2 - y1 });
206
+ }
207
+
208
+ export function viewportSnapshot(win: Window): ViewportSnapshot {
209
+ const vv = (win as Window & { visualViewport?: VisualViewport | null }).visualViewport;
210
+ const int = (n: number) => Math.min(20000, Math.max(1, Math.round(Number.isFinite(n) ? n : 1)));
211
+ const scale = vv && Number.isFinite(vv.scale) && vv.scale > 0 ? Math.min(10, vv.scale) : 1;
212
+ const dpr =
213
+ Number.isFinite(win.devicePixelRatio) && win.devicePixelRatio > 0 ? Math.min(10, win.devicePixelRatio) : 1;
214
+ return {
215
+ width: int(win.innerWidth),
216
+ height: int(win.innerHeight),
217
+ scrollX: finite(Math.max(0, win.scrollX || 0), 0, 10_000_000),
218
+ scrollY: finite(Math.max(0, win.scrollY || 0), 0, 10_000_000),
219
+ scale: finite(scale, 0.01, 10),
220
+ devicePixelRatio: finite(dpr, 0.01, 10),
221
+ };
222
+ }
223
+
224
+ /**
225
+ * A short orientation label for the UI ("button", "link", "canvas (whole element)"). Reads only
226
+ * the tag and, when allowlisted, the role attribute token. Never user-visible text.
227
+ */
228
+ export function safeLabel(el: Element): string {
229
+ const tag = tagOf(el);
230
+ const roleAttr = (el.getAttribute("role") || "").trim().toLowerCase().split(/\s+/)[0] ?? "";
231
+ const role = SAFE_ROLES.has(roleAttr) ? roleAttr : IMPLICIT_ROLE[tag];
232
+ const base = role ?? (TAG_PATTERN.test(tag) && !tag.includes("-") ? `<${tag}>` : "element");
233
+ return isHostBoundary(el) ? `${base} (contents not inspected)` : base;
234
+ }
235
+
236
+ export function describeTarget(target: FeedbackTarget, label?: string): string {
237
+ switch (target.kind) {
238
+ case "page":
239
+ return "Whole page";
240
+ case "region":
241
+ return `Region ${Math.round(target.bounds?.width ?? 0)}×${Math.round(target.bounds?.height ?? 0)}`;
242
+ default:
243
+ return label ?? "Element";
244
+ }
245
+ }
246
+
247
+ /** Parse a coarse browser family/major from a user agent. Bounded; never stores the UA. */
248
+ export function browserFamily(ua: string): BrowserFamily {
249
+ const s = String(ua ?? "").slice(0, 512);
250
+ const pick = (re: RegExp) => {
251
+ const m = s.match(re);
252
+ const n = m ? Number.parseInt(m[1] as string, 10) : 0;
253
+ return Number.isFinite(n) ? Math.min(999, Math.max(0, n)) : 0;
254
+ };
255
+ if (/Firefox\/\d+/.test(s)) return { family: "firefox", major: pick(/Firefox\/(\d+)/) };
256
+ if (/(?:Chrome|Chromium|Edg|CriOS)\/\d+/.test(s)) {
257
+ return { family: "chromium", major: pick(/(?:Chrome|Chromium|Edg|CriOS)\/(\d+)/) };
258
+ }
259
+ if (/AppleWebKit\/\d+/.test(s)) return { family: "webkit", major: pick(/Version\/(\d+)/) };
260
+ return { family: "other", major: 0 };
261
+ }
262
+
263
+ /** A selector re-derivable from structure alone: `tag:nth-child(n)` steps joined by ` > `. */
264
+ export function structuralSelector(path: readonly PathSegment[]): StableSelector | null {
265
+ if (path.length === 0) return null;
266
+ return {
267
+ strategy: "structural",
268
+ value: path.map((s) => `${s.tag}:nth-child(${s.childIndex + 1})`).join(" > "),
269
+ };
270
+ }
271
+
272
+ export interface BuildEvidenceInput {
273
+ win: Window;
274
+ target: FeedbackTarget;
275
+ /** Page context from the host adapter, when it supplies one. */
276
+ page?: PageContext;
277
+ selection?: TextSelection;
278
+ }
279
+
280
+ /** Assemble the v1 target evidence for a pick. Pure apart from reading the viewport. */
281
+ export function buildTargetEvidence(input: BuildEvidenceInput): TargetEvidenceV1 {
282
+ const evidence: TargetEvidenceV1 = {
283
+ schemaVersion: "feedback.v1",
284
+ kind: "feedback.target",
285
+ target: input.target,
286
+ viewport: viewportSnapshot(input.win),
287
+ };
288
+ if (input.page) evidence.page = input.page;
289
+ if (input.selection) evidence.selection = input.selection;
290
+ const selector = input.target.structuralPath ? structuralSelector(input.target.structuralPath) : null;
291
+ if (selector) evidence.selectors = [selector];
292
+ return evidence;
293
+ }
@@ -0,0 +1,318 @@
1
+ /**
2
+ * Hand-written runtime validators for the feedback v1 contracts. Strict: unknown keys are refused
3
+ * at every level. Each `validate*` returns the list of offending paths (empty when valid); each
4
+ * `is*` is the matching type guard. Pure, dependency-free, never throws on hostile input.
5
+ */
6
+ import {
7
+ FEEDBACK_SCHEMA_VERSION,
8
+ type FeedbackActionV1,
9
+ type FeedbackContractV1,
10
+ type SubmissionReceiptV1,
11
+ type TargetEvidenceV1,
12
+ } from "./types.js";
13
+
14
+ export const TITLE_MIN = 3;
15
+ export const TITLE_MAX = 200;
16
+ export const DESCRIPTION_MIN = 3;
17
+ export const DESCRIPTION_MAX = 4000;
18
+ export const SUGGESTED_FIX_MAX = 2000;
19
+ export const EXCERPT_MAX = 500;
20
+ export const PAGE_PATH_MAX = 512;
21
+ export const MAX_PATH_SEGMENTS = 16;
22
+ export const MAX_SELECTORS = 4;
23
+ export const COORD_LIMIT = 1_000_000;
24
+
25
+ const INTENTS = ["problem", "improvement", "feature", "comment"];
26
+ const IMPACTS = ["low", "medium", "high", "critical"];
27
+ const TARGET_KINDS = ["element", "region", "page"];
28
+ const PRECISIONS = ["source", "structure", "region", "host", "page"];
29
+ const REDACTION_KEYS = ["email", "phone", "credential", "url", "markup", "other"];
30
+ const BROWSER_FAMILIES = ["chromium", "firefox", "webkit", "other"];
31
+
32
+ const TAG = /^[a-z][a-z0-9-]{0,31}$/;
33
+ const ANCHOR = /^[A-Za-z0-9][A-Za-z0-9._/:-]{0,199}$/;
34
+ const ROUTE_ID = /^[A-Za-z0-9[\]][A-Za-z0-9._/:[\]-]{0,199}$/;
35
+ const POLICY_VERSION = /^[a-z0-9][a-z0-9.-]{0,31}$/;
36
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
37
+ const FEEDBACK_ID = /^fb-[A-Za-z0-9_-]{1,96}$/;
38
+ const ISO_UTC = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?Z$/;
39
+ const STRUCTURAL_STEP = "[a-z][a-z0-9-]{0,31}:nth-child\\([1-9][0-9]{0,3}\\)";
40
+ const STRUCTURAL_SELECTOR = new RegExp(`^${STRUCTURAL_STEP}(?: > ${STRUCTURAL_STEP}){0,15}$`);
41
+ const PAGE_PATH = /^\/[^?#\s]*$/;
42
+
43
+ type Rec = Record<string, unknown>;
44
+
45
+ function isRecord(v: unknown): v is Rec {
46
+ return typeof v === "object" && v !== null && !Array.isArray(v);
47
+ }
48
+
49
+ function onlyKeys(obj: Rec, allowed: readonly string[], where: string, out: string[]): void {
50
+ for (const key of Object.keys(obj)) if (!allowed.includes(key)) out.push(`${where}.${key}`);
51
+ }
52
+
53
+ function num(v: unknown, min: number, max: number, exclusiveMin = false): boolean {
54
+ return typeof v === "number" && Number.isFinite(v) && (exclusiveMin ? v > min : v >= min) && v <= max;
55
+ }
56
+
57
+ function int(v: unknown, min: number, max: number): boolean {
58
+ return num(v, min, max) && Number.isInteger(v);
59
+ }
60
+
61
+ function oneOf(v: unknown, values: readonly string[]): boolean {
62
+ return typeof v === "string" && values.includes(v);
63
+ }
64
+
65
+ /** Any C0/C1 control other than tab, LF and CR, or U+2028/U+2029. */
66
+ export function hasForbiddenControl(value: string): boolean {
67
+ for (let i = 0; i < value.length; i += 1) {
68
+ const c = value.charCodeAt(i);
69
+ if (c === 0x09 || c === 0x0a || c === 0x0d) continue;
70
+ if (c < 0x20 || (c >= 0x7f && c <= 0x9f) || c === 0x2028 || c === 0x2029) return true;
71
+ }
72
+ return false;
73
+ }
74
+
75
+ function prose(v: unknown, min: number, max: number): boolean {
76
+ return typeof v === "string" && v.trim().length >= min && v.length <= max && !hasForbiddenControl(v);
77
+ }
78
+
79
+ function rect(v: unknown, lo: number, hi: number, where: string, out: string[]): void {
80
+ if (!isRecord(v)) {
81
+ out.push(where);
82
+ return;
83
+ }
84
+ onlyKeys(v, ["x", "y", "width", "height"], where, out);
85
+ if (!num(v.x, lo, hi) || !num(v.y, lo, hi) || !num(v.width, 0, hi) || !num(v.height, 0, hi)) out.push(where);
86
+ }
87
+
88
+ function redactionCounts(v: unknown, where: string, out: string[]): void {
89
+ if (!isRecord(v)) {
90
+ out.push(where);
91
+ return;
92
+ }
93
+ onlyKeys(v, REDACTION_KEYS, where, out);
94
+ for (const [key, value] of Object.entries(v)) {
95
+ if (REDACTION_KEYS.includes(key) && !int(value, 0, 10_000)) out.push(`${where}.${key}`);
96
+ }
97
+ }
98
+
99
+ function excerpt(v: unknown, where: string, out: string[]): void {
100
+ if (!isRecord(v)) {
101
+ out.push(where);
102
+ return;
103
+ }
104
+ onlyKeys(v, ["text", "truncated", "redactionCounts"], where, out);
105
+ if (!prose(v.text, 1, EXCERPT_MAX)) out.push(`${where}.text`);
106
+ if (typeof v.truncated !== "boolean") out.push(`${where}.truncated`);
107
+ if (v.redactionCounts !== undefined) redactionCounts(v.redactionCounts, `${where}.redactionCounts`, out);
108
+ }
109
+
110
+ function header(v: Rec, kind: string, where: string, out: string[]): void {
111
+ if (v.schemaVersion !== FEEDBACK_SCHEMA_VERSION) out.push(`${where}.schemaVersion`);
112
+ if (v.kind !== kind) out.push(`${where}.kind`);
113
+ }
114
+
115
+ function validateEvidenceAt(value: unknown, where: string, out: string[]): void {
116
+ if (!isRecord(value)) {
117
+ out.push(where);
118
+ return;
119
+ }
120
+ onlyKeys(value, ["schemaVersion", "kind", "target", "viewport", "page", "selection", "selectors", "domExcerpt"], where, out);
121
+ header(value, "feedback.target", where, out);
122
+
123
+ const t = value.target;
124
+ const tw = `${where}.target`;
125
+ if (!isRecord(t)) out.push(tw);
126
+ else {
127
+ onlyKeys(t, ["kind", "precision", "sourceAnchor", "structuralPath", "bounds", "relativeRegion"], tw, out);
128
+ if (!oneOf(t.kind, TARGET_KINDS)) out.push(`${tw}.kind`);
129
+ if (!oneOf(t.precision, PRECISIONS)) out.push(`${tw}.precision`);
130
+ if (t.kind === "page" && t.precision !== "page") out.push(`${tw}.precision`);
131
+ if (t.precision === "source" && t.sourceAnchor === undefined) out.push(`${tw}.sourceAnchor`);
132
+ if (t.sourceAnchor !== undefined && !(typeof t.sourceAnchor === "string" && ANCHOR.test(t.sourceAnchor))) {
133
+ out.push(`${tw}.sourceAnchor`);
134
+ }
135
+ if (t.structuralPath !== undefined) {
136
+ const p = t.structuralPath;
137
+ if (!Array.isArray(p) || p.length < 1 || p.length > MAX_PATH_SEGMENTS) out.push(`${tw}.structuralPath`);
138
+ else
139
+ p.forEach((seg: unknown, i) => {
140
+ const sw = `${tw}.structuralPath[${i}]`;
141
+ if (
142
+ !isRecord(seg) ||
143
+ Object.keys(seg).some((k) => k !== "tag" && k !== "childIndex") ||
144
+ !(typeof seg.tag === "string" && TAG.test(seg.tag)) ||
145
+ !int(seg.childIndex, 0, 9999)
146
+ ) {
147
+ out.push(sw);
148
+ }
149
+ });
150
+ }
151
+ if (t.bounds !== undefined) rect(t.bounds, -COORD_LIMIT, COORD_LIMIT, `${tw}.bounds`, out);
152
+ if (t.relativeRegion !== undefined) rect(t.relativeRegion, 0, 1, `${tw}.relativeRegion`, out);
153
+ }
154
+
155
+ const v = value.viewport;
156
+ const vw = `${where}.viewport`;
157
+ if (!isRecord(v)) out.push(vw);
158
+ else {
159
+ onlyKeys(v, ["width", "height", "scrollX", "scrollY", "scale", "devicePixelRatio"], vw, out);
160
+ if (!int(v.width, 1, 20_000)) out.push(`${vw}.width`);
161
+ if (!int(v.height, 1, 20_000)) out.push(`${vw}.height`);
162
+ if (!num(v.scrollX, 0, 10_000_000)) out.push(`${vw}.scrollX`);
163
+ if (!num(v.scrollY, 0, 10_000_000)) out.push(`${vw}.scrollY`);
164
+ if (!num(v.scale, 0, 10, true)) out.push(`${vw}.scale`);
165
+ if (!num(v.devicePixelRatio, 0, 10, true)) out.push(`${vw}.devicePixelRatio`);
166
+ }
167
+
168
+ if (value.page !== undefined) {
169
+ const pg = value.page;
170
+ const pw = `${where}.page`;
171
+ if (!isRecord(pg)) out.push(pw);
172
+ else {
173
+ onlyKeys(pg, ["path", "routeId"], pw, out);
174
+ if (!(typeof pg.path === "string" && pg.path.length <= PAGE_PATH_MAX && PAGE_PATH.test(pg.path) && !hasForbiddenControl(pg.path))) {
175
+ out.push(`${pw}.path`);
176
+ }
177
+ if (pg.routeId !== undefined && !(typeof pg.routeId === "string" && ROUTE_ID.test(pg.routeId))) out.push(`${pw}.routeId`);
178
+ }
179
+ }
180
+
181
+ if (value.selection !== undefined) {
182
+ const s = value.selection;
183
+ const sw = `${where}.selection`;
184
+ if (!isRecord(s)) out.push(sw);
185
+ else {
186
+ onlyKeys(s, ["kind", "charCount", "bounds", "excerpt"], sw, out);
187
+ if (s.kind !== "text") out.push(`${sw}.kind`);
188
+ if (!int(s.charCount, 0, 100_000)) out.push(`${sw}.charCount`);
189
+ if (s.bounds !== undefined) rect(s.bounds, -COORD_LIMIT, COORD_LIMIT, `${sw}.bounds`, out);
190
+ if (s.excerpt !== undefined) excerpt(s.excerpt, `${sw}.excerpt`, out);
191
+ }
192
+ }
193
+
194
+ if (value.selectors !== undefined) {
195
+ const sel = value.selectors;
196
+ const sw = `${where}.selectors`;
197
+ if (!Array.isArray(sel) || sel.length < 1 || sel.length > MAX_SELECTORS) out.push(sw);
198
+ else
199
+ sel.forEach((item: unknown, i) => {
200
+ const iw = `${sw}[${i}]`;
201
+ if (!isRecord(item)) {
202
+ out.push(iw);
203
+ return;
204
+ }
205
+ onlyKeys(item, ["strategy", "value"], iw, out);
206
+ const ok =
207
+ typeof item.value === "string" &&
208
+ ((item.strategy === "structural" && STRUCTURAL_SELECTOR.test(item.value)) ||
209
+ (item.strategy === "source-anchor" && ANCHOR.test(item.value)));
210
+ if (!ok) out.push(iw);
211
+ });
212
+ }
213
+
214
+ if (value.domExcerpt !== undefined) excerpt(value.domExcerpt, `${where}.domExcerpt`, out);
215
+ }
216
+
217
+ export function validateTargetEvidence(value: unknown): string[] {
218
+ const out: string[] = [];
219
+ validateEvidenceAt(value, "evidence", out);
220
+ return out;
221
+ }
222
+
223
+ export function validateFeedbackAction(value: unknown): string[] {
224
+ const out: string[] = [];
225
+ const where = "action";
226
+ if (!isRecord(value)) return [where];
227
+ onlyKeys(
228
+ value,
229
+ ["schemaVersion", "kind", "submissionId", "capturePolicyVersion", "report", "evidence", "redactionCounts", "browser"],
230
+ where,
231
+ out,
232
+ );
233
+ header(value, "feedback.action", where, out);
234
+ if (!(typeof value.submissionId === "string" && UUID.test(value.submissionId))) out.push(`${where}.submissionId`);
235
+ if (!(typeof value.capturePolicyVersion === "string" && POLICY_VERSION.test(value.capturePolicyVersion))) {
236
+ out.push(`${where}.capturePolicyVersion`);
237
+ }
238
+
239
+ const r = value.report;
240
+ const rw = `${where}.report`;
241
+ if (!isRecord(r)) out.push(rw);
242
+ else {
243
+ onlyKeys(r, ["intent", "impact", "title", "description", "suggestedFix"], rw, out);
244
+ if (!oneOf(r.intent, INTENTS)) out.push(`${rw}.intent`);
245
+ if (!oneOf(r.impact, IMPACTS)) out.push(`${rw}.impact`);
246
+ if (!prose(r.title, TITLE_MIN, TITLE_MAX)) out.push(`${rw}.title`);
247
+ if (!prose(r.description, DESCRIPTION_MIN, DESCRIPTION_MAX)) out.push(`${rw}.description`);
248
+ if (r.suggestedFix !== undefined && !prose(r.suggestedFix, 1, SUGGESTED_FIX_MAX)) out.push(`${rw}.suggestedFix`);
249
+ }
250
+
251
+ validateEvidenceAt(value.evidence, `${where}.evidence`, out);
252
+
253
+ if (value.redactionCounts !== undefined) redactionCounts(value.redactionCounts, `${where}.redactionCounts`, out);
254
+ if (value.browser !== undefined) {
255
+ const b = value.browser;
256
+ const bw = `${where}.browser`;
257
+ if (!isRecord(b)) out.push(bw);
258
+ else {
259
+ onlyKeys(b, ["family", "major"], bw, out);
260
+ if (!oneOf(b.family, BROWSER_FAMILIES) || !int(b.major, 0, 999)) out.push(bw);
261
+ }
262
+ }
263
+ return out;
264
+ }
265
+
266
+ export function validateSubmissionReceipt(value: unknown): string[] {
267
+ const out: string[] = [];
268
+ const where = "receipt";
269
+ if (!isRecord(value)) return [where];
270
+ onlyKeys(value, ["schemaVersion", "kind", "feedbackId", "submissionId", "title", "acceptedAt", "replayed"], where, out);
271
+ header(value, "feedback.receipt", where, out);
272
+ if (!(typeof value.feedbackId === "string" && FEEDBACK_ID.test(value.feedbackId))) out.push(`${where}.feedbackId`);
273
+ if (!(typeof value.submissionId === "string" && UUID.test(value.submissionId))) out.push(`${where}.submissionId`);
274
+ if (!(typeof value.title === "string" && value.title.length <= TITLE_MAX && !hasForbiddenControl(value.title))) {
275
+ out.push(`${where}.title`);
276
+ }
277
+ if (!(typeof value.acceptedAt === "string" && ISO_UTC.test(value.acceptedAt) && Number.isFinite(Date.parse(value.acceptedAt)))) {
278
+ out.push(`${where}.acceptedAt`);
279
+ }
280
+ if (typeof value.replayed !== "boolean") out.push(`${where}.replayed`);
281
+ return out;
282
+ }
283
+
284
+ export function isTargetEvidence(value: unknown): value is TargetEvidenceV1 {
285
+ return validateTargetEvidence(value).length === 0;
286
+ }
287
+
288
+ export function isFeedbackAction(value: unknown): value is FeedbackActionV1 {
289
+ return validateFeedbackAction(value).length === 0;
290
+ }
291
+
292
+ export function isSubmissionReceipt(value: unknown): value is SubmissionReceiptV1 {
293
+ return validateSubmissionReceipt(value).length === 0;
294
+ }
295
+
296
+ /** A receipt answers an action only when it echoes that action's submission id. */
297
+ export function receiptMatchesAction(receipt: SubmissionReceiptV1, action: FeedbackActionV1): boolean {
298
+ return receipt.submissionId === action.submissionId;
299
+ }
300
+
301
+ /** Dispatch on `kind`; refuses anything that is not a v1 contract. */
302
+ export function validateFeedbackContract(value: unknown): string[] {
303
+ if (!isRecord(value) || value.schemaVersion !== FEEDBACK_SCHEMA_VERSION) return ["schemaVersion"];
304
+ switch (value.kind) {
305
+ case "feedback.action":
306
+ return validateFeedbackAction(value);
307
+ case "feedback.target":
308
+ return validateTargetEvidence(value);
309
+ case "feedback.receipt":
310
+ return validateSubmissionReceipt(value);
311
+ default:
312
+ return ["kind"];
313
+ }
314
+ }
315
+
316
+ export function isFeedbackContract(value: unknown): value is FeedbackContractV1 {
317
+ return validateFeedbackContract(value).length === 0;
318
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./types.js";
2
+ export * from "./guards.js";