Skip to content
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

Add optional redacting response in debug output #238

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion fritzconnection/core/fritzconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 12 additions & 2 deletions fritzconnection/core/soaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


import datetime
from logging import DEBUG
import html
import re

Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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."""
Expand All @@ -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)
Expand Down
58 changes: 58 additions & 0 deletions fritzconnection/tests/test_soaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
get_html_safe_value,
is_html_response,
raise_fritzconnection_error,
redact_response,
remove_html_tags,
)

Expand Down Expand Up @@ -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 = """
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetInfoResponse xmlns:u="urn:dslforum-org:service:DeviceInfo:1">
<NewManufacturerName>AVM</NewManufacturerName>
<NewManufacturerOUI>00040E</NewManufacturerOUI>
<NewModelName>FRITZ!Box 7530 AX</NewModelName>
<NewDescription>FRITZ!Box 7530 AX Release 256.08.00</NewDescription>
<NewProductClass>FRITZ!Box</NewProductClass>
<NewSerialNumber>aabbccddeeff</NewSerialNumber>
<NewSoftwareVersion>256.08.00</NewSoftwareVersion>
<NewHardwareVersion>FRITZ!Box 7530 AX</NewHardwareVersion>
<NewSpecVersion>1.0</NewSpecVersion>
<NewProvisioningCode></NewProvisioningCode>
<NewUpTime>86446</NewUpTime>
<NewDeviceLog>
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
</u:GetInfoResponse>
</s:Body>
</s:Envelope>
"""

result = redact_response(False, response)
assert result == response

result = redact_response(True, response)
assert result == """
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetInfoResponse xmlns:u="urn:dslforum-org:service:DeviceInfo:1">
<NewManufacturerName>AVM</NewManufacturerName>
<NewManufacturerOUI>00040E</NewManufacturerOUI>
<NewModelName>FRITZ!Box 7530 AX</NewModelName>
<NewDescription>FRITZ!Box 7530 AX Release 256.08.00</NewDescription>
<NewProductClass>FRITZ!Box</NewProductClass>
<NewSerialNumber>aabbccddeeff</NewSerialNumber>
<NewSoftwareVersion>256.08.00</NewSoftwareVersion>
<NewHardwareVersion>FRITZ!Box 7530 AX</NewHardwareVersion>
<NewSpecVersion>1.0</NewSpecVersion>
<NewProvisioningCode></NewProvisioningCode>
<NewUpTime>86446</NewUpTime>
<NewDeviceLog>
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
</u:GetInfoResponse>
</s:Body>
</s:Envelope>
"""