The personal finance app for everyone (by everyone)
Ruby
Fix Codespaces login CSRF origin validation (#3610)
Juan José Mata589e72bc71fe
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex f1462f3b77..b36f281b77 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -4,6 +4,7 @@ class ApplicationController < ActionController::BaseFeatureGuardable, Notifiable, SafePagination, AccountAuthorizable,PreviewGateableinclude Pundit::Authorization+ include CodespacesForgeryProtectioninclude Pagy::Backenddiff --git a/app/controllers/concerns/codespaces_forgery_protection.rb b/app/controllers/concerns/codespaces_forgery_protection.rbnew file mode 100644index 0000000000..2637719b02--- /dev/null+++ b/app/controllers/concerns/codespaces_forgery_protection.rb@@ -0,0 +1,22 @@+module CodespacesForgeryProtection+ private+ def valid_request_origin?+ super || codespaces_local_preview_origin?+ end++ def codespaces_local_preview_origin?+ return false unless Rails.env.development? && ENV["CODESPACES"] == "true"++ forwarding_domain = ENV["GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN"].to_s.downcase+ codespace_name = ENV["CODESPACE_NAME"].to_s.downcase+ port = ENV.fetch("PORT", 3000).to_i+ forwarded_host = "#{codespace_name}-#{port}.#{forwarding_domain}"+ return false if forwarding_domain.blank? || codespace_name.blank?+ return false unless request.ssl? && request.host.downcase == forwarded_host++ origin = URI.parse(request.origin.to_s)+ origin.scheme == "https" && origin.host == "localhost" && origin.port == port+ rescue URI::InvalidURIError+ false+ end+enddiff --git a/config/initializers/codespaces.rb b/config/initializers/codespaces.rbindex bf759d3f57..dc44956b9d 100644--- a/config/initializers/codespaces.rb+++ b/config/initializers/codespaces.rb@@ -19,7 +19,4 @@ if Rails.env.development? && ENV["CODESPACES"] == "true"key: "_app_session",same_site: :none,secure: true-- # Behind the proxy, Origin can differ; relax the origin check instead of disabling CSRF entirely.- Rails.application.config.action_controller.forgery_protection_origin_check = falseenddiff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rbindex 796a617ab6..007535fe39 100644--- a/test/controllers/sessions_controller_test.rb+++ b/test/controllers/sessions_controller_test.rb@@ -20,6 +20,28 @@ class SessionsControllerTest < ActionDispatch::IntegrationTestOmniAuth.config.mock_auth[:openid_connect] = nilend+ def with_origin_check+ original = ActionController::Base.allow_forgery_protection+ ActionController::Base.allow_forgery_protection = true+ yield+ ensure+ ActionController::Base.allow_forgery_protection = original+ end++ def with_codespaces_origin_check(&block)+ with_env_overrides(+ CODESPACES: "true",+ GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN: "app.github.dev",+ CODESPACE_NAME: "shiny-guide-6vj6x5qpjf5wpv",+ PORT: "3000"+ ) do+ Rails.env.stubs(:development?).returns(true)+ host! "shiny-guide-6vj6x5qpjf5wpv-3000.app.github.dev"+ https!+ with_origin_check(&block)+ end+ end+def setup_omniauth_mock(provider:, uid:, email:, name:, first_name: nil, last_name: nil)OmniAuth.config.mock_auth[:openid_connect] = OmniAuth::AuthHash.new({provider: provider,@@ -33,6 +55,86 @@ class SessionsControllerTest < ActionDispatch::IntegrationTest})end+ test "codespaces accepts its localhost preview origin with a valid CSRF token" do+ with_codespaces_origin_check do+ get new_session_url+ token = css_select("input[name=authenticity_token]").first["value"]++ post sessions_url,+ params: { email: @user.email, password: user_password_test, authenticity_token: token },+ headers: { "Origin" => "https://localhost:3000" }++ assert_redirected_to root_url+ assert Session.exists?(user_id: @user.id)+ end+ end++ test "codespaces still requires a valid CSRF token" do+ with_codespaces_origin_check do+ post sessions_url,+ params: { email: @user.email, password: user_password_test },+ headers: { "Origin" => "https://localhost:3000" }++ assert_response :unprocessable_content+ end+ end++ test "codespaces still rejects unrelated origins" do+ with_codespaces_origin_check do+ get new_session_url+ token = css_select("input[name=authenticity_token]").first["value"]++ post sessions_url,+ params: { email: @user.email, password: user_password_test, authenticity_token: token },+ headers: { "Origin" => "https://attacker.example" }++ assert_response :unprocessable_content+ end+ end++ test "codespaces rejects a mismatched forwarded host with a valid CSRF token" do+ with_codespaces_origin_check do+ host! "other-codespace-3000.app.github.dev"+ get new_session_url+ token = css_select("input[name=authenticity_token]").first["value"]++ post sessions_url,+ params: { email: @user.email, password: user_password_test, authenticity_token: token },+ headers: { "Origin" => "https://localhost:3000" }++ assert_response :unprocessable_content+ end+ end++ test "codespaces rejects a mismatched localhost port with a valid CSRF token" do+ with_codespaces_origin_check do+ get new_session_url+ token = css_select("input[name=authenticity_token]").first["value"]++ post sessions_url,+ params: { email: @user.email, password: user_password_test, authenticity_token: token },+ headers: { "Origin" => "https://localhost:3001" }++ assert_response :unprocessable_content+ end+ end++ test "ordinary local development keeps standard same-origin validation" do+ Rails.env.stubs(:development?).returns(true)++ with_origin_check do+ host! "localhost:3000"+ get new_session_url+ token = css_select("input[name=authenticity_token]").first["value"]++ post sessions_url,+ params: { email: @user.email, password: user_password_test, authenticity_token: token },+ headers: { "Origin" => "http://localhost:3000" }++ assert_redirected_to root_url+ end+ end+test "login page" doget new_session_urlassert_response :success