From 9db331ae77637ee45dc65a2581938994bcc3958d Mon Sep 17 00:00:00 2001 From: Marcello Costagliola Date: Fri, 23 Oct 2026 23:28:25 +0200 Subject: [PATCH 1/2] Hash a cached page's HTML for its ETag, not its gzip bytes The page cache hashed the bytes it stores, gzip included, while a render gets its ETag from Rack::ETag over the HTML. A browser revalidates every page it has seen, revalidations skip the cache and render, so the first one after a page came from the cache never matched and got the whole page back instead of a 304. Gzip output also records when it was made, so the same HTML cached again a second later got a different ETag as well. Co-Authored-By: Claude Opus 5.5 --- app/controllers/concerns/cached_responses.rb | 10 ++++++---- test/controllers/cached_responses_test.rb | 9 +++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/controllers/concerns/cached_responses.rb b/app/controllers/concerns/cached_responses.rb index b17e29a9b..09cf3e7ef 100644 --- a/app/controllers/concerns/cached_responses.rb +++ b/app/controllers/concerns/cached_responses.rb @@ -84,10 +84,12 @@ def cache_read_response def cache_completed_response(key, original_session, encoding) if response.status == 200 && response.media_type == "text/html" && session.to_hash == original_session && !response.headers["Content-Encoding"] - body = encoding == "gzip" ? Zlib.gzip(response.body) : response.body - unless body.empty? || response.headers["ETag"] || response.headers["Last-Modified"] - # Match Rack::ETag once, rather than hashing the same bytes on each hit. - response.headers["ETag"] = %(W/"#{Digest::SHA256.hexdigest(body).byteslice(0, 32)}") + html = response.body + body = encoding == "gzip" ? Zlib.gzip(html) : html + unless html.empty? || response.headers["ETag"] || response.headers["Last-Modified"] + # Match Rack::ETag once, rather than hashing the same bytes on each hit. Hash the HTML, as it + # does: gzip bytes carry the time they were compressed, so no render would ever match them. + response.headers["ETag"] = %(W/"#{Digest::SHA256.hexdigest(html).byteslice(0, 32)}") end response.headers["Content-Encoding"] = "gzip" if encoding == "gzip" response.headers["Vary"] = (response.headers["Vary"].to_s.split(/,\s*/) | [ "Accept-Encoding" ]).join(", ") diff --git a/test/controllers/cached_responses_test.rb b/test/controllers/cached_responses_test.rb index 9168f3bf2..b6809bba4 100644 --- a/test/controllers/cached_responses_test.rb +++ b/test/controllers/cached_responses_test.rb @@ -178,6 +178,15 @@ class CachedResponsesTest < ActionDispatch::IntegrationTest assert_not_equal etag, response.headers["ETag"] end + test "a compressed page answers a browser's revalidation" do + get room_url(@room), headers: { "Accept-Encoding" => "gzip" } + assert_equal "gzip", response.headers["Content-Encoding"] + etag = response.headers["ETag"] + assert etag.present? + get room_url(@room), headers: { "Accept-Encoding" => "gzip", "If-None-Match" => etag } + assert_response :not_modified + end + test "origins and frame variants have separate entries" do get room_url(@room) ResponseCache.instance.expects(:write).twice From 6f7eede457de16aa0a17a8ed613c449d39bf15ea Mon Sep 17 00:00:00 2001 From: Marcello Costagliola Date: Fri, 23 Oct 2026 23:28:25 +0200 Subject: [PATCH 2/2] Leave the CSRF token out of frame responses turbo-rails renders frame requests in its own minimal layout, which calls csrf_meta_tags. Forgery protection here is header-only and keeps secrets out of cached HTML, yet the sidebar frame carried a token masked afresh on every render: its ETag changed each time, so a browser revalidating the sidebar, twice on every full page load, always got the whole frame back. Co-Authored-By: Claude Opus 5.5 --- app/views/layouts/turbo_rails/frame.html.erb | 9 +++++++++ test/controllers/cached_responses_test.rb | 8 ++++++++ 2 files changed, 17 insertions(+) create mode 100644 app/views/layouts/turbo_rails/frame.html.erb diff --git a/app/views/layouts/turbo_rails/frame.html.erb b/app/views/layouts/turbo_rails/frame.html.erb new file mode 100644 index 000000000..eb1a2d248 --- /dev/null +++ b/app/views/layouts/turbo_rails/frame.html.erb @@ -0,0 +1,9 @@ +<%# Turbo's frame layout without its csrf_meta_tags. Header-only forgery protection needs no secret in cached HTML, and a token masked afresh on each render would give every frame a new ETag. %> + + + <%= yield :head %> + + + <%= yield %> + + diff --git a/test/controllers/cached_responses_test.rb b/test/controllers/cached_responses_test.rb index b6809bba4..9af865b59 100644 --- a/test/controllers/cached_responses_test.rb +++ b/test/controllers/cached_responses_test.rb @@ -187,6 +187,14 @@ class CachedResponsesTest < ActionDispatch::IntegrationTest assert_response :not_modified end + test "a frame answers a browser's revalidation" do + get user_sidebar_url(:me), headers: { "Turbo-Frame" => "user_sidebar" } + etag = response.headers["ETag"] + assert etag.present? + get user_sidebar_url(:me), headers: { "Turbo-Frame" => "user_sidebar", "If-None-Match" => etag } + assert_response :not_modified + end + test "origins and frame variants have separate entries" do get room_url(@room) ResponseCache.instance.expects(:write).twice