From 2500cca3ce09bd583fa5271d129fec3da7d58f6a Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Tue, 15 Aug 2023 17:21:05 +0100 Subject: [PATCH] Add warning for disallowed keys --- .../config/configurable_from_database.ex | 10 ++++++-- lib/pleroma/config/transfer_task.ex | 12 +++++++++ test/pleroma/config/transfer_task_test.exs | 25 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/config/configurable_from_database.ex b/lib/pleroma/config/configurable_from_database.ex index 6913325d9..1e97bc640 100644 --- a/lib/pleroma/config/configurable_from_database.ex +++ b/lib/pleroma/config/configurable_from_database.ex @@ -85,14 +85,20 @@ defmodule Pleroma.Config.ConfigurableFromDatabase do def enabled, do: Config.get(:configurable_from_database) + # the whitelist check can be called from either the loader or the + # doc generator, which is spitting out strings + defp maybe_stringified_atom_equal(a, b) do + a == inspect(b) || a == b + end + def whitelisted_config?(group, key) do allowed_groups() |> Enum.any?(fn {whitelisted_group} -> - group == inspect(whitelisted_group) + maybe_stringified_atom_equal(group, whitelisted_group) {whitelisted_group, whitelisted_key} -> - group == inspect(whitelisted_group) && key == inspect(whitelisted_key) + maybe_stringified_atom_equal(group, whitelisted_group) && maybe_stringified_atom_equal(key, whitelisted_key) end) end diff --git a/lib/pleroma/config/transfer_task.ex b/lib/pleroma/config/transfer_task.ex index a8a96f393..0a55aab3c 100644 --- a/lib/pleroma/config/transfer_task.ex +++ b/lib/pleroma/config/transfer_task.ex @@ -8,6 +8,7 @@ defmodule Pleroma.Config.TransferTask do alias Pleroma.Config alias Pleroma.ConfigDB alias Pleroma.Repo + alias Pleroma.Config.ConfigurableFromDatabase require Logger @@ -91,6 +92,17 @@ defmodule Pleroma.Config.TransferTask do defp invalid_key_or_group(_), do: false defp merge_with_default(%{group: group, key: key, value: value} = setting) do + if !ConfigurableFromDatabase.whitelisted_config?(setting) do + Logger.warning(~s[ + config #{inspect(group)}, #{inspect(key)} is set in the database, + but it is not explicitly allowed to be there. Consider removing it + with + MIX: mix pleroma.config delete #{group} #{key} + OTP: ./bin/pleroma_ctl config delete #{group} #{key} + and setting it in your .exs file instead + ]) + end + default = if group == :pleroma do Config.get([key], Config.Holder.default_config(group, key)) diff --git a/test/pleroma/config/transfer_task_test.exs b/test/pleroma/config/transfer_task_test.exs index 2cb9f2f4b..298e7459e 100644 --- a/test/pleroma/config/transfer_task_test.exs +++ b/test/pleroma/config/transfer_task_test.exs @@ -93,6 +93,31 @@ defmodule Pleroma.Config.TransferTaskTest do assert assets_env[:mascots] == [a: 1, b: 2] end + test "transfer config values that are not explicitly whitelisted" do + insert(:config, key: :whoops, value: [not_allowed: true]) + + log = + capture_log(fn -> + TransferTask.start_link([]) + end) + + assert log =~ "config :pleroma, :whoops is set in the database" + assert log =~ "Consider removing it" + assert log =~ "mix pleroma.config delete pleroma whoops" + end + + test "transferring whitelisted values should not warn" do + insert(:config, key: :emoji, value: [allowed: true]) + insert(:config, key: Pleroma.Workers.PurgeExpiredActivity, value: [allowed: true]) + + log = + capture_log(fn -> + TransferTask.start_link([]) + end) + + refute log =~ "Consider removing it" + end + describe "pleroma restart" do setup do on_exit(fn ->