From 70be8124a3169f75c41e8db232a018627a996dd4 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 28 Mar 2018 11:22:43 +0800 Subject: [PATCH] SECURITY: Don't expose development route in production. --- app/controllers/session_controller.rb | 18 +++++++++++------- config/routes.rb | 5 ++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb index 2421ebbcae8..5d9f8473bcc 100644 --- a/app/controllers/session_controller.rb +++ b/app/controllers/session_controller.rb @@ -10,7 +10,7 @@ class SessionController < ApplicationController before_action :check_local_login_allowed, only: %i(create forgot_password email_login) before_action :rate_limit_login, only: %i(create email_login) skip_before_action :redirect_to_login_if_required - skip_before_action :preload_json, :check_xhr, only: %i(sso sso_login become sso_provider destroy email_login) + skip_before_action :preload_json, :check_xhr, only: %i(sso sso_login sso_provider destroy email_login) ACTIVATE_USER_KEY = "activate_user" @@ -75,13 +75,17 @@ class SessionController < ApplicationController # For use in development mode only when login options could be limited or disabled. # NEVER allow this to work in production. - def become - raise Discourse::InvalidAccess.new unless Rails.env.development? - user = User.find_by_username(params[:session_id]) - raise "User #{params[:session_id]} not found" if user.blank? + if Rails.env.development? + skip_before_action :check_xhr, only: [:become] - log_on_user(user) - redirect_to path("/") + def become + raise Discourse::InvalidAccess if Rails.env.production? + user = User.find_by_username(params[:session_id]) + raise "User #{params[:session_id]} not found" if user.blank? + + log_on_user(user) + redirect_to path("/") + end end def sso_login diff --git a/config/routes.rb b/config/routes.rb index 7f49f013e91..241d404f0bc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -292,7 +292,10 @@ Discourse::Application.routes.draw do get "extra-locales/:bundle" => "extra_locales#show" resources :session, id: RouteFormat.username, only: [:create, :destroy, :become] do - get 'become' + if Rails.env.development? + get 'become' + end + collection do post "forgot_password" end