security_SUITE.erl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. %% Copyright (c) 2018, Loïc Hoguin <essen@ninenines.eu>
  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. -module(security_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [gun_open/1]).
  20. -import(cowboy_test, [raw_open/1]).
  21. -import(cowboy_test, [raw_send/2]).
  22. -import(cowboy_test, [raw_recv_head/1]).
  23. -import(cowboy_test, [raw_recv/3]).
  24. %% ct.
  25. all() ->
  26. cowboy_test:common_all().
  27. groups() ->
  28. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  29. init_per_suite(Config) ->
  30. ct_helper:create_static_dir(config(priv_dir, Config) ++ "/static"),
  31. Config.
  32. end_per_suite(Config) ->
  33. ct_helper:delete_static_dir(config(priv_dir, Config) ++ "/static").
  34. init_per_group(Name, Config) ->
  35. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  36. end_per_group(Name, _) ->
  37. cowboy:stop_listener(Name).
  38. %% Routes.
  39. init_dispatch(_) ->
  40. cowboy_router:compile([{"localhost", [
  41. {"/", hello_h, []}
  42. ]}]).
  43. %% Tests.
  44. nc_rand(Config) ->
  45. doc("Throw random garbage at the server, then check if it's still up."),
  46. do_nc(Config, "/dev/urandom").
  47. nc_zero(Config) ->
  48. doc("Throw zeroes at the server, then check if it's still up."),
  49. do_nc(Config, "/dev/zero").
  50. do_nc(Config, Input) ->
  51. Cat = os:find_executable("cat"),
  52. Nc = os:find_executable("nc"),
  53. case {Cat, Nc} of
  54. {false, _} ->
  55. {skip, {not_found, cat}};
  56. {_, false} ->
  57. {skip, {not_found, nc}};
  58. _ ->
  59. StrPort = integer_to_list(config(port, Config)),
  60. _ = [
  61. os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  62. || _ <- lists:seq(1, 100)],
  63. ConnPid = gun_open(Config),
  64. Ref = gun:get(ConnPid, "/"),
  65. {response, _, 200, _} = gun:await(ConnPid, Ref),
  66. ok
  67. end.
  68. slowloris(Config) ->
  69. doc("Send request headers one byte at a time. "
  70. "Confirm that the connection gets closed."),
  71. _ = case config(protocol, Config) of
  72. http ->
  73. do_http_slowloris(Config);
  74. http2 ->
  75. %% @todo Write an equivalent test for HTTP2.
  76. ok
  77. end.
  78. do_http_slowloris(Config) ->
  79. Client = raw_open(Config),
  80. try
  81. [begin
  82. ok = raw_send(Client, [C]),
  83. timer:sleep(250)
  84. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  85. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  86. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  87. error(failure)
  88. catch error:{badmatch, _} ->
  89. ok
  90. end.
  91. slowloris_chunks(Config) ->
  92. _ = case config(protocol, Config) of
  93. http ->
  94. do_http_slowloris_chunks(Config);
  95. http2 ->
  96. %% @todo Write an equivalent test for HTTP2.
  97. ok
  98. end.
  99. do_http_slowloris_chunks(Config) ->
  100. doc("Send request headers one line at a time. "
  101. "Confirm that the connection gets closed."),
  102. Client = raw_open(Config),
  103. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  104. timer:sleep(300),
  105. ok = raw_send(Client, "Host: localhost\r\n"),
  106. timer:sleep(300),
  107. Data = raw_recv_head(Client),
  108. {'HTTP/1.1', 408, _, Rest} = cow_http:parse_status_line(Data),
  109. {Headers, _} = cow_http:parse_headers(Rest),
  110. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  111. {error, closed} = raw_recv(Client, 0, 1000).