diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7e0ef07..a57d604 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -13,7 +13,9 @@ In case you think that something is not working as expected or you have a new id In general pull requests are welcome. Please create an issue first before putting too much work into a pull request that may not get merged at the end, for whatever reason. An issue can help to clarify points of view and motivation. -For pull requests there is a golden rule: **keep them small**. Smaller pull requests are easier to review and easier to merge – especially in cases when not every part of a larger changeset should get merged and has to get modifications. +In case of providing a pull-request please do this for the **development-branch** and not for the master-branch. + +For pull-requests there is a golden rule: **keep them small**. Smaller pull-requests are easier to review and easier to merge – especially in cases when not every part of a larger changeset should get merged and has to get modifications. Please avoid to just change the formatting. The result is most often nothing else than git-diff pollution. This is especially true for `black` (or `blue` or the corresponding modes in `ruff`) – this project startet before `black`. It is ok to use these tools for modified code snippets, but not for a module. diff --git a/fritzconnection/core/fritzconnection.py b/fritzconnection/core/fritzconnection.py index 0a4a46b..82e1871 100644 --- a/fritzconnection/core/fritzconnection.py +++ b/fritzconnection/core/fritzconnection.py @@ -186,6 +186,7 @@ def __init__( cache_format: str | None = None, pool_connections: int = DEFAULT_POOL_CONNECTIONS, pool_maxsize: int = DEFAULT_POOL_MAXSIZE, + redact_debug_log: bool = False ): """ Initialisation of FritzConnection: reads all data from the box @@ -279,7 +280,7 @@ def __init__( self.port = port self.soaper = Soaper( - address, port, user, password, timeout=timeout, session=session + address, port, user, password, timeout=timeout, session=session, redact_debug_log=redact_debug_log ) self.device_manager = DeviceManager(timeout=timeout, session=session) self._load_router_api( diff --git a/fritzconnection/core/soaper.py b/fritzconnection/core/soaper.py index 7813370..87c8db6 100644 --- a/fritzconnection/core/soaper.py +++ b/fritzconnection/core/soaper.py @@ -8,6 +8,7 @@ import datetime +from logging import DEBUG import html import re @@ -190,6 +191,14 @@ def raise_fritzconnection_error(response): exception = FRITZ_ERRORS.get(error_code, FritzConnectionException) raise exception(message) +def redact_response(redact: bool, input: str): + # avoid expansive regex matching, when not neccessary + if fritzlogger.level != DEBUG or not redact: + return input + # redact possible phone numbers + # numbers with len of 5 or more, sourounded by white spaces or bracket + redacted = re.sub(r"([\s\[\(])\d{5,}([\s\]\)])", r"\1******\2", input) + return redacted class Soaper: """ @@ -238,13 +247,14 @@ class Soaper: "ui4": int, } - def __init__(self, address, port, user, password, timeout=None, session=None): + def __init__(self, address, port, user, password, timeout=None, session=None, redact_debug_log=False): self.address = address self.port = port self.user = user self.password = password self.timeout = timeout self.session = session + self.redact_debug_log = redact_debug_log def get_body(self, service, action_name, arguments): """Returns the body by template substitution.""" @@ -263,7 +273,7 @@ def execute(self, service, action_name, arguments): def handle_response(response): fritzlogger.debug(f"response status: {response.status_code}") - fritzlogger.debug(response.text) + fritzlogger.debug(redact_response(self.redact_debug_log, response.text)) if response.status_code != 200: raise_fritzconnection_error(response) return self.parse_response(response, service, action_name) diff --git a/fritzconnection/tests/test_soaper.py b/fritzconnection/tests/test_soaper.py index f8684c5..d683524 100644 --- a/fritzconnection/tests/test_soaper.py +++ b/fritzconnection/tests/test_soaper.py @@ -30,6 +30,7 @@ get_html_safe_value, is_html_response, raise_fritzconnection_error, + redact_response, remove_html_tags, ) @@ -286,3 +287,60 @@ def test_get_converted_value(data_type, value, expected_value): def test_get_converted_value_fails(data_type, value): with pytest.raises(ValueError): get_converted_value(data_type, value) + +def test_react_debug_log(): + response = """ + + + + + AVM + 00040E + FRITZ!Box 7530 AX + FRITZ!Box 7530 AX Release 256.08.00 + FRITZ!Box + aabbccddeeff + 256.08.00 + FRITZ!Box 7530 AX + 1.0 + + 86446 + + 23.11.24 12:28:10 Anmeldung der Internetrufnummer 491234567890 war nicht erfolgreich. Ursache: DNS-Fehler + 23.11.24 12:28:10 Anmeldung der Internetrufnummer 491234567891 war nicht erfolgreich. Ursache: DNS-Fehler + 23.11.24 12:28:10 Anmeldung der Internetrufnummer 491234567892 war nicht erfolgreich. Ursache: DNS-Fehler + 23.11.24 12:28:10 Anmeldung der Internetrufnummer 491234567893 war nicht erfolgreich. Ursache: DNS-Fehler + + + + """ + + result = redact_response(False, response) + assert result == response + + result = redact_response(True, response) + assert result == """ + + + + + AVM + 00040E + FRITZ!Box 7530 AX + FRITZ!Box 7530 AX Release 256.08.00 + FRITZ!Box + aabbccddeeff + 256.08.00 + FRITZ!Box 7530 AX + 1.0 + + 86446 + + 23.11.24 12:28:10 Anmeldung der Internetrufnummer ****** war nicht erfolgreich. Ursache: DNS-Fehler + 23.11.24 12:28:10 Anmeldung der Internetrufnummer ****** war nicht erfolgreich. Ursache: DNS-Fehler + 23.11.24 12:28:10 Anmeldung der Internetrufnummer ****** war nicht erfolgreich. Ursache: DNS-Fehler + 23.11.24 12:28:10 Anmeldung der Internetrufnummer ****** war nicht erfolgreich. Ursache: DNS-Fehler + + + + """