Skip to content

Commit 4a0477e

Browse files
authored
Ignore Range in Plug.Static unless If-Range matches the ETag (#1337)
1 parent b761023 commit 4a0477e

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎lib/plug/static.ex‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ defmodule Plug.Static do
285285
defp serve_range(conn, file_info, path, [range], options) do
286286
file_info(size: file_size) = file_info
287287

288-
with %{"bytes" => bytes} <- Plug.Conn.Utils.params(range),
288+
with true <- if_range_matches?(conn),
289+
%{"bytes" => bytes} <- Plug.Conn.Utils.params(range),
289290
# 41 bytes covers two 64 bit byte offsets, enough to address files up to 16 EiB.
290291
true <- byte_size(bytes) <= 41,
291292
{range_start, range_end} <- start_and_end(bytes, file_size) do
@@ -300,6 +301,16 @@ defmodule Plug.Static do
300301
send_entire_file(conn, path, options)
301302
end
302303

304+
# The range is only served if If-Range, when given, is the current strong
305+
# ETag. Dates never match because no Last-Modified is sent. See RFC 9110, 13.1.5.
306+
defp if_range_matches?(conn) do
307+
case {get_req_header(conn, "if-range"), get_resp_header(conn, "etag")} do
308+
{[], _} -> true
309+
{[etag], [etag]} -> not String.starts_with?(etag, "W/")
310+
_ -> false
311+
end
312+
end
313+
303314
defp start_and_end(_range, 0), do: :error
304315

305316
defp start_and_end("-" <> rest, file_size) do

‎test/plug/static_test.exs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,46 @@ defmodule Plug.StaticTest do
553553
end
554554
end
555555

556+
test "serves the range only if if-range matches the etag" do
557+
conn = call(conn(:get, "/public/fixtures/static.txt"))
558+
assert [etag] = get_resp_header(conn, "etag")
559+
560+
conn =
561+
conn(:get, "/public/fixtures/static.txt", [])
562+
|> put_req_header("range", "bytes=0-1")
563+
|> put_req_header("if-range", etag)
564+
|> call()
565+
566+
assert conn.status == 206
567+
assert conn.resp_body == "HE"
568+
assert get_resp_header(conn, "content-range") == ["bytes 0-1/5"]
569+
570+
for if_range <- [~s("other"), "W/" <> etag, "Sun, 25 Oct 2026 00:00:00 GMT"],
571+
range <- ["bytes=0-1", "bytes=10-20"] do
572+
conn =
573+
conn(:get, "/public/fixtures/static.txt", [])
574+
|> put_req_header("range", range)
575+
|> put_req_header("if-range", if_range)
576+
|> call()
577+
578+
assert conn.status == 200, "expected 200 for #{if_range} and #{range}"
579+
assert conn.resp_body == "HELLO"
580+
assert get_resp_header(conn, "content-range") == []
581+
end
582+
end
583+
584+
test "ignores range if if-range is given and there is no etag" do
585+
conn =
586+
conn(:get, "/public/fixtures/static.txt?vsn=bar", [])
587+
|> put_req_header("range", "bytes=0-1")
588+
|> put_req_header("if-range", ~s("abc"))
589+
|> call()
590+
591+
assert conn.status == 200
592+
assert conn.resp_body == "HELLO"
593+
assert get_resp_header(conn, "etag") == []
594+
end
595+
556596
test "performs etag negotiation" do
557597
conn =
558598
conn(:get, "/public/fixtures/static.txt")

0 commit comments

Comments
 (0)