Skip to content
Open
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 config/initializers/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module ActiveStorageDirectUploadsControllerExtensions
included do
include Authentication
include Authorization
skip_forgery_protection if: :authenticate_by_bearer_token
skip_forgery_protection if: -> { authenticate_by_bearer_token || (resume_session && request.format.json?) }
end
end

Expand Down
42 changes: 42 additions & 0 deletions test/controllers/active_storage/direct_uploads_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,40 @@ class ActiveStorage::DirectUploadsControllerTest < ActionDispatch::IntegrationTe
assert_includes response.parsed_body.keys, "direct_upload"
end

test "create with session token" do
sign_in_as :david

post rails_direct_uploads_path,
params: @blob_params,
as: :json

assert_response :success
assert_includes response.parsed_body.keys, "direct_upload"
end

test "create with session token skips forgery protection" do
sign_in_as :david

with_forgery_protection do
post rails_direct_uploads_path,
params: @blob_params,
as: :json

assert_response :success
assert_includes response.parsed_body.keys, "direct_upload"
end
end

test "create with session token in another account is forbidden" do
sign_in_as :david

post rails_direct_uploads_path(script_name: "/#{ActiveRecord::FixtureSet.identify("initech")}"),
params: @blob_params,
as: :json

assert_response :forbidden
end
Comment thread
rbarbosa marked this conversation as resolved.

test "create with read-only access token" do
post rails_direct_uploads_path,
params: @blob_params,
Expand Down Expand Up @@ -83,4 +117,12 @@ class ActiveStorage::DirectUploadsControllerTest < ActionDispatch::IntegrationTe
def bearer_token_header(token)
{ "Authorization" => "Bearer #{token}" }
end

def with_forgery_protection
original = ActionController::Base.allow_forgery_protection
ActionController::Base.allow_forgery_protection = true
yield
ensure
ActionController::Base.allow_forgery_protection = original
end
end
Loading