From 9cb65b112311c986b86c801121022b70abd84874 Mon Sep 17 00:00:00 2001 From: chovin Date: Wed, 2 Oct 2024 02:46:28 +1000 Subject: [PATCH 1/4] [config] prevents registering defaults for reserved identifiers --- redbot/core/config.py | 20 ++++++++++++++++++++ tests/core/test_config.py | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/redbot/core/config.py b/redbot/core/config.py index dc6bbed7464..cde94c79bc6 100644 --- a/redbot/core/config.py +++ b/redbot/core/config.py @@ -141,6 +141,9 @@ class Value: """ + # to reserve these attributes for __getattr__ + __slots__ = ("identifier_data", "default", "_driver", "_config", "__dict__") + def __init__(self, identifier_data: IdentifierData, default_value, driver, config: "Config"): self.identifier_data = identifier_data self.default = default_value @@ -291,6 +294,9 @@ class Group(Value): """ + # to reserve these attributes for __getattr__ + __slots__ = ("_defaults", "force_registration") + def __init__( self, identifier_data: IdentifierData, @@ -640,6 +646,18 @@ class Config(metaclass=ConfigMeta): USER = "USER" MEMBER = "MEMBER" + # to reserve these attributes for __getattr__ + __slots__ = ( + "cog_name", + "unique_identifier", + "_driver", + "force_registration", + "_defaults", + "custom_groups", + "_lock_cache", + "__weakref__", + ) + def __init__( self, cog_name: str, @@ -782,6 +800,8 @@ def _get_defaults_dict(key: str, value) -> dict: for i, k in enumerate(splitted, start=1): if not k.isidentifier(): raise RuntimeError("'{}' is an invalid config key.".format(k)) + if k in [*vars(Config), *vars(Group), *vars(Value)]: + raise RuntimeError("'{}' is a reserved config key.".format(k)) if i == len(splitted): partial[k] = value else: diff --git a/tests/core/test_config.py b/tests/core/test_config.py index ac34ae6885b..8c7a18b55f4 100644 --- a/tests/core/test_config.py +++ b/tests/core/test_config.py @@ -16,6 +16,12 @@ def test_config_register_global_badvalues(config): config.register_global(**{"invalid var name": True}) +def test_config_register_reserved_keys(config): + for attr in vars(config): + with pytest.raises((RuntimeError, ValueError)): + config.register_global(**{attr: True}) + + async def test_config_register_guild(config, empty_guild): config.register_guild(enabled=False, some_list=[], some_dict={}) assert config.defaults[config.GUILD]["enabled"] is False From 6d3a670b275c8556efe8783ab8a1fac3f031f66c Mon Sep 17 00:00:00 2001 From: chovin Date: Wed, 2 Oct 2024 14:43:22 +1000 Subject: [PATCH 2/4] [config] better test register_reserved_keys --- tests/core/test_config.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/core/test_config.py b/tests/core/test_config.py index 8c7a18b55f4..f3861354dba 100644 --- a/tests/core/test_config.py +++ b/tests/core/test_config.py @@ -17,8 +17,9 @@ def test_config_register_global_badvalues(config): def test_config_register_reserved_keys(config): - for attr in vars(config): - with pytest.raises((RuntimeError, ValueError)): + config.register_global(**{"group": {"value": True}, "value": True}) + for attr in [*dir(config), *dir(config.group), *dir(config.value)]: + with pytest.raises(RuntimeError): config.register_global(**{attr: True}) From 6f74098c1c44172527beb578012311143a462564 Mon Sep 17 00:00:00 2001 From: chovin Date: Wed, 2 Oct 2024 14:44:54 +1000 Subject: [PATCH 3/4] fix config not passing new test --- redbot/core/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redbot/core/config.py b/redbot/core/config.py index cde94c79bc6..007c6833679 100644 --- a/redbot/core/config.py +++ b/redbot/core/config.py @@ -800,7 +800,7 @@ def _get_defaults_dict(key: str, value) -> dict: for i, k in enumerate(splitted, start=1): if not k.isidentifier(): raise RuntimeError("'{}' is an invalid config key.".format(k)) - if k in [*vars(Config), *vars(Group), *vars(Value)]: + if k in [*dir(Config), *dir(Group), *dir(Value)]: raise RuntimeError("'{}' is a reserved config key.".format(k)) if i == len(splitted): partial[k] = value From bbb0e5a83a8406bd3ebb1affa6cf9388c569596f Mon Sep 17 00:00:00 2001 From: chovin Date: Wed, 2 Oct 2024 14:46:25 +1000 Subject: [PATCH 4/4] add __dict__ to Config slots in case any cog creators had added attributes to config at runtime --- redbot/core/config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/redbot/core/config.py b/redbot/core/config.py index 007c6833679..3b3f921d032 100644 --- a/redbot/core/config.py +++ b/redbot/core/config.py @@ -656,6 +656,7 @@ class Config(metaclass=ConfigMeta): "custom_groups", "_lock_cache", "__weakref__", + "__dict__", ) def __init__(