| 1 |
# META: timeout=long |
| 2 |
|
| 3 |
import pytest |
| 4 |
from support.network import get_host, websocket_request |
| 5 |
|
| 6 |
|
| 7 |
@pytest.mark.parametrize( |
| 8 |
"hostname, port_type, status", |
| 9 |
[ |
| 10 |
# Valid hosts |
| 11 |
("localhost", "server_port", 101), |
| 12 |
("localhost", "default_port", 101), |
| 13 |
("127.0.0.1", "server_port", 101), |
| 14 |
("127.0.0.1", "default_port", 101), |
| 15 |
("[::1]", "server_port", 101), |
| 16 |
("[::1]", "default_port", 101), |
| 17 |
("192.168.8.1", "server_port", 101), |
| 18 |
("192.168.8.1", "default_port", 101), |
| 19 |
("[fdf8:f535:82e4::53]", "server_port", 101), |
| 20 |
("[fdf8:f535:82e4::53]", "default_port", 101), |
| 21 |
# Invalid hosts |
| 22 |
("mozilla.org", "server_port", 400), |
| 23 |
("mozilla.org", "wrong_port", 400), |
| 24 |
("mozilla.org", "default_port", 400), |
| 25 |
("localhost", "wrong_port", 400), |
| 26 |
("127.0.0.1", "wrong_port", 400), |
| 27 |
("[::1]", "wrong_port", 400), |
| 28 |
("192.168.8.1", "wrong_port", 400), |
| 29 |
("[fdf8:f535:82e4::53]", "wrong_port", 400), |
| 30 |
], |
| 31 |
ids=[ |
| 32 |
# Valid hosts |
| 33 |
"localhost with same port as RemoteAgent", |
| 34 |
"localhost with default port", |
| 35 |
"127.0.0.1 (loopback) with same port as RemoteAgent", |
| 36 |
"127.0.0.1 (loopback) with default port", |
| 37 |
"[::1] (ipv6 loopback) with same port as RemoteAgent", |
| 38 |
"[::1] (ipv6 loopback) with default port", |
| 39 |
"ipv4 address with same port as RemoteAgent", |
| 40 |
"ipv4 address with default port", |
| 41 |
"ipv6 address with same port as RemoteAgent", |
| 42 |
"ipv6 address with default port", |
| 43 |
# Invalid hosts |
| 44 |
"random hostname with the same port as RemoteAgent", |
| 45 |
"random hostname with a different port than RemoteAgent", |
| 46 |
"random hostname with default port", |
| 47 |
"localhost with a different port than RemoteAgent", |
| 48 |
"127.0.0.1 (loopback) with a different port than RemoteAgent", |
| 49 |
"[::1] (ipv6 loopback) with a different port than RemoteAgent", |
| 50 |
"ipv4 address with a different port than RemoteAgent", |
| 51 |
"ipv6 address with a different port than RemoteAgent", |
| 52 |
], |
| 53 |
) |
| 54 |
def test_host_header(browser, hostname, port_type, status): |
| 55 |
# Request a default browser |
| 56 |
current_browser = browser(use_bidi=True) |
| 57 |
server_host = current_browser.remote_agent_host |
| 58 |
server_port = current_browser.remote_agent_port |
| 59 |
test_host = get_host(port_type, hostname, server_port) |
| 60 |
|
| 61 |
response = websocket_request(server_host, server_port, host=test_host) |
| 62 |
assert response.status == status |
| 63 |
|
| 64 |
|
| 65 |
@pytest.mark.parametrize( |
| 66 |
"hostname, port_type, status", |
| 67 |
[ |
| 68 |
# Allowed hosts |
| 69 |
("testhost", "server_port", 101), |
| 70 |
("testhost", "default_port", 101), |
| 71 |
("testhost", "wrong_port", 400), |
| 72 |
# IP addresses |
| 73 |
("192.168.8.1", "server_port", 101), |
| 74 |
("192.168.8.1", "default_port", 101), |
| 75 |
("[fdf8:f535:82e4::53]", "server_port", 101), |
| 76 |
("[fdf8:f535:82e4::53]", "default_port", 101), |
| 77 |
("127.0.0.1", "server_port", 101), |
| 78 |
("127.0.0.1", "default_port", 101), |
| 79 |
("[::1]", "server_port", 101), |
| 80 |
("[::1]", "default_port", 101), |
| 81 |
# Localhost |
| 82 |
("localhost", "server_port", 400), |
| 83 |
("localhost", "default_port", 400), |
| 84 |
], |
| 85 |
ids=[ |
| 86 |
# Allowed hosts |
| 87 |
"allowed host with same port as RemoteAgent", |
| 88 |
"allowed host with default port", |
| 89 |
"allowed host with wrong port", |
| 90 |
# IP addresses |
| 91 |
"ipv4 address with same port as RemoteAgent", |
| 92 |
"ipv4 address with default port", |
| 93 |
"ipv6 address with same port as RemoteAgent", |
| 94 |
"ipv6 address with default port", |
| 95 |
"127.0.0.1 (loopback) with same port as RemoteAgent", |
| 96 |
"127.0.0.1 (loopback) with default port", |
| 97 |
"[::1] (ipv6 loopback) with same port as RemoteAgent", |
| 98 |
"[::1] (ipv6 loopback) with default port", |
| 99 |
# Localhost |
| 100 |
"localhost with same port as RemoteAgent", |
| 101 |
"localhost with default port", |
| 102 |
], |
| 103 |
) |
| 104 |
def test_allowed_hosts(browser, hostname, port_type, status): |
| 105 |
# Request a browser with custom allowed hosts. |
| 106 |
current_browser = browser( |
| 107 |
use_bidi=True, |
| 108 |
extra_args=["--remote-allow-hosts", "testhost"], |
| 109 |
) |
| 110 |
server_host = current_browser.remote_agent_host |
| 111 |
server_port = current_browser.remote_agent_port |
| 112 |
test_host = get_host(port_type, hostname, server_port) |
| 113 |
|
| 114 |
response = websocket_request(server_host, server_port, host=test_host) |
| 115 |
assert response.status == status |
| 116 |
|
| 117 |
|
| 118 |
@pytest.mark.parametrize( |
| 119 |
"origin, status", |
| 120 |
[ |
| 121 |
(None, 101), |
| 122 |
("", 400), |
| 123 |
("sometext", 400), |
| 124 |
("http://localhost:1234", 400), |
| 125 |
], |
| 126 |
) |
| 127 |
def test_origin_header(browser, origin, status): |
| 128 |
# Request a default browser. |
| 129 |
current_browser = browser(use_bidi=True) |
| 130 |
server_host = current_browser.remote_agent_host |
| 131 |
server_port = current_browser.remote_agent_port |
| 132 |
response = websocket_request(server_host, server_port, origin=origin) |
| 133 |
assert response.status == status |
| 134 |
|
| 135 |
|
| 136 |
@pytest.mark.parametrize( |
| 137 |
"origin, status", |
| 138 |
[ |
| 139 |
(None, 101), |
| 140 |
("", 400), |
| 141 |
("sometext", 400), |
| 142 |
("http://localhost:1234", 101), |
| 143 |
("https://localhost:1234", 400), |
| 144 |
], |
| 145 |
) |
| 146 |
def test_allowed_origins(browser, origin, status): |
| 147 |
# Request a browser with custom allowed origins. |
| 148 |
current_browser = browser( |
| 149 |
use_bidi=True, |
| 150 |
extra_args=["--remote-allow-origins", "http://localhost:1234"], |
| 151 |
) |
| 152 |
server_port = current_browser.remote_agent_port |
| 153 |
|
| 154 |
# Both `localhost` and `127.0.0.1` have to accept connections. |
| 155 |
for target_host in ["127.0.0.1", "localhost"]: |
| 156 |
print(f"Connecting to the WebSocket via host {target_host}") |
| 157 |
response = websocket_request(target_host, server_port, origin=origin) |
| 158 |
assert response.status == status |