-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unsafe directory traversal #3
Open
jhlywa
wants to merge
7
commits into
elli-lib:develop
Choose a base branch
from
jhlywa:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
02c6919
Add test demonstrating unsafe directory traversal
jhlywa 89ad172
Prevent unsafe directory traversal
jhlywa 9be3b80
Backport safe_relative_path/1 from OTP 19.3
jhlywa e04221f
Add utils module to hold safe_relative_path/1
jhlywa 0c27dc7
Add additional safe_relative_path tests
jhlywa c360da3
Add platform_define for filename:safe_relative_path/1 inclusion
jhlywa ed51c07
Add test for 404 edge case
jhlywa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,10 @@ elli_static_test_() -> | |
fun setup/0, fun teardown/1, | ||
[?_test(readme()), | ||
?_test(no_file()), | ||
?_test(not_found())]}. | ||
?_test(not_found()), | ||
?_test(safe_traversal()), | ||
?_test(unsafe_traversal()), | ||
?_test(invalid_path_separator())]}. | ||
|
||
|
||
readme() -> | ||
|
@@ -28,6 +31,34 @@ not_found() -> | |
{ok, Response} = httpc:request("http://localhost:3000/not_found"), | ||
?assertMatch({{"HTTP/1.1",404,"Not Found"}, _Headers, "Not Found"}, Response). | ||
|
||
safe_traversal() -> | ||
{ok, File} = file:read_file("README.md"), | ||
Expected = binary_to_list(File), | ||
|
||
{ok, Response} = httpc:request("http://localhost:3000/elli_static/" | ||
"../elli_static/README.md"), | ||
?assertEqual([integer_to_list(iolist_size(Expected))], | ||
proplists:get_all_values("content-length", element(2, Response))), | ||
?assertMatch({_Status, _Headers, Expected}, Response), | ||
|
||
|
||
%% `Response' should match the same request above | ||
{ok, Response} = httpc:request("http://localhost:3000/elli_static/./README.md"). | ||
|
||
unsafe_traversal() -> | ||
%% compute the relative path to /etc/passwd | ||
{ok, Cwd} = file:get_cwd(), | ||
PasswdPath = [".." || _ <- filename:split(Cwd)] ++ ["etc", "passwd"], | ||
Path = filename:join(PasswdPath), | ||
|
||
{ok, Response} = httpc:request("http://localhost:3000/elli_static/" ++ Path), | ||
?assertMatch({{"HTTP/1.1",404,"Not Found"}, _Headers, "Not Found"}, Response). | ||
|
||
invalid_path_separator() -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. |
||
%% https://www.ietf.org/rfc/rfc2396.txt defines a path separator to be a | ||
%% single slash | ||
{ok, Response} = httpc:request("http://localhost:3000////elli_static/README.md"), | ||
?assertMatch({{"HTTP/1.1",404,"Not Found"}, _Headers, "Not Found"}, Response). | ||
|
||
setup() -> | ||
{ok, Dir} = file:get_cwd(), | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think I've seen this sort of
ifdef
before, but it appears to work as desired.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My preference would be to defer to
filename:safe_relative_path/1
if available. However, checking this at runtime (viamodule_info(exports)
) caused lots of coverall coverage complaints.Xref and dialyzer are run under the test profile, so in-module eunit tests ended up failing xref right out of the gate (
elli_static:safe_relative_path_test/0 is unused export
). Doh!I didn't want to introduce a new module, but I suppose I could create something like
elli_static_utils
, placesafe_relative_path/1
in there, and test independently of the OTP version. The utils module would be a cleaner approach IMO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 to a utils module, if you're up for it.