File size: 9,470 Bytes
bee9bbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""Site-specific semantic checks for interactive clone markup and its inventory."""

from __future__ import annotations

from dataclasses import dataclass
from html.parser import HTMLParser
import json
from pathlib import Path
from typing import Mapping
from urllib.parse import urlsplit


_BEHAVIORS = {
    "navigate",
    "submit",
    "client-state",
    "durable-state",
    "safe-boundary",
}
_TRACE_IDS = {f"trace-{number:03d}" for number in range(1, 24)}
_VOID_TAGS = {
    "area",
    "base",
    "br",
    "col",
    "embed",
    "hr",
    "img",
    "input",
    "link",
    "meta",
    "param",
    "source",
    "track",
    "wbr",
}
_CONTROL_TAGS = {"button", "input", "select", "textarea"}
_BOOLEAN_CLIENT_ACTION_ATTRIBUTES = {"data-login-open"}


@dataclass(frozen=True)
class InteractionFinding:
    route: str
    code: str
    label: str


@dataclass(frozen=True)
class ControlContract:
    id: str
    route: str
    selector: str
    behavior: str
    target: str
    persistence: str
    trace_ids: tuple[str, ...]
    evidence_refs: tuple[str, ...]


@dataclass
class _Element:
    tag: str
    attrs: Mapping[str, str]
    text: list[str]


class _InteractionParser(HTMLParser):
    def __init__(self, route: str) -> None:
        super().__init__(convert_charrefs=True)
        self.route = route
        self.findings: list[InteractionFinding] = []
        self._pending_findings: list[tuple[str, _Element]] = []
        self._open: list[_Element] = []
        self._anchors: list[_Element] = []
        self._elements_by_id: dict[str, _Element] = {}
        self._disabled_controls: list[_Element] = []

    def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
        normalized_tag = tag.lower()
        attributes = {
            name.lower(): "" if value is None else value for name, value in attrs
        }
        element = _Element(normalized_tag, attributes, [])
        element_id = attributes.get("id", "").strip()
        if element_id:
            self._elements_by_id[element_id] = element

        if normalized_tag == "a":
            self._anchors.append(element)
            href = attributes.get("href", "").strip()
            if not href or href == "#" or href.lower().startswith("javascript:"):
                self._pending_findings.append(("placeholder-link", element))
        elif normalized_tag == "button":
            self._audit_button(element)
        elif normalized_tag == "input" and attributes.get("type", "").lower() == "submit":
            self._audit_submit(element)

        if normalized_tag in _CONTROL_TAGS and "disabled" in attributes:
            self._disabled_controls.append(element)
        if normalized_tag == "img" and attributes.get("alt", "").strip():
            for ancestor in self._open:
                ancestor.text.append(attributes["alt"])
        if normalized_tag == "area" or (
            normalized_tag == "img" and attributes.get("usemap", "").strip()
        ):
            self._finding("image-map-control", self._label(element, normalized_tag))

        if normalized_tag not in _VOID_TAGS:
            self._open.append(element)

    def handle_startendtag(
        self, tag: str, attrs: list[tuple[str, str | None]]
    ) -> None:
        self.handle_starttag(tag, attrs)
        if tag.lower() not in _VOID_TAGS:
            self.handle_endtag(tag)

    def handle_endtag(self, tag: str) -> None:
        normalized_tag = tag.lower()
        for index in range(len(self._open) - 1, -1, -1):
            if self._open[index].tag == normalized_tag:
                del self._open[index:]
                return

    def handle_data(self, data: str) -> None:
        for element in self._open:
            element.text.append(data)

    def close(self) -> None:
        super().close()
        for code, element in self._pending_findings:
            self._finding(code, self._label(element, "control"))
        for anchor in self._anchors:
            if _is_local_target(anchor.attrs.get("href", "").strip()) and not self._accessible_name(
                anchor
            ):
                self._finding("unlabeled-link", "link")
        for control in self._disabled_controls:
            description_ids = control.attrs.get("aria-describedby", "").split()
            if not description_ids or not all(
                self._describes_control(description_id)
                for description_id in description_ids
            ):
                self._finding(
                    "unexplained-disabled-control", self._label(control, "control")
                )

    def _audit_button(self, element: _Element) -> None:
        if "disabled" in element.attrs:
            return
        button_type = element.attrs.get("type", "submit").strip().lower()
        if button_type == "button" and not self._has_client_action(element):
            self._pending_findings.append(("inert-button", element))
        elif button_type == "submit":
            self._audit_submit(element)

    def _audit_submit(self, element: _Element) -> None:
        if "disabled" in element.attrs:
            return
        form = next(
            (item for item in reversed(self._open) if item.tag == "form"), None
        )
        if form is None or not _is_local_target(form.attrs.get("action", "")):
            self._pending_findings.append(
                ("orphan-submit" if form is None else "nonlocal-submit", element)
            )

    def _describes_control(self, description_id: str) -> bool:
        description = self._elements_by_id.get(description_id)
        return description is not None and bool("".join(description.text).strip())

    @staticmethod
    def _has_client_action(element: _Element) -> bool:
        return bool(element.attrs.get("data-control-action", "").strip()) or any(
            attribute in element.attrs
            for attribute in _BOOLEAN_CLIENT_ACTION_ATTRIBUTES
        )

    def _finding(self, code: str, label: str) -> None:
        self.findings.append(InteractionFinding(self.route, code, label))

    def _label(self, element: _Element, fallback: str) -> str:
        return self._accessible_name(element) or fallback

    def _accessible_name(self, element: _Element) -> str:
        labelled_by = element.attrs.get("aria-labelledby", "").split()
        referenced = " ".join(
            "".join(self._elements_by_id[item_id].text).strip()
            for item_id in labelled_by
            if item_id in self._elements_by_id
        )
        return (
            element.attrs.get("aria-label", "").strip()
            or element.attrs.get("title", "").strip()
            or element.attrs.get("alt", "").strip()
            or referenced
            or "".join(element.text).strip()
        )


def audit_markup(html: str, *, route: str) -> tuple[InteractionFinding, ...]:
    parser = _InteractionParser(route)
    parser.feed(html)
    parser.close()
    return tuple(parser.findings)


def load_control_contract(path: Path) -> tuple[ControlContract, ...]:
    payload = json.loads(path.read_text(encoding="utf-8"))
    entries = payload.get("controls") if isinstance(payload, dict) else None
    if not isinstance(entries, list):
        raise ValueError("control contract must provide a controls list")

    controls: list[ControlContract] = []
    seen_ids: set[str] = set()
    for entry in entries:
        if not isinstance(entry, dict):
            raise ValueError("control contract entries must be objects")
        control = _control_from_entry(entry)
        if control.id in seen_ids:
            raise ValueError(f"duplicate control id: {control.id}")
        seen_ids.add(control.id)
        controls.append(control)
    return tuple(controls)


def _control_from_entry(entry: Mapping[str, object]) -> ControlContract:
    required_strings = (
        "id",
        "route",
        "selector",
        "behavior",
        "target",
        "persistence",
    )
    values: dict[str, str] = {}
    for field in required_strings:
        value = entry.get(field)
        if not isinstance(value, str) or not value.strip():
            raise ValueError(f"control {field} must be a non-empty string")
        values[field] = value

    if values["behavior"] not in _BEHAVIORS:
        raise ValueError(f"unknown control behavior: {values['behavior']}")
    if not _is_local_target(values["route"]) or not _is_local_target(
        values["target"]
    ):
        raise ValueError("control routes and targets must be local paths")

    trace_ids = _string_list(entry.get("trace_ids"), "trace_ids")
    unknown_trace_ids = set(trace_ids) - _TRACE_IDS
    if unknown_trace_ids:
        raise ValueError(f"unknown trace bindings: {sorted(unknown_trace_ids)}")
    evidence_refs = _string_list(entry.get("evidence_refs"), "evidence_refs")
    return ControlContract(
        **values,
        trace_ids=trace_ids,
        evidence_refs=evidence_refs,
    )


def _string_list(value: object, field: str) -> tuple[str, ...]:
    if not isinstance(value, list) or not value or any(
        not isinstance(item, str) or not item.strip() for item in value
    ):
        raise ValueError(f"control {field} must be a non-empty list of strings")
    return tuple(value)


def _is_local_target(target: str) -> bool:
    parsed = urlsplit(target)
    return (
        target.startswith("/")
        and not target.startswith("//")
        and not parsed.scheme
        and not parsed.netloc
    )