Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/plug/static.ex
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ defmodule Plug.Static do
segments = Enum.map(segments, &uri_decode/1)

if invalid_path?(segments) do
raise InvalidPathError
raise InvalidPathError, "invalid path for static asset: #{conn.request_path}"
end

path = path(from, segments)
Expand Down
34 changes: 23 additions & 11 deletions test/plug/static_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -206,37 +206,49 @@ defmodule Plug.StaticTest do
end

test "returns 400 for unsafe paths" do
path = "/public/fixtures/../fixtures/static/file.txt"

exception =
assert_raise Plug.Static.InvalidPathError, "invalid path for static asset", fn ->
call(conn(:get, "/public/fixtures/../fixtures/static/file.txt"))
end
assert_raise Plug.Static.InvalidPathError,
"invalid path for static asset: #{path}",
fn ->
call(conn(:get, path))
end

assert Plug.Exception.status(exception) == 400

path = "/public/fixtures/%2E%2E/fixtures/static/file.txt"

exception =
assert_raise Plug.Static.InvalidPathError, "invalid path for static asset", fn ->
call(conn(:get, "/public/fixtures/%2E%2E/fixtures/static/file.txt"))
assert_raise Plug.Static.InvalidPathError, "invalid path for static asset: #{path}", fn ->
call(conn(:get, path))
end

assert Plug.Exception.status(exception) == 400

path = "/public/c:\\foo.txt"

exception =
assert_raise Plug.Static.InvalidPathError, "invalid path for static asset", fn ->
call(conn(:get, "/public/c:\\foo.txt"))
assert_raise Plug.Static.InvalidPathError, "invalid path for static asset: #{path}", fn ->
call(conn(:get, path))
end

assert Plug.Exception.status(exception) == 400

path = "/public/sample.txt%00.html"

exception =
assert_raise Plug.Static.InvalidPathError, "invalid path for static asset", fn ->
call(conn(:get, "/public/sample.txt%00.html"))
assert_raise Plug.Static.InvalidPathError, "invalid path for static asset: #{path}", fn ->
call(conn(:get, path))
end

assert Plug.Exception.status(exception) == 400

path = "/public/sample.txt\0.html"

exception =
assert_raise Plug.Static.InvalidPathError, "invalid path for static asset", fn ->
call(conn(:get, "/public/sample.txt\0.html"))
assert_raise Plug.Static.InvalidPathError, "invalid path for static asset: #{path}", fn ->
call(conn(:get, path))
end

assert Plug.Exception.status(exception) == 400
Expand Down