From e8f7ff2bc3a6b7d7c67662ccc77952556af76341 Mon Sep 17 00:00:00 2001 From: Peter Foldes Date: Mon, 30 Nov 2026 19:32:02 -0800 Subject: [PATCH 1/2] fix: keep credentials out of websocket DEBUG logs websockets traces every handshake header at DEBUG, so any application running with DEBUG logging wrote the X-API-Key / Authorization bearer token in clear text. The DEBUG-suppressing transport logger was only attached for execution-ownership connections; attach it to every websocket connection (initial connect and reconnects alike). Refs INF-242 --- tests/test_driver.py | 71 ++++++++++++++++++++++++++++++++++++++++++ wherobots/db/driver.py | 11 +++---- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/tests/test_driver.py b/tests/test_driver.py index 0da90b8..dbe3106 100644 --- a/tests/test_driver.py +++ b/tests/test_driver.py @@ -8,6 +8,8 @@ import pytest import requests +from websockets.client import ClientProtocol +from websockets.uri import parse_uri from wherobots.db.constants import DEFAULT_CLOSE_TIMEOUT_SECONDS from wherobots.db.driver import ( @@ -581,6 +583,75 @@ def test_close_handshake_is_bounded(self, mock_ws): assert kwargs["close_timeout"] == DEFAULT_CLOSE_TIMEOUT_SECONDS +def _send_handshake_request(*, uri, additional_headers, logger, **_): + """Stand-in for websockets' connect that sends its real handshake request. + + ``ClientProtocol.send_request`` is where websockets traces each request + header at DEBUG, so this exercises the same logging the real connect does. + """ + protocol = ClientProtocol(parse_uri(uri), logger=logger) + request = protocol.connect() + request.headers.update(additional_headers) + protocol.send_request(request) + return MagicMock() + + +SECRET = "s3cr3t-credential-value" + + +class TestCredentialsStayOutOfDebugLogs: + def test_unfiltered_handshake_trace_exposes_headers(self, caplog): + caplog.set_level(logging.DEBUG) + _send_handshake_request( + uri="wss://compute.example.com/sql/org/session-id", + additional_headers={"X-API-Key": SECRET}, + logger=None, + ) + assert SECRET in caplog.text + + @pytest.mark.parametrize( + "headers", + [{"X-API-Key": SECRET}, {"Authorization": f"Bearer {SECRET}"}], + ids=["api key", "token"], + ) + @pytest.mark.parametrize("require_execution_ownership", [False, True]) + def test_connect_direct(self, caplog, headers, require_execution_ownership): + caplog.set_level(logging.DEBUG) + with ( + patch( + "wherobots.db.driver.websockets.sync.client.connect", + side_effect=_send_handshake_request, + ), + patch("wherobots.db.driver.Connection"), + ): + connect_direct( + uri="wss://compute.example.com/sql/org/session-id", + headers=headers, + require_execution_ownership=require_execution_ownership, + ) + assert SECRET not in caplog.text + + @pytest.mark.parametrize( + "credential", + [{"api_key": SECRET}, {"token": SECRET}], + ids=["api key", "token"], + ) + @patch("wherobots.db.driver.requests.get") + @patch("wherobots.db.driver.requests.post") + def test_connect(self, mock_post, mock_get, caplog, credential): + caplog.set_level(logging.DEBUG) + _mock_ready_session(mock_post, mock_get) + with ( + patch( + "wherobots.db.driver.websockets.sync.client.connect", + side_effect=_send_handshake_request, + ), + patch("wherobots.db.driver.Connection"), + ): + connect(**credential) + assert SECRET not in caplog.text + + class TestWherobotsClientHeader: """connect() emits/appends the shared X-Wherobots-Client hop. diff --git a/wherobots/db/driver.py b/wherobots/db/driver.py index 5136499..8db8c3e 100644 --- a/wherobots/db/driver.py +++ b/wherobots/db/driver.py @@ -60,9 +60,10 @@ DEFAULT_HTTP_TIMEOUT = 30 -class _OwnershipTransportLogger(logging.LoggerAdapter): +class _TransportLogger(logging.LoggerAdapter): def isEnabledFor(self, level: int) -> bool: - # WebSocket frame tracing includes credentials in the message body. + # websockets' DEBUG tracing writes handshake headers (API key, bearer + # token) and frame payloads (execution claims) in clear text. return level >= logging.INFO and super().isEnabledFor(level) @@ -444,11 +445,7 @@ def open_ws( if require_execution_ownership else None ), - logger=( - _OwnershipTransportLogger(logging.getLogger("websockets.client")) - if require_execution_ownership - else None - ), + logger=_TransportLogger(logging.getLogger("websockets.client")), ) @tenacity.retry( From 138c002552faa38402853211f47cd4f21f1ece4d Mon Sep 17 00:00:00 2001 From: Peter Foldes Date: Mon, 30 Nov 2026 19:32:36 -0800 Subject: [PATCH 2/2] ci: pin third-party actions to commit SHAs The PyPI publish job holds an OIDC token that can publish the package, so a retargeted upstream tag would run with publish rights. Pin every third-party action to the commit its current tag resolves to; the org Renovate policy keeps these digests updated. Refs INF-249 --- .github/workflows/pre-commit.yaml | 6 +++--- .github/workflows/pypi-publish.yaml | 8 ++++---- .github/workflows/test.yaml | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/pre-commit.yaml b/.github/workflows/pre-commit.yaml index 89b693c..07cd32c 100644 --- a/.github/workflows/pre-commit.yaml +++ b/.github/workflows/pre-commit.yaml @@ -16,12 +16,12 @@ jobs: name: "pre-commit" runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: python-version: '3.x' - name: pre-commit (cache) - uses: actions/cache@v4 + uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 with: path: ~/.cache/pre-commit key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }} diff --git a/.github/workflows/pypi-publish.yaml b/.github/workflows/pypi-publish.yaml index 8ac3509..9fb89c1 100644 --- a/.github/workflows/pypi-publish.yaml +++ b/.github/workflows/pypi-publish.yaml @@ -14,12 +14,12 @@ jobs: permissions: id-token: write steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: python-version: "3.12" - name: Install uv - uses: astral-sh/setup-uv@v5 + uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2 with: version: "0.13.7" - name: Compare tag and project version @@ -29,4 +29,4 @@ jobs: run: | uv build - name: Publish package distributions to PyPI - uses: pypa/gh-action-pypi-publish@release/v1 + uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index bddf797..2b6f65e 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -19,16 +19,16 @@ jobs: python-version: ['3.10', '3.11', '3.12', '3.13', '3.14'] steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 + uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: python-version: ${{ matrix.python-version }} cache: 'pip' - name: Install uv - uses: astral-sh/setup-uv@v5 + uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2 with: version: "0.13.7"