Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Stop logging email addresses

Petr Bauch58522c5f5b72
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 37109bbab..fc5227ff2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -153,6 +153,10 @@ version of their own to them without also giving them a release of their own.
anything past that reaches agents unchecked, and the message carries a note
saying the rest was not checked. switch-core now depends on `pypdf` to read
PDFs.
+- **Logs no longer carry email addresses.** Signing up, creating a user,
+ creating or deleting an API key, seeding the admin account and finding two
+ accounts with the same address each logged the person's email. They now log
+ the user id. A test fails if a log call passes an email again.
- **Counts and replies no longer name private rooms either.** Beyond the
room listings below, a reference, document or package's attached-rooms count
took in every room it was attached to, an agent's detail named every room
diff --git a/core/switch_core/db/stores/user_store.py b/core/switch_core/db/stores/user_store.py
index 4af5d7f53..c6f0bfbdd 100644
--- a/core/switch_core/db/stores/user_store.py
+++ b/core/switch_core/db/stores/user_store.py
@@ -249,11 +249,10 @@ class UserStore:
users = result.scalars().all()
if len(users) > 1:
logger.error(
- "Multiple users share email %r case-insensitively (ids: %s); "
- "returning %s (lowest id, an arbitrary but stable pick). This "
- "is pre-existing duplicate data, not something this login "
- "caused — merge or rename the extra account(s).",
- email,
+ "Multiple users share an email address case-insensitively "
+ "(ids: %s); returning %s (lowest id, an arbitrary but stable "
+ "pick). This is pre-existing duplicate data, not something "
+ "this login caused — merge or rename the extra account(s).",
[u.id for u in users],
users[0].id,
)
diff --git a/core/switch_core/gateway/api_keys.py b/core/switch_core/gateway/api_keys.py
index 39c078e0f..ff6cb910f 100644
--- a/core/switch_core/gateway/api_keys.py
+++ b/core/switch_core/gateway/api_keys.py
@@ -134,7 +134,7 @@ async def create_api_key(
)
await session.commit()
- logger.info("Created API key '%s' for user %s", req.label, user.email)
+ logger.info("Created API key '%s' for user %s", req.label, user.id)
emit_safely(
current_telemetry(), "api_key_created", {"key_type": _key_type(key.type)}
)
@@ -215,7 +215,7 @@ async def delete_api_key(
await session.commit()
get_protocol().api_key_cache.invalidate(key_hash)
- logger.info("Deleted API key '%s' for user %s", key.label, user.email)
+ logger.info("Deleted API key '%s' for user %s", key.label, user.id)
emit_safely(
current_telemetry(),
"api_key_revoked",
diff --git a/core/switch_core/gateway/auth_routes.py b/core/switch_core/gateway/auth_routes.py
index cf24c88c6..64f49ff9f 100644
--- a/core/switch_core/gateway/auth_routes.py
+++ b/core/switch_core/gateway/auth_routes.py
@@ -293,7 +293,7 @@ async def signup(
status_code=409, detail="Email already registered"
) from None
await session.commit()
- logger.info("Signed up user: %s (%s)", user.email, user.id)
+ logger.info("Signed up user %s", user.id)
emit_safely(current_telemetry(), "account_created", {"method": "signup"})
set_session_cookie(
@@ -744,7 +744,7 @@ async def create_user(
) from None
await session.commit()
- logger.info("Created user: %s (%s)", user.email, user.id)
+ logger.info("Created user %s", user.id)
emit_safely(current_telemetry(), "account_created", {"method": "admin"})
return OperatorUserResponse(
id=user.id,
diff --git a/core/switch_core/main.py b/core/switch_core/main.py
index cd1ddc609..6f50fa74c 100644
--- a/core/switch_core/main.py
+++ b/core/switch_core/main.py
@@ -1935,11 +1935,11 @@ async def _seed_admin_user(
logger.warning(
"Admin user %s had no tenant membership and could not have "
"signed in; joined it to tenant %s.",
- config.gateway_admin_email,
+ existing.id,
TENANT_ZERO_ID,
)
else:
- logger.info("Admin user already exists: %s", config.gateway_admin_email)
+ logger.info("Admin user already exists: %s", existing.id)
return
admin = User(
@@ -1950,7 +1950,7 @@ async def _seed_admin_user(
)
await user_store.create(session, admin)
await session.commit()
- logger.info("Seeded admin user: %s", config.gateway_admin_email)
+ logger.info("Seeded admin user: %s", admin.id)
async def _seed_agent_registration_bootstrap_key(
@@ -1999,8 +1999,8 @@ async def _seed_agent_registration_bootstrap_key(
admin = await user_store.get_by_email(session, config.gateway_admin_email)
if admin is None:
raise RuntimeError(
- "Cannot seed agent-registration bootstrap key: admin user "
- f"{config.gateway_admin_email} not found"
+ "Cannot seed agent-registration bootstrap key: no user has "
+ "the GATEWAY_ADMIN_EMAIL address"
)
admin_id = admin.id
bootstrap_owner = await ensure_bootstrap_owner(session, user_store)
@@ -2027,13 +2027,13 @@ async def _seed_agent_registration_bootstrap_key(
)
if admin_owned_agents:
logger.warning(
- "%d agent(s) are owned by the admin user (%s) and carry "
+ "%d agent(s) are owned by the admin user (id %s) and carry "
"admin-equivalent authority over every room and resource in "
"this deployment, not just their own: %s. If any were "
"registered through AGENT_REGISTRATION_TOKEN, reassign or "
"re-register them under a non-admin owner.",
len(admin_owned_agents),
- config.gateway_admin_email,
+ admin_id,
", ".join(admin_owned_agents),
)
diff --git a/core/tests/switch_core/observability/test_no_email_addresses_in_logs.py b/core/tests/switch_core/observability/test_no_email_addresses_in_logs.py
new file mode 100644
index 000000000..a043f3e3c
--- /dev/null
+++ b/core/tests/switch_core/observability/test_no_email_addresses_in_logs.py
@@ -0,0 +1,51 @@
+"""No log call passes an email address.
+
+Application logs leave the database's region, so they carry ids and never a
+person's email address. This scans every logger call in `switch_core` for an
+argument (or f-string value) that reads an email: a name or attribute called
+`email` or ending in `_email`.
+"""
+
+import ast
+from pathlib import Path
+
+import switch_core
+
+LOG_METHODS = {"debug", "info", "warning", "error", "exception", "critical", "log"}
+
+
+def _is_email(node: ast.AST) -> bool:
+ for child in ast.walk(node):
+ name = (
+ child.id
+ if isinstance(child, ast.Name)
+ else child.attr
+ if isinstance(child, ast.Attribute)
+ else None
+ )
+ if name is not None and (name == "email" or name.endswith("_email")):
+ return True
+ return False
+
+
+def _is_log_call(node: ast.AST) -> bool:
+ return (
+ isinstance(node, ast.Call)
+ and isinstance(node.func, ast.Attribute)
+ and node.func.attr in LOG_METHODS
+ and "log" in ast.unparse(node.func.value).lower()
+ )
+
+
+def test_no_logger_call_passes_an_email_address() -> None:
+ package = Path(switch_core.__file__).parent
+ offenders = []
+ for path in sorted(package.rglob("*.py")):
+ for node in ast.walk(ast.parse(path.read_text(), filename=str(path))):
+ if not _is_log_call(node):
+ continue
+ assert isinstance(node, ast.Call)
+ values = [*node.args, *(keyword.value for keyword in node.keywords)]
+ if any(_is_email(value) for value in values):
+ offenders.append(f"{path.relative_to(package)}:{node.lineno}")
+ assert offenders == [], f"log calls that pass an email address: {offenders}"
diff --git a/core/tests/switch_core/test_seed_admin_membership.py b/core/tests/switch_core/test_seed_admin_membership.py
index fcf0ccf79..bdd621891 100644
--- a/core/tests/switch_core/test_seed_admin_membership.py
+++ b/core/tests/switch_core/test_seed_admin_membership.py
@@ -127,8 +127,8 @@ class TestSeedingRepairsTheAdmin:
caplog: pytest.LogCaptureFixture,
) -> None:
"""An account that could not sign in until this boot is not routine
- news, so it is a warning naming the address — and the ordinary case
- stays at `info` so the warning means something."""
+ news, so it is a warning naming the account (by id, never its address)
+ — and the ordinary case stays at `info` so the warning means something."""
await _seed_admin_user(session_factory, UserStore(), _config())
user_id = await _admin_id(session_factory)
await _strand_the_admin(session_factory, user_id)
@@ -140,7 +140,8 @@ class TestSeedingRepairsTheAdmin:
await _seed_admin_user(session_factory, UserStore(), _config())
settled = [r for r in caplog.records if r.levelname == "WARNING"]
- assert [ADMIN_EMAIL in r.getMessage() for r in repaired] == [True]
+ assert [str(user_id) in r.getMessage() for r in repaired] == [True]
+ assert [ADMIN_EMAIL in r.getMessage() for r in repaired] == [False]
assert settled == []
async def test_an_admin_that_already_belongs_somewhere_is_left_alone(