Skip to content

Commit 021ac02

Browse files
authored
Fix NoMethodError in FilesController when Accept header is missing (#5448)
* Fix NoMethodError in FilesController when Accept header is missing * Use safe navigation on both the split and the include? so a missing Accept header simply leaves the default request format alone
1 parent 5315499 commit 021ac02

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

apps/dashboard/app/controllers/files_controller.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class FilesController < ApplicationController
88
before_action :strip_sendfile_headers, only: [:fs, :directory_frame]
99

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

@@ -211,6 +211,17 @@ def edit
211211

212212
private
213213

214+
# Whether the incoming request explicitly accepts application/json.
215+
#
216+
# Coerces the Accept header to a string because some user agents
217+
# (notably Chrome on `<a download>` links for files linked from
218+
# Interactive App session views) omit the header entirely, which
219+
# would otherwise raise NoMethodError on nil.split and leave the
220+
# global rescue_action handler to redirect users to $HOME.
221+
def json_request?
222+
request.headers['HTTP_ACCEPT'].to_s.split(',').include?('application/json')
223+
end
224+
214225
def rescue_action(exception)
215226
@files = []
216227
flash.now[:alert] = exception.message.to_s

apps/dashboard/test/controllers/files_controller_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,22 @@ def setup
166166
@controller.instance_variable_set(:@path, path)
167167
refute @controller.send(:posix_file?)
168168
end
169+
170+
# Tests for files_controller#json_request?
171+
# Regression test for #5447: a request with no Accept header must not
172+
# crash FilesController#fs with NoMethodError on nil.split.
173+
test 'json_request? returns false when Accept header is missing' do
174+
@request.env.delete('HTTP_ACCEPT')
175+
refute @controller.send(:json_request?)
176+
end
177+
178+
test 'json_request? returns truthy when Accept includes application/json' do
179+
@request.env['HTTP_ACCEPT'] = 'text/html,application/json'
180+
assert @controller.send(:json_request?)
181+
end
182+
183+
test 'json_request? returns falsy when Accept does not include application/json' do
184+
@request.env['HTTP_ACCEPT'] = 'text/html'
185+
refute @controller.send(:json_request?)
186+
end
169187
end

0 commit comments

Comments
 (0)