GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(beacon): redact path credentials and live-check relay output · CortexLM/hypertrain@811c23c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/hypertrain/beacon
    • __main__.py
    • push.py
  • tests/beacon
    • test_push.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/hypertrain/beacon/__main__.py‎

Copy file name to clipboardExpand all lines: src/hypertrain/beacon/__main__.py
+8-5Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from hypertrain.beacon.core import BeaconError, BeaconUnavailable
1111
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
1313

1414

1515
def main(argv: list[str] | None = None) -> int:
@@ -32,18 +32,21 @@ def main(argv: list[str] | None = None) -> int:
3232

3333
relays = tuple(args.relay or RELAYS)
3434
beacon = DrandQuicknet(relays)
35+
shown = [redact_urls(r) for r in relays]
3536
try:
3637
info_ok = beacon.info_matches()
3738
br = beacon.latest()
3839
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+
)
4043
return 3
4144
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))}))
4346
return 1
4447
out = {
4548
"live": "PASS" if info_ok else "FAIL",
46-
"relays": relays,
49+
"relays": shown,
4750
"chain_info_matches_pinned": info_ok,
4851
"round": br.round,
4952
"randomness": br.randomness,
@@ -58,7 +61,7 @@ def _http_url(p: argparse.ArgumentParser, flag: str, value: str) -> httpx.URL:
5861
try:
5962
url = httpx.URL(value)
6063
except httpx.InvalidURL as e:
61-
p.error(f"{flag}: {e}")
64+
p.error(f"{flag}: {redact_urls(str(e))}")
6265
if url.scheme not in ("http", "https") or not url.host:
6366
p.error(f"{flag} must be an http(s) URL")
6467
return url
Collapse file

‎src/hypertrain/beacon/push.py‎

Copy file name to clipboardExpand all lines: src/hypertrain/beacon/push.py
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
# Userinfo runs to the last `@` before the path: a password may hold quotes or `@`.
3333
_USERINFO = re.compile(r"(\w[\w+.-]*://)[^/\s]*@")
3434
_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_-])")
3537
# A rejected (429) or cut-off (408) POST was not applied, so replaying it is safe.
3638
_RETRY = frozenset({408, 429})
3739

3840

3941
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)))
4143

4244

4345
class ServiceError(Exception):
Collapse file

‎tests/beacon/test_push.py‎

Copy file name to clipboardExpand all lines: tests/beacon/test_push.py
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,36 @@ def test_failure_message_redacts_relay_credentials(wire: Any, cli: Any) -> None:
277277
assert (rc, out) == (1, "")
278278
assert "failure 1/" in err and "***@relay.example/x" in err
279279
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+
relay = f"https://u:[email protected]/{PATH_SECRET}?apikey=SECRETKEY"
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

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.