2018-02-15 13:06:14 -08:00
|
|
|
"""Test real IP middleware."""
|
2019-12-09 11:59:38 +01:00
|
|
|
from ipaddress import ip_network
|
|
|
|
|
2018-02-15 13:06:14 -08:00
|
|
|
from aiohttp import web
|
|
|
|
from aiohttp.hdrs import X_FORWARDED_FOR
|
|
|
|
|
|
|
|
from homeassistant.components.http.const import KEY_REAL_IP
|
2019-12-09 11:59:38 +01:00
|
|
|
from homeassistant.components.http.real_ip import setup_real_ip
|
2018-02-15 13:06:14 -08:00
|
|
|
|
|
|
|
|
2018-03-09 09:51:49 +08:00
|
|
|
async def mock_handler(request):
|
2018-08-24 11:28:43 +03:00
|
|
|
"""Return the real IP as text."""
|
2018-02-15 13:06:14 -08:00
|
|
|
return web.Response(text=str(request[KEY_REAL_IP]))
|
|
|
|
|
|
|
|
|
2018-03-15 13:49:49 -07:00
|
|
|
async def test_ignore_x_forwarded_for(aiohttp_client):
|
2018-02-15 13:06:14 -08:00
|
|
|
"""Test that we get the IP from the transport."""
|
|
|
|
app = web.Application()
|
2019-07-31 12:25:30 -07:00
|
|
|
app.router.add_get("/", mock_handler)
|
2018-06-28 09:16:11 -04:00
|
|
|
setup_real_ip(app, False, [])
|
2018-02-15 13:06:14 -08:00
|
|
|
|
2018-03-15 13:49:49 -07:00
|
|
|
mock_api_client = await aiohttp_client(app)
|
2018-02-15 13:06:14 -08:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
resp = await mock_api_client.get("/", headers={X_FORWARDED_FOR: "255.255.255.255"})
|
2018-02-15 13:06:14 -08:00
|
|
|
assert resp.status == 200
|
2018-03-09 09:51:49 +08:00
|
|
|
text = await resp.text()
|
2019-07-31 12:25:30 -07:00
|
|
|
assert text != "255.255.255.255"
|
2018-02-15 13:06:14 -08:00
|
|
|
|
|
|
|
|
2018-06-28 09:16:11 -04:00
|
|
|
async def test_use_x_forwarded_for_without_trusted_proxy(aiohttp_client):
|
2018-02-15 13:06:14 -08:00
|
|
|
"""Test that we get the IP from the transport."""
|
|
|
|
app = web.Application()
|
2019-07-31 12:25:30 -07:00
|
|
|
app.router.add_get("/", mock_handler)
|
2018-06-28 09:16:11 -04:00
|
|
|
setup_real_ip(app, True, [])
|
|
|
|
|
|
|
|
mock_api_client = await aiohttp_client(app)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
resp = await mock_api_client.get("/", headers={X_FORWARDED_FOR: "255.255.255.255"})
|
2018-06-28 09:16:11 -04:00
|
|
|
assert resp.status == 200
|
|
|
|
text = await resp.text()
|
2019-07-31 12:25:30 -07:00
|
|
|
assert text != "255.255.255.255"
|
2018-06-28 09:16:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def test_use_x_forwarded_for_with_trusted_proxy(aiohttp_client):
|
|
|
|
"""Test that we get the IP from the transport."""
|
|
|
|
app = web.Application()
|
2019-07-31 12:25:30 -07:00
|
|
|
app.router.add_get("/", mock_handler)
|
|
|
|
setup_real_ip(app, True, [ip_network("127.0.0.1")])
|
2018-02-15 13:06:14 -08:00
|
|
|
|
2018-03-15 13:49:49 -07:00
|
|
|
mock_api_client = await aiohttp_client(app)
|
2018-02-15 13:06:14 -08:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
resp = await mock_api_client.get("/", headers={X_FORWARDED_FOR: "255.255.255.255"})
|
2018-02-15 13:06:14 -08:00
|
|
|
assert resp.status == 200
|
2018-03-09 09:51:49 +08:00
|
|
|
text = await resp.text()
|
2019-07-31 12:25:30 -07:00
|
|
|
assert text == "255.255.255.255"
|
2018-06-29 16:27:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def test_use_x_forwarded_for_with_untrusted_proxy(aiohttp_client):
|
|
|
|
"""Test that we get the IP from the transport."""
|
|
|
|
app = web.Application()
|
2019-07-31 12:25:30 -07:00
|
|
|
app.router.add_get("/", mock_handler)
|
|
|
|
setup_real_ip(app, True, [ip_network("1.1.1.1")])
|
2018-06-29 16:27:06 -04:00
|
|
|
|
|
|
|
mock_api_client = await aiohttp_client(app)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
resp = await mock_api_client.get("/", headers={X_FORWARDED_FOR: "255.255.255.255"})
|
2018-06-29 16:27:06 -04:00
|
|
|
assert resp.status == 200
|
|
|
|
text = await resp.text()
|
2019-07-31 12:25:30 -07:00
|
|
|
assert text != "255.255.255.255"
|
2018-06-29 16:27:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def test_use_x_forwarded_for_with_spoofed_header(aiohttp_client):
|
|
|
|
"""Test that we get the IP from the transport."""
|
|
|
|
app = web.Application()
|
2019-07-31 12:25:30 -07:00
|
|
|
app.router.add_get("/", mock_handler)
|
|
|
|
setup_real_ip(app, True, [ip_network("127.0.0.1")])
|
2018-06-29 16:27:06 -04:00
|
|
|
|
|
|
|
mock_api_client = await aiohttp_client(app)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
resp = await mock_api_client.get(
|
|
|
|
"/", headers={X_FORWARDED_FOR: "222.222.222.222, 255.255.255.255"}
|
|
|
|
)
|
2018-06-29 16:27:06 -04:00
|
|
|
assert resp.status == 200
|
|
|
|
text = await resp.text()
|
2019-07-31 12:25:30 -07:00
|
|
|
assert text == "255.255.255.255"
|
2018-06-29 16:27:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def test_use_x_forwarded_for_with_nonsense_header(aiohttp_client):
|
|
|
|
"""Test that we get the IP from the transport."""
|
|
|
|
app = web.Application()
|
2019-07-31 12:25:30 -07:00
|
|
|
app.router.add_get("/", mock_handler)
|
|
|
|
setup_real_ip(app, True, [ip_network("127.0.0.1")])
|
2018-06-29 16:27:06 -04:00
|
|
|
|
|
|
|
mock_api_client = await aiohttp_client(app)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
resp = await mock_api_client.get(
|
|
|
|
"/", headers={X_FORWARDED_FOR: "This value is invalid"}
|
|
|
|
)
|
2018-06-29 16:27:06 -04:00
|
|
|
assert resp.status == 200
|
|
|
|
text = await resp.text()
|
2019-07-31 12:25:30 -07:00
|
|
|
assert text == "127.0.0.1"
|