Skip to content

Commit c035669

Browse files
committed
Ensure that permitted event host types are allow-listed to prevent security issues
1 parent 874426e commit c035669

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

app/controllers/better_together/events_controller.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ def index
2121
def build_event_hosts # rubocop:disable Metrics/AbcSize
2222
return unless params[:host_id].present? && params[:host_type].present?
2323

24-
host_klass = params[:host_type].safe_constantize
25-
return unless host_klass
24+
return unless event_host_class
2625

27-
policy_scope = Pundit.policy_scope!(current_user, host_klass)
26+
policy_scope = Pundit.policy_scope!(current_user, event_host_class)
2827
host_record = policy_scope.find_by(id: params[:host_id])
2928
return unless host_record
3029

@@ -34,6 +33,14 @@ def build_event_hosts # rubocop:disable Metrics/AbcSize
3433
)
3534
end
3635

36+
def event_host_class
37+
param_type = params[:host_type]
38+
39+
# Allow-list only specific classes to be set as host for an event
40+
valid_host_types = BetterTogether::HostsEvents.included_in_models
41+
valid_host_types.find { |klass| klass.to_s == param_type }
42+
end
43+
3744
def resource_class
3845
::BetterTogether::Event
3946
end

app/models/better_together/person.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def self.primary_community_delegation_attrs
1212
include Author
1313
include Contactable
1414
include FriendlySlug
15+
include HostsEvents
1516
include Identifier
1617
include Identity
1718
include Member

app/models/concerns/better_together/hosts_events.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
module BetterTogether
44
# Concern that when included gives the model access to events through event_host records
5+
# This module must be included in a model to permit assigning instances as an event host
56
module HostsEvents
67
extend ActiveSupport::Concern
78

89
included do
910
has_many :event_hosts, as: :host
1011
has_many :hosted_events, through: :event_hosts, source: :event
1112
end
13+
14+
def self.included_in_models
15+
included_module = self
16+
Rails.application.eager_load! if Rails.env.development? # Ensure all models are loaded
17+
ActiveRecord::Base.descendants.select { |model| model.included_modules.include?(included_module) }
18+
end
1219
end
1320
end

0 commit comments

Comments
 (0)