| 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 |
"""Test where template values may be placed and what they can change. |
| 28 |
|
| 29 |
URL markers can change any field, including the destination. Markers in named |
| 30 |
YAML settings change only that setting, making them safer for untrusted input. |
| 31 |
The Apprise API uses the same rules at every access level. |
| 32 |
""" |
| 33 |
|
| 34 |
# Disable logging for a cleaner testing output |
| 35 |
import logging |
| 36 |
from unittest import mock |
| 37 |
|
| 38 |
import pytest |
| 39 |
import requests |
| 40 |
|
| 41 |
from apprise import Apprise, AppriseConfig |
| 42 |
|
| 43 |
logging.disable(logging.CRITICAL) |
| 44 |
|
| 45 |
|
| 46 |
def load(content): |
| 47 |
"""Build an Apprise object around a YAML configuration.""" |
| 48 |
apobj = Apprise() |
| 49 |
config = AppriseConfig() |
| 50 |
assert config.add_config(content, format="yaml") |
| 51 |
assert apobj.add(config) |
| 52 |
return apobj |
| 53 |
|
| 54 |
|
| 55 |
@pytest.fixture |
| 56 |
def sent(): |
| 57 |
"""Accept every outbound request so delivery reports success.""" |
| 58 |
with mock.patch("requests.request") as request: |
| 59 |
response = mock.Mock() |
| 60 |
response.status_code = requests.codes.ok |
| 61 |
response.content = "" |
| 62 |
response.headers = {} |
| 63 |
request.return_value = response |
| 64 |
yield request |
| 65 |
|
| 66 |
|
| 67 |
# A value that would reach further than one field if anything re-parsed |
| 68 |
# it. Every placement below is checked against this. |
| 69 |
REACHING_VALUE = "evil.example/hook" |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.parametrize( |
| 73 |
("placement", "entry"), |
| 74 |
[ |
| 75 |
("whole host", "json://${V}/"), |
| 76 |
("part of a host", "json://${V}.example/"), |
| 77 |
("host beside credentials", "json://user:pass@${V}/"), |
| 78 |
("host beside a path", "json://${V}/api/notify"), |
| 79 |
("host beside a query value", "json://${V}/?apikey=stored"), |
| 80 |
("host beside a header", "json://${V}/?:Authorization=Bearer+s"), |
| 81 |
("host beside a payload field", "json://${V}/?+token=stored"), |
| 82 |
("user", "json://${V}:pass@host/"), |
| 83 |
("password", "json://user:${V}@host/"), |
| 84 |
("path", "json://host/${V}"), |
| 85 |
("query value", "json://host/?:custom=${V}"), |
| 86 |
], |
| 87 |
) |
| 88 |
def test_variable_in_any_url_field(placement, entry, sent): |
| 89 |
"""Confirm every URL field accepts a variable, including the host.""" |
| 90 |
apobj = load("template:\n - v\nurls:\n - {}\n".format(entry)) |
| 91 |
|
| 92 |
assert next(apobj.find(template={"v": "filled-in"})) is not None |
| 93 |
|
| 94 |
|
| 95 |
def test_all_url_fields_as_variables(sent): |
| 96 |
"""One URL can be made entirely of variables.""" |
| 97 |
apobj = load( |
| 98 |
"template:\n - user\n - secret\n - host\n" |
| 99 |
"urls:\n - json://${USER}:${SECRET}@${HOST}/\n" |
| 100 |
) |
| 101 |
|
| 102 |
service = next( |
| 103 |
apobj.find( |
| 104 |
template={"user": "me", "secret": "mine", "host": "my.example"} |
| 105 |
) |
| 106 |
) |
| 107 |
assert service.user == "me" |
| 108 |
assert service.password == "mine" |
| 109 |
assert service.host == "my.example" |
| 110 |
|
| 111 |
|
| 112 |
def test_host_variable_with_settings(sent): |
| 113 |
"""Settings written under the URL do not hold the entry back.""" |
| 114 |
apobj = load( |
| 115 |
"template:\n - host\nurls:\n - json://user:pass@${HOST}/:\n" |
| 116 |
" - headers:\n Authorization: Bearer stored\n" |
| 117 |
) |
| 118 |
|
| 119 |
assert next(apobj.find(template={"host": "my.example"})).host == ( |
| 120 |
"my.example" |
| 121 |
) |
| 122 |
|
| 123 |
|
| 124 |
@pytest.mark.parametrize( |
| 125 |
"source", |
| 126 |
["supplied", "environment", "default"], |
| 127 |
) |
| 128 |
def test_host_variable_value_sources(source, monkeypatch, sent): |
| 129 |
"""Treat supplied, environment, and default host values alike.""" |
| 130 |
declaration = " host: my.example" if source == "default" else " - host" |
| 131 |
supplied = {"host": "my.example"} if source == "supplied" else None |
| 132 |
if source == "environment": |
| 133 |
monkeypatch.setenv("APPRISE_TEMPLATE_HOST", "my.example") |
| 134 |
|
| 135 |
apobj = load( |
| 136 |
"template:\n{}\nurls:\n - json://user:pass@${{HOST}}/\n".format( |
| 137 |
declaration |
| 138 |
) |
| 139 |
) |
| 140 |
|
| 141 |
service = next(apobj.find(template=supplied)) |
| 142 |
assert service.host == "my.example" |
| 143 |
assert service.user == "user" |
| 144 |
|
| 145 |
|
| 146 |
def test_url_variable_changes_destination(sent): |
| 147 |
"""A host variable chooses where the URL and its credentials are sent.""" |
| 148 |
apobj = load("template:\n - v\nurls:\n - json://user:pass@${V}/\n") |
| 149 |
|
| 150 |
service = next(apobj.find(template={"v": REACHING_VALUE})) |
| 151 |
assert service.host == REACHING_VALUE |
| 152 |
assert service.user == "user" |
| 153 |
assert service.password == "pass" |
| 154 |
|
| 155 |
|
| 156 |
def test_setting_variable_keeps_destination(sent): |
| 157 |
"""A named setting changes one option without moving the destination.""" |
| 158 |
apobj = load( |
| 159 |
"template:\n - v\n" |
| 160 |
"urls:\n - json://user:[email protected]/:\n - to: ${V}\n" |
| 161 |
) |
| 162 |
|
| 163 |
service = next(apobj.find(template={"v": REACHING_VALUE})) |
| 164 |
assert service.host == "fixed.example" |
| 165 |
assert service.user == "user" |
| 166 |
assert service.password == "pass" |
| 167 |
|
| 168 |
|
| 169 |
def test_url_and_setting_scope(sent): |
| 170 |
"""Contrast unrestricted URL placement with a scoped YAML setting.""" |
| 171 |
in_url = load("template:\n - v\nurls:\n - json://user:pass@${V}/\n") |
| 172 |
in_setting = load( |
| 173 |
"template:\n - v\n" |
| 174 |
"urls:\n - json://user:[email protected]/:\n - to: ${V}\n" |
| 175 |
) |
| 176 |
|
| 177 |
moved = next(in_url.find(template={"v": REACHING_VALUE})) |
| 178 |
fixed = next(in_setting.find(template={"v": REACHING_VALUE})) |
| 179 |
|
| 180 |
assert moved.host != fixed.host |
| 181 |
assert fixed.host == "fixed.example" |
| 182 |
|
| 183 |
|
| 184 |
@pytest.mark.parametrize( |
| 185 |
"position", |
| 186 |
[ |
| 187 |
"json://localhost/?tag=${V}", |
| 188 |
"json://localhost/:\n - tag: ${V}", |
| 189 |
"${V}://localhost/", |
| 190 |
], |
| 191 |
) |
| 192 |
def test_disallowed_variable_positions(position): |
| 193 |
"""Do not let values choose a service or a tag. |
| 194 |
|
| 195 |
These fields are read before template values are applied. |
| 196 |
""" |
| 197 |
apobj = Apprise() |
| 198 |
config = AppriseConfig() |
| 199 |
config.add_config( |
| 200 |
"template:\n - v\nurls:\n - {}\n".format(position), |
| 201 |
format="yaml", |
| 202 |
) |
| 203 |
apobj.add(config) |
| 204 |
|
| 205 |
assert list(apobj.find(template={"v": "anything"})) == [] |
| 206 |
|
| 207 |
|
| 208 |
def test_yaml_setting_name_stays_literal(): |
| 209 |
"""Only values are replaced underneath a URL, never the names. |
| 210 |
|
| 211 |
A setting is called whatever the configuration file called it, so a |
| 212 |
marker written as one stays plain text. |
| 213 |
""" |
| 214 |
apobj = Apprise() |
| 215 |
config = AppriseConfig() |
| 216 |
config.add_config( |
| 217 |
"template:\n - v\n" |
| 218 |
"urls:\n - json://localhost/:\n" |
| 219 |
" '+${V}': 1\n '+X': ${V}\n", |
| 220 |
format="yaml", |
| 221 |
) |
| 222 |
apobj.add(config) |
| 223 |
|
| 224 |
service = next(apobj.find(template={"v": "supplied"})) |
| 225 |
|
| 226 |
# The name kept its marker; only the value took the supplied text |
| 227 |
assert service.headers == {"${V}": "1", "X": "supplied"} |