| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 2 |
%% |
| 3 |
%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
%% purpose with or without fee is hereby granted, provided that the above |
| 5 |
%% copyright notice and this permission notice appear in all copies. |
| 6 |
%% |
| 7 |
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
-module(proxy_header_SUITE). |
| 16 |
-compile(export_all). |
| 17 |
-compile(nowarn_export_all). |
| 18 |
|
| 19 |
-import(ct_helper, [config/2]). |
| 20 |
-import(ct_helper, [doc/1]). |
| 21 |
-import(cowboy_test, [raw_send/2]). |
| 22 |
-import(cowboy_test, [raw_recv_head/1]). |
| 23 |
-import(cowboy_test, [raw_recv/3]). |
| 24 |
|
| 25 |
%% ct. |
| 26 |
|
| 27 |
all() -> |
| 28 |
[ |
| 29 |
{group, http}, |
| 30 |
{group, https}, |
| 31 |
{group, h2}, |
| 32 |
{group, h2c}, |
| 33 |
{group, h2c_upgrade} |
| 34 |
]. |
| 35 |
|
| 36 |
groups() -> |
| 37 |
Tests = ct_helper:all(?MODULE), |
| 38 |
[{h2c_upgrade, [parallel], Tests}|cowboy_test:common_groups(Tests)]. |
| 39 |
|
| 40 |
init_per_group(Name=http, Config) -> |
| 41 |
cowboy_test:init_http(Name, #{ |
| 42 |
env => #{dispatch => init_dispatch()}, |
| 43 |
proxy_header => true |
| 44 |
}, Config); |
| 45 |
init_per_group(Name=https, Config) -> |
| 46 |
cowboy_test:init_https(Name, #{ |
| 47 |
env => #{dispatch => init_dispatch()}, |
| 48 |
proxy_header => true |
| 49 |
}, Config); |
| 50 |
init_per_group(Name=h2, Config) -> |
| 51 |
cowboy_test:init_http2(Name, #{ |
| 52 |
env => #{dispatch => init_dispatch()}, |
| 53 |
proxy_header => true |
| 54 |
}, Config); |
| 55 |
init_per_group(Name, Config) -> |
| 56 |
Config1 = cowboy_test:init_http(Name, #{ |
| 57 |
env => #{dispatch => init_dispatch()}, |
| 58 |
proxy_header => true |
| 59 |
}, Config), |
| 60 |
lists:keyreplace(protocol, 1, Config1, {protocol, http2}). |
| 61 |
|
| 62 |
end_per_group(Name, _) -> |
| 63 |
cowboy:stop_listener(Name). |
| 64 |
|
| 65 |
%% Routes. |
| 66 |
|
| 67 |
init_dispatch() -> |
| 68 |
cowboy_router:compile([{"[...]", [ |
| 69 |
{"/direct/:key/[...]", echo_h, []} |
| 70 |
]}]). |
| 71 |
|
| 72 |
%% Tests. |
| 73 |
|
| 74 |
fail_gracefully_on_disconnect(Config) -> |
| 75 |
doc("Probing a port must not generate a crash"), |
| 76 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), |
| 77 |
[binary, {active, false}, {packet, raw}]), |
| 78 |
timer:sleep(50), |
| 79 |
Pid = do_get_remote_pid(Socket, Config), |
| 80 |
Ref = erlang:monitor(process, Pid), |
| 81 |
gen_tcp:close(Socket), |
| 82 |
receive |
| 83 |
{'DOWN', Ref, process, Pid, {shutdown, closed}} -> |
| 84 |
ok; |
| 85 |
{'DOWN', Ref, process, Pid, Reason} -> |
| 86 |
error(Reason) |
| 87 |
after 5000 -> |
| 88 |
error(timeout) |
| 89 |
end. |
| 90 |
|
| 91 |
fail_gracefully_on_timeout(Config) -> |
| 92 |
doc("A connection that does not send a PROXY header must not generate a crash"), |
| 93 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), |
| 94 |
[binary, {active, false}, {packet, raw}]), |
| 95 |
timer:sleep(50), |
| 96 |
Pid = do_get_remote_pid(Socket, Config), |
| 97 |
Ref = erlang:monitor(process, Pid), |
| 98 |
receive |
| 99 |
{'DOWN', Ref, process, Pid, {shutdown, closed}} -> |
| 100 |
ok; |
| 101 |
{'DOWN', Ref, process, Pid, Reason} -> |
| 102 |
error(Reason) |
| 103 |
after 5000 -> |
| 104 |
error(timeout) |
| 105 |
end. |
| 106 |
|
| 107 |
fail_gracefully_on_invalid_proxy_header(Config) -> |
| 108 |
doc("Sending data that is not a PROXY header must not generate a crash"), |
| 109 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), |
| 110 |
[binary, {active, false}, {packet, raw}]), |
| 111 |
timer:sleep(50), |
| 112 |
Pid = do_get_remote_pid(Socket, Config), |
| 113 |
Ref = erlang:monitor(process, Pid), |
| 114 |
ok = gen_tcp:send(Socket, <<"invalid data instead of a proxy header">>), |
| 115 |
receive |
| 116 |
{'DOWN', Ref, process, Pid, {shutdown, {connection_error, protocol_error, _}}} -> |
| 117 |
ok; |
| 118 |
{'DOWN', Ref, process, Pid, Reason} -> |
| 119 |
error(Reason) |
| 120 |
after 5000 -> |
| 121 |
error(timeout) |
| 122 |
end. |
| 123 |
|
| 124 |
fail_gracefully_on_partial_header_disconnect(Config) -> |
| 125 |
doc("Disconnecting after sending a partial PROXY header must not generate a crash"), |
| 126 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), |
| 127 |
[binary, {active, false}, {packet, raw}]), |
| 128 |
timer:sleep(50), |
| 129 |
Pid = do_get_remote_pid(Socket, Config), |
| 130 |
Ref = erlang:monitor(process, Pid), |
| 131 |
ok = gen_tcp:send(Socket, <<"PROXY TCP4 127.0.0.1">>), |
| 132 |
gen_tcp:close(Socket), |
| 133 |
receive |
| 134 |
{'DOWN', Ref, process, Pid, {shutdown, {connection_error, protocol_error, _}}} -> |
| 135 |
ok; |
| 136 |
{'DOWN', Ref, process, Pid, Reason} -> |
| 137 |
error(Reason) |
| 138 |
after 5000 -> |
| 139 |
error(timeout) |
| 140 |
end. |
| 141 |
|
| 142 |
do_get_remote_pid(Socket, Config) -> |
| 143 |
case config(type, Config) of |
| 144 |
tcp -> ct_helper:get_remote_pid_tcp(Socket); |
| 145 |
ssl -> ct_helper:get_remote_pid_tls(Socket) |
| 146 |
end. |
| 147 |
|
| 148 |
v1_proxy_header(Config) -> |
| 149 |
doc("Confirm we can read the proxy header at the start of the connection."), |
| 150 |
ProxyInfo = #{ |
| 151 |
version => 1, |
| 152 |
command => proxy, |
| 153 |
transport_family => ipv4, |
| 154 |
transport_protocol => stream, |
| 155 |
src_address => {127, 0, 0, 1}, |
| 156 |
src_port => 444, |
| 157 |
dest_address => {192, 168, 0, 1}, |
| 158 |
dest_port => 443 |
| 159 |
}, |
| 160 |
do_proxy_header(Config, ProxyInfo). |
| 161 |
|
| 162 |
v2_proxy_header(Config) -> |
| 163 |
doc("Confirm we can read the proxy header at the start of the connection."), |
| 164 |
ProxyInfo = #{ |
| 165 |
version => 2, |
| 166 |
command => proxy, |
| 167 |
transport_family => ipv4, |
| 168 |
transport_protocol => stream, |
| 169 |
src_address => {127, 0, 0, 1}, |
| 170 |
src_port => 444, |
| 171 |
dest_address => {192, 168, 0, 1}, |
| 172 |
dest_port => 443 |
| 173 |
}, |
| 174 |
do_proxy_header(Config, ProxyInfo). |
| 175 |
|
| 176 |
v2_local_header(Config) -> |
| 177 |
doc("Confirm we can read the proxy header at the start of the connection."), |
| 178 |
ProxyInfo = #{ |
| 179 |
version => 2, |
| 180 |
command => local |
| 181 |
}, |
| 182 |
do_proxy_header(Config, ProxyInfo). |
| 183 |
|
| 184 |
do_proxy_header(Config, ProxyInfo) -> |
| 185 |
case config(ref, Config) of |
| 186 |
http -> do_proxy_header_http(Config, ProxyInfo); |
| 187 |
https -> do_proxy_header_https(Config, ProxyInfo); |
| 188 |
h2 -> do_proxy_header_h2(Config, ProxyInfo); |
| 189 |
h2c -> do_proxy_header_h2c(Config, ProxyInfo); |
| 190 |
h2c_upgrade -> do_proxy_header_h2c_upgrade(Config, ProxyInfo) |
| 191 |
end. |
| 192 |
|
| 193 |
do_proxy_header_http(Config, ProxyInfo) -> |
| 194 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), |
| 195 |
[binary, {active, false}, {packet, raw}]), |
| 196 |
ok = gen_tcp:send(Socket, ranch_proxy_header:header(ProxyInfo)), |
| 197 |
do_proxy_header_http_common({raw_client, Socket, gen_tcp}, ProxyInfo). |
| 198 |
|
| 199 |
do_proxy_header_https(Config, ProxyInfo) -> |
| 200 |
{ok, Socket0} = gen_tcp:connect("localhost", config(port, Config), |
| 201 |
[binary, {active, false}, {packet, raw}]), |
| 202 |
ok = gen_tcp:send(Socket0, ranch_proxy_header:header(ProxyInfo)), |
| 203 |
TlsOpts = ct_helper:get_certs_from_ets(), |
| 204 |
{ok, Socket} = ssl:connect(Socket0, TlsOpts, 1000), |
| 205 |
do_proxy_header_http_common({raw_client, Socket, ssl}, ProxyInfo). |
| 206 |
|
| 207 |
do_proxy_header_http_common(Client, ProxyInfo) -> |
| 208 |
ok = raw_send(Client, |
| 209 |
"GET /direct/proxy_header HTTP/1.1\r\n" |
| 210 |
"Host: localhost\r\n" |
| 211 |
"\r\n"), |
| 212 |
{_, 200, _, Rest0} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 213 |
{Headers, Body0} = cow_http:parse_headers(Rest0), |
| 214 |
{_, LenBin} = lists:keyfind(<<"content-length">>, 1, Headers), |
| 215 |
Len = binary_to_integer(LenBin), |
| 216 |
Body = if |
| 217 |
byte_size(Body0) =:= Len -> Body0; |
| 218 |
true -> |
| 219 |
{ok, Body1} = raw_recv(Client, Len - byte_size(Body0), 5000), |
| 220 |
<<Body0/bits, Body1/bits>> |
| 221 |
end, |
| 222 |
ProxyInfo = do_parse_term(Body), |
| 223 |
ok. |
| 224 |
|
| 225 |
do_proxy_header_h2(Config, ProxyInfo) -> |
| 226 |
{ok, Socket0} = gen_tcp:connect("localhost", config(port, Config), |
| 227 |
[binary, {active, false}, {packet, raw}]), |
| 228 |
ok = gen_tcp:send(Socket0, ranch_proxy_header:header(ProxyInfo)), |
| 229 |
TlsOpts = ct_helper:get_certs_from_ets(), |
| 230 |
{ok, Socket} = ssl:connect(Socket0, |
| 231 |
[{alpn_advertised_protocols, [<<"h2">>]}|TlsOpts], 1000), |
| 232 |
do_proxy_header_h2_common({raw_client, Socket, ssl}, ProxyInfo). |
| 233 |
|
| 234 |
do_proxy_header_h2c(Config, ProxyInfo) -> |
| 235 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), |
| 236 |
[binary, {active, false}, {packet, raw}]), |
| 237 |
ok = gen_tcp:send(Socket, ranch_proxy_header:header(ProxyInfo)), |
| 238 |
do_proxy_header_h2_common({raw_client, Socket, gen_tcp}, ProxyInfo). |
| 239 |
|
| 240 |
do_proxy_header_h2c_upgrade(Config, ProxyInfo) -> |
| 241 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), |
| 242 |
[binary, {active, false}, {packet, raw}]), |
| 243 |
ok = gen_tcp:send(Socket, ranch_proxy_header:header(ProxyInfo)), |
| 244 |
Client = {raw_client, Socket, gen_tcp}, |
| 245 |
ok = raw_send(Client, [ |
| 246 |
"GET /direct/proxy_header HTTP/1.1\r\n" |
| 247 |
"Host: localhost\r\n" |
| 248 |
"Connection: Upgrade, HTTP2-Settings\r\n" |
| 249 |
"Upgrade: h2c\r\n" |
| 250 |
"HTTP2-Settings: ", base64:encode(iolist_to_binary(cow_http2:settings_payload(#{}))), "\r\n" |
| 251 |
"\r\n"]), |
| 252 |
ok = do_recv_101(Client), |
| 253 |
%% Receive the server preface. |
| 254 |
{ok, <<PrefaceLen:24>>} = raw_recv(Client, 3, 1000), |
| 255 |
{ok, <<4:8, 0:40, _:PrefaceLen/binary>>} = raw_recv(Client, 6 + PrefaceLen, 1000), |
| 256 |
do_proxy_header_h2_response_common(Client, ProxyInfo), |
| 257 |
ok. |
| 258 |
|
| 259 |
do_proxy_header_h2_common(Client, ProxyInfo) -> |
| 260 |
%% Send a valid preface. |
| 261 |
ok = raw_send(Client, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]), |
| 262 |
%% Receive the server preface. |
| 263 |
{ok, <<PrefaceLen:24>>} = raw_recv(Client, 3, 1000), |
| 264 |
{ok, <<4:8, 0:40, _:PrefaceLen/binary>>} = raw_recv(Client, 6 + PrefaceLen, 1000), |
| 265 |
%% Send the SETTINGS ack. |
| 266 |
ok = raw_send(Client, cow_http2:settings_ack()), |
| 267 |
%% Receive the SETTINGS ack. |
| 268 |
{ok, <<0:24, 4:8, 1:8, 0:32>>} = raw_recv(Client, 9, 1000), |
| 269 |
%% Send a GET request. |
| 270 |
{HeadersBlock, _} = cow_hpack:encode([ |
| 271 |
{<<":method">>, <<"GET">>}, |
| 272 |
{<<":scheme">>, <<"http">>}, |
| 273 |
{<<":authority">>, <<"localhost">>}, %% @todo Correct port number. |
| 274 |
{<<":path">>, <<"/direct/proxy_header">>} |
| 275 |
]), |
| 276 |
Len = iolist_size(HeadersBlock), |
| 277 |
ok = raw_send(Client, [ |
| 278 |
<<Len:24, 1:8, |
| 279 |
0:2, %% Undefined. |
| 280 |
0:1, %% PRIORITY. |
| 281 |
0:1, %% Undefined. |
| 282 |
0:1, %% PADDED. |
| 283 |
1:1, %% END_HEADERS. |
| 284 |
0:1, %% Undefined. |
| 285 |
1:1, %% END_STREAM. |
| 286 |
0:1, 1:31>>, |
| 287 |
HeadersBlock |
| 288 |
]), |
| 289 |
do_proxy_header_h2_response_common(Client, ProxyInfo). |
| 290 |
|
| 291 |
do_proxy_header_h2_response_common(Client, ProxyInfo) -> |
| 292 |
%% Receive a response with the proxy header data. |
| 293 |
{ok, <<SkipLen:24, 1:8, _:8, 1:32>>} = raw_recv(Client, 9, 1000), |
| 294 |
{ok, _} = raw_recv(Client, SkipLen, 1000), |
| 295 |
{ok, <<BodyLen:24, 0:8, 1:8, 1:32>>} = raw_recv(Client, 9, 1000), |
| 296 |
{ok, Body} = raw_recv(Client, BodyLen, 1000), |
| 297 |
ProxyInfo = do_parse_term(Body), |
| 298 |
ok. |
| 299 |
|
| 300 |
do_parse_term(Body) -> |
| 301 |
{ok, Tokens, _} = erl_scan:string(binary_to_list(Body) ++ "."), |
| 302 |
{ok, Exprs} = erl_parse:parse_exprs(Tokens), |
| 303 |
{value, Term, _} = erl_eval:exprs(Exprs, erl_eval:new_bindings()), |
| 304 |
Term. |
| 305 |
|
| 306 |
%% Match directly for now. |
| 307 |
do_recv_101(Client) -> |
| 308 |
{ok, << |
| 309 |
"HTTP/1.1 101 Switching Protocols\r\n" |
| 310 |
"connection: Upgrade\r\n" |
| 311 |
"upgrade: h2c\r\n" |
| 312 |
"\r\n" |
| 313 |
>>} = raw_recv(Client, 71, 1000), |
| 314 |
ok. |