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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
import pytest
from werkzeug.middleware.proxy_fix import ProxyFix
from werkzeug.routing import Map
from werkzeug.routing import Rule
from werkzeug.test import create_environ
from werkzeug.utils import redirect
from werkzeug.wrappers import Request
from werkzeug.wrappers import Response
@pytest.mark.parametrize(
("kwargs", "base", "url_root"),
(
pytest.param(
{},
{
"REMOTE_ADDR": "192.168.0.2",
"HTTP_HOST": "spam",
"HTTP_X_FORWARDED_FOR": "192.168.0.1",
"HTTP_X_FORWARDED_PROTO": "https",
},
"https://spam/",
id="for",
),
pytest.param(
{"x_proto": 1},
{"HTTP_HOST": "spam", "HTTP_X_FORWARDED_PROTO": "https"},
"https://spam/",
id="proto",
),
pytest.param(
{"x_host": 1},
{"HTTP_HOST": "spam", "HTTP_X_FORWARDED_HOST": "eggs"},
"http://eggs/",
id="host",
),
pytest.param(
{"x_port": 1},
{"HTTP_HOST": "spam", "HTTP_X_FORWARDED_PORT": "8080"},
"http://spam:8080/",
id="port, host without port",
),
pytest.param(
{"x_port": 1},
{"HTTP_HOST": "spam:9000", "HTTP_X_FORWARDED_PORT": "8080"},
"http://spam:8080/",
id="port, host with port",
),
pytest.param(
{"x_port": 1},
{
"SERVER_NAME": "spam",
"SERVER_PORT": "9000",
"HTTP_X_FORWARDED_PORT": "8080",
},
"http://spam:8080/",
id="port, name",
),
pytest.param(
{"x_prefix": 1},
{"HTTP_HOST": "spam", "HTTP_X_FORWARDED_PREFIX": "/eggs"},
"http://spam/eggs/",
id="prefix",
),
pytest.param(
{"x_for": 1, "x_proto": 1, "x_host": 1, "x_port": 1, "x_prefix": 1},
{
"REMOTE_ADDR": "192.168.0.2",
"HTTP_HOST": "spam:9000",
"HTTP_X_FORWARDED_FOR": "192.168.0.1",
"HTTP_X_FORWARDED_PROTO": "https",
"HTTP_X_FORWARDED_HOST": "eggs",
"HTTP_X_FORWARDED_PORT": "443",
"HTTP_X_FORWARDED_PREFIX": "/ham",
},
"https://eggs/ham/",
id="all",
),
pytest.param(
{"x_for": 2},
{
"REMOTE_ADDR": "192.168.0.3",
"HTTP_HOST": "spam",
"HTTP_X_FORWARDED_FOR": "192.168.0.1, 192.168.0.2",
},
"http://spam/",
id="multiple for",
),
pytest.param(
{"x_for": 0},
{
"REMOTE_ADDR": "192.168.0.1",
"HTTP_HOST": "spam",
"HTTP_X_FORWARDED_FOR": "192.168.0.2",
},
"http://spam/",
id="ignore 0",
),
pytest.param(
{"x_for": 3},
{
"REMOTE_ADDR": "192.168.0.1",
"HTTP_HOST": "spam",
"HTTP_X_FORWARDED_FOR": "192.168.0.3, 192.168.0.2",
},
"http://spam/",
id="ignore len < trusted",
),
pytest.param(
{},
{
"REMOTE_ADDR": "192.168.0.2",
"HTTP_HOST": "spam",
"HTTP_X_FORWARDED_FOR": "192.168.0.3, 192.168.0.1",
},
"http://spam/",
id="ignore untrusted",
),
pytest.param(
{"x_for": 2},
{
"REMOTE_ADDR": "192.168.0.1",
"HTTP_HOST": "spam",
"HTTP_X_FORWARDED_FOR": ", 192.168.0.3",
},
"http://spam/",
id="ignore empty",
),
pytest.param(
{"x_for": 2, "x_prefix": 1},
{
"REMOTE_ADDR": "192.168.0.2",
"HTTP_HOST": "spam",
"HTTP_X_FORWARDED_FOR": "192.168.0.1, 192.168.0.3",
"HTTP_X_FORWARDED_PREFIX": "/ham, /eggs",
},
"http://spam/eggs/",
id="prefix < for",
),
),
)
def test_proxy_fix(kwargs, base, url_root):
@Request.application
def app(request):
# for header
assert request.remote_addr == "192.168.0.1"
# proto, host, port, prefix headers
assert request.url_root == url_root
urls = url_map.bind_to_environ(request.environ)
# build includes prefix
assert urls.build("parrot") == "/".join((request.script_root, "parrot"))
# match doesn't include prefix
assert urls.match("/parrot")[0] == "parrot"
return Response("success")
url_map = Map([Rule("/parrot", endpoint="parrot")])
app = ProxyFix(app, **kwargs)
base.setdefault("REMOTE_ADDR", "192.168.0.1")
environ = create_environ(environ_overrides=base)
# host is always added, remove it if the test doesn't set it
if "HTTP_HOST" not in base:
del environ["HTTP_HOST"]
# ensure app request has correct headers
response = Response.from_app(app, environ)
assert response.get_data() == b"success"
# ensure redirect location is correct
redirect_app = redirect(url_map.bind_to_environ(environ).build("parrot"))
response = Response.from_app(redirect_app, environ)
location = response.headers["Location"]
assert location == url_root + "parrot"
|