diff --git a/tenable/utils.py b/tenable/utils.py index 6d2000e14..ddfc1a830 100644 --- a/tenable/utils.py +++ b/tenable/utils.py @@ -1,3 +1,4 @@ +import logging import string import warnings from typing import Any @@ -13,6 +14,9 @@ ) from tenable.base._restfly_v1 import dict_merge as _dm +logger = logging.getLogger(__name__) + + __all__ = [ 'check', 'dict_clean', @@ -85,5 +89,10 @@ def scrub(value: Any) -> str: """ Scrubs converts the value to a string and then scrubs out any illegal characters. """ - safe_chars = string.ascii_letters + string.digits + '-_%@' - return ''.join([c for c in str(value) if c in safe_chars]) + safe_chars = string.ascii_letters + string.digits + '-_%@:' + scrubbed_value = ''.join([c for c in str(value) if c in safe_chars]) + if value != scrubbed_value: + logger.warning( + f"Value '{value}' has unsafe chars, scrubbing to '{scrubbed_value}'" + ) + return scrubbed_value diff --git a/tests/test_utils_scrub.py b/tests/test_utils_scrub.py index 951b58099..140f4727d 100644 --- a/tests/test_utils_scrub.py +++ b/tests/test_utils_scrub.py @@ -1,3 +1,4 @@ +import logging from uuid import UUID from tenable.utils import scrub @@ -19,3 +20,20 @@ def test_scrub_uuid(): def test_scrub_remove_path_traversal(): assert 'test' == scrub('../test') + + +def test_scrub_scan_id_formats(): + formats = [ + 12345, + '44346bcb-4afc-4db0-b283-2dd823fa8579' + 'SSEUF-ee904e9c-4fb6-4643-88a2-a4e388651568-C:e112bd1-754-946-e35-1a7bf1cbd33-pdf', + 'SSE-85b9353d-45f6-47ca-8510-abdb38bf1d5a-csv', + ] + for f in formats: + assert str(f) == scrub(f) + + +def test_scrub_warning(caplog): + caplog.set_level(logging.WARN) + _ = scrub('This_is_unsafe!../') + assert "Value 'This_is_unsafe!../' has unsafe chars, scrubbing to 'This_is_unsafe!'"