| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
"""Unit tests for :mod:`apprise.utils.sanitize`.""" |
| 29 |
|
| 30 |
from __future__ import annotations |
| 31 |
|
| 32 |
from hashlib import sha256 |
| 33 |
import logging |
| 34 |
import sys |
| 35 |
|
| 36 |
import pytest |
| 37 |
|
| 38 |
from apprise.utils.sanitize import SanitizeOptions, sanitize_payload |
| 39 |
|
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
sys.dont_write_bytecode = True |
| 43 |
|
| 44 |
|
| 45 |
class _ReprOnly: |
| 46 |
"""Helper type that only supports repr(), used to cover fallback paths.""" |
| 47 |
|
| 48 |
def __repr__(self) -> str: |
| 49 |
return "<repr-only>" |
| 50 |
|
| 51 |
|
| 52 |
def test_sanitize_payload_passthrough_primitives() -> None: |
| 53 |
"""Primitives should pass through with minimal transformation.""" |
| 54 |
assert sanitize_payload(None) is None |
| 55 |
assert sanitize_payload(True) is True |
| 56 |
assert sanitize_payload(False) is False |
| 57 |
assert sanitize_payload(123) == 123 |
| 58 |
assert sanitize_payload(3.14) == pytest.approx(3.14) |
| 59 |
|
| 60 |
|
| 61 |
def test_sanitize_payload_small_string_passthrough() -> None: |
| 62 |
"""Small strings are not altered, preserving debugging usefulness.""" |
| 63 |
s = "hello world" |
| 64 |
assert sanitize_payload(s) == s |
| 65 |
|
| 66 |
|
| 67 |
def test_sanitize_payload_large_string_is_summarized() -> None: |
| 68 |
"""Strings beyond max_str_len are summarized with head/tail previews.""" |
| 69 |
opts = SanitizeOptions(max_str_len=64, preview=8) |
| 70 |
s = "x" * 200 |
| 71 |
out = sanitize_payload(s, options=opts) |
| 72 |
|
| 73 |
assert isinstance(out, str) |
| 74 |
assert out.startswith("<string len=200") |
| 75 |
assert "head=" in out |
| 76 |
assert "tail=" in out |
| 77 |
|
| 78 |
|
| 79 |
def test_sanitize_payload_bytes_are_summarized_and_hash_is_bounded() -> None: |
| 80 |
"""Bytes are always summarized with a bounded sha256 digest.""" |
| 81 |
opts = SanitizeOptions(hash_sample_size=8) |
| 82 |
b = b"01234567" + b"EXTRA-DATA" |
| 83 |
|
| 84 |
out = sanitize_payload(b, options=opts) |
| 85 |
assert isinstance(out, str) |
| 86 |
assert out.startswith(f"<bytes len={len(b)}") |
| 87 |
|
| 88 |
expected = sha256(b[: opts.hash_sample_size]).hexdigest()[:12] |
| 89 |
assert f"sha256={expected}" in out |
| 90 |
|
| 91 |
|
| 92 |
def test_sanitize_payload_dict_keys_are_sanitised_for_bytes() -> None: |
| 93 |
"""Bytes keys become readable string markers rather than raw bytes.""" |
| 94 |
opts = SanitizeOptions(hash_sample_size=16) |
| 95 |
|
| 96 |
bkey = b"abc" |
| 97 |
payload = { |
| 98 |
bkey: "value1", |
| 99 |
"k": "v", |
| 100 |
} |
| 101 |
|
| 102 |
out = sanitize_payload(payload, options=opts) |
| 103 |
assert isinstance(out, dict) |
| 104 |
|
| 105 |
expected_b_digest = sha256(bkey[: opts.hash_sample_size]).hexdigest()[:12] |
| 106 |
bkey_s = f"<bytes len={len(bkey)} sha256={expected_b_digest}>" |
| 107 |
assert bkey_s in out |
| 108 |
assert out[bkey_s] == "value1" |
| 109 |
assert out["k"] == "v" |
| 110 |
|
| 111 |
|
| 112 |
def test_sanitize_payload_sequence_types() -> None: |
| 113 |
"""Lists, tuples, sets, and frozensets are walked safely.""" |
| 114 |
payload = [1, "x" * 200, b"xyz"] |
| 115 |
opts = SanitizeOptions(max_str_len=64, preview=8) |
| 116 |
|
| 117 |
out = sanitize_payload(payload, options=opts) |
| 118 |
assert isinstance(out, list) |
| 119 |
assert out[0] == 1 |
| 120 |
assert isinstance(out[1], str) and out[1].startswith("<string len=") |
| 121 |
assert isinstance(out[2], str) and out[2].startswith("<bytes len=3") |
| 122 |
|
| 123 |
out_t = sanitize_payload(tuple(payload), options=opts) |
| 124 |
assert isinstance(out_t, tuple) |
| 125 |
|
| 126 |
|
| 127 |
out_s = sanitize_payload(set(payload), options=opts) |
| 128 |
assert isinstance(out_s, list) |
| 129 |
|
| 130 |
out_fs = sanitize_payload(frozenset(payload), options=opts) |
| 131 |
assert isinstance(out_fs, list) |
| 132 |
|
| 133 |
|
| 134 |
def test_sanitize_payload_recursive_structure_is_detected() -> None: |
| 135 |
"""Self-referential objects should not trigger infinite recursion.""" |
| 136 |
payload: dict[str, object] = {} |
| 137 |
payload["self"] = payload |
| 138 |
|
| 139 |
out = sanitize_payload(payload) |
| 140 |
assert isinstance(out, dict) |
| 141 |
assert out["self"] == "<recursive>" |
| 142 |
|
| 143 |
|
| 144 |
def test_sanitize_payload_max_depth_truncation() -> None: |
| 145 |
"""Depth limits protect against overly deep nested structures.""" |
| 146 |
opts = SanitizeOptions(max_depth=2) |
| 147 |
payload = {"a": {"b": {"c": "d"}}} |
| 148 |
|
| 149 |
out = sanitize_payload(payload, options=opts) |
| 150 |
assert isinstance(out, dict) |
| 151 |
assert isinstance(out["a"], dict) |
| 152 |
assert isinstance(out["a"]["b"], dict) |
| 153 |
assert out["a"]["b"]["c"] == "<truncated: max depth reached>" |
| 154 |
|
| 155 |
|
| 156 |
def test_sanitize_payload_max_items_truncation_in_list_branch() -> None: |
| 157 |
"""Item limits protect against very large sequences.""" |
| 158 |
opts = SanitizeOptions(max_items=2) |
| 159 |
payload = [1, 2, 3, 4] |
| 160 |
|
| 161 |
out = sanitize_payload(payload, options=opts) |
| 162 |
assert isinstance(out, list) |
| 163 |
assert out[-1] == "<truncated: limit reached>" |
| 164 |
|
| 165 |
|
| 166 |
def test_sanitize_payload_global_item_limit_guard_message() -> None: |
| 167 |
"""The global max_items guard stops work consistently.""" |
| 168 |
opts = SanitizeOptions(max_items=1) |
| 169 |
payload = {"a": "x", "b": "y"} |
| 170 |
|
| 171 |
out = sanitize_payload(payload, options=opts) |
| 172 |
assert isinstance(out, dict) |
| 173 |
assert any( |
| 174 |
v == "<truncated: global item limit reached>" or v == "..." |
| 175 |
for v in out.values() |
| 176 |
) |
| 177 |
|
| 178 |
|
| 179 |
def test_sanitize_payload_falls_back_to_repr_for_unknown_objects() -> None: |
| 180 |
"""Unknown objects are converted with repr() for logging.""" |
| 181 |
assert sanitize_payload(_ReprOnly()) == "<repr-only>" |
| 182 |
|
| 183 |
|
| 184 |
def test_sanitize_payload_blob_key_enables_blob_mode_and_summarizes() -> None: |
| 185 |
"""Blob-like keys enable blob_mode and trigger <blob-string ...>.""" |
| 186 |
opts = SanitizeOptions( |
| 187 |
aggressive_blob_keys=True, |
| 188 |
|
| 189 |
max_str_len=999999, |
| 190 |
preview=8, |
| 191 |
) |
| 192 |
|
| 193 |
payload = { |
| 194 |
|
| 195 |
"base64_attachments": "ABCDEF", |
| 196 |
} |
| 197 |
|
| 198 |
out = sanitize_payload(payload, options=opts) |
| 199 |
|
| 200 |
assert isinstance(out, dict) |
| 201 |
assert "base64_attachments" in out |
| 202 |
assert isinstance(out["base64_attachments"], str) |
| 203 |
assert out["base64_attachments"].startswith("<string len=6 blob ") |
| 204 |
assert "head=" in out["base64_attachments"] |
| 205 |
assert "tail=" in out["base64_attachments"] |
| 206 |
|
| 207 |
|
| 208 |
def test_sanitize_payload_dict_key_passthrough_for_non_str_bytes_key() -> None: |
| 209 |
""" |
| 210 |
Verify non-string, non-bytes dictionary keys are preserved unchanged. |
| 211 |
|
| 212 |
This exercises the fallback path in _summarize_key(), ensuring unexpected |
| 213 |
key types (such as integers) are passed through safely during sanitisation. |
| 214 |
""" |
| 215 |
payload = { |
| 216 |
|
| 217 |
42: "value", |
| 218 |
|
| 219 |
"k": "v", |
| 220 |
} |
| 221 |
|
| 222 |
out = sanitize_payload(payload) |
| 223 |
|
| 224 |
assert isinstance(out, dict) |
| 225 |
assert 42 in out |
| 226 |
assert out[42] == "value" |
| 227 |
assert out["k"] == "v" |