Skip to content

Commit 70ab9dc

Browse files
adhami3310masenf
andauthored
validate classname (#5204)
* validate classname * add subclass check * has_var = bool??? what did i do Co-authored-by: Masen Furer <m_github@0x26.net> --------- Co-authored-by: Masen Furer <m_github@0x26.net>
1 parent a24f08f commit 70ab9dc

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

reflex/components/component.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@
5050
from reflex.vars import VarData
5151
from reflex.vars.base import (
5252
CachedVarOperation,
53+
LiteralNoneVar,
5354
LiteralVar,
5455
Var,
5556
cached_property_no_lock,
5657
)
5758
from reflex.vars.function import ArgsFunctionOperation, FunctionStringVar, FunctionVar
5859
from reflex.vars.number import ternary_operation
59-
from reflex.vars.object import ObjectVar
60-
from reflex.vars.sequence import LiteralArrayVar
60+
from reflex.vars.object import LiteralObjectVar, ObjectVar
61+
from reflex.vars.sequence import LiteralArrayVar, LiteralStringVar, StringVar
6162

6263

6364
class BaseComponent(Base, ABC):
@@ -598,13 +599,36 @@ def determine_key(value: Any):
598599
# Convert class_name to str if it's list
599600
class_name = kwargs.get("class_name", "")
600601
if isinstance(class_name, (list, tuple)):
601-
if any(isinstance(c, Var) for c in class_name):
602+
has_var = False
603+
for c in class_name:
604+
if isinstance(c, str):
605+
continue
606+
if isinstance(c, Var):
607+
if not isinstance(c, StringVar) and not issubclass(
608+
c._var_type, str
609+
):
610+
raise TypeError(
611+
f"Invalid class_name passed for prop {type(self).__name__}.class_name, expected type str, got value {c._js_expr} of type {c._var_type}."
612+
)
613+
has_var = True
614+
else:
615+
raise TypeError(
616+
f"Invalid class_name passed for prop {type(self).__name__}.class_name, expected type str, got value {c} of type {type(c)}."
617+
)
618+
if has_var:
602619
kwargs["class_name"] = LiteralArrayVar.create(
603620
class_name, _var_type=list[str]
604621
).join(" ")
605622
else:
606623
kwargs["class_name"] = " ".join(class_name)
607-
624+
elif (
625+
isinstance(class_name, Var)
626+
and not isinstance(class_name, StringVar)
627+
and not issubclass(class_name._var_type, str)
628+
):
629+
raise TypeError(
630+
f"Invalid class_name passed for prop {type(self).__name__}.class_name, expected type str, got value {class_name._js_expr} of type {class_name._var_type}."
631+
)
608632
# Construct the component.
609633
for key, value in kwargs.items():
610634
setattr(self, key, value)
@@ -1146,7 +1170,7 @@ def _get_vars(
11461170
vars.append(comp_prop)
11471171
elif isinstance(comp_prop, str):
11481172
# Collapse VarData encoded in f-strings.
1149-
var = LiteralVar.create(comp_prop)
1173+
var = LiteralStringVar.create(comp_prop)
11501174
if var._get_all_var_data() is not None:
11511175
vars.append(var)
11521176

@@ -2494,7 +2518,7 @@ def render_dict_to_var(tag: dict | Component | str, imported_names: set[str]) ->
24942518
return Var.create(tag)
24952519

24962520
if "iterable" in tag:
2497-
function_return = Var.create(
2521+
function_return = LiteralArrayVar.create(
24982522
[
24992523
render_dict_to_var(child.render(), imported_names)
25002524
for child in tag["children"]
@@ -2537,7 +2561,7 @@ def render_dict_to_var(tag: dict | Component | str, imported_names: set[str]) ->
25372561
render_dict_to_var(tag["true_value"], imported_names),
25382562
render_dict_to_var(tag["false_value"], imported_names)
25392563
if tag["false_value"] is not None
2540-
else Var.create(None),
2564+
else LiteralNoneVar.create(),
25412565
)
25422566

25432567
props = {}
@@ -2553,7 +2577,9 @@ def render_dict_to_var(tag: dict | Component | str, imported_names: set[str]) ->
25532577
value = prop_str[prop + 2 : -1]
25542578
props[key] = value
25552579

2556-
props = Var.create({Var.create(k): Var(v) for k, v in props.items()})
2580+
props = LiteralObjectVar.create(
2581+
{LiteralStringVar.create(k): Var(v) for k, v in props.items()}
2582+
)
25572583

25582584
for prop in special_props:
25592585
props = props.merge(prop)
@@ -2564,7 +2590,7 @@ def render_dict_to_var(tag: dict | Component | str, imported_names: set[str]) ->
25642590
tag_name = Var(raw_tag_name or "Fragment")
25652591

25662592
tag_name = (
2567-
Var.create(raw_tag_name)
2593+
LiteralStringVar.create(raw_tag_name)
25682594
if raw_tag_name
25692595
and raw_tag_name.split(".")[0] not in imported_names
25702596
and raw_tag_name.lower() == raw_tag_name

0 commit comments

Comments
 (0)