Skip to content
Merged
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
13 changes: 11 additions & 2 deletions tenable/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import string
import warnings
from typing import Any
Expand All @@ -13,6 +14,9 @@
)
from tenable.base._restfly_v1 import dict_merge as _dm

logger = logging.getLogger(__name__)


__all__ = [
'check',
'dict_clean',
Expand Down Expand Up @@ -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
18 changes: 18 additions & 0 deletions tests/test_utils_scrub.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from uuid import UUID

from tenable.utils import scrub
Expand All @@ -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!'"
Loading