Skip to content

Fix NoMethodError in FilesController when Accept header is missing (#5447)#5448

Merged
Bubballoo3 merged 3 commits into
OSC:masterfrom
dantreiman:bug-fix/files-nil-accept-5447
May 20, 2026
Merged

Fix NoMethodError in FilesController when Accept header is missing (#5447)#5448
Bubballoo3 merged 3 commits into
OSC:masterfrom
dantreiman:bug-fix/files-nil-accept-5447

Conversation

@dantreiman
Copy link
Copy Markdown
Contributor

fixes #5447

Bug

FilesController#fs starts with:

request.format = 'json' if request.headers['HTTP_ACCEPT'].split(',').include?('application/json')

If the incoming request has no Accept header, request.headers['HTTP_ACCEPT'] is nil, nil.split raises NoMethodError, the global rescue_action catches it, and the user is redirected to $HOME instead of being served the file.

How it was hit

Building an Interactive App whose view.html.erb links to a session-specific file with <a download href="/pun/sys/dashboard/files/api/v1/fs<%= abs_path %>">. Chrome 147 on macOS omits Accept on download clicks; error.log filled with undefined method 'split' for nil:NilClass. Full context in #5447.

Fix

Extract the Accept-header check into a json_request? helper that safe-navigates the split and include?. When the header is absent we simply leave request.format alone and Rails's normal content negotiation picks html:

def fs
  request.format = 'json' if json_request?
  ...
end

private

def json_request?
  request.headers['HTTP_ACCEPT']&.split(',')&.include?('application/json')
end

The extraction also slightly reduces fs's complexity scores (per rubocop: AbcSize 97.59 → 93.34, Cyclomatic 19 → 17, Perceived 25 → 23), though the method is still over the project's metric limits — preexisting debt tracked by #1187.

Tests

New controller tests in files_controller_test.rb covering the three cases:

  • Accept header absent → json_request? returns nil (regression test for this issue).
  • Accept contains application/json → returns truthy.
  • Accept lacks application/json → returns falsy.

Integration-style get :fs tests would need extensive stubbing for an unrelated surface area; the direct helper test exercises the exact logic that was broken.

Lint

rubocop apps/dashboard/app/controllers/files_controller.rb — no new offenses introduced; 30 preexisting offenses unchanged, four of which (on fs) actually improved slightly.

Changelog

### Fixed entry added under [Unreleased].

FilesController#fs starts with:

    request.format = 'json' if request.headers['HTTP_ACCEPT'].split(',').include?('application/json')

If the request has no Accept header (for example, Chrome's <a download>
link sometimes omits it when fetching a file referenced by an Interactive
App session view), HTTP_ACCEPT is nil and nil.split raises
NoMethodError. The exception is rescued by rescue_action, which silently
redirects the user to $HOME instead of returning the file.

Use safe navigation on both the split and the include? so a missing
Accept header simply leaves the default request format alone (Rails's
normal content negotiation then picks html).
@github-project-automation github-project-automation Bot moved this to Awaiting Review in PR Review Pipeline May 8, 2026
@dantreiman dantreiman changed the title Fix NoMethodError in FilesController when Accept header is missing Fix NoMethodError in FilesController when Accept header is missing (#5447) May 8, 2026
Comment thread CHANGELOG.md
Comment thread apps/dashboard/app/controllers/files_controller.rb Outdated
Theo (Xcimer Energy) added 2 commits May 12, 2026 12:48
Per reviewer feedback:
- CHANGELOG updates are handled by release automation, drop the hunk.
- Prefer .to_s.split(',').include?(...) over &.split(',')&.include?(...)
  for readability and consistency with other code in the codebase.
Array#include? returns true/false, never nil, so the original
'returns nil when Accept header is missing' test could never pass
(CI failed with 'Expected false to be nil.' on Ruby 3.1-3.4).

Rename and switch to refute, matching the symmetric 'returns falsy
when Accept does not include application/json' test below it.
Copy link
Copy Markdown
Contributor

@johrstrom johrstrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks!

Copy link
Copy Markdown
Contributor

@Bubballoo3 Bubballoo3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and works well!

@Bubballoo3 Bubballoo3 merged commit 021ac02 into OSC:master May 20, 2026
44 of 46 checks passed
@github-project-automation github-project-automation Bot moved this from Awaiting Review to Merged/Closed in PR Review Pipeline May 20, 2026
@dantreiman dantreiman deleted the bug-fix/files-nil-accept-5447 branch May 21, 2026 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Merged/Closed

Development

Successfully merging this pull request may close these issues.

FilesController#fs crashes with NoMethodError when request has no Accept header

4 participants