From 01a53ace8aa812a7ba88bbc7d4de7cb32b44b093 Mon Sep 17 00:00:00 2001 From: Katsuya HIDAKA Date: Wed, 25 Nov 2026 11:38:43 +0900 Subject: [PATCH] Exclude email addresses from the project members filter (#44581). The user/group filter of the project members list used Principal.like, which also matches email addresses that are not displayed in the list. Since the members page is now available to anonymous users on public projects, it also allowed guessing email addresses hidden by their users. Co-Authored-By: Claude Opus 5.5 --- app/models/member.rb | 5 +++-- app/models/principal.rb | 8 ++++++-- test/functional/members_controller_test.rb | 9 +++++++++ test/unit/member_test.rb | 8 ++++++++ test/unit/principal_test.rb | 7 +++++++ 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/models/member.rb b/app/models/member.rb index dee561655..67b39124c 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -37,10 +37,11 @@ class Member < ApplicationRecord joins(:project). where(Project.allowed_to_condition(args.shift || User.current, :view_members, *args)) end) - # Members whose principal matches the given string + # Members whose principal name or login matches the given string + # (email addresses are not searched since they are not displayed in the members list) scope :like, (lambda do |arg| if arg.present? - where(:user_id => Principal.like(arg).select(:id)) + where(:user_id => Principal.like(arg, mail: false).select(:id)) end end) # Members having the given role, inherited or not diff --git a/app/models/principal.rb b/app/models/principal.rb index 48094241d..b0f61a407 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -68,14 +68,18 @@ class Principal < ApplicationRecord end end) - scope :like, (lambda do |q| + # Principals whose login, name or email address matches the given string. + # Email addresses are not searched when mail is false. + scope :like, (lambda do |q, mail: true| q = q.to_s if q.blank? where({}) else pattern = "%#{sanitize_sql_like q}%" sql = "LOWER(#{table_name}.login) LIKE LOWER(:p) ESCAPE :s" - sql << " OR #{table_name}.id IN (SELECT user_id FROM #{EmailAddress.table_name} WHERE LOWER(address) LIKE LOWER(:p) ESCAPE :s)" + if mail + sql << " OR #{table_name}.id IN (SELECT user_id FROM #{EmailAddress.table_name} WHERE LOWER(address) LIKE LOWER(:p) ESCAPE :s)" + end params = {:p => pattern, :s => '\\'} tokens = q.split(/\s+/).reject(&:blank?).map {|token| "%#{sanitize_sql_like token}%"} diff --git a/test/functional/members_controller_test.rb b/test/functional/members_controller_test.rb index c6ae33917..a56fa9799 100644 --- a/test/functional/members_controller_test.rb +++ b/test/functional/members_controller_test.rb @@ -531,6 +531,15 @@ class MembersControllerTest < Redmine::ControllerTest assert_select 'tr#member-1' end + def test_index_html_filter_by_email_address_should_not_find_the_member + with_settings :login_required => '0' do + @request.session[:user_id] = nil + get(:index, :params => {:project_id => 1, :member_name => 'jsmith@somenet'}) + assert_response :success + assert_select 'tr#member-1', :count => 0 + end + end + def test_index_html_filter_by_role get(:index, :params => {:project_id => 1, :member_role_id => 2}) assert_response :success diff --git a/test/unit/member_test.rb b/test/unit/member_test.rb index dae800d04..6c3229cf8 100644 --- a/test/unit/member_test.rb +++ b/test/unit/member_test.rb @@ -62,6 +62,14 @@ class MemberTest < ActiveSupport::TestCase assert_equal [9], Project.find(2).memberships.like('B Team').ids end + def test_like_scope_should_match_login + assert_equal [1], Project.find(1).memberships.like('jsmi').ids + end + + def test_like_scope_should_not_match_email_addresses + assert_empty Project.find(1).memberships.like('jsmith@somenet').ids + end + def test_like_scope_with_blank_value_should_return_all_the_members project = Project.find(1) assert_equal project.memberships.ids.sort, project.memberships.like('').ids.sort diff --git a/test/unit/principal_test.rb b/test/unit/principal_test.rb index fc9c943b3..f8af9115a 100644 --- a/test/unit/principal_test.rb +++ b/test/unit/principal_test.rb @@ -120,6 +120,13 @@ class PrincipalTest < ActiveSupport::TestCase assert results.all? {|u| u.mail.match(/somenet/i)} end + test "like scope should not search mail if mail option is false" do + assert_include User.find(2), Principal.like('jsmith@somenet') + assert_not_include User.find(2), Principal.like('jsmith@somenet', mail: false) + assert_include User.find(2), Principal.like('jsmi', mail: false) + assert_include User.find(2), Principal.like('john smi', mail: false) + end + test "like scope should search firstname and lastname" do results = Principal.like('john smi') -- 2.51.0