-
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 all 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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
-module(elli_static_utils). | ||
|
||
-export([safe_relative_path/1]). | ||
|
||
%% @doc Backport of `filename:safe_relative_path/1' from 19.3. This code was | ||
%% lifted from: | ||
%% https://github.com/erlang/otp/blob/master/lib/stdlib/src/filename.erl#L811 | ||
-spec safe_relative_path(Filename) -> 'unsafe' | SafeFilename when | ||
Filename :: file:name_all(), | ||
SafeFilename :: file:name_all(). | ||
|
||
-ifndef(custom_safe_relative_path). | ||
safe_relative_path(Path) -> | ||
filename:safe_relative_path(Path). | ||
-else. | ||
safe_relative_path(Path) -> | ||
case filename:pathtype(Path) of | ||
relative -> | ||
Cs0 = filename:split(Path), | ||
safe_relative_path_1(Cs0, []); | ||
_ -> | ||
unsafe | ||
end. | ||
|
||
safe_relative_path_1(["."|T], Acc) -> | ||
safe_relative_path_1(T, Acc); | ||
safe_relative_path_1([<<".">>|T], Acc) -> | ||
safe_relative_path_1(T, Acc); | ||
safe_relative_path_1([".."|T], Acc) -> | ||
climb(T, Acc); | ||
safe_relative_path_1([<<"..">>|T], Acc) -> | ||
climb(T, Acc); | ||
safe_relative_path_1([H|T], Acc) -> | ||
safe_relative_path_1(T, [H|Acc]); | ||
safe_relative_path_1([], []) -> | ||
[]; | ||
safe_relative_path_1([], Acc) -> | ||
filename:join(lists:reverse(Acc)). | ||
|
||
climb(_, []) -> | ||
unsafe; | ||
climb(T, [_|Acc]) -> | ||
safe_relative_path_1(T, Acc). | ||
-endif. |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
-module(elli_static_utils_tests). | ||
-include_lib("eunit/include/eunit.hrl"). | ||
-compile(export_all). | ||
|
||
elli_static_utils_test_() -> | ||
[?_test(safe_relative_path())]. | ||
|
||
safe_relative_path() -> | ||
%% lists | ||
?assertEqual("a", elli_static_utils:safe_relative_path("a")), | ||
?assertEqual("a/b", elli_static_utils:safe_relative_path("a/b")), | ||
?assertEqual("a/b", elli_static_utils:safe_relative_path("a/./b")), | ||
?assertEqual("a/b", elli_static_utils:safe_relative_path("a/./b/.")), | ||
?assertEqual("", elli_static_utils:safe_relative_path("a/..")), | ||
?assertEqual("", elli_static_utils:safe_relative_path("a/./..")), | ||
?assertEqual("", elli_static_utils:safe_relative_path("a/../.")), | ||
?assertEqual("a", elli_static_utils:safe_relative_path("a/b/..")), | ||
?assertEqual("a", elli_static_utils:safe_relative_path("a/../a")), | ||
?assertEqual("a", elli_static_utils:safe_relative_path("a/../a/../a")), | ||
?assertEqual("a/b/c", elli_static_utils:safe_relative_path("a/../a/b/c")), | ||
?assertEqual(unsafe, elli_static_utils:safe_relative_path("a/../..")), | ||
?assertEqual(unsafe, elli_static_utils:safe_relative_path("a/../../..")), | ||
?assertEqual(unsafe, elli_static_utils:safe_relative_path("a/./../..")), | ||
?assertEqual(unsafe, elli_static_utils:safe_relative_path("a/././../../..")), | ||
?assertEqual(unsafe, elli_static_utils:safe_relative_path("a/b/././../../..")), | ||
?assertEqual(unsafe, elli_static_utils:safe_relative_path("/root")), | ||
|
||
%% binaries | ||
?assertEqual(<<"a">>, elli_static_utils:safe_relative_path(<<"a">>)), | ||
?assertEqual(<<"a/b">>, elli_static_utils:safe_relative_path(<<"a/b">>)), | ||
?assertEqual(<<"a/b">>, elli_static_utils:safe_relative_path(<<"a/./b">>)), | ||
?assertEqual(<<"a/b">>, elli_static_utils:safe_relative_path(<<"a/./b/.">>)), | ||
?assertEqual([], elli_static_utils:safe_relative_path(<<"a/..">>)), | ||
?assertEqual([], elli_static_utils:safe_relative_path(<<"a/./..">>)), | ||
?assertEqual([], elli_static_utils:safe_relative_path(<<"a/../.">>)), | ||
?assertEqual(<<"a">>, elli_static_utils:safe_relative_path(<<"a/b/..">>)), | ||
?assertEqual(<<"a">>, elli_static_utils:safe_relative_path(<<"a/../a">>)), | ||
?assertEqual(<<"a">>, elli_static_utils:safe_relative_path(<<"a/../a/../a">>)), | ||
?assertEqual(<<"a/b/c">>, elli_static_utils:safe_relative_path(<<"a/../a/b/c">>)), | ||
?assertEqual(unsafe, elli_static_utils:safe_relative_path(<<"a/../..">>)), | ||
?assertEqual(unsafe, elli_static_utils:safe_relative_path(<<"a/../../..">>)), | ||
?assertEqual(unsafe, elli_static_utils:safe_relative_path(<<"a/./../..">>)), | ||
?assertEqual(unsafe, elli_static_utils:safe_relative_path(<<"a/././../../..">>)), | ||
?assertEqual(unsafe, elli_static_utils:safe_relative_path(<<"a/b/././../../..">>)), | ||
?assertEqual(unsafe, elli_static_utils:safe_relative_path(<<"/root">>)). |
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.
Good call.