Skip to content

Commit

Permalink
Return 401s if an unauthorized user attempts to download a workflow r…
Browse files Browse the repository at this point in the history
…estricted file
  • Loading branch information
bbpennel committed Nov 30, 2022
1 parent 7356ffe commit f0a0ee0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/controllers/hyrax/downloads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Hyrax
class DownloadsController < ApplicationController
include Hydra::Controller::DownloadBehavior
include Hyrax::LocalFileDownloadsControllerBehavior
include Hyrax::WorkflowsHelper # Provides #workflow_restriction?

def self.default_content_path
:original_file
Expand Down Expand Up @@ -42,7 +43,11 @@ def derivative_download_options
# that files are in a LDP basic container, and thus, included in the asset's uri.
def authorize_download!
authorize! :download, params[asset_param_key]
rescue CanCan::AccessDenied
# Deny access if the work containing this file is restricted by a workflow
file_set = Hyrax.query_service.find_by_alternate_identifier(alternate_identifier: params[asset_param_key], use_valkyrie: Hyrax.config.use_valkyrie?)
return unless workflow_restriction?(file_set.parent, ability: current_ability)
raise Hyrax::WorkflowAuthorizationException
rescue CanCan::AccessDenied, Hyrax::WorkflowAuthorizationException
unauthorized_image = Rails.root.join("app", "assets", "images", "unauthorized.png")
send_file unauthorized_image, status: :unauthorized
end
Expand Down
12 changes: 12 additions & 0 deletions spec/controllers/hyrax/downloads_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
end
end

context 'when restricted by workflow' do
before do
allow(subject).to receive(:workflow_restriction?).and_return(true)
end

it 'returns :unauthorized status with image content' do
get :show, params: { id: file_set.to_param }
expect(response).to have_http_status(:unauthorized)
expect(response.content_type).to eq 'image/png'
end
end

context "when user isn't logged in" do
context "and the unauthorized image exists" do
before do
Expand Down

0 comments on commit f0a0ee0

Please sign in to comment.