diff --git a/src/loopzero/mergify.py b/src/loopzero/mergify.py index 61665b00..6b7419d7 100644 --- a/src/loopzero/mergify.py +++ b/src/loopzero/mergify.py @@ -73,7 +73,11 @@ def redirect_request(self, *_args, **_kwargs): def _open(request: urllib.request.Request): - return urllib.request.build_opener(_NoRedirect).open(request, timeout=30) + try: + return urllib.request.build_opener(_NoRedirect).open(request, timeout=30) + except ValueError: + # The HTTP client can include the Authorization value in a header error. + raise MergifyError("Mergify API request has an invalid URL or header") from None def api_get(repo: str, suffix: str) -> object | None: diff --git a/tests/test_mergify.py b/tests/test_mergify.py index ff3b70e0..88ce09f8 100644 --- a/tests/test_mergify.py +++ b/tests/test_mergify.py @@ -1,10 +1,12 @@ from __future__ import annotations import base64 +import http.client import io import json import subprocess import sys +import traceback import urllib.error import urllib.request @@ -17,6 +19,26 @@ def response(payload: object) -> io.BytesIO: return io.BytesIO(json.dumps(payload).encode()) + +@pytest.mark.parametrize(("operation", "args"), [ + (mergify.api_get, ("acme/widgets", "/merge-queue/status")), + (mergify.dequeue, ("acme/widgets", 7)), +]) +def test_malformed_authorization_is_not_logged(monkeypatch, operation, args): + fake_key = "synthetic-mergify-key" + monkeypatch.setenv("MERGIFY_API_KEY", fake_key + "\nsecond-line") + monkeypatch.setattr(urllib.request, "getproxies", dict) + monkeypatch.setattr(mergify, "_retry_sleep", lambda _delay: None) + + def reject_network(_connection): + pytest.fail("invalid Authorization header reached the network") + + monkeypatch.setattr(http.client.HTTPSConnection, "connect", reject_network) + with pytest.raises(mergify.MergifyError, match="invalid URL or header") as failure: + operation(*args) + assert fake_key not in "".join(traceback.format_exception(failure.value)) + + def test_membership_uses_bearer_and_validates_queue(monkeypatch): monkeypatch.setenv("MERGIFY_API_KEY", "secret") seen = []