GeezerVison is showing github.com — fix(beacon): redact path credentials and live-check relay output · CortexLM/hypertrain@811c23c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/hypertrain/beacon/__main__.py +8-5Lines changed: 8 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 | from hypertrain.beacon.core import BeaconError, BeaconUnavailable | 11 | 11 | from hypertrain.beacon.drand import RELAYS, DrandQuicknet | 12 | | -from hypertrain.beacon.push import Pusher, read_token | | 12 | +from hypertrain.beacon.push import Pusher, read_token, redact_urls | 13 | 13 |
| 14 | 14 |
| 15 | 15 | def main(argv: list[str] | None = None) -> int: | @@ -32,18 +32,21 @@ def main(argv: list[str] | None = None) -> int: | 32 | 32 |
| 33 | 33 | relays = tuple(args.relay or RELAYS) | 34 | 34 | beacon = DrandQuicknet(relays) | | 35 | + shown = [redact_urls(r) for r in relays] | 35 | 36 | try: | 36 | 37 | info_ok = beacon.info_matches() | 37 | 38 | br = beacon.latest() | 38 | 39 | except BeaconUnavailable as e: | 39 | | - print(json.dumps({"live": "CENSORED(network)", "relays": relays, "error": str(e)})) | | 40 | + print( | | 41 | + json.dumps({"live": "CENSORED(network)", "relays": shown, "error": redact_urls(str(e))}) | | 42 | + ) | 40 | 43 | return 3 | 41 | 44 | except BeaconError as e: | 42 | | - print(json.dumps({"live": "FAIL", "relays": relays, "error": str(e)})) | | 45 | + print(json.dumps({"live": "FAIL", "relays": shown, "error": redact_urls(str(e))})) | 43 | 46 | return 1 | 44 | 47 | out = { | 45 | 48 | "live": "PASS" if info_ok else "FAIL", | 46 | | - "relays": relays, | | 49 | + "relays": shown, | 47 | 50 | "chain_info_matches_pinned": info_ok, | 48 | 51 | "round": br.round, | 49 | 52 | "randomness": br.randomness, | @@ -58,7 +61,7 @@ def _http_url(p: argparse.ArgumentParser, flag: str, value: str) -> httpx.URL: | 58 | 61 | try: | 59 | 62 | url = httpx.URL(value) | 60 | 63 | except httpx.InvalidURL as e: | 61 | | - p.error(f"{flag}: {e}") | | 64 | + p.error(f"{flag}: {redact_urls(str(e))}") | 62 | 65 | if url.scheme not in ("http", "https") or not url.host: | 63 | 66 | p.error(f"{flag} must be an http(s) URL") | 64 | 67 | return url | |
Collapse file Copy file name to clipboardExpand all lines: src/hypertrain/beacon/push.py +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 32 | 32 | # Userinfo runs to the last `@` before the path: a password may hold quotes or `@`. | 33 | 33 | _USERINFO = re.compile(r"(\w[\w+.-]*://)[^/\s]*@") | 34 | 34 | _QUERY = re.compile(r"[?#][^\s'\"]*") | | 35 | +# A path segment (not a `//host`) of >= 24 token chars is taken for a credential (`/key/<apikey>`). | | 36 | +_PATH_TOKEN = re.compile(r"(?<=[^/]/)[A-Za-z0-9_-]{24,}(?![A-Za-z0-9_-])") | 35 | 37 | # A rejected (429) or cut-off (408) POST was not applied, so replaying it is safe. | 36 | 38 | _RETRY = frozenset({408, 429}) | 37 | 39 |
| 38 | 40 |
| 39 | 41 | def redact_urls(text: str) -> str: | 40 | | - return _QUERY.sub("", _USERINFO.sub(r"\1***@", text)) | | 42 | + return _PATH_TOKEN.sub("***", _QUERY.sub("", _USERINFO.sub(r"\1***@", text))) | 41 | 43 |
| 42 | 44 |
| 43 | 45 | class ServiceError(Exception): | |
Collapse file Copy file name to clipboardExpand all lines: tests/beacon/test_push.py +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -277,3 +277,36 @@ def test_failure_message_redacts_relay_credentials(wire: Any, cli: Any) -> None: | 277 | 277 | assert (rc, out) == (1, "") | 278 | 278 | assert "failure 1/" in err and "***@relay.example/x" in err | 279 | 279 | assert "SSW" not in err and "SECRETKEY" not in err and "SECFRAG" not in err | | 280 | + | | 281 | + | | 282 | +PATH_SECRET = "sk_" + "A1b2C3d4E5f6G7h8I9j0K1l2" | | 283 | + | | 284 | + | | 285 | +def test_failure_message_redacts_relay_path_credential(wire: Any, cli: Any) -> None: | | 286 | + relay = f"https://relay.example/key/{PATH_SECRET}" | | 287 | + wire(Service(), relay_client({"relay.example": 503, "b.test": V0})) | | 288 | + rc, out, err = cli("--once", relays=["--relay", relay, "--relay", "https://b.test"]) | | 289 | + assert (rc, out) == (1, "") | | 290 | + assert "failure 1/" in err and "https://relay.example/key/***" in err | | 291 | + assert PATH_SECRET not in err and PATH_SECRET[:8] not in err | | 292 | + | | 293 | + | | 294 | +def test_live_check_redacts_relays_and_error(wire: Any, capsys: pytest.CaptureFixture[str]) -> None: | | 295 | + | | 296 | + wire(Service(), relay_client({"relay.example": 503, "b.test": 503})) | | 297 | + rc = beacon_cli.main(["check", "--live", "--relay", relay, "--relay", "https://b.test"]) | | 298 | + out = json.loads(capsys.readouterr().out) | | 299 | + assert (rc, out["live"]) == (3, "CENSORED(network)") | | 300 | + assert out["relays"] == ["https://***@relay.example/***", "https://b.test"] | | 301 | + assert "relay.example/***" in out["error"] and "503" in out["error"] | | 302 | + text = json.dumps(out) | | 303 | + assert PATH_SECRET not in text and "PASSW" not in text and "SECRETKEY" not in text | | 304 | + | | 305 | + | | 306 | +def test_redact_urls_keeps_short_segments_and_plain_text() -> None: | | 307 | + text = f"GET https://h.test/v1/{PATH_SECRET}/latest failed; see /{PATH_SECRET[:23]}" | | 308 | + assert push.redact_urls(text) == ( | | 309 | + f"GET https://h.test/v1/***/latest failed; see /{PATH_SECRET[:23]}" | | 310 | + ) | | 311 | + host = "https://a-relay-hostname-of-24-chars.test/x" | | 312 | + assert push.redact_urls(host) == host |
|