| 1 | """The public skill never names the hosted service's private repository or domain. |
| 2 | |
| 3 | The skill talks to the hosted service only through a user-supplied |
| 4 | LAST30DAYS_API_BASE, and calls it "the hosted service". The forbidden names are |
| 5 | compared by SHA-256 so this guard does not spell them out itself. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import hashlib |
| 11 | import os |
| 12 | import re |
| 13 | from pathlib import Path |
| 14 | |
| 15 | ROOT = Path(__file__).resolve().parents[1] |
| 16 | |
| 17 | _FORBIDDEN_SHA256 = frozenset({ |
| 18 | "a8f1e61dfeb30b0abc99ed7da20a055442cf4a679d99063016a96800495a5540", |
| 19 | "bc413c626e226d752af2ca2e2c0ce1bc7fe79863850184a9a5527f40b1900060", |
| 20 | "a5191e7f997452922618c7dc14d84bd926e3a92ddddbf94e8eaffdeb73702734", |
| 21 | }) |
| 22 | _CANDIDATE = re.compile(r"last30days[-._][a-z0-9]+", re.IGNORECASE) |
| 23 | _SKIP_DIRS = {".git", ".venv", "node_modules", "__pycache__", ".pytest_cache", "vendored", "dist", "build"} |
| 24 | _MAX_BYTES = 4 * 1024 * 1024 |
| 25 | |
| 26 | |
| 27 | def _text_files(): |
| 28 | for dirpath, dirnames, filenames in os.walk(ROOT): |
| 29 | dirnames[:] = [name for name in dirnames if name not in _SKIP_DIRS] |
| 30 | for filename in filenames: |
| 31 | path = Path(dirpath) / filename |
| 32 | try: |
| 33 | if path.stat().st_size > _MAX_BYTES: |
| 34 | continue |
| 35 | yield path, path.read_text(encoding="utf-8") |
| 36 | except (OSError, UnicodeDecodeError): |
| 37 | continue |
| 38 | |
| 39 | |
| 40 | def test_no_file_names_the_private_hosted_repo_or_domain(): |
| 41 | hits = [] |
| 42 | for path, text in _text_files(): |
| 43 | for match in _CANDIDATE.finditer(text): |
| 44 | digest = hashlib.sha256(match.group(0).lower().encode("utf-8")).hexdigest() |
| 45 | if digest in _FORBIDDEN_SHA256: |
| 46 | line = text.count("\n", 0, match.start()) + 1 |
| 47 | hits.append(f"{path.relative_to(ROOT)}:{line}") |
| 48 | assert not hits, "private hosted-service name found (say 'hosted service'): " + ", ".join(hits) |
| 49 |