提交 af0caadb 编写于 作者: G George Claghorn

Handle invalid signed blob IDs gracefully

上级 ca2c4cb7
......@@ -5,12 +5,10 @@
# security-through-obscurity factor of the signed blob references, you'll need to implement your own
# authenticated redirection controller.
class ActiveStorage::BlobsController < ActionController::Base
include ActiveStorage::SetBlob
def show
if blob = ActiveStorage::Blob.find_signed(params[:signed_id])
expires_in ActiveStorage::Blob.service.url_expires_in
redirect_to blob.service_url(disposition: params[:disposition])
else
head :not_found
end
expires_in ActiveStorage::Blob.service.url_expires_in
redirect_to @blob.service_url(disposition: params[:disposition])
end
end
# frozen_string_literal: true
class ActiveStorage::PreviewsController < ActionController::Base
include ActiveStorage::SetBlob
def show
if blob = ActiveStorage::Blob.find_signed(params[:signed_blob_id])
expires_in ActiveStorage::Blob.service.url_expires_in
redirect_to ActiveStorage::Preview.new(blob, params[:variation_key]).processed.service_url(disposition: params[:disposition])
else
head :not_found
end
expires_in ActiveStorage::Blob.service.url_expires_in
redirect_to ActiveStorage::Preview.new(@blob, params[:variation_key]).processed.service_url(disposition: params[:disposition])
end
end
......@@ -5,12 +5,10 @@
# security-through-obscurity factor of the signed blob and variation reference, you'll need to implement your own
# authenticated redirection controller.
class ActiveStorage::VariantsController < ActionController::Base
include ActiveStorage::SetBlob
def show
if blob = ActiveStorage::Blob.find_signed(params[:signed_blob_id])
expires_in ActiveStorage::Blob.service.url_expires_in
redirect_to ActiveStorage::Variant.new(blob, params[:variation_key]).processed.service_url(disposition: params[:disposition])
else
head :not_found
end
expires_in ActiveStorage::Blob.service.url_expires_in
redirect_to ActiveStorage::Variant.new(@blob, params[:variation_key]).processed.service_url(disposition: params[:disposition])
end
end
# frozen_string_literal: true
module ActiveStorage::SetBlob
extend ActiveSupport::Concern
included do
before_action :set_blob
end
private
def set_blob
@blob = ActiveStorage::Blob.find_signed(params[:signed_blob_id] || params[:signed_id])
rescue ActiveSupport::MessageVerifier::InvalidSignature
head :not_found
end
end
......@@ -8,6 +8,11 @@ class ActiveStorage::BlobsControllerTest < ActionDispatch::IntegrationTest
@blob = create_file_blob filename: "racecar.jpg"
end
test "showing blob with invalid signed ID" do
get rails_service_blob_url("invalid", "racecar.jpg")
assert_response :not_found
end
test "showing blob utilizes browser caching" do
get rails_blob_url(@blob)
......
......@@ -21,4 +21,13 @@ class ActiveStorage::PreviewsControllerTest < ActionDispatch::IntegrationTest
assert_equal 77, image.width
assert_equal 100, image.height
end
test "showing preview with invalid signed blob ID" do
get rails_blob_preview_url(
filename: @blob.filename,
signed_blob_id: "invalid",
variation_key: ActiveStorage::Variation.encode(resize: "100x100"))
assert_response :not_found
end
end
......@@ -20,4 +20,13 @@ class ActiveStorage::VariantsControllerTest < ActionDispatch::IntegrationTest
assert_equal 100, image.width
assert_equal 67, image.height
end
test "showing variant with invalid signed blob ID" do
get rails_blob_variation_url(
filename: @blob.filename,
signed_blob_id: "invalid",
variation_key: ActiveStorage::Variation.encode(resize: "100x100"))
assert_response :not_found
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册