Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/source/release-history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ Release History
===============


Unreleased
==========

Fixed
-----

- The server settings logged at startup no longer contain secrets. The token signing keys
(``secret_keys``), the single-user API key and the password in the database URI were logged
in plain text at the INFO level; deployments whose logs others can read should replace them.


v0.0.15 (2026-11-05)
====================

Expand Down
17 changes: 16 additions & 1 deletion src/bluesky_httpserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import sys
from pathlib import Path

from sqlalchemy.engine import make_url

import bluesky_httpserver

from .app import build_app
Expand Down Expand Up @@ -114,12 +116,25 @@ def formatter(prog):
uvicorn.run(web_app, **uvicorn_kwargs)


def loggable_settings(settings):
"""
Return the settings as a dictionary that is safe to log: the token signing keys and the single-user
API key are masked, and the password is removed from the database URI.
"""
loggable = dict(settings)
loggable["secret_keys"] = ["*******"] * len(settings.secret_keys)
loggable["single_user_api_key"] = "*******"
if settings.database_uri is not None:
loggable["database_uri"] = make_url(settings.database_uri).render_as_string(hide_password=True)
return loggable


def print_admin_api_key_if_generated(web_app, host, port):
# host = host or "127.0.0.1"
# port = port or 8000
settings = web_app.dependency_overrides.get(get_settings, get_settings)()

logger.info("APP settings: %s", pprint.pformat(dict(settings)))
logger.info("APP settings: %s", pprint.pformat(loggable_settings(settings)))
authenticators = web_app.dependency_overrides.get(get_authenticators, get_authenticators)()
if settings.allow_anonymous_access:
print(
Expand Down
33 changes: 33 additions & 0 deletions src/bluesky_httpserver/tests/test_server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
import os
import pprint
import secrets

import pytest
from bluesky_queueserver import generate_zmq_keys
Expand All @@ -14,6 +16,9 @@
set_qserver_zmq_public_key,
)

from bluesky_httpserver.app import build_app
from bluesky_httpserver.server import print_admin_api_key_if_generated
from bluesky_httpserver.settings import get_settings
from bluesky_httpserver.tests.conftest import ( # noqa F401
SERVER_ADDRESS,
SERVER_PORT,
Expand Down Expand Up @@ -292,3 +297,31 @@ def test_http_server_custom_routers_1(tmpdir, monkeypatch, re_manager, fastapi_s
assert "manager_state" in resp3, pprint.pformat(resp3)
resp4 = request_to_json("post", "/some_prefix/status_duplicate_post", request_prefix="")
assert resp3 == resp4


def test_startup_log_hides_secrets(caplog):
"""
The settings logged at startup contain neither the token signing keys, nor the single-user API key, nor the
database password.
"""
signing_keys = [secrets.token_hex(32), secrets.token_hex(32)]
api_key, password = secrets.token_hex(32), secrets.token_hex(16)
# build_app() configures the settings object cached by get_settings(); start and leave with a fresh one.
get_settings.cache_clear()
try:
app = build_app(
authentication={"secret_keys": signing_keys, "single_user_api_key": api_key},
server_settings={
"server_configuration": {},
"database": {"uri": f"postgresql://qserver:{password}@db.example/qserver"},
},
)
with caplog.at_level(logging.INFO, logger="bluesky_httpserver"):
print_admin_api_key_if_generated(app, host="localhost", port=60610)
finally:
get_settings.cache_clear()

for secret in [*signing_keys, api_key, password]:
assert secret not in caplog.text
# The rest of the settings, including where the database is, are still logged.
assert "db.example" in caplog.text
Loading