Skip to content
Merged
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
13 changes: 12 additions & 1 deletion apps/dashboard/app/controllers/files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class FilesController < ApplicationController
before_action :strip_sendfile_headers, only: [:fs, :directory_frame]

def fs
request.format = 'json' if request.headers['HTTP_ACCEPT'].split(',').include?('application/json')
request.format = 'json' if json_request?
parse_path(fs_params[:filepath], fs_params[:fs])
validate_path!

Expand Down Expand Up @@ -211,6 +211,17 @@ def edit

private

# Whether the incoming request explicitly accepts application/json.
#
# Coerces the Accept header to a string because some user agents
# (notably Chrome on `<a download>` links for files linked from
# Interactive App session views) omit the header entirely, which
# would otherwise raise NoMethodError on nil.split and leave the
# global rescue_action handler to redirect users to $HOME.
def json_request?
request.headers['HTTP_ACCEPT'].to_s.split(',').include?('application/json')
end

def rescue_action(exception)
@files = []
flash.now[:alert] = exception.message.to_s
Expand Down
18 changes: 18 additions & 0 deletions apps/dashboard/test/controllers/files_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,22 @@ def setup
@controller.instance_variable_set(:@path, path)
refute @controller.send(:posix_file?)
end

# Tests for files_controller#json_request?
# Regression test for #5447: a request with no Accept header must not
# crash FilesController#fs with NoMethodError on nil.split.
test 'json_request? returns false when Accept header is missing' do
@request.env.delete('HTTP_ACCEPT')
refute @controller.send(:json_request?)
end

test 'json_request? returns truthy when Accept includes application/json' do
@request.env['HTTP_ACCEPT'] = 'text/html,application/json'
assert @controller.send(:json_request?)
end

test 'json_request? returns falsy when Accept does not include application/json' do
@request.env['HTTP_ACCEPT'] = 'text/html'
refute @controller.send(:json_request?)
end
end