test_wordpress_at_scale_lab.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """Structure and rule checks for labs/wordpress-at-scale, no Docker needed.
  2. The lab is local only, so these tests guard what could make it unsafe or misleading: a
  3. published port that is not loopback, an unpinned image, a mounted Docker socket, a web tier on
  4. a network with a route out, an edge that bypasses the cache but still stores private pages,
  5. and a reviewer's-rules script that stops biting.
  6. """
  7. import importlib.util
  8. import json
  9. import re
  10. from pathlib import Path
  11. import yaml
  12. ROOT = Path(__file__).resolve().parent.parent
  13. LAB = ROOT / "labs" / "wordpress-at-scale"
  14. _spec = importlib.util.spec_from_file_location("ci_policy_check", LAB / "ci" / "ci_policy_check.py")
  15. cpc = importlib.util.module_from_spec(_spec)
  16. _spec.loader.exec_module(cpc)
  17. def compose():
  18. return yaml.safe_load((LAB / "compose.yaml").read_text())
  19. def test_published_ports_are_loopback_only():
  20. for name, svc in compose()["services"].items():
  21. for port in svc.get("ports", []):
  22. assert str(port).startswith("127.0.0.1:"), f"{name} publishes {port}"
  23. def test_images_are_pinned_and_no_docker_socket():
  24. text = (LAB / "compose.yaml").read_text()
  25. assert "docker.sock" not in text
  26. for name, svc in compose()["services"].items():
  27. image = svc["image"]
  28. assert ":" in image and not image.endswith(":latest"), f"{name}: {image}"
  29. def test_the_origin_tier_has_no_route_out_and_publishes_nothing():
  30. c = compose()
  31. assert c["networks"]["backend"]["internal"] is True
  32. for name in ("lb", "web1", "web2", "redis"):
  33. assert c["services"][name]["networks"] == ["backend"], name
  34. assert "ports" not in c["services"][name], name
  35. assert set(c["services"]["edge"]["networks"]) == {"public", "backend"}
  36. def test_the_careful_edge_pairs_bypass_with_no_cache_and_locks():
  37. text = (LAB / "edge" / "edge.conf.template").read_text()
  38. careful = text.split("listen 8080;")[1].split("listen 8081;")[0]
  39. for directive in ("proxy_cache_bypass $has_session;", "proxy_no_cache $has_session;", "proxy_cache_lock on;"):
  40. assert directive in careful, directive
  41. careless = text.split("listen 8081;")[1]
  42. assert "proxy_cache_lock" not in careless and "proxy_cache_bypass" not in careless
  43. def test_the_origin_secret_is_never_a_literal():
  44. for path in LAB.rglob("*"):
  45. if path.is_file() and path.name != ".env" and ".state" not in path.parts:
  46. assert not re.search(r"ORIGIN_SECRET\s*[=:]\s*[0-9a-f]{16,}", path.read_text(errors="ignore")), path
  47. def test_every_lab_file_carries_the_spdx_header():
  48. for path in LAB.rglob("*"):
  49. if path.is_file() and path.suffix in {".sh", ".ps1", ".py", ".yaml", ".yml", ".php", ".template"}:
  50. head = "\n".join(path.read_text().splitlines()[:4])
  51. assert "SPDX-License-Identifier: Apache-2.0" in head, path
  52. def test_the_sample_site_and_example_workflow_pass_the_reviewers_rules():
  53. assert cpc.check_workflow(LAB / "ci" / "example-wordpress-ci.yml") == []
  54. assert cpc.check_site(LAB / "site") == []
  55. def _bad_workflow(tmp_path, body):
  56. path = tmp_path / "w.yml"
  57. path.write_text(body)
  58. return cpc.check_workflow(path)
  59. def test_each_workflow_rule_bites(tmp_path):
  60. good_pin = "a" * 40
  61. base = f"name: x\npermissions: {{}}\njobs:\n p:\n environment: production\n uses: o/r/.github/workflows/w.yml@{good_pin} # v1\n"
  62. assert _bad_workflow(tmp_path, base) == []
  63. assert any("W1" in f for f in _bad_workflow(tmp_path, base.replace(good_pin, "main")))
  64. assert any("W2" in f for f in _bad_workflow(tmp_path, base.replace("permissions: {}\n", "")))
  65. assert any("W3" in f for f in _bad_workflow(tmp_path, base.replace("name: x\n", "name: x\non:\n pull_request_target:\n")))
  66. assert any("W4" in f for f in _bad_workflow(tmp_path, base + " secrets: inherit\n"))
  67. assert any("W6" in f for f in _bad_workflow(tmp_path, base.replace(" environment: production\n", "")))
  68. injected = base + " steps:\n - run: echo ${{ github.event.pull_request.title }}\n"
  69. assert any("W5" in f for f in _bad_workflow(tmp_path, injected))
  70. literal = base + " env:\n api_key: 'abcdefghijkl'\n"
  71. findings = _bad_workflow(tmp_path, literal)
  72. assert any("W7" in f for f in findings) and not any("abcdefghijkl" in f for f in findings)
  73. def test_each_site_rule_bites(tmp_path):
  74. site = tmp_path / "site"
  75. (site / "config").mkdir(parents=True)
  76. for name in ("composer.json", ".gitignore"):
  77. (site / name).write_text((LAB / "site" / name).read_text())
  78. (site / "config" / "application.php").write_text((LAB / "site" / "config" / "application.php").read_text())
  79. assert cpc.check_site(site) == []
  80. data = json.loads((site / "composer.json").read_text())
  81. data["require"]["example-vendor/example-plugin-a"] = "^2.3"
  82. (site / "composer.json").write_text(json.dumps(data))
  83. assert any("S2" in f for f in cpc.check_site(site))
  84. app = site / "config" / "application.php"
  85. app.write_text(app.read_text().replace("'DISALLOW_FILE_MODS', true", "'DISALLOW_FILE_MODS', false"))
  86. assert any("S6" in f for f in cpc.check_site(site))
  87. (site / "web" / "app" / "uploads").mkdir(parents=True)
  88. assert any("S4" in f for f in cpc.check_site(site))
  89. def test_review_findings_stay_fixed(tmp_path):
  90. pin = "a" * 40
  91. head = "name: x\npermissions: {}\njobs:\n p:\n environment: production\n"
  92. # W1 cannot be satisfied by a SHA that only appears in a comment
  93. bypass = head + f" uses: o/r/.github/workflows/w.yml@main # {pin}\n"
  94. assert any("W1" in f for f in _bad_workflow(tmp_path, bypass))
  95. # W3 sees the scalar and list forms
  96. assert any("W3" in f for f in _bad_workflow(tmp_path, "on: pull_request_target\n" + head))
  97. assert any("W3" in f for f in _bad_workflow(tmp_path, "on:\n - pull_request_target\n" + head))
  98. # W7 sees unquoted scalars and still hides the value
  99. findings = _bad_workflow(tmp_path, head + " env:\n API_KEY: abcdefghijkl\n")
  100. assert any("W7" in f for f in findings) and not any("abcdefghijkl" in f for f in findings)