test_open_firmware_lesson.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # SPDX-License-Identifier: Apache-2.0
  2. """The open firmware lesson: registered, dated, honest about what was run, and read-only.
  3. docs/wiki/Open-Firmware-and-Trustworthy-Hardware-...-Disabled.md reports what four hardware vendors claim about
  4. the Intel Management Engine. A claim with no date or status, a vendor named in the table but missing from the
  5. vendor section, a flashing command, an affiliate link, a real address or a stale registration would each teach
  6. the wrong thing.
  7. """
  8. import re
  9. import textwrap
  10. from pathlib import Path
  11. ROOT = Path(__file__).resolve().parent.parent
  12. WIKI = ROOT / "docs" / "wiki"
  13. NAME = "Open-Firmware-and-Trustworthy-Hardware-coreboot-Heads-Nitrokey-and-Vendors-That-Ship-the-Intel-ME-Disabled"
  14. PAGE = WIKI / f"{NAME}.md"
  15. FENCE = re.compile(r"^[ ]*```(\w*)\n(.*?)^[ ]*```$", re.MULTILINE | re.DOTALL)
  16. LINK = re.compile(r"\]\(([A-Za-z0-9_-]+)\.md(?:#[^)]*)?\)")
  17. ADDR = re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b")
  18. STATUS_WORDS = ("Read", "Observed", "General knowledge", "**est.**", "**not verified**")
  19. VENDORS = ("Novacustom", "System76", "Purism", "Tuxedo")
  20. WRITE_TOKENS = re.compile(r"\bsudo\b|\bflashrom\b|\bifdtool\b|\bme_cleaner\b|\bnvramtool\b|\bdd\b|\bchmod\b|>\s*/(?:sys|dev|boot)", re.I)
  21. AFFILIATE = re.compile(r"[?&](ref|aff|affiliate|utm_[a-z]+|tag|partner)=", re.I)
  22. def text() -> str:
  23. return PAGE.read_text(encoding="utf-8")
  24. def blocks(tag: str) -> list[str]:
  25. return [textwrap.dedent(b) for t, b in FENCE.findall(text()) if t == tag]
  26. def register_rows(body: str) -> list[list[str]]:
  27. reg = body.split("## Claims register")[1].split("\n## ")[0]
  28. rows = [line for line in reg.splitlines() if line.startswith("|") and not line.startswith("|---")]
  29. return [[c.strip() for c in r.strip("|").split("|")] for r in rows[1:]]
  30. def rows_missing_status(body: str) -> list[str]:
  31. return [r[0] for r in register_rows(body) if not any(w in r[-1] for w in STATUS_WORDS)]
  32. def unsafe_lines(commands: str) -> list[str]:
  33. return [l for l in commands.splitlines() if WRITE_TOKENS.search(l)]
  34. def vendor_section(body: str, vendor: str) -> str:
  35. start = body.index(f"### {vendor}")
  36. first_line_end = body.index("\n", start) + 1
  37. rest = body[first_line_end:]
  38. nxt = re.search(r"^#{2,3} ", rest, re.MULTILINE)
  39. return body[start : first_line_end + (nxt.start() if nxt else len(rest))]
  40. def test_the_page_exists_and_says_what_was_and_was_not_run():
  41. body = text()
  42. assert body.startswith("# Open firmware and trustworthy hardware")
  43. assert "> **Status: unverified draft." in body
  44. assert "Nothing was flashed" in body
  45. assert "**Not run:**" in body and "2026-11-07" in body
  46. assert "Not verified, plainly" in body
  47. def test_every_linked_wiki_page_exists():
  48. links = set(LINK.findall(text()))
  49. assert len(links) >= 5, "the guard collected too few links"
  50. missing = sorted(t for t in links if not (WIKI / f"{t}.md").exists())
  51. assert not missing, missing
  52. def test_the_lesson_is_registered_everywhere():
  53. nav = (WIKI / "_nav.yaml").read_text(encoding="utf-8")
  54. entry = nav.split(f"page: {NAME},")[1].split("\n")[0]
  55. assert 'status: "draft"' in entry
  56. for special in ("Contents.md", "_Sidebar.md", "Reading-Order.md"):
  57. assert f"{NAME}.md" in (WIKI / special).read_text(encoding="utf-8"), special
  58. rows = [l for l in (WIKI / "Glossary.md").read_text(encoding="utf-8").splitlines() if f"{NAME}.md" in l]
  59. assert len(rows) >= 6, "expected glossary rows for the new terms"
  60. def test_glossary_rows_point_at_real_headings():
  61. heads = {re.sub(r"[^a-z0-9 -]", "", h.lower()).strip().replace(" ", "-") for h in re.findall(r"^#+ (.+)$", text(), re.MULTILINE)}
  62. anchors = set()
  63. for line in (WIKI / "Glossary.md").read_text(encoding="utf-8").splitlines():
  64. anchors.update(re.findall(rf"{NAME}\.md#([a-z0-9-]+)", line))
  65. assert anchors, "the guard collected nothing"
  66. assert anchors <= heads, sorted(anchors - heads)
  67. def test_no_em_dashes_no_addresses_and_no_affiliate_links():
  68. body = text()
  69. assert "—" not in body and "–" not in body
  70. assert not ADDR.findall(body), ADDR.findall(body)
  71. urls = re.findall(r"\]\((https?://[^)]+)\)", body)
  72. assert len(urls) >= 20, "the guard collected too few links"
  73. assert not [u for u in urls if AFFILIATE.search(u)]
  74. def test_the_guards_bite():
  75. assert ADDR.findall("host 192.0.2.7")
  76. assert AFFILIATE.search("https://example.com/p?ref=abc")
  77. assert not AFFILIATE.search("https://example.com/p")
  78. def test_there_are_no_flashing_or_write_commands_in_any_code_block():
  79. cmds = "\n".join(b for _, b in FENCE.findall(text()))
  80. assert "curl" in cmds and "fwupdmgr" in cmds, "the guard collected nothing"
  81. assert not unsafe_lines(cmds)
  82. def test_the_write_guard_bites():
  83. assert unsafe_lines("sudo flashrom -p internal -w rom.bin")
  84. assert unsafe_lines("python me_cleaner.py -S image.bin")
  85. assert unsafe_lines("echo 1 > /sys/class/x")
  86. assert not unsafe_lines("cat /sys/class/tpm/tpm0/tpm_version_major")
  87. def test_each_vendor_has_a_section_with_a_firmware_and_an_me_line():
  88. body = text()
  89. for vendor in VENDORS:
  90. sec = vendor_section(body, vendor)
  91. assert "Firmware" in sec or "Tuxedo" in vendor, vendor
  92. assert "ME" in sec, vendor
  93. assert "I could not verify a claim" in vendor_section(body, "Tuxedo Computers")
  94. def test_the_vendor_section_helper_bites():
  95. sample = "### A\nFirmware ME\n### B\nother\n"
  96. assert "Firmware ME" in vendor_section(sample, "A")
  97. assert "other" not in vendor_section(sample, "A")
  98. def test_the_four_me_words_and_the_dasharo_modes_are_named():
  99. body = text()
  100. for word in ("Absent", "Neutralized", "Soft-disabled", "HAP-disabled"):
  101. assert f"**{word}**" in body, word
  102. for mode in ("Enabled", "Disabled (Soft)", "Disabled (HAP)"):
  103. assert mode in body, mode
  104. def test_every_vendor_page_is_dated_in_the_register():
  105. body = text()
  106. assert body.count("2026-11-07") >= 8
  107. reg = body.split("## Claims register")[1]
  108. for vendor in VENDORS:
  109. assert vendor in reg, vendor
  110. def test_every_claims_register_row_has_a_status():
  111. rows = register_rows(text())
  112. assert len(rows) >= 35, "the guard collected too few rows"
  113. assert not rows_missing_status(text())
  114. def test_the_register_guard_bites():
  115. bad = "## Claims register\n\n| Claim | Source | Status |\n|---|---|---|\n| X | y | maybe |\n"
  116. assert rows_missing_status(bad) == ["X"]
  117. def test_the_heads_key_table_matches_the_heads_prerequisites_wording():
  118. body = text()
  119. assert "Heads does NOT support FIDO2 or U2F" in body
  120. assert "OpenPGP only" in body
  121. for key in ("YubiKey 5 Series", "Nitrokey Pro 2", "Nitrokey Storage 2", "Nitrokey 3", "Librem Key"):
  122. assert key in body, key
  123. def test_the_page_ends_by_pointing_at_the_firmware_lesson_as_plain_text():
  124. body = text().split("<!-- BEGIN GENERATED PAGE NAV bottom")[0]
  125. tail = body.split("## Where this goes next")[1]
  126. assert "#465" in tail and "no link" in tail
  127. assert "](" not in tail, "the springboard must not link a page that does not exist yet"
  128. assert "#463" in text()