-
Notifications
You must be signed in to change notification settings - Fork 522
Add debug log when HMAC incorrect
#18474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+201
−0
Merged
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4be4ea0
Add debug log when `HMAC incorrect`
MadLittleMods 7feedb9
Fix order
MadLittleMods 944800d
Remove extra testing log
MadLittleMods 8e45c6f
Add changelog
MadLittleMods 95a084e
Add to sensitive part of logging template for Docker
MadLittleMods e079104
Document dangers of `DEBUG` level logging
MadLittleMods a0ada44
Merge branch 'develop' into madlittlemods/debug-hmac-incorrect
MadLittleMods 1aac00f
Not fully working correctly: `ExplicitlyConfiguredLogger` via filters
MadLittleMods 7e5ef26
Working `ExplicitlyConfiguredLogger` but not a great setup experience
MadLittleMods fea75ae
Better comments
MadLittleMods 7f4a06b
Revert "Document dangers of `DEBUG` level logging"
MadLittleMods 6b13fbd
Revert "Add to sensitive part of logging template for Docker"
MadLittleMods b44e5d7
Remove debug logs
MadLittleMods 57da8ba
Fix typo
MadLittleMods 34c101b
Merge branch 'develop' into madlittlemods/debug-hmac-incorrect
MadLittleMods 5735d66
Merge branch 'develop' into madlittlemods/debug-hmac-incorrect
MadLittleMods 3cf7dbf
Merge branch 'develop' into madlittlemods/debug-hmac-incorrect
MadLittleMods b1e96ae
Override `isEnabledFor` so that we can use it accurately
MadLittleMods 19aeeee
No need to add a filter with `isEnabledFor`
MadLittleMods b8bc1e3
Add `ExplicitlyConfiguredLoggerTestCase`
MadLittleMods 3707374
Add tests for `isEnabledFor`
MadLittleMods 5b1020c
Workaround `assertNoLogs` not beinga available
MadLittleMods 78e29dc
Remove unused change
MadLittleMods c00a3e7
Remove unused boilerplate
MadLittleMods 5716db6
Merge branch 'develop' into madlittlemods/debug-hmac-incorrect
MadLittleMods e2e0e0c
Fix matching pair back-ticks typo in test log
MadLittleMods b15bf3e
Merge branch 'develop' into madlittlemods/debug-hmac-incorrect
MadLittleMods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add debug logging for HMAC digest verification failures when using the admin API to register users. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| 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) | ||
|
|
||
| def isEnabledFor(self, level: int) -> bool: | ||
| # 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 level >= log_level |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # | ||
| # This file is licensed under the Affero General Public License (AGPL) version 3. | ||
| # | ||
| # Copyright (C) 2025 New Vector, Ltd | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU Affero General Public License as | ||
| # published by the Free Software Foundation, either version 3 of the | ||
| # License, or (at your option) any later version. | ||
| # | ||
| # See the GNU Affero General Public License for more details: | ||
| # <https://www.gnu.org/licenses/agpl-3.0.html>. | ||
| # | ||
| # | ||
| # | ||
| import logging | ||
|
|
||
| from synapse.logging.loggers import ExplicitlyConfiguredLogger | ||
|
|
||
| from tests.unittest import TestCase | ||
|
|
||
|
|
||
| class ExplicitlyConfiguredLoggerTestCase(TestCase): | ||
| def _create_explicitly_configured_logger(self) -> logging.Logger: | ||
| original_logger_class = logging.getLoggerClass() | ||
| logging.setLoggerClass(ExplicitlyConfiguredLogger) | ||
| logger = logging.getLogger("test") | ||
| # Restore the original logger class | ||
| logging.setLoggerClass(original_logger_class) | ||
|
|
||
| return logger | ||
|
|
||
| def test_no_logs_when_not_set(self) -> None: | ||
| """ | ||
| Test to make sure that nothing is logged when the logger is *not* explicitly | ||
| configured. | ||
| """ | ||
| root_logger = logging.getLogger() | ||
| root_logger.setLevel(logging.DEBUG) | ||
|
|
||
| logger = self._create_explicitly_configured_logger() | ||
|
|
||
| with self.assertLogs(logger=logger, level=logging.NOTSET) as cm: | ||
| # XXX: We have to set this again because of a Python bug: | ||
| # https://github.com/python/cpython/issues/136958 (feel free to remove once | ||
| # that is resolved and we update to a newer Python version that includes the | ||
| # fix) | ||
| logger.setLevel(logging.NOTSET) | ||
|
Comment on lines
+44
to
+48
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ran into a bug with the built-in |
||
|
|
||
| logger.debug("debug message") | ||
| logger.info("info message") | ||
| logger.warning("warning message") | ||
| logger.error("error message") | ||
|
|
||
| # Nothing should be logged since the logger is *not* explicitly configured | ||
| # | ||
| # FIXME: Remove this whole block once we update to Python 3.10 or later and | ||
| # have access to `assertNoLogs` (replace `assertLogs` with `assertNoLogs`) | ||
| self.assertIncludes( | ||
| set(cm.output), | ||
| set(), | ||
| exact=True, | ||
| ) | ||
| # Stub log message to avoid `assertLogs` failing since it expects at least | ||
| # one log message to be logged. | ||
| logger.setLevel(logging.INFO) | ||
| logger.info("stub message so assertLogs` doesn't fail") | ||
|
MadLittleMods marked this conversation as resolved.
Outdated
|
||
|
|
||
| def test_logs_when_explicitly_configured(self) -> None: | ||
| """ | ||
| Test to make sure that logs are emitted when the logger is explicitly configured. | ||
| """ | ||
| root_logger = logging.getLogger() | ||
| root_logger.setLevel(logging.INFO) | ||
|
|
||
| logger = self._create_explicitly_configured_logger() | ||
|
|
||
| with self.assertLogs(logger=logger, level=logging.DEBUG) as cm: | ||
| logger.debug("debug message") | ||
| logger.info("info message") | ||
| logger.warning("warning message") | ||
| logger.error("error message") | ||
|
|
||
| self.assertIncludes( | ||
| set(cm.output), | ||
| { | ||
| "DEBUG:test:debug message", | ||
| "INFO:test:info message", | ||
| "WARNING:test:warning message", | ||
| "ERROR:test:error message", | ||
| }, | ||
| exact=True, | ||
| ) | ||
|
|
||
| def test_is_enabled_for_not_set(self) -> None: | ||
| """ | ||
| Test to make sure `logger.isEnabledFor(...)` returns False when the logger is | ||
| not explicitly configured. | ||
| """ | ||
|
|
||
| logger = self._create_explicitly_configured_logger() | ||
|
|
||
| # Unset the logger (not configured) | ||
| logger.setLevel(logging.NOTSET) | ||
|
|
||
| # The logger shouldn't be enabled for any level | ||
| self.assertFalse(logger.isEnabledFor(logging.DEBUG)) | ||
| self.assertFalse(logger.isEnabledFor(logging.INFO)) | ||
| self.assertFalse(logger.isEnabledFor(logging.WARNING)) | ||
| self.assertFalse(logger.isEnabledFor(logging.ERROR)) | ||
|
|
||
| def test_is_enabled_for_info(self) -> None: | ||
| """ | ||
| Test to make sure `logger.isEnabledFor(...)` returns True any levels above the | ||
| explicitly configured level. | ||
| """ | ||
|
|
||
| logger = self._create_explicitly_configured_logger() | ||
|
|
||
| # Explicitly configure the logger to `INFO` level | ||
| logger.setLevel(logging.INFO) | ||
|
|
||
| # The logger should be enabled for INFO and above once explicitly configured | ||
| self.assertFalse(logger.isEnabledFor(logging.DEBUG)) | ||
| self.assertTrue(logger.isEnabledFor(logging.INFO)) | ||
| self.assertTrue(logger.isEnabledFor(logging.WARNING)) | ||
| self.assertTrue(logger.isEnabledFor(logging.ERROR)) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.