Skip to content

Commit 02c6919

Browse files
committed
Add test demonstrating unsafe directory traversal
1 parent af44305 commit 02c6919

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

test/elli_static_tests.erl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ elli_static_test_() ->
77
fun setup/0, fun teardown/1,
88
[?_test(readme()),
99
?_test(no_file()),
10-
?_test(not_found())]}.
10+
?_test(not_found()),
11+
?_test(safe_traversal()),
12+
?_test(unsafe_traversal())]}.
1113

1214

1315
readme() ->
@@ -28,6 +30,23 @@ not_found() ->
2830
{ok, Response} = httpc:request("http://localhost:3000/not_found"),
2931
?assertMatch({{"HTTP/1.1",404,"Not Found"}, _Headers, "Not Found"}, Response).
3032

33+
safe_traversal() ->
34+
{ok, Response} = httpc:request("http://localhost:3000/elli_static/"
35+
"../elli_static/README.md"),
36+
{ok, File} = file:read_file("README.md"),
37+
Expected = binary_to_list(File),
38+
?assertEqual([integer_to_list(iolist_size(Expected))],
39+
proplists:get_all_values("content-length", element(2, Response))),
40+
?assertMatch({_Status, _Headers, Expected}, Response).
41+
42+
unsafe_traversal() ->
43+
%% compute the relative path to /etc/passwd
44+
{ok, Cwd} = file:get_cwd(),
45+
PasswdPath = [".." || _ <- filename:split(Cwd)] ++ ["etc", "passwd"],
46+
Path = filename:join(PasswdPath),
47+
48+
{ok, Response} = httpc:request("http://localhost:3000/elli_static/" ++ Path),
49+
?assertMatch({{"HTTP/1.1",404,"Not Found"}, _Headers, "Not Found"}, Response).
3150

3251
setup() ->
3352
{ok, Dir} = file:get_cwd(),

0 commit comments

Comments
 (0)