@@ -896,12 +896,68 @@ defmodule Plug.StaticTest do
896896 assert conn . status == 200
897897 end
898898
899- test "raise_on_missing_only does not raise on non-matching paths with colons" do
900- conn =
901- conn ( :get , "/public/resource:identifier" )
902- |> call ( only: [ "assets" ] , raise_on_missing_only: true )
899+ describe "raise_on_missing_only" do
900+ test "passes through when path has colons and does not match :only" do
901+ conn =
902+ conn ( :get , "/public/resource:identifier" )
903+ |> call ( only: [ "assets" ] , raise_on_missing_only: true )
903904
904- assert conn . status == 404
905- assert conn . resp_body == "Passthrough"
905+ assert conn . status == 404
906+ assert conn . resp_body == "Passthrough"
907+ end
908+
909+ test "passes through when path has dot-dot segments and does not match :only" do
910+ conn =
911+ conn ( :get , "/public/..%2Fsecret" )
912+ |> call ( only: [ "assets" ] , raise_on_missing_only: true )
913+
914+ assert conn . status == 404
915+ assert conn . resp_body == "Passthrough"
916+ end
917+
918+ test "passes through when path has null bytes and does not match :only" do
919+ conn =
920+ conn ( :get , "/public/file%00.txt" )
921+ |> call ( only: [ "assets" ] , raise_on_missing_only: true )
922+
923+ assert conn . status == 404
924+ assert conn . resp_body == "Passthrough"
925+ end
926+
927+ test "passes through when non-existent file does not match :only" do
928+ conn =
929+ conn ( :get , "/public/nonexistent.txt" )
930+ |> call ( only: [ "assets" ] , raise_on_missing_only: true )
931+
932+ assert conn . status == 404
933+ assert conn . resp_body == "Passthrough"
934+ end
935+
936+ test "passes through with only_matching when path does not match prefix" do
937+ conn =
938+ conn ( :get , "/public/resource:identifier" )
939+ |> call ( only_matching: [ "assets" ] , raise_on_missing_only: true )
940+
941+ assert conn . status == 404
942+ assert conn . resp_body == "Passthrough"
943+ end
944+
945+ test "raises when existing file is not in :only list" do
946+ assert_raise Plug.Static.InvalidPathError ,
947+ ~r/ static file exists but is not in the :only list/ ,
948+ fn ->
949+ conn ( :get , "/public/fixtures/static.txt" )
950+ |> call ( only: [ "assets" ] , raise_on_missing_only: true )
951+ end
952+ end
953+
954+ test "serves file normally when it matches :only list" do
955+ conn =
956+ conn ( :get , "/public/fixtures/static.txt" )
957+ |> call ( only: [ "fixtures" ] , raise_on_missing_only: true )
958+
959+ assert conn . status == 200
960+ assert conn . resp_body == "HELLO"
961+ end
906962 end
907963end
0 commit comments