Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Avoid misleading "Unpermitted parameters: xxx" log

Christophe Bliardc6f332620f9c
diff --git a/app/models/permitted_params.rb b/app/models/permitted_params.rb
index c768af525dd..90295e9f3fc 100644
--- a/app/models/permitted_params.rb
+++ b/app/models/permitted_params.rb
@@ -180,10 +180,8 @@ class PermittedParams
end
def settings
- permitted_params = params.require(:settings).permit
- all_valid_keys = AllowedSettings.all
-
- permitted_params.merge(params[:settings].to_unsafe_hash.slice(*all_valid_keys))
+ scalar_filters, complex_filters = AllowedSettings.scalar_and_complex_filters
+ params.require(:settings).permit(*scalar_filters, **complex_filters)
end
def user(additional_params = [])
diff --git a/app/models/permitted_params/allowed_settings.rb b/app/models/permitted_params/allowed_settings.rb
index ef30aab777d..b96f44b7ec8 100644
--- a/app/models/permitted_params/allowed_settings.rb
+++ b/app/models/permitted_params/allowed_settings.rb
@@ -29,6 +29,31 @@ class PermittedParams
keys
end
+ def scalar_and_complex_filters
+ restricted_keys = Set.new(self.restricted_keys)
+ scalar_filters = []
+ complex_filters = {}
+ Settings::Definition.all.each do |key, definition| # rubocop:disable Rails/FindEach
+ next if restricted_keys.include?(key)
+
+ case definition.format
+ when :hash
+ complex_filters[key] = {}
+ when :array
+ complex_filters[key] = []
+ else
+ scalar_filters << key
+ end
+ end
+
+ [scalar_filters, complex_filters]
+ end
+
+ def restricted_keys
+ restrictions.select(&:applicable?)
+ .flat_map(&:restricted_keys)
+ end
+
def add_restriction!(keys:, condition:)
restrictions << Restriction.new(keys, condition)
end
diff --git a/spec/models/permitted_params_spec.rb b/spec/models/permitted_params_spec.rb
index e410c07013b..23d42e88fda 100644
--- a/spec/models/permitted_params_spec.rb
+++ b/spec/models/permitted_params_spec.rb
@@ -812,6 +812,41 @@ RSpec.describe PermittedParams do
it { expect(subject).to eq(expected_permitted_hash) }
end
+
+ context "when fetching settings" do
+ include_context "with prepare params comparison"
+
+ let(:hash) do
+ {
+ "registration_footer" => {
+ "en" => "some footer"
+ },
+ "working_days" => ["", "1", "2", "3", "4", "5"]
+ }
+ end
+
+ def recording_notifications_for(notification)
+ events = []
+ subscription = ActiveSupport::Notifications.subscribe notification do |*args|
+ events << ActiveSupport::Notifications::Event.new(*args)
+ end
+
+ begin
+ yield
+ ensure
+ ActiveSupport::Notifications.unsubscribe(subscription)
+ end
+
+ events
+ end
+
+ it "does not log any 'unpermitted' message" do
+ events = recording_notifications_for(/unpermitted_parameters/) do
+ subject
+ end
+ expect(events).to be_empty
+ end
+ end
end
describe "#enumerations" do