Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

fix(files): send a request without a login to the login page, not a 500

Guido Boykead0ffeff13c0
diff --git a/app/controllers/organisations/file_downloads_controller.rb b/app/controllers/organisations/file_downloads_controller.rb
index 1acc105..3e1e5ef 100644
--- a/app/controllers/organisations/file_downloads_controller.rb
+++ b/app/controllers/organisations/file_downloads_controller.rb
@@ -85,6 +85,24 @@ module Organisations
private
+ # Devise answers a missing login with `throw :warden`. The catch for it sits in a
+ # middleware of the request thread, and this action runs in a thread of its own
+ # (ActionController::Live, through ActiveStorage::Streaming): there the throw arrives
+ # nowhere, and until 2026-09-30 every request without a login ended as a 500 instead
+ # of at the login. No file was ever sent, the request failed closed; but an expired
+ # session looked like a server error, to the reader and in the error log.
+ #
+ # Caught here, in the thread that throws. A timed-out session throws the same way,
+ # from inside Devise, and lands here too.
+ def authenticate_user!(*)
+ signed_in = false
+ catch(:warden) do
+ super
+ signed_in = true
+ end
+ redirect_to new_user_session_path unless signed_in
+ end
+
def not_found
head :not_found
end
diff --git a/spec/requests/organisations/file_downloads_spec.rb b/spec/requests/organisations/file_downloads_spec.rb
index 9374925..a265152 100644
--- a/spec/requests/organisations/file_downloads_spec.rb
+++ b/spec/requests/organisations/file_downloads_spec.rb
@@ -60,6 +60,30 @@ RSpec.describe 'Organisations::FileDownloads', :ci_guard, type: :request do
expect(response.body).not_to include('dummy content')
end
+ # The example above was green while every such request ended as a 500 outside the
+ # tests (found in a browser, 2026-09-30). This controller streams, so its action runs
+ # in a thread of its own (ActionController::Live, through ActiveStorage::Streaming).
+ # Devise answers a missing login with `throw :warden`, and the catch for it sits in
+ # a middleware of the REQUEST thread: in the action's thread the throw arrives
+ # nowhere. Rails runs a live action inline in tests (action_controller/test_case.rb
+ # redefines new_controller_thread), which is exactly what hid it. This example takes
+ # the thread back.
+ context 'with the action in a thread of its own, as outside the tests' do
+ before do
+ allow_any_instance_of(Organisations::FileDownloadsController)
+ .to receive(:new_controller_thread) { |controller, &action| controller.send(:original_new_controller_thread, &action) }
+ end
+
+ it 'redirects unauthenticated requests to login, and does not answer 500' do
+ # No record is needed, and none would be visible: the thread has its own
+ # database connection, outside this example's transaction. The login is
+ # checked before anything is looked up.
+ download(id: SecureRandom.uuid)
+
+ expect(response).to redirect_to(new_user_session_path)
+ end
+ end
+
it 'returns 404 for a file from a different organisation (cross-org)' do
create(:project_permission, user: member, project: project_a, documents_access: 'read_only')
other_org = create(:organisation)