| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
"""Verify that logging masks credentials in file-like URL inputs. |
| 29 |
|
| 30 |
See CWE-312 for the related cleartext-storage weakness. |
| 31 |
""" |
| 32 |
|
| 33 |
import logging |
| 34 |
import os |
| 35 |
from unittest import mock |
| 36 |
from urllib.parse import urlparse |
| 37 |
|
| 38 |
import pytest |
| 39 |
import requests |
| 40 |
|
| 41 |
from apprise import ( |
| 42 |
LOGGER_NAME, |
| 43 |
AppriseAsset, |
| 44 |
AppriseAttachment, |
| 45 |
AppriseConfig, |
| 46 |
utils, |
| 47 |
) |
| 48 |
from apprise.attachment import AttachBase |
| 49 |
from apprise.common import ContentLocation |
| 50 |
from apprise.utils.pgp import ApprisePGPController |
| 51 |
|
| 52 |
# A password that must never turn up in a log line |
| 53 |
SECRET = "pass123" |
| 54 |
|
| 55 |
# URLs shaped like supported key and attachment inputs. |
| 56 |
SECRET_KEY_URL = f"https://user:{SECRET}@example.com/private_key.pem" |
| 57 |
SECRET_ATTACH_URL = f"https://user:{SECRET}@example.com/image.png" |
| 58 |
|
| 59 |
|
| 60 |
@pytest.fixture |
| 61 |
def logs(caplog): |
| 62 |
"""Capture Apprise logs, undoing logging disabled by other test modules.""" |
| 63 |
|
| 64 |
disabled = logging.root.manager.disable |
| 65 |
logging.disable(logging.NOTSET) |
| 66 |
caplog.set_level(1, logger=LOGGER_NAME) |
| 67 |
yield caplog |
| 68 |
logging.disable(disabled) |
| 69 |
|
| 70 |
|
| 71 |
@pytest.fixture |
| 72 |
def unreachable(): |
| 73 |
"""Makes any attempt to fetch a remote file fail.""" |
| 74 |
|
| 75 |
with mock.patch( |
| 76 |
"requests.get", side_effect=requests.RequestException("nope") |
| 77 |
): |
| 78 |
yield |
| 79 |
|
| 80 |
|
| 81 |
def test_pem_private_keyfile_credentials(logs, unreachable, tmpdir): |
| 82 |
"""A PEM private key url does not put its password in the log.""" |
| 83 |
|
| 84 |
pem = utils.pem.ApprisePEMController(path=str(tmpdir)) |
| 85 |
assert pem.load_private_key(SECRET_KEY_URL) is False |
| 86 |
|
| 87 |
# The failure is reported without the password. |
| 88 |
assert "Could not access PEM Private Key" in logs.text |
| 89 |
assert SECRET not in logs.text |
| 90 |
|
| 91 |
# Parse logged URLs before checking which host was retained. |
| 92 |
urls_in_logs = [token for token in logs.text.split() if "://" in token] |
| 93 |
assert any( |
| 94 |
urlparse(token).hostname == "example.com" for token in urls_in_logs |
| 95 |
) |
| 96 |
|
| 97 |
# The masked form is what was written. |
| 98 |
assert "user:[email protected]" in logs.text |
| 99 |
|
| 100 |
|
| 101 |
def test_pem_public_keyfile_credentials(logs, unreachable, tmpdir): |
| 102 |
"""A PEM public key url does not put its password in the log.""" |
| 103 |
|
| 104 |
pem = utils.pem.ApprisePEMController(path=str(tmpdir)) |
| 105 |
assert pem.load_public_key(SECRET_KEY_URL) is False |
| 106 |
|
| 107 |
assert "Could not access PEM Public Key" in logs.text |
| 108 |
assert SECRET not in logs.text |
| 109 |
|
| 110 |
|
| 111 |
def test_attachment_url_credentials(logs, unreachable): |
| 112 |
"""Adding a remote attachment does not log its password.""" |
| 113 |
|
| 114 |
attach = AppriseAttachment() |
| 115 |
assert attach.add(SECRET_ATTACH_URL) is True |
| 116 |
|
| 117 |
assert "Loading attachment" in logs.text |
| 118 |
assert SECRET not in logs.text |
| 119 |
|
| 120 |
|
| 121 |
def test_attachment_disabled_credentials(logs): |
| 122 |
"""Refusing an attachment does not log its password either.""" |
| 123 |
|
| 124 |
attach = AppriseAttachment(location=ContentLocation.INACCESSIBLE) |
| 125 |
assert attach.add(SECRET_ATTACH_URL) is False |
| 126 |
|
| 127 |
assert "Attachments are disabled" in logs.text |
| 128 |
assert SECRET not in logs.text |
| 129 |
|
| 130 |
|
| 131 |
def test_attachment_parse_failure_credentials(logs): |
| 132 |
"""Mask credentials when an attachment URL cannot be parsed.""" |
| 133 |
|
| 134 |
url = f"https://user:{SECRET}@example.com:bad/image.png" |
| 135 |
assert AppriseAttachment().add(url) is False |
| 136 |
|
| 137 |
assert "Unparseable URL" in logs.text |
| 138 |
assert SECRET not in logs.text |
| 139 |
|
| 140 |
|
| 141 |
def test_attachment_load_failure_credentials(logs): |
| 142 |
"""Mask credentials when attachment construction raises an exception.""" |
| 143 |
|
| 144 |
class BadAttach(AttachBase): |
| 145 |
def __init__(self, **kwargs): |
| 146 |
super().__init__(**kwargs) |
| 147 |
raise TypeError() |
| 148 |
|
| 149 |
manager = AppriseAttachment.instantiate.__globals__["A_MGR"] |
| 150 |
manager["securebad"] = BadAttach |
| 151 |
try: |
| 152 |
url = f"securebad://user:{SECRET}@localhost/file" |
| 153 |
assert AppriseAttachment.instantiate(url) is None |
| 154 |
finally: |
| 155 |
del manager["securebad"] |
| 156 |
|
| 157 |
assert "Could not load URL" in logs.text |
| 158 |
assert SECRET not in logs.text |
| 159 |
|
| 160 |
|
| 161 |
def test_secure_logging_disabled(logs, unreachable, tmpdir): |
| 162 |
"""Disabling secure logging exposes the full value for debugging.""" |
| 163 |
|
| 164 |
asset = AppriseAsset(secure_logging=False) |
| 165 |
pem = utils.pem.ApprisePEMController(path=str(tmpdir), asset=asset) |
| 166 |
assert pem.load_private_key(SECRET_KEY_URL) is False |
| 167 |
|
| 168 |
# With masking off, the full URL and password appear. |
| 169 |
assert SECRET in logs.text |
| 170 |
assert SECRET_KEY_URL in logs.text |
| 171 |
|
| 172 |
|
| 173 |
def test_pem_keyfile_read_errors(logs, tmpdir): |
| 174 |
"""Keep a failed local key path readable so users can identify it.""" |
| 175 |
|
| 176 |
keyfile = os.path.join(str(tmpdir), "private_key.pem") |
| 177 |
with open(keyfile, "w") as f: |
| 178 |
f.write("not a real key") |
| 179 |
|
| 180 |
pem = utils.pem.ApprisePEMController(path=str(tmpdir)) |
| 181 |
|
| 182 |
# Simulate a file disappearing before it is opened. |
| 183 |
with mock.patch("builtins.open", side_effect=FileNotFoundError): |
| 184 |
assert pem.load_private_key(keyfile) is False |
| 185 |
|
| 186 |
assert "PEM Private Key file not found" in logs.text |
| 187 |
assert keyfile in logs.text |
| 188 |
|
| 189 |
# Simulate a file that cannot be read. |
| 190 |
logs.clear() |
| 191 |
with mock.patch("builtins.open", side_effect=OSError): |
| 192 |
assert pem.load_private_key(keyfile) is False |
| 193 |
|
| 194 |
assert "Error accessing PEM Private Key file" in logs.text |
| 195 |
assert keyfile in logs.text |
| 196 |
|
| 197 |
# Public keys handle the same failures. |
| 198 |
logs.clear() |
| 199 |
with mock.patch("builtins.open", side_effect=FileNotFoundError): |
| 200 |
assert pem.load_public_key(keyfile) is False |
| 201 |
|
| 202 |
assert "PEM Public Key file not found" in logs.text |
| 203 |
assert keyfile in logs.text |
| 204 |
|
| 205 |
logs.clear() |
| 206 |
with mock.patch("builtins.open", side_effect=OSError): |
| 207 |
assert pem.load_public_key(keyfile) is False |
| 208 |
|
| 209 |
assert "Error accessing PEM Public Key file" in logs.text |
| 210 |
assert keyfile in logs.text |
| 211 |
|
| 212 |
|
| 213 |
@pytest.mark.parametrize("method", ("private_key", "public_key")) |
| 214 |
def test_pgp_key_credentials(logs, tmpdir, method): |
| 215 |
"""Mask credentials in PGP key read failures.""" |
| 216 |
|
| 217 |
pgp = ApprisePGPController(path=str(tmpdir)) |
| 218 |
keyfile_method = f"{method}file" |
| 219 |
error = OSError("disk error") |
| 220 |
with ( |
| 221 |
mock.patch.object(pgp, keyfile_method, return_value=SECRET_KEY_URL), |
| 222 |
mock.patch("builtins.open", side_effect=error), |
| 223 |
): |
| 224 |
assert getattr(pgp, method)() is None |
| 225 |
|
| 226 |
assert "disk error" in logs.text |
| 227 |
assert SECRET not in logs.text |
| 228 |
|
| 229 |
|
| 230 |
def test_config_parse_failure_credentials(logs): |
| 231 |
"""Mask credentials when a configuration URL cannot be parsed.""" |
| 232 |
|
| 233 |
url = f"https://user:{SECRET}@example.com:bad/config.yml" |
| 234 |
assert AppriseConfig().add(url) is False |
| 235 |
|
| 236 |
assert "Unparseable URL" in logs.text |
| 237 |
assert SECRET not in logs.text |