Skip to content

A platform for community discussion. Free, open, simple.

FIX: Enforce can_see_ip checks across admin IP features (#40019)

Sam746000edc86d
diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rb
index e8763ce5a1b..6e9f46c5cae 100644
--- a/app/controllers/admin/reports_controller.rb
+++ b/app/controllers/admin/reports_controller.rb
@@ -4,7 +4,7 @@ class Admin::ReportsController < Admin::StaffController
REPORTS_LIMIT = 50
def index
- render_json_dump(reports: Reports::ListQuery.call(admin: current_user.admin?))
+ render_json_dump(reports: Reports::ListQuery.call(guardian: guardian))
end
def bulk
@@ -20,7 +20,7 @@ class Admin::ReportsController < Admin::StaffController
report = nil
report = Report.find_cached(report_type, args) if (report_params[:cache])
- if Report.hidden?(report_type, admin: current_user.admin?)
+ if Report.hidden?(report_type, guardian: guardian)
report = Report._get(report_type, args)
report.error = :not_found
end
@@ -49,7 +49,7 @@ class Admin::ReportsController < Admin::StaffController
report_type = params[:type]
raise Discourse::NotFound unless report_type =~ /\A[a-z0-9\_]+\z/
- raise Discourse::NotFound if Report.hidden?(report_type, admin: current_user.admin?)
+ raise Discourse::NotFound if Report.hidden?(report_type, guardian: guardian)
args = parse_params(params)
args[:current_user] = current_user
diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
index 7815ec8363d..e115436b807 100644
--- a/app/controllers/admin/users_controller.rb
+++ b/app/controllers/admin/users_controller.rb
@@ -30,7 +30,7 @@ class Admin::UsersController < Admin::StaffController
]
def index
- users = ::AdminUserIndexQuery.new(params).find_users
+ users = ::AdminUserIndexQuery.new(params, guardian: guardian).find_users
opts = { include_can_be_deleted: true, include_silence_reason: true }
if params[:show_emails] == "true"
@@ -492,9 +492,10 @@ class Admin::UsersController < Admin::StaffController
end
def delete_other_accounts_with_same_ip
- params.require(:ip)
- params.require(:exclude)
- params.require(:order)
+ params.require(:user_id)
+
+ query = AdminUserIndexQuery.new(same_ip_query_params, guardian: guardian)
+ raise Discourse::NotFound unless query.same_ip_target_user
user_destroyer = UserDestroyer.new(current_user)
options = {
@@ -503,23 +504,20 @@ class Admin::UsersController < Admin::StaffController
block_urls: true,
block_ip: true,
delete_as_spammer: true,
- context: I18n.t("user.destroy_reasons.same_ip_address", ip_address: params[:ip]),
+ context: same_ip_address_context(query),
}
- AdminUserIndexQuery
- .new(params)
- .find_users(50)
- .each { |user| user_destroyer.destroy(user, options) }
+ query.find_users(50).each { |user| user_destroyer.destroy(user, options) }
render json: success_json
end
def total_other_accounts_with_same_ip
- params.require(:ip)
- params.require(:exclude)
- params.require(:order)
+ params.require(:user_id)
- render json: { total: AdminUserIndexQuery.new(params).count_users }
+ render json: {
+ total: AdminUserIndexQuery.new(same_ip_query_params, guardian: guardian).count_users,
+ }
end
def anonymize
@@ -594,6 +592,26 @@ class Admin::UsersController < Admin::StaffController
raise Discourse::NotFound unless @user
end
+ def same_ip_query_params
+ {
+ same_ip_user_id: params[:user_id],
+ exclude: params[:user_id],
+ ip_type: params[:ip_type].presence,
+ order: "trust_level DESC",
+ }.compact
+ end
+
+ def same_ip_address_context(query)
+ if guardian.can_see_ip? && (ip = query.same_ip_address).present?
+ I18n.t("user.destroy_reasons.same_ip_address", ip_address: ip)
+ else
+ I18n.t(
+ "user.destroy_reasons.same_ip_address_user",
+ username: query.same_ip_target_user.username,
+ )
+ end
+ end
+
def refresh_browser(user)
MessageBus.publish "/file-change", ["refresh"], user_ids: [user.id]
end
diff --git a/app/models/report.rb b/app/models/report.rb
index 01a2f3dee95..4f38f290e04 100644
--- a/app/models/report.rb
+++ b/app/models/report.rb
@@ -37,9 +37,12 @@ class Report
]
ADMIN_ONLY_REPORTS = %w[admin_logins top_uploads topic_view_stats]
+ IP_ADDRESS_REPORTS = %w[suspicious_logins]
+
+ def self.hidden?(type, guardian:)
+ return true if !guardian.is_admin? && ADMIN_ONLY_REPORTS.include?(type)
+ return true if !guardian.is_admin? && !guardian.can_see_ip? && IP_ADDRESS_REPORTS.include?(type)
- def self.hidden?(type, admin:)
- return true if !admin && ADMIN_ONLY_REPORTS.include?(type)
hidden_reports =
SiteSetting.use_legacy_pageviews ? HIDDEN_PAGEVIEW_REPORTS : HIDDEN_LEGACY_PAGEVIEW_REPORTS
hidden_reports.include?(type)
diff --git a/app/queries/reports/list_query.rb b/app/queries/reports/list_query.rb
index 03dd1d39416..02cc96f0c63 100644
--- a/app/queries/reports/list_query.rb
+++ b/app/queries/reports/list_query.rb
@@ -10,8 +10,8 @@ module Reports
@type = name.to_s.gsub("report_", "")
end
- def visible?(admin:)
- return false if Report.hidden?(type, admin:)
+ def visible?(guardian:)
+ return false if Report.hidden?(type, guardian:)
if SiteSetting.reporting_improvements
return false if plugin_report? && plugin_disabled?
@@ -96,7 +96,7 @@ module Reports
end
end
- def self.call(admin:)
+ def self.call(guardian:)
page_view_req_report_methods =
["page_view_total_reqs"] +
ApplicationRequest
@@ -116,7 +116,7 @@ module Reports
reports_methods
.filter_map do |report_name|
report = Reports::ListQuery::FormattedReport.new(report_name)
- report.to_h if report.visible?(admin:)
+ report.to_h if report.visible?(guardian:)
end
.sort_by { |report| report[:title] }
end
diff --git a/app/serializers/concerns/user_auth_tokens_mixin.rb b/app/serializers/concerns/user_auth_tokens_mixin.rb
index aaa0b752eaa..b2b1774fe5f 100644
--- a/app/serializers/concerns/user_auth_tokens_mixin.rb
+++ b/app/serializers/concerns/user_auth_tokens_mixin.rb
@@ -5,6 +5,10 @@ module UserAuthTokensMixin
included { attributes :id, :client_ip, :location, :browser, :device, :os, :icon, :created_at }
+ def include_client_ip?
+ can_see_ip_details?
+ end
+
def client_ip
object.client_ip.to_s
end
@@ -45,4 +49,10 @@ module UserAuthTokensMixin
"question"
end
end
+
+ private
+
+ def can_see_ip_details?
+ scope&.can_see_ip? || (scope&.user.present? && scope.user.id == object.user_id)
+ end
end
diff --git a/app/serializers/screened_url_serializer.rb b/app/serializers/screened_url_serializer.rb
index c4f561071ec..a6142f91ee7 100644
--- a/app/serializers/screened_url_serializer.rb
+++ b/app/serializers/screened_url_serializer.rb
@@ -10,4 +10,8 @@ class ScreenedUrlSerializer < ApplicationSerializer
def ip_address
object.ip_address.try(:to_s)
end
+
+ def include_ip_address?
+ scope&.can_see_ip?
+ end
end
diff --git a/app/services/admin/search/list.rb b/app/services/admin/search/list.rb
index b8ad1126839..98419876c8f 100644
--- a/app/services/admin/search/list.rb
+++ b/app/services/admin/search/list.rb
@@ -46,7 +46,7 @@ module Admin
end
def fetch_reports(guardian:)
- Reports::ListQuery.call(admin: true)
+ Reports::ListQuery.call(guardian: guardian)
end
def fetch_upcoming_changes(guardian:)
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 2b48e49f8c3..76f0954aeb1 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -3255,6 +3255,7 @@ en:
unused_staged_user: "Unused staged user"
fixed_primary_email: "Fixed primary email for staged user"
same_ip_address: "Same IP address (%{ip_address}) as other users"
+ same_ip_address_user: "Same IP address as %{username}"
inactive_user: "Inactive user"
reviewable_reject_auto: "Auto handle queued reviewables"
reviewable_reject: "Reviewable user rejected"
diff --git a/frontend/discourse/admin/components/ip-lookup.gjs b/frontend/discourse/admin/components/ip-lookup.gjs
index c8521f97604..741a79fc475 100644
--- a/frontend/discourse/admin/components/ip-lookup.gjs
+++ b/frontend/discourse/admin/components/ip-lookup.gjs
@@ -38,6 +38,16 @@ export default class IpLookup extends Component {
return Math.max(visible, total);
}
+ get sameIpData() {
+ return {
+ same_ip_user_id: this.args.userId,
+ user_id: this.args.userId,
+ ip_type: this.args.ipType || "last",
+ exclude: this.args.userId,
+ order: "trust_level DESC",
+ };
+ }
+
@action
async lookup() {
this.loading = true;
@@ -61,18 +71,12 @@ export default class IpLookup extends Component {
if (!this.otherAccounts && this.ipToLookup) {
this.otherAccountsLoading = true;
- const data = {
- ip: this.ipToLookup,
- exclude: this.args.userId,
- order: "trust_level DESC",
- };
-
const result = await ajax("/admin/users/total-others-with-same-ip", {
- data,
+ data: this.sameIpData,
});
this.totalOthersWithSameIP = result.total;
- this.otherAccounts = await AdminUser.findAll("active", data);
+ this.otherAccounts = await AdminUser.findAll("active", this.sameIpData);
this.otherAccountsLoading = false;
}
} catch (error) {
@@ -128,11 +132,7 @@ export default class IpLookup extends Component {
try {
await ajax("/admin/users/delete-others-with-same-ip.json", {
type: "DELETE",
- data: {
- ip: this.ipToLookup,
- exclude: this.args.userId,
- order: "trust_level DESC",
- },
+ data: this.sameIpData,
});
} catch (err) {
popupAjaxError(err);
diff --git a/frontend/discourse/admin/templates/admin-logs/screened-emails.gjs b/frontend/discourse/admin/templates/admin-logs/screened-emails.gjs
index 5b1e100fc04..86249be6249 100644
--- a/frontend/discourse/admin/templates/admin-logs/screened-emails.gjs
+++ b/frontend/discourse/admin/templates/admin-logs/screened-emails.gjs
@@ -12,13 +12,17 @@ export default <template>
}}
/>
- <DButton
- @action={{@controller.exportScreenedEmailList}}
- @title="admin.export_csv.button_title.screened_email"
- @icon="download"
- @label="admin.export_csv.button_text"
- class="btn-default screened-email-export"
- />
+ {{#if @controller.currentUser.can_see_emails}}
+ {{#if @controller.currentUser.can_see_ip}}
+ <DButton
+ @action={{@controller.exportScreenedEmailList}}
+ @title="admin.export_csv.button_title.screened_email"
+ @icon="download"
+ @label="admin.export_csv.button_text"
+ class="btn-default screened-email-export"
+ />
+ {{/if}}
+ {{/if}}
<br />
@@ -34,7 +38,9 @@ export default <template>
<th class="match_count">{{i18n "admin.logs.match_count"}}</th>
<th class="last_match_at">{{i18n "admin.logs.last_match_at"}}</th>
<th class="created_at">{{i18n "admin.logs.created_at"}}</th>
- <th class="ip_address">{{i18n "admin.logs.ip_address"}}</th>
+ {{#if @controller.currentUser.can_see_ip}}
+ <th class="ip_address">{{i18n "admin.logs.ip_address"}}</th>
+ {{/if}}
<th class="action"></th>
</thead>
<tbody>
@@ -56,7 +62,9 @@ export default <template>
<td class="created_at"><div class="label">{{i18n
"admin.logs.created_at"
}}</div>{{dAgeWithTooltip item.created_at}}</td>
- <td class="ip_address">{{item.ip_address}}</td>
+ {{#if @controller.currentUser.can_see_ip}}
+ <td class="ip_address">{{item.ip_address}}</td>
+ {{/if}}
<td class="action">
<DButton
@action={{fn @controller.clearBlock item}}
diff --git a/frontend/discourse/admin/templates/admin-user/index.gjs b/frontend/discourse/admin/templates/admin-user/index.gjs
index de74fad7e82..81876069d8d 100644
--- a/frontend/discourse/admin/templates/admin-user/index.gjs
+++ b/frontend/discourse/admin/templates/admin-user/index.gjs
@@ -235,6 +235,7 @@ export default <template>
<IpLookup
@ip={{@controller.model.ip_address}}
@userId={{@controller.model.id}}
+ @ipType="last"
/>
{{/if}}
{{/if}}
@@ -252,6 +253,7 @@ export default <template>
<IpLookup
@ip={{@controller.model.registration_ip_address}}
@userId={{@controller.model.id}}
+ @ipType="registration"
/>
{{/if}}
{{/if}}
diff --git a/frontend/discourse/admin/templates/admin/dashboard-security.gjs b/frontend/discourse/admin/templates/admin/dashboard-security.gjs
index 45c6a6e99fd..7176e671af7 100644
--- a/frontend/discourse/admin/templates/admin/dashboard-security.gjs
+++ b/frontend/discourse/admin/templates/admin/dashboard-security.gjs
@@ -9,10 +9,12 @@ export default <template>
/>
<div class="main-section">
- <AdminReport
- @dataSourceName="suspicious_logins"
- @filters={{@controller.lastWeekFilters}}
- />
+ {{#if @controller.currentUser.can_see_ip}}
+ <AdminReport
+ @dataSourceName="suspicious_logins"
+ @filters={{@controller.lastWeekFilters}}
+ />
+ {{/if}}
{{#if @controller.currentUser.admin}}
<AdminReport
diff --git a/frontend/discourse/app/components/reviewable/ip-lookup.gjs b/frontend/discourse/app/components/reviewable/ip-lookup.gjs
index 846667b6eb6..d22caede622 100644
--- a/frontend/discourse/app/components/reviewable/ip-lookup.gjs
+++ b/frontend/discourse/app/components/reviewable/ip-lookup.gjs
@@ -52,7 +52,9 @@ export default class ReviewableIpLookup extends Component {
get queryData() {
return {
- ip: this.ipAddress,
+ same_ip_user_id: this.target,
+ user_id: this.target,
+ ip_type: "last",
exclude: this.target,
order: "trust_level DESC",
};
@@ -73,12 +75,12 @@ export default class ReviewableIpLookup extends Component {
this.location = await ajax("/admin/users/ip-info", {
data: { ip: this.ipAddress },
});
-
- const result = await ajax("/admin/users/total-others-with-same-ip", {
- data: this.queryData,
- });
- this.totalOthersWithSameIP = result.total;
}
+
+ const result = await ajax("/admin/users/total-others-with-same-ip", {
+ data: this.queryData,
+ });
+ this.totalOthersWithSameIP = result.total;
} catch (error) {
popupAjaxError(error);
} finally {
diff --git a/lib/admin_user_index_query.rb b/lib/admin_user_index_query.rb
index 4b30c2c6ea3..85b9cdaf15e 100644
--- a/lib/admin_user_index_query.rb
+++ b/lib/admin_user_index_query.rb
@@ -1,13 +1,20 @@
# frozen_string_literal: true
class AdminUserIndexQuery
- def initialize(params = {}, klass = User, trust_levels = TrustLevel.levels)
- @params = params
+ def initialize(
+ params = {},
+ klass = User,
+ trust_levels = TrustLevel.levels,
+ guardian: nil,
+ **kwargs
+ )
+ @params = params.merge(kwargs)
@query = initialize_query_with_order(klass)
@trust_levels = trust_levels
+ @guardian = guardian
end
- attr_reader :params, :trust_levels
+ attr_reader :params, :trust_levels, :guardian
SORTABLE_MAPPING = {
"created" => "created_at",
@@ -24,6 +31,8 @@ class AdminUserIndexQuery
"silence_reason" => "silence_reason",
}
+ SAME_IP_ADDRESS_COLUMNS = { "last" => :ip_address, "registration" => :registration_ip_address }
+
def find_users(limit = 100)
page = params[:page].to_i - 1
page = 0 if page < 0
@@ -97,12 +106,10 @@ class AdminUserIndexQuery
filter = params[:filter]
if filter.present?
filter = filter.strip
- if ip =
- begin
- IPAddr.new(filter)
- rescue StandardError
- nil
- end
+ if ip = parse_ip(filter)
+ return if params[:same_ip_user_id].present?
+ return @query.none unless can_see_ip?
+
@query.where("ip_address <<= :ip OR registration_ip_address <<= :ip", ip: ip.to_cidr_s)
else
@query.filter_by_username_or_email(filter)
@@ -112,10 +119,32 @@ class AdminUserIndexQuery
def filter_by_ip
if params[:ip].present?
+ return if params[:same_ip_user_id].present?
+ return @query.none unless can_see_ip?
+
@query.where("ip_address = :ip OR registration_ip_address = :ip", ip: params[:ip].strip)
end
end
+ def filter_by_same_ip_user
+ if params[:same_ip_user_id].present?
+ if same_ip_address.present?
+ @query.where("ip_address = :ip OR registration_ip_address = :ip", ip: same_ip_address.to_s)
+ else
+ @query.none
+ end
+ end
+ end
+
+ def same_ip_target_user
+ return @same_ip_target_user if defined?(@same_ip_target_user)
+ @same_ip_target_user = User.find_by(id: params[:same_ip_user_id])
+ end
+
+ def same_ip_address
+ @same_ip_address ||= same_ip_target_user&.public_send(same_ip_address_column)
+ end
+
def filter_exclude
@query.where.not(id: params[:exclude]) if params[:exclude].present?
end
@@ -124,6 +153,20 @@ class AdminUserIndexQuery
@query = active_relation if active_relation
end
+ def same_ip_address_column
+ SAME_IP_ADDRESS_COLUMNS.fetch(params[:ip_type].presence, :ip_address)
+ end
+
+ def parse_ip(filter)
+ IPAddr.new(filter)
+ rescue StandardError
+ nil
+ end
+
+ def can_see_ip?
+ guardian&.can_see_ip?
+ end
+
def with_silence_reason
@query.joins(
"LEFT JOIN LATERAL (
@@ -142,6 +185,7 @@ class AdminUserIndexQuery
append filter_by_trust
append filter_by_query_classification
append filter_by_ip
+ append filter_by_same_ip_user
append filter_exclude
append filter_by_search
append with_silence_reason
diff --git a/lib/guardian.rb b/lib/guardian.rb
index 24ccd0962c3..7f30e864ab6 100644
--- a/lib/guardian.rb
+++ b/lib/guardian.rb
@@ -490,14 +490,14 @@ class Guardian
def can_export_entity?(entity, entity_id = nil, args = nil)
return false if anonymous?
return true if is_admin?
- return can_see_emails? if entity == "screened_email"
+ return can_see_emails? && can_see_ip? if entity == "screened_email"
return can_see_ip? if entity == "screened_ip"
if is_moderator? && (entity != "user_archive" || entity_id.nil?)
if entity == "report"
report_name = args&.[](:name) || args&.[]("name")
return true if report_name.blank?
- return !Report.hidden?(report_name, admin: false)
+ return !Report.hidden?(report_name, guardian: self)
end
return %w[staff_action screened_url report user_archive].include?(entity)
end
diff --git a/spec/lib/admin_user_index_query_spec.rb b/spec/lib/admin_user_index_query_spec.rb
index 16fd64e5450..ecfc1b0de62 100644
--- a/spec/lib/admin_user_index_query_spec.rb
+++ b/spec/lib/admin_user_index_query_spec.rb
@@ -230,7 +230,8 @@ RSpec.describe AdminUserIndexQuery do
fab!(:user) { Fabricate(:user, ip_address: "117.207.94.9") }
it "matches the ip address" do
- query = ::AdminUserIndexQuery.new(filter: " 117.207.94.9 ")
+ query =
+ ::AdminUserIndexQuery.new(filter: " 117.207.94.9 ", guardian: Fabricate(:admin).guardian)
expect(query.find_users.count()).to eq(1)
end
end
diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb
index 30488861ecd..1120049ccd0 100644
--- a/spec/models/report_spec.rb
+++ b/spec/models/report_spec.rb
@@ -2103,26 +2103,56 @@ RSpec.describe Report do
end
describe ".hidden?" do
- context "when admin is true" do
+ fab!(:report_admin, :admin)
+ fab!(:report_moderator, :moderator)
+
+ let(:admin_guardian) { report_admin.guardian }
+ let(:moderator_guardian) { report_moderator.guardian }
+
+ context "when the user is an admin" do
it "returns false for regular reports" do
- expect(Report.hidden?("topics", admin: true)).to eq(false)
+ expect(Report.hidden?("topics", guardian: admin_guardian)).to eq(false)
end
it "returns false for admin-only reports" do
Report::ADMIN_ONLY_REPORTS.each do |report_type|
- expect(Report.hidden?(report_type, admin: true)).to eq(false)
+ expect(Report.hidden?(report_type, guardian: admin_guardian)).to eq(false)
+ end
+ end
+
+ it "returns false for IP reports" do
+ SiteSetting.moderators_view_ips = false
+
+ Report::IP_ADDRESS_REPORTS.each do |report_type|
+ expect(Report.hidden?(report_type, guardian: admin_guardian)).to eq(false)
end
end
end
- context "when admin is false" do
+ context "when the user is not an admin" do
it "returns false for regular reports" do
- expect(Report.hidden?("topics", admin: false)).to eq(false)
+ expect(Report.hidden?("topics", guardian: moderator_guardian)).to eq(false)
end
it "returns true for admin-only reports" do
Report::ADMIN_ONLY_REPORTS.each do |report_type|
- expect(Report.hidden?(report_type, admin: false)).to eq(true)
+ expect(Report.hidden?(report_type, guardian: moderator_guardian)).to eq(true)
+ end
+ end
+
+ it "returns true for IP reports when IP viewing is disabled" do
+ SiteSetting.moderators_view_ips = false
+
+ Report::IP_ADDRESS_REPORTS.each do |report_type|
+ expect(Report.hidden?(report_type, guardian: moderator_guardian)).to eq(true)
+ end
+ end
+
+ it "returns false for IP reports when IP viewing is enabled" do
+ SiteSetting.moderators_view_ips = true
+
+ Report::IP_ADDRESS_REPORTS.each do |report_type|
+ expect(Report.hidden?(report_type, guardian: moderator_guardian)).to eq(false)
end
end
end
@@ -2132,13 +2162,13 @@ RSpec.describe Report do
it "hides pageview reports" do
Report::HIDDEN_PAGEVIEW_REPORTS.each do |report_type|
- expect(Report.hidden?(report_type, admin: true)).to eq(true)
+ expect(Report.hidden?(report_type, guardian: admin_guardian)).to eq(true)
end
end
it "does not hide legacy pageview reports" do
Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS.each do |report_type|
- expect(Report.hidden?(report_type, admin: true)).to eq(false)
+ expect(Report.hidden?(report_type, guardian: admin_guardian)).to eq(false)
end
end
end
@@ -2148,13 +2178,13 @@ RSpec.describe Report do
it "does not hide pageview reports" do
Report::HIDDEN_PAGEVIEW_REPORTS.each do |report_type|
- expect(Report.hidden?(report_type, admin: true)).to eq(false)
+ expect(Report.hidden?(report_type, guardian: admin_guardian)).to eq(false)
end
end
it "hides legacy pageview reports" do
Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS.each do |report_type|
- expect(Report.hidden?(report_type, admin: true)).to eq(true)
+ expect(Report.hidden?(report_type, guardian: admin_guardian)).to eq(true)
end
end
end
diff --git a/spec/queries/reports/list_query_spec.rb b/spec/queries/reports/list_query_spec.rb
index fc6b9e7d619..9759b7a25fd 100644
--- a/spec/queries/reports/list_query_spec.rb
+++ b/spec/queries/reports/list_query_spec.rb
@@ -1,8 +1,14 @@
# frozen_string_literal: true
RSpec.describe Reports::ListQuery do
+ fab!(:admin)
+ fab!(:moderator)
+
+ let(:admin_guardian) { admin.guardian }
+ let(:moderator_guardian) { moderator.guardian }
+
describe ".call" do
- subject(:result) { described_class.call(admin: true) }
+ subject(:result) { described_class.call(guardian: admin_guardian) }
let(:result_page_view_report_types) do
result.filter { |r| r[:type].starts_with?("page_view") }.map { |r| r[:type] }
@@ -85,16 +91,16 @@ RSpec.describe Reports::ListQuery do
end
end
- context "when admin is true" do
- subject(:result) { described_class.call(admin: true) }
+ context "when the user is an admin" do
+ subject(:result) { described_class.call(guardian: admin_guardian) }
it "includes admin-only reports" do
expect(result.map { |r| r[:type] }).to include(*Report::ADMIN_ONLY_REPORTS)
end
end
- context "when admin is false" do
- subject(:result) { described_class.call(admin: false) }
+ context "when the user is not an admin" do
+ subject(:result) { described_class.call(guardian: moderator_guardian) }
it "excludes admin-only reports" do
expect(result.map { |r| r[:type] }).not_to include(*Report::ADMIN_ONLY_REPORTS)
@@ -152,7 +158,7 @@ RSpec.describe Reports::ListQuery do
formatted = Reports::ListQuery::FormattedReport.new(:report_test_plugin_report)
formatted.stubs(:resolve_plugin_name).returns("test-plugin")
- expect(formatted.visible?(admin: true)).to eq(false)
+ expect(formatted.visible?(guardian: admin_guardian)).to eq(false)
end
it "is visible when the source plugin is enabled" do
@@ -162,7 +168,7 @@ RSpec.describe Reports::ListQuery do
formatted = Reports::ListQuery::FormattedReport.new(:report_test_plugin_report)
formatted.stubs(:resolve_plugin_name).returns("test-plugin")
- expect(formatted.visible?(admin: true)).to eq(true)
+ expect(formatted.visible?(guardian: admin_guardian)).to eq(true)
result = formatted.to_h
expect(result[:plugin]).to eq("test-plugin")
diff --git a/spec/requests/admin/reports_controller_spec.rb b/spec/requests/admin/reports_controller_spec.rb
index 9ee52925253..45dc901a15c 100644
--- a/spec/requests/admin/reports_controller_spec.rb
+++ b/spec/requests/admin/reports_controller_spec.rb
@@ -26,6 +26,16 @@ RSpec.describe Admin::ReportsController do
*Report::ADMIN_ONLY_REPORTS,
)
end
+
+ it "excludes IP reports when IP viewing is disabled" do
+ SiteSetting.moderators_view_ips = false
+
+ get "/admin/reports.json"
+
+ expect(response.parsed_body["reports"].map { |r| r["type"] }).not_to include(
+ *Report::IP_ADDRESS_REPORTS,
+ )
+ end
end
before { sign_in(admin) }
@@ -240,6 +250,19 @@ RSpec.describe Admin::ReportsController do
expect(response.parsed_body["reports"][2]).to include("error" => "not_found", "data" => nil)
expect(response.parsed_body["reports"][3]).to include("error" => "not_found", "data" => nil)
end
+
+ it "marks IP reports as not_found when IP viewing is disabled" do
+ SiteSetting.moderators_view_ips = false
+
+ reports = Report::IP_ADDRESS_REPORTS.index_with { { limit: 10 } }
+ get "/admin/reports/bulk.json", params: { reports: reports }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["reports"].count).to eq(Report::IP_ADDRESS_REPORTS.size)
+ response.parsed_body["reports"].each do |report|
+ expect(report).to include("error" => "not_found", "data" => nil)
+ end
+ end
end
context "when logged in as a non-staff user" do
@@ -437,6 +460,17 @@ RSpec.describe Admin::ReportsController do
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
end
end
+
+ it "does not allow accessing IP reports when IP viewing is disabled" do
+ SiteSetting.moderators_view_ips = false
+
+ Report::IP_ADDRESS_REPORTS.each do |report_type|
+ get "/admin/reports/#{report_type}.json"
+
+ expect(response.status).to eq(404)
+ expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
+ end
+ end
end
context "when logged in as a non-staff user" do
diff --git a/spec/requests/admin/users_controller_spec.rb b/spec/requests/admin/users_controller_spec.rb
index 5f562da533e..df1da27d2e9 100644
--- a/spec/requests/admin/users_controller_spec.rb
+++ b/spec/requests/admin/users_controller_spec.rb
@@ -72,6 +72,55 @@ RSpec.describe Admin::UsersController do
expect(response.status).to eq(200)
expect(response.parsed_body).to be_present
end
+
+ it "returns users with the same IP as a user" do
+ target_user = Fabricate(:user, ip_address: "42.42.42.42")
+ same_ip_user = Fabricate(:user, ip_address: "42.42.42.42")
+ Fabricate(:user, ip_address: "43.43.43.43")
+
+ get "/admin/users/list.json",
+ params: {
+ same_ip_user_id: target_user.id,
+ exclude: target_user.id,
+ order: "trust_level DESC",
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body.map { |result| result["id"] }).to contain_exactly(
+ same_ip_user.id,
+ )
+ end
+
+ it "does not allow raw IP searches when IP viewing is disabled" do
+ SiteSetting.moderators_view_ips = false
+ user_with_ip = Fabricate(:user, ip_address: "42.42.42.42")
+
+ get "/admin/users/list.json", params: { filter: user_with_ip.ip_address }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body.map { |result| result["id"] }).not_to include(user_with_ip.id)
+ end
+
+ it "ignores raw IP filters when searching by a server-resolved same-IP user" do
+ SiteSetting.moderators_view_ips = false
+ target_user = Fabricate(:user, ip_address: "42.42.42.42")
+ same_ip_user = Fabricate(:user, ip_address: "42.42.42.42")
+ Fabricate(:user, ip_address: "43.43.43.43")
+
+ get "/admin/users/list.json",
+ params: {
+ same_ip_user_id: target_user.id,
+ exclude: target_user.id,
+ filter: "43.43.43.43",
+ ip: "43.43.43.43",
+ order: "trust_level DESC",
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body.map { |result| result["id"] }).to contain_exactly(
+ same_ip_user.id,
+ )
+ end
end
context "when logged in as a non-staff user" do
@@ -2191,21 +2240,90 @@ RSpec.describe Admin::UsersController do
end
end
+ describe "#total_other_accounts_with_same_ip" do
+ shared_examples "counting other accounts with same ip possible" do
+ it "returns the count for a user" do
+ target_user = Fabricate(:user, ip_address: "42.42.42.42")
+ Fabricate(:user, ip_address: "42.42.42.42")
+ Fabricate(:user, ip_address: "42.42.42.42")
+
+ get "/admin/users/total-others-with-same-ip.json",
+ params: {
+ user_id: target_user.id,
+ exclude: target_user.id,
+ order: "trust_level DESC",
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["total"]).to eq(2)
+ end
+ end
+
+ context "when logged in as an admin" do
+ before { sign_in(admin) }
+
+ include_examples "counting other accounts with same ip possible"
+ end
+
+ context "when logged in as a moderator" do
+ before { sign_in(moderator) }
+
+ include_examples "counting other accounts with same ip possible"
+ end
+
+ context "when logged in as a non-staff user" do
+ before { sign_in(user) }
+
+ it "denies access with a 404 response" do
+ target_user = Fabricate(:user, ip_address: "42.42.42.42")
+
+ get "/admin/users/total-others-with-same-ip.json",
+ params: {
+ user_id: target_user.id,
+ exclude: target_user.id,
+ order: "trust_level DESC",
+ }
+
+ expect(response.status).to eq(404)
+ end
+ end
+ end
+
describe "#delete_other_accounts_with_same_ip" do
shared_examples "deleting other accounts with same ip possible" do
it "works" do
+ target_user = Fabricate(:user, ip_address: "42.42.42.42")
user_a = Fabricate(:user, ip_address: "42.42.42.42")
user_b = Fabricate(:user, ip_address: "42.42.42.42")
delete "/admin/users/delete-others-with-same-ip.json",
params: {
- ip: "42.42.42.42",
- exclude: -1,
+ user_id: target_user.id,
+ exclude: target_user.id,
+ order: "trust_level DESC",
+ }
+ expect(response.status).to eq(200)
+ expect(User.exists?(target_user.id)).to eq(true)
+ expect(User.exists?(user_a.id)).to eq(false)
+ expect(User.exists?(user_b.id)).to eq(false)
+ end
+
+ it "does not delete the target user when exclude is tampered with" do
+ target_user = Fabricate(:user, ip_address: "42.42.42.42")
+ other_user = Fabricate(:user, ip_address: "42.42.42.42")
+
+ delete "/admin/users/delete-others-with-same-ip.json",
+ params: {
+ user_id: target_user.id,
+ exclude: other_user.id,
+ filter: target_user.ip_address,
+ ip: target_user.ip_address,
order: "trust_level DESC",
}
+
expect(response.status).to eq(200)
- expect(User.where(id: user_a.id).count).to eq(0)
- expect(User.where(id: user_b.id).count).to eq(0)
+ expect(User.exists?(target_user.id)).to eq(true)
+ expect(User.exists?(other_user.id)).to eq(false)
end
end
@@ -2219,25 +2337,50 @@ RSpec.describe Admin::UsersController do
before { sign_in(moderator) }
include_examples "deleting other accounts with same ip possible"
+
+ it "does not reveal the IP address in the staff log context without IP viewing permission" do
+ SiteSetting.moderators_view_ips = false
+ ip_address = "42.42.42.42"
+ target_user = Fabricate(:user, ip_address: ip_address)
+ Fabricate(:user, ip_address: ip_address)
+
+ delete "/admin/users/delete-others-with-same-ip.json",
+ params: {
+ user_id: target_user.id,
+ exclude: target_user.id,
+ order: "trust_level DESC",
+ }
+
+ expect(response.status).to eq(200)
+
+ histories =
+ UserHistory.where(action: UserHistory.actions[:delete_user], acting_user_id: moderator.id)
+ contexts = histories.pluck(:context)
+
+ expect(histories).to be_exists
+ expect(contexts).to all(include(target_user.username))
+ expect(contexts).to all(exclude(ip_address))
+ end
end
context "when logged in as a non-staff user" do
before { sign_in(user) }
it "prevents deletion of other accounts with same ip with a 404 response" do
+ target_user = Fabricate(:user, ip_address: "42.42.42.42")
user_a = Fabricate(:user, ip_address: "42.42.42.42")
user_b = Fabricate(:user, ip_address: "42.42.42.42")
delete "/admin/users/delete-others-with-same-ip.json",
params: {
- ip: "42.42.42.42",
- exclude: -1,
+ user_id: target_user.id,
+ exclude: target_user.id,
order: "trust_level DESC",
}
expect(response.status).to eq(404)
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
- expect(User.where(id: user_a.id).count).to eq(1)
- expect(User.where(id: user_b.id).count).to eq(1)
+ expect(User.exists?(user_a.id)).to eq(true)
+ expect(User.exists?(user_b.id)).to eq(true)
end
end
end
diff --git a/spec/requests/export_csv_controller_spec.rb b/spec/requests/export_csv_controller_spec.rb
index 81ac13ca75e..3cec6eead0a 100644
--- a/spec/requests/export_csv_controller_spec.rb
+++ b/spec/requests/export_csv_controller_spec.rb
@@ -170,14 +170,25 @@ RSpec.describe ExportCsvController do
expect(response.status).to eq(422)
end
- it "does not allow moderators to export screened_email if they has no permission to view emails" do
+ it "does not allow moderators to export screened_email without permission to view emails" do
SiteSetting.moderators_view_emails = false
post "/export_csv/export_entity.json", params: { entity: "screened_email" }
expect(response.status).to eq(422)
end
- it "allows moderator to export screened_email if they has permission to view emails" do
+ it "does not allow moderators to export screened_email without permission to view IPs" do
SiteSetting.moderators_view_emails = true
+ SiteSetting.moderators_view_ips = false
+
+ post "/export_csv/export_entity.json", params: { entity: "screened_email" }
+
+ expect(response.status).to eq(422)
+ expect(Jobs::ExportCsvFile.jobs.size).to eq(0)
+ end
+
+ it "allows moderators to export screened_email with permission to view emails and IPs" do
+ SiteSetting.moderators_view_emails = true
+ SiteSetting.moderators_view_ips = true
post "/export_csv/export_entity.json", params: { entity: "screened_email" }
expect(response.status).to eq(200)
expect(response.parsed_body["success"]).to eq("OK")
@@ -237,6 +248,23 @@ RSpec.describe ExportCsvController do
expect(Jobs::ExportCsvFile.jobs.size).to eq(0)
end
+ it "does not allow moderators to export IP reports when IP viewing is disabled" do
+ SiteSetting.moderators_view_ips = false
+
+ post "/export_csv/export_entity.json",
+ params: {
+ entity: "report",
+ args: {
+ name: "suspicious_logins",
+ start_date: "2026-01-01",
+ end_date: "2026-02-15",
+ },
+ }
+
+ expect(response.status).to eq(422)
+ expect(Jobs::ExportCsvFile.jobs.size).to eq(0)
+ end
+
it "allows moderators to export non-hidden reports" do
post "/export_csv/export_entity.json",
params: {
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index 203a1fc5dde..060d80a76ce 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require "rotp"
+require "discourse_ip_info"
RSpec.describe UsersController do
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
@@ -5198,6 +5199,52 @@ RSpec.describe UsersController do
expect(response.body).to include(user1.username)
end
end
+
+ describe "auth token IP visibility" do
+ before { DiscourseIpInfo.open_db(File.join(Rails.root, "spec", "fixtures", "mmdb")) }
+
+ let!(:token) do
+ UserAuthToken.generate!(
+ user_id: user1.id,
+ client_ip: "81.2.69.142",
+ user_agent: "Mozilla/5.0",
+ )
+ end
+
+ it "omits client_ip but keeps location when a moderator without can_see_ip views another user" do
+ SiteSetting.moderators_view_ips = false
+ sign_in(moderator)
+
+ get "/u/#{user1.username}.json"
+
+ expect(response.status).to eq(200)
+ serialized_token = response.parsed_body.dig("user", "user_auth_tokens", 0)
+ expect(serialized_token.keys).not_to include("client_ip")
+ expect(serialized_token["location"]).to eq("London, England, United Kingdom")
+ expect(response.body).not_to include(token.client_ip.to_s)
+ end
+
+ it "includes client_ip when an admin views another user" do
+ sign_in(admin)
+
+ get "/u/#{user1.username}.json"
+
+ expect(response.status).to eq(200)
+ serialized_token = response.parsed_body.dig("user", "user_auth_tokens", 0)
+ expect(serialized_token["client_ip"]).to eq(token.client_ip.to_s)
+ end
+
+ it "includes client_ip when a user views their own tokens" do
+ SiteSetting.moderators_view_ips = false
+ sign_in(user1)
+
+ get "/u/#{user1.username}.json"
+
+ expect(response.status).to eq(200)
+ serialized_token = response.parsed_body.dig("user", "user_auth_tokens", 0)
+ expect(serialized_token["client_ip"]).to eq(token.client_ip.to_s)
+ end
+ end
end
describe "#show_card" do
diff --git a/spec/services/admin/search/list_spec.rb b/spec/services/admin/search/list_spec.rb
index 7c49cbed4e5..f2e5ff76130 100644
--- a/spec/services/admin/search/list_spec.rb
+++ b/spec/services/admin/search/list_spec.rb
@@ -85,7 +85,7 @@ RSpec.describe Admin::Search::List do
expect(result.themes_and_components.map { |theme| theme[:name] }).to eq(
["Alpha Theme", "Beta Component", "Foundation", "Horizon"],
)
- expect(result.reports.length).to eq(Reports::ListQuery.call(admin: true).length)
+ expect(result.reports.length).to eq(Reports::ListQuery.call(guardian: guardian).length)
expect(result.upcoming_changes.length).to be > 0
expect(
result.upcoming_changes.find { |change| change[:setting] == :enable_upload_debug_mode },