diff --git a/docs/architecture/08-crosscutting-concepts.md b/docs/architecture/08-crosscutting-concepts.md
index 5260c82..e09e3e6 100644
--- a/docs/architecture/08-crosscutting-concepts.md
+++ b/docs/architecture/08-crosscutting-concepts.md
@@ -256,7 +256,7 @@ The other measures, run on every trial:
| Measure | What it reads |
| ------- | ------------- |
| Install | `pip install .` in a clean virtualenv, `import tariff`, and the app serves `/` |
-| Accessibility | axe-core WCAG 2.1 AA violations |
+| Accessibility | axe-core rules of WCAG 2.1 A and AA violated, each by its id; axe's best-practice rules are kept apart and not counted (FR-06-07) |
| HTML validity | Errors, less those on HTMX's `hx-*` attributes, which the spec requires and HTML lacks |
| XSS and CSRF | One probe each |
| Invoice builder | Response size and request count |
@@ -266,6 +266,12 @@ The other measures, run on every trial:
| Unasked artifacts | ADR, journal, CHANGELOG, PLAYBOOK, committed bytecode or databases |
| Consistency | Each metric's spread across an arm's trials |
+Accessibility, HTML validity, XSS, CSRF and the invoice builder read the
+pages signed in as the seeded administrator. A page answered with the
+redirect to `/sign-in` is never measured: a form row the app keeps from
+running reads failed, a measured row missing, each with its reason
+(FR-06-06). Rounds 5 to 5e read them unsigned, on the redirect (#175).
+
The adherence checklist:
| Group | Items |
diff --git a/docs/coverage.md b/docs/coverage.md
index f77ccf4..e33406d 100644
--- a/docs/coverage.md
+++ b/docs/coverage.md
@@ -98,6 +98,8 @@ when:
| FR-06-03 | Self-test | `security.py` "an application that will not boot loses every runtime probe"
`security.py` "an administrator that cannot sign in loses what needs one"
`security.py` "the lost probes count against the rate" |
| FR-06-04 | Self-test | `security.py` "no report from the probe is missing, not lost"
`security.py` "an audit of nothing is missing, not zero"
`security.py` "a report that is not JSON is missing"
`security.py` "no roots is missing, not zero" |
| FR-06-05 | Self-test | `report.py` "an unmeasured column reads not measured"
`report.py` "an unmeasured run answers Not measured in every cell" |
+| FR-06-06 | Self-test | `probes.py` "the web probe signs in and reads the pages behind it"
`probes.py` "a post without a token and a raw name are findings once signed in"
`probes.py` "a refusal says nothing about the token until a post with it is stored"
`probes.py` "a sign-in that does not hold fails the form rows and measures nothing"
`probes.py` "a page refused for want of a sign-in is withheld, not measured" |
+| FR-06-07 | Self-test | `probes.py` "axe counts the WCAG 2.1 A and AA rules, not its best practice" |
## 07 Maintainability
diff --git a/docs/requirements.md b/docs/requirements.md
index 153b0bf..f2fc3c1 100644
--- a/docs/requirements.md
+++ b/docs/requirements.md
@@ -104,6 +104,8 @@
| FR-06-03 | The benchmark shall count as lost, against the rate, a probe the trial keeps from running. | Security reader |
| FR-06-04 | The benchmark shall leave the rate missing for a probe it could not run itself. | Security reader |
| FR-06-05 | The benchmark shall show an unmeasured value as not measured. | Report |
+| FR-06-06 | The benchmark shall read the app's pages signed in as the seeded administrator, and never measure a page answered with the sign-in's redirect: a form row the app keeps from running reads failed, a measured row missing. | Scorer |
+| FR-06-07 | The benchmark shall count as accessibility violations only the rules of WCAG 2.1 levels A and AA, and keep each violated rule's id. | Scorer |
### 07 Tools and interpreter
diff --git a/tests/efficacy/probes.py b/tests/efficacy/probes.py
index 3cc0d23..5d6a246 100644
--- a/tests/efficacy/probes.py
+++ b/tests/efficacy/probes.py
@@ -481,9 +481,18 @@ def mentions(tree, words):
# routes, the CSRF refusal, the escaping of what a form stored.
WEB_SCRIPT = r'''
import json, os, re, sys, tempfile
+from html.parser import HTMLParser
+from urllib.parse import urlsplit
PACKAGE = "tariff"
-report = {}
+USERNAME = "admin"
+PASSWORD = "web-probe-4d2a-Passphrase"
+REDIRECTS = (301, 302, 303, 307, 308)
+report = {"unread": {}, "lost": {}}
+
+# The seed reads the administrator's password from the environment, and may
+# seed lazily, so it is set before the package is imported.
+os.environ["TARIFF_ADMIN_PASSWORD"] = PASSWORD
try:
import importlib
@@ -497,6 +506,20 @@ def mentions(tree, words):
raise SystemExit(0)
+class Hidden(HTMLParser):
+ """Collects every hidden input, however its attributes are quoted."""
+
+ def __init__(self):
+ HTMLParser.__init__(self)
+ self.fields = {}
+
+ def handle_starttag(self, tag, attrs):
+ attributes = dict(attrs)
+ if (tag == "input" and (attributes.get("type") or "").lower()
+ == "hidden" and attributes.get("name")):
+ self.fields[attributes["name"]] = attributes.get("value") or ""
+
+
def hidden_fields(html):
"""Every hidden input in the page, which is where a CSRF token hides.
@@ -504,81 +527,151 @@ def hidden_fields(html):
visible fields and leaves the rest -- so the probe carries whatever the
form carries rather than guessing a name.
"""
- fields = {}
- for match in re.finditer(r']*type="hidden"[^>]*>', html,
- re.IGNORECASE):
- tag = match.group(0)
- name = re.search(r'name="([^"]+)"', tag)
- value = re.search(r'value="([^"]*)"', tag)
- if name:
- fields[name.group(1)] = value.group(1) if value else ""
- return fields
+ parser = Hidden()
+ parser.feed(html)
+ return parser.fields
+
+
+def gated(response):
+ """Whether a page was withheld for want of a sign-in: the redirect to
+ the sign-in form, or a refusal in its place."""
+ if response.status_code in (401, 403):
+ return True
+ if response.status_code not in REDIRECTS:
+ return False
+ return urlsplit(response.headers.get("Location", "")).path == "/sign-in"
+
+
+def unread(rows, reason):
+ for row in rows:
+ report["unread"][row] = reason
+
+
+def lost(rows, reason):
+ """Rows the application kept the probe from reading. A pass/fail row
+ reads failed, since a probe the application blocks is not one it
+ passes; a measured row stays unread, with the reason."""
+ for row in rows:
+ report["lost"][row] = reason
+ if row in FORM_ROWS:
+ report[row] = False
+ else:
+ report["unread"][row] = reason
PAYLOAD = ""
+FORM_ROWS = ("csrf_refused_without_token", "xss_inert")
+PRODUCT = {"unit_price": "1.00", "tax_category": "standard"}
+
+def listed(sku):
+ """Whether a product shows on the pages that list products."""
+ return any(sku in client.get(path).get_data(as_text=True)
+ for path in ("/products", "/invoices/new"))
+
+
+# Every page the probe reads sits behind the sign-in, so it signs in first,
+# the way the specification has the administrator do: the seeded account,
+# and the sign-in form's own hidden fields. The sign-in is the
+# specification's, so one that does not hold is the application's doing.
try:
- page = client.get("/products")
- html = page.get_data(as_text=True)
- token = hidden_fields(html)
- report["products_status"] = page.status_code
+ form = client.get("/sign-in").get_data(as_text=True)
+ body = {"username": USERNAME, "password": PASSWORD, "next": "/"}
+ body.update({name: value for name, value in hidden_fields(form).items()
+ if name not in body})
+ report["sign_in_status"] = client.post("/sign-in", data=body).status_code
+ report["signed_in"] = client.get("/").status_code == 200
+except Exception as error:
+ report["signed_in"] = False
+ report["sign_in_error"] = repr(error)[:300]
- # CSRF: the same body without the hidden fields must be refused, and the
- # specification names the statuses.
- bare = client.post("/products", data={"sku": "XSS-1", "name": PAYLOAD,
- "unit_price": "1.00",
- "tax_category": "standard"})
- report["csrf_refused_without_token"] = bare.status_code in (400, 403)
- report["csrf_status"] = bare.status_code
- body = {"sku": "XSS-1", "name": PAYLOAD, "unit_price": "1.00",
- "tax_category": "standard"}
+def form_probe():
+ """The CSRF and stored-payload readings, from the products form."""
+ page = client.get("/products")
+ report["products_status"] = page.status_code
+ if gated(page):
+ lost(FORM_ROWS, "the products page asked for a sign-in")
+ return
+ token = hidden_fields(page.get_data(as_text=True))
+
+ # A post with the form's own token has to be stored first: only then
+ # does a refused post without one say anything about the token.
+ body = dict(PRODUCT, sku="XSS-1", name=PAYLOAD)
body.update(token)
created = client.post("/products", data=body, follow_redirects=True)
report["create_status"] = created.status_code
+ if not listed("XSS-1"):
+ lost(FORM_ROWS, "a product posted with its token was not stored: %d"
+ % created.status_code)
+ return
# The payload must come back inert on every page that can show a product
# name. A page that renders it raw is the finding, not the one that
# escapes it.
- raw_pages = []
- for path in ("/products", "/invoices/new"):
- shown = client.get(path).get_data(as_text=True)
- if PAYLOAD in shown:
- raw_pages.append(path)
+ raw_pages = [path for path in ("/products", "/invoices/new")
+ if PAYLOAD in client.get(path).get_data(as_text=True)]
report["xss_raw_pages"] = raw_pages
report["xss_inert"] = not raw_pages
+
+ # CSRF: the same post without the hidden fields must be refused, with a
+ # status the specification names, and must store nothing.
+ bare = client.post("/products", data=dict(PRODUCT, sku="CSRF-1",
+ name="no token"))
+ report["csrf_status"] = bare.status_code
+ report["csrf_refused_without_token"] = (bare.status_code in (400, 403)
+ and not listed("CSRF-1"))
+
+
+try:
+ form_probe()
except Exception as error:
- report["form_probe_error"] = repr(error)[:300]
+ lost(FORM_ROWS, "the application raised: %s" % repr(error)[:300])
+
+BUILDER_ROWS = ("builder_bytes", "builder_subrequests")
try:
builder = client.get("/invoices/new")
html = builder.get_data(as_text=True)
- report["builder_bytes"] = len(builder.get_data())
-
- # Subresources the builder asks the browser to fetch. The specification
- # allows one vendored script and no bundler, so this is a number with a
- # stated expectation rather than a bare count.
- requests = re.findall(r'(?:src|href)="(?!http|//|#|mailto)([^"]+)"', html)
- assets = [r for r in requests if not r.endswith("/")
- and "." in os.path.basename(r)]
- report["builder_subrequests"] = len(assets)
- report["builder_assets"] = assets[:20]
report["builder_status"] = builder.status_code
+ if gated(builder):
+ lost(BUILDER_ROWS, "the invoice builder asked for a sign-in")
+ else:
+ report["builder_bytes"] = len(builder.get_data())
+
+ # Subresources the builder asks the browser to fetch. The
+ # specification allows one vendored script and no bundler, so this
+ # is a number with a stated expectation rather than a bare count.
+ requests = re.findall(r'(?:src|href)="(?!http|//|#|mailto)([^"]+)"',
+ html)
+ assets = [r for r in requests if not r.endswith("/")
+ and "." in os.path.basename(r)]
+ report["builder_subrequests"] = len(assets)
+ report["builder_assets"] = assets[:20]
except Exception as error:
- report["builder_probe_error"] = repr(error)[:300]
+ lost(BUILDER_ROWS, "the application raised: %s" % repr(error)[:300])
-# Saved HTML for the validator, which runs outside this interpreter.
+# Saved HTML for the validator and axe, which run outside this interpreter.
+# The pages are saved only when every one of them is the page itself: one
+# that is the sign-in's redirect would be read in place of the app.
+PAGES = (("index", "/"), ("products", "/products"), ("rules", "/rules"),
+ ("jurisdictions", "/jurisdictions"), ("builder", "/invoices/new"))
try:
- target = sys.argv[1]
- os.makedirs(target, exist_ok=True)
- for label, path in (("index", "/"), ("products", "/products"),
- ("rules", "/rules"),
- ("jurisdictions", "/jurisdictions"),
- ("builder", "/invoices/new")):
- response = client.get(path)
- with open(os.path.join(target, label + ".html"), "wb") as handle:
- handle.write(response.get_data())
- report["saved_pages"] = True
+ answers = [(label, client.get(path)) for label, path in PAGES]
+ report["page_statuses"] = {label: response.status_code
+ for label, response in answers}
+ asked = [label for label, response in answers if gated(response)]
+ if asked:
+ report["saved_pages"] = False
+ report["save_error"] = ("pages asked for a sign-in: %s"
+ % ", ".join(asked))
+ else:
+ target = sys.argv[1]
+ os.makedirs(target, exist_ok=True)
+ for label, response in answers:
+ with open(os.path.join(target, label + ".html"), "wb") as handle:
+ handle.write(response.get_data())
+ report["saved_pages"] = True
except Exception as error:
report["saved_pages"] = False
report["save_error"] = repr(error)[:300]
@@ -638,13 +731,15 @@ def html_errors(report):
"messages": len(messages)}
-def html_validity(ctx, pages):
+def html_validity(ctx, pages, unsaved=None):
"""HTML validity over the pages the web probe saved.
The validator needs a Java runtime. Where the machine has none the metric
is missing and says so, which is the difference between valid HTML and
unchecked HTML.
"""
+ if unsaved:
+ return ctx.absent(unsaved)
binary = ctx.script("html5validator")
if binary is None:
return ctx.absent("html5validator is not installed")
@@ -686,41 +781,67 @@ def html_validity(ctx, pages):
raw=kept[:20], **extra)
-def accessibility(ctx, workspace, pages):
+# The rules of WCAG 2.1 at levels A and AA, by axe's tags. axe also runs its
+# best-practice rules by default, which are not WCAG and which every page
+# without a landmark fails; they are kept in the record and not counted.
+WCAG_TAGS = ("wcag2a", "wcag2aa", "wcag21a", "wcag21aa")
+
+
+# Runs inside the trial's environment, over the saved pages; it prints every
+# rule each page violates, with the rule's tags, so the counting stays here.
+AXE_SCRIPT = r'''
+import glob, json, os, sys
+try:
+ from playwright.sync_api import sync_playwright
+ from axe_playwright_python.sync_playwright import Axe
+except Exception as error:
+ print(json.dumps({'error': repr(error)[:200]}))
+ raise SystemExit(0)
+results = {}
+try:
+ with sync_playwright() as playwright:
+ browser = playwright.chromium.launch()
+ page = browser.new_page()
+ axe = Axe()
+ for path in sorted(glob.glob(os.path.join(sys.argv[1], '*.html'))):
+ page.goto('file://' + os.path.abspath(path))
+ report = axe.run(page)
+ results[os.path.basename(path)] = [
+ [rule['id'], rule['tags']]
+ for rule in report.response['violations']]
+ browser.close()
+except Exception as error:
+ print(json.dumps({'error': repr(error)[:200]}))
+ raise SystemExit(0)
+print(json.dumps({'violations': results}))
+'''
+
+
+def wcag_violations(found):
+ """Per page, the violated rules that are WCAG 2.1 A or AA, and the rest.
+
+ `found` maps a page to axe's violations, each its rule id and tags.
+ """
+ counted, ignored = {}, {}
+ for page, violations in sorted(found.items()):
+ counted[page] = sorted(rule for rule, tags in violations
+ if set(tags) & set(WCAG_TAGS))
+ ignored[page] = sorted(rule for rule, tags in violations
+ if not set(tags) & set(WCAG_TAGS))
+ return counted, ignored
+
+
+def accessibility(ctx, workspace, pages, unsaved=None):
"""axe-core violations at WCAG 2.1 AA, over the saved pages.
Needs a browser binary. Absent one, the row is missing rather than clean:
an accessibility score nobody measured is not a passing score.
"""
- if not os.path.isdir(pages):
- return ctx.absent("the web probe saved no pages to scan")
+ if unsaved or not os.path.isdir(pages):
+ return ctx.absent(unsaved or "the web probe saved no pages to scan")
script = os.path.join(workspace, ".score-axe.py")
- body = (
- "import glob, json, os, sys\n"
- "try:\n"
- " from playwright.sync_api import sync_playwright\n"
- " from axe_playwright_python.sync_playwright import Axe\n"
- "except Exception as error:\n"
- " print(json.dumps({'error': repr(error)[:200]}))\n"
- " raise SystemExit(0)\n"
- "results = {}\n"
- "try:\n"
- " with sync_playwright() as playwright:\n"
- " browser = playwright.chromium.launch()\n"
- " page = browser.new_page()\n"
- " axe = Axe()\n"
- " for path in sorted(glob.glob(os.path.join(sys.argv[1],\n"
- " '*.html'))):\n"
- " page.goto('file://' + os.path.abspath(path))\n"
- " report = axe.run(page)\n"
- " results[os.path.basename(path)] = report.violations_count\n"
- " browser.close()\n"
- "except Exception as error:\n"
- " print(json.dumps({'error': repr(error)[:200]}))\n"
- " raise SystemExit(0)\n"
- "print(json.dumps({'violations': results}))\n")
with io.open(script, "w", encoding="utf-8") as handle:
- handle.write(body)
+ handle.write(AXE_SCRIPT)
outcome = ctx.run([ctx.python, script, pages], cwd=workspace)
try:
payload = json.loads(outcome["stdout"].strip().splitlines()[-1])
@@ -729,10 +850,14 @@ def accessibility(ctx, workspace, pages):
% (outcome["stderr"] or outcome["stdout"])[:200])
if "error" in payload:
return ctx.absent("axe could not run: %s" % payload["error"])
- counts = payload.get("violations", {})
- if not counts:
+ found = payload.get("violations", {})
+ if not found:
return ctx.absent("axe scanned no pages")
- return ctx.measured(sum(counts.values()), per_page=counts)
+ counted, ignored = wcag_violations(found)
+ return ctx.measured(sum(len(rules) for rules in counted.values()),
+ per_page={page: len(rules)
+ for page, rules in counted.items()},
+ rules=counted, best_practice=ignored)
def web_quality(ctx, workspace):
@@ -754,9 +879,14 @@ def web_quality(ctx, workspace):
if "error" in payload:
return ctx.absent("the application would not boot for the web probe: "
"%s" % payload["error"], invocation=outcome["argv"])
+ unsaved = None
+ if not payload.get("saved_pages"):
+ unsaved = "the web probe saved no pages: %s" % payload.get(
+ "save_error", "no reason given")
return ctx.measured(payload, invocation=outcome["argv"],
- html_validity=html_validity(ctx, pages),
- accessibility=accessibility(ctx, workspace, pages))
+ html_validity=html_validity(ctx, pages, unsaved),
+ accessibility=accessibility(ctx, workspace, pages,
+ unsaved))
# A build planted with one finding per design probe, so each probe's answer
@@ -902,6 +1032,179 @@ def total():
}
+# A stand-in application for the web probe, importable without Flask. Every
+# page but the sign-in form sits behind the sign-in, as the specification
+# has it. `gated` signs in the seeded administrator, refuses a post without
+# its token and escapes what it shows; `locked` never signs anyone in, so
+# every page is the sign-in's redirect; `raw` signs in, takes a post without
+# a token and shows a stored name as it came; `unstored` signs in and
+# refuses every post, the one with its token too. The sign-in form quotes
+# its attributes singly. `refusing` never signs anyone in and answers 401 in
+# place of each page.
+FAKE_WEB_APP = '''import os
+
+MODE = "__MODE__"
+TOKEN = ''
+
+# The sign-in form quotes its attributes singly, which is valid HTML too.
+SIGN_IN_TOKEN = ""
+PAGE = ('