Compare commits

...

16 Commits

Author SHA1 Message Date
Eric Eastwood
5735d669da Merge branch 'develop' into madlittlemods/debug-hmac-incorrect 2025-06-20 08:56:43 -05:00
Eric Eastwood
34c101b0da Merge branch 'develop' into madlittlemods/debug-hmac-incorrect 2025-06-17 16:59:47 -05:00
Eric Eastwood
57da8ba457 Fix typo 2025-06-06 17:51:58 -05:00
Eric Eastwood
b44e5d7346 Remove debug logs 2025-06-06 17:51:00 -05:00
Eric Eastwood
6b13fbdc24 Revert "Add to sensitive part of logging template for Docker"
This reverts commit 95a084e697.
2025-06-06 17:41:06 -05:00
Eric Eastwood
7f4a06bf7e Revert "Document dangers of DEBUG level logging"
This reverts commit e07910486a.
2025-06-06 17:38:57 -05:00
Eric Eastwood
fea75ae790 Better comments 2025-06-06 17:37:38 -05:00
Eric Eastwood
7e5ef26d0f Working ExplicitlyConfiguredLogger but not a great setup experience 2025-06-06 17:33:07 -05:00
Eric Eastwood
1aac00fc62 Not fully working correctly: ExplicitlyConfiguredLogger via filters 2025-06-06 17:01:20 -05:00
Eric Eastwood
a0ada440b8 Merge branch 'develop' into madlittlemods/debug-hmac-incorrect 2025-06-06 15:49:03 -05:00
Eric Eastwood
e07910486a Document dangers of DEBUG level logging 2025-05-23 17:31:04 -05:00
Eric Eastwood
95a084e697 Add to sensitive part of logging template for Docker 2025-05-23 17:24:04 -05:00
Eric Eastwood
8e45c6fe5f Add changelog 2025-05-23 16:49:55 -05:00
Eric Eastwood
944800d4e2 Remove extra testing log 2025-05-23 16:19:06 -05:00
Eric Eastwood
7feedb9fd7 Fix order 2025-05-23 16:16:49 -05:00
Eric Eastwood
4be4ea06ad Add debug log when HMAC incorrect 2025-05-23 15:59:55 -05:00
4 changed files with 63 additions and 0 deletions

1
changelog.d/18474.misc Normal file
View File

@@ -0,0 +1 @@
Add debug logging for HMAC digest verification failures when using the admin API to register users.

View File

@@ -21,6 +21,8 @@
import logging
from typing import Literal
root_logger = logging.getLogger()
class MetadataFilter(logging.Filter):
"""Logging filter that adds constant values to each record.

View File

@@ -0,0 +1,33 @@
import logging
root_logger = logging.getLogger()
class ExplicitlyConfiguredLogger(logging.Logger):
"""
A custom logger class that only allows logging if the logger is explicitly
configured (does not inherit log level from parent).
"""
def __init__(self, name: str, level: int = logging.NOTSET) -> None:
super().__init__(name, level)
self.addFilter(self._filter)
def _filter(self, record: logging.LogRecord) -> bool:
"""
Only allow logging if the logger is explicitly configured.
"""
# Check if the logger is explicitly configured
explicitly_configured_logger = self.manager.loggerDict.get(self.name)
log_level = logging.NOTSET
if isinstance(explicitly_configured_logger, logging.Logger):
log_level = explicitly_configured_logger.level
# If the logger is not configured, we don't log anything
if log_level == logging.NOTSET:
return False
# Otherwise, follow the normal logging behavior
return record.levelno >= log_level

View File

@@ -42,6 +42,7 @@ from synapse.http.servlet import (
parse_strings_from_args,
)
from synapse.http.site import SynapseRequest
from synapse.logging.loggers import ExplicitlyConfiguredLogger
from synapse.rest.admin._base import (
admin_patterns,
assert_requester_is_admin,
@@ -60,6 +61,25 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
original_logger_class = logging.getLoggerClass()
# Because this can log sensitive information, use a custom logger class that only allows
# logging if the logger is explicitly configured.
logging.setLoggerClass(ExplicitlyConfiguredLogger)
user_registration_debug_logger = logging.getLogger(
"synapse.rest.admin.users.registration_debug"
)
"""
A logger for debugging the user registration process.
Because this can log sensitive information (such as passwords and
`registration_shared_secret`), we want people to explictly opt-in before seeing anything
in the logs. Requires explicitly setting `synapse.rest.admin.users.registration_debug`
in the logging configuration and does not inherit the log level from the parent logger.
"""
# Restore the original logger class
logging.setLoggerClass(original_logger_class)
class UsersRestServletV2(RestServlet):
PATTERNS = admin_patterns("/users$", "v2")
@@ -635,6 +655,13 @@ class UserRegisterServlet(RestServlet):
want_mac = want_mac_builder.hexdigest()
if not hmac.compare_digest(want_mac.encode("ascii"), got_mac.encode("ascii")):
user_registration_debug_logger.debug(
"UserRegisterServlet: Incorrect HMAC digest: actual=%s, expected=%s, registration_shared_secret=%s, body=%s",
got_mac,
want_mac,
self.hs.config.registration.registration_shared_secret,
body,
)
raise SynapseError(HTTPStatus.FORBIDDEN, "HMAC incorrect")
should_issue_refresh_token = body.get("refresh_token", False)