From bb9ec0b9a23085793d73e977590efce58efb8f66 Mon Sep 17 00:00:00 2001 From: Ahesh Lakmal Date: Thu, 26 Nov 2026 13:32:29 +0100 Subject: [PATCH] Set the secure flag of the session cookie for requests over HTTPS (#21697). The session cookie was sent without the secure flag even when Redmine was accessed over HTTPS, so browsers could send it over an unencrypted connection. The flag is now set when the request is made over HTTPS, including requests forwarded by a reverse proxy with X-Forwarded-Proto, in the same way as the autologin cookie. Requests over HTTP are not affected. --- app/controllers/application_controller.rb | 8 +++++- test/integration/sessions_test.rb | 32 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cf6d4d626..dc185c84c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -61,7 +61,7 @@ class ApplicationController < ActionController::Base end end - before_action :api_session, :session_expiration, :user_setup, :check_if_login_required, :set_localization, :check_password_change, :check_twofa_activation + before_action :secure_session_cookie, :api_session, :session_expiration, :user_setup, :check_if_login_required, :set_localization, :check_password_change, :check_twofa_activation after_action :record_project_usage rescue_from ::Unauthorized, :with => :deny_access @@ -73,6 +73,12 @@ class ApplicationController < ActionController::Base include Redmine::SudoMode::Controller + # Sets the secure flag of the session cookie for requests made over HTTPS, + # including requests forwarded by a reverse proxy with X-Forwarded-Proto + def secure_session_cookie + request.session_options[:secure] = true if request.ssl? + end + def api_session return unless api_request? diff --git a/test/integration/sessions_test.rb b/test/integration/sessions_test.rb index 0946ccc2d..a929d1d2b 100644 --- a/test/integration/sessions_test.rb +++ b/test/integration/sessions_test.rb @@ -28,6 +28,29 @@ class SessionsTest < Redmine::IntegrationTest Rails.application.config.redmine_verify_sessions = false end + def test_session_cookie_should_be_secure_over_https + https! + post '/login', :params => {:username => 'jsmith', :password => 'jsmith'} + assert_redirected_to 'https://www.example.com/my/page' + assert_match /;\s*secure(;|\z)/i, session_cookie + end + + def test_session_cookie_should_be_secure_behind_reverse_proxy_over_https + post '/login', :params => {:username => 'jsmith', :password => 'jsmith'}, + :headers => {'X-Forwarded-Proto' => 'https'} + assert_redirected_to 'https://www.example.com/my/page' + assert_match /;\s*secure(;|\z)/i, session_cookie + end + + def test_session_cookie_should_not_be_secure_over_http + post '/login', :params => {:username => 'jsmith', :password => 'jsmith'} + assert_redirected_to 'http://www.example.com/my/page' + assert_no_match /;\s*secure(;|\z)/i, session_cookie + + get '/my/page' + assert_response :success + end + def test_change_password_kills_sessions log_user('jsmith', 'jsmith') @@ -223,4 +246,13 @@ class SessionsTest < Redmine::IntegrationTest assert_nil response.headers['Set-Cookie'] end end + + private + + def session_cookie + cookies = Array(response.headers['Set-Cookie']).flat_map {|cookie| cookie.split("\n")} + cookies.find {|cookie| cookie.start_with?('_redmine_session=')}.tap do |cookie| + assert_not_nil cookie, 'session cookie is not set' + end + end end -- 2.56.1.windows.1