[BACKEND] Disallow override names conflicting with plugin names

This commit is contained in:
Jesse Bannon 2025-12-26 14:39:26 -08:00
parent 157a0b59c4
commit 55cf6bc48d
4 changed files with 45 additions and 5 deletions

View file

@ -1,4 +1,4 @@
from typing import Any
from typing import Any, List, Iterable
from typing import Dict
from typing import Optional
from typing import Set
@ -88,6 +88,19 @@ class Overrides(UnstructuredDictFormatterValidator, Scriptable):
return True
def ensure_variable_names_not_a_plugin(self, plugin_names: Iterable[str]) -> None:
for name in self.keys:
if name.startswith("%"):
name = name[1:]
if name in plugin_names:
raise self._validation_exception(
f"Override variable with name {name} cannot be used since it is"
" the name of a plugin. Perhaps you meant to define it as a plugin? If so,"
" indent it left to make it at the same level as overrides.",
exception_class=InvalidVariableNameException,
)
def ensure_variable_name_valid(self, name: str) -> None:
"""
Ensures the variable name does not collide with any entry variables or built-in functions.

View file

@ -194,6 +194,10 @@ class Preset(_PresetShell):
self.plugins: PresetPlugins = self._validate_and_get_plugins()
self.overrides = self._validate_key(key="overrides", validator=Overrides, default={})
self.overrides.ensure_variable_names_not_a_plugin(
plugin_names=PRESET_KEYS
)
VariableValidation(
downloader_options=self.downloader_options,
output_options=self.output_options,

View file

@ -11,9 +11,6 @@ from ytdl_sub.entries.script.variable_types import Variable
from ytdl_sub.script.functions import Functions
from ytdl_sub.script.utils.name_validation import is_valid_name
# TODO: use this
SUBSCRIPTION_ARRAY = "subscription_array"
class SubscriptionVariables:
@staticmethod
@ -163,7 +160,7 @@ class OverrideHelpers:
True if the override name itself is valid. False otherwise.
"""
if name.startswith("%"):
return is_valid_name(name=name[1:])
name = name[1:]
return is_valid_name(name=name)

View file

@ -435,3 +435,29 @@ class TestPreset:
"output_options": {"output_directory": "dir", "file_name": "acjk"},
},
)
def test_preset_error_override_name_conflicts_with_plugin(self, config_file, output_options):
with pytest.raises(
ValidationException,
match=re.escape(
"Override variable with name throttle_protection cannot be used since it is the "
"name of a plugin. Perhaps you meant to define it as a plugin? If so, indent it "
"left to make it at the same level as overrides."
),
):
_ = Preset(
config=config_file,
name="test",
value={
"download": {
"url": "youtube.com/watch?v=123abc",
},
"subtitles": {
"embed_subtitles": True,
},
"output_options": {"output_directory": "dir", "file_name": "acjk"},
"overrides": {
"throttle_protection": "nope"
}
},
)