From b179a43679656941e0f733229a37eff2599c2431 Mon Sep 17 00:00:00 2001 From: Harsh Thakare Date: Thu, 26 Nov 2026 10:30:00 +0530 Subject: [PATCH 1/3] fix: escape toast dismissal IDs --- .../news/+8017-toast-dismiss-string-id.bugfix.md | 1 + .../src/reflex_components_sonner/toast.py | 2 +- tests/units/reflex_components_sonner/test_toast.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 packages/reflex-components-sonner/news/+8017-toast-dismiss-string-id.bugfix.md diff --git a/packages/reflex-components-sonner/news/+8017-toast-dismiss-string-id.bugfix.md b/packages/reflex-components-sonner/news/+8017-toast-dismiss-string-id.bugfix.md new file mode 100644 index 00000000000..1340595184b --- /dev/null +++ b/packages/reflex-components-sonner/news/+8017-toast-dismiss-string-id.bugfix.md @@ -0,0 +1 @@ +Toast IDs passed to ``rx.toast.dismiss`` are now serialized safely when they contain quotes or newlines. diff --git a/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py b/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py index ff6bece4b13..4d2cfab7335 100644 --- a/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py +++ b/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py @@ -382,7 +382,7 @@ def toast_dismiss(id: Var[str] | str | None = None) -> EventSpec: dismiss = f"{toast_ref}.dismiss({id!s})" dismiss_var_data = id._get_all_var_data() elif isinstance(id, str): - dismiss = f"{toast_ref}.dismiss('{id}')" + dismiss = f"{toast_ref}.dismiss({LiteralVar.create(id)!s})" else: dismiss = f"{toast_ref}.dismiss()" dismiss_action = Var( diff --git a/tests/units/reflex_components_sonner/test_toast.py b/tests/units/reflex_components_sonner/test_toast.py index bca21a536ed..b63fd2243f5 100644 --- a/tests/units/reflex_components_sonner/test_toast.py +++ b/tests/units/reflex_components_sonner/test_toast.py @@ -2,6 +2,7 @@ import json +import pytest from reflex_base.style import Style from reflex_components_sonner.toast import Toaster, ToastProps @@ -25,3 +26,12 @@ def test_toast_button_styles_use_sonner_prop_names(): assert serialized["cancelButtonStyle"]["color"] == "white" assert "actionButtonStyles" not in serialized assert "cancelButtonStyles" not in serialized + + +@pytest.mark.parametrize("toast_id", ["toast's-id", 'toast"-id', "toast\nid"]) +def test_toast_dismiss_serializes_string_id(toast_id: str): + """Serialize string IDs as valid JavaScript string literals.""" + event = Toaster.toast_dismiss(toast_id) + function = next(value for key, value in event.args if str(key) == "function") + + assert f".dismiss({json.dumps(toast_id)})" in str(function) From e2c2718b3fa4920a0a390c413eaea0b512a573ee Mon Sep 17 00:00:00 2001 From: Harsh Thakare Date: Thu, 26 Nov 2026 10:47:48 +0530 Subject: [PATCH 2/3] fix: keep toast dismissal IDs opaque --- .../src/reflex_components_sonner/toast.py | 4 ++- .../reflex_components_sonner/test_toast.py | 28 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py b/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py index 4d2cfab7335..61351a252e0 100644 --- a/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py +++ b/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py @@ -4,6 +4,7 @@ import builtins import dataclasses +import json from typing import Any, Literal from reflex_base.components.component import Component, ComponentNamespace, field @@ -382,7 +383,8 @@ def toast_dismiss(id: Var[str] | str | None = None) -> EventSpec: dismiss = f"{toast_ref}.dismiss({id!s})" dismiss_var_data = id._get_all_var_data() elif isinstance(id, str): - dismiss = f"{toast_ref}.dismiss({LiteralVar.create(id)!s})" + serialized_id = json.dumps(id).replace("<", r"\u003c") + dismiss = f"{toast_ref}.dismiss({serialized_id})" else: dismiss = f"{toast_ref}.dismiss()" dismiss_action = Var( diff --git a/tests/units/reflex_components_sonner/test_toast.py b/tests/units/reflex_components_sonner/test_toast.py index b63fd2243f5..7e9c9c52285 100644 --- a/tests/units/reflex_components_sonner/test_toast.py +++ b/tests/units/reflex_components_sonner/test_toast.py @@ -28,10 +28,30 @@ def test_toast_button_styles_use_sonner_prop_names(): assert "cancelButtonStyles" not in serialized -@pytest.mark.parametrize("toast_id", ["toast's-id", 'toast"-id', "toast\nid"]) -def test_toast_dismiss_serializes_string_id(toast_id: str): - """Serialize string IDs as valid JavaScript string literals.""" +@pytest.mark.parametrize( + ("toast_id", "expected"), + [ + ( + "toast's-id", + '''(() => refs['__toast'].dismiss("toast's-id"))''', + ), + ( + 'toast"-id', + r'''(() => refs['__toast'].dismiss("toast\"-id"))''', + ), + ( + "toast\nid", + r'''(() => refs['__toast'].dismiss("toast\nid"))''', + ), + ( + "x", + r'''(() => refs['__toast'].dismiss("\u003creflex.Var>x\u003c/reflex.Var>"))''', + ), + ], +) +def test_toast_dismiss_serializes_string_id(toast_id: str, expected: str): + """Serialize string IDs as opaque JavaScript string literals.""" event = Toaster.toast_dismiss(toast_id) function = next(value for key, value in event.args if str(key) == "function") - assert f".dismiss({json.dumps(toast_id)})" in str(function) + assert str(function) == expected From d1cc7ac57eb3eb291ee50d342f3022ee958b4d6f Mon Sep 17 00:00:00 2001 From: Harsh Thakare Date: Fri, 27 Nov 2026 07:30:49 +0000 Subject: [PATCH 3/3] Format the toast dismissal regression test --- .../src/reflex_components_sonner/toast.py | 1 + .../reflex_components_sonner/test_toast.py | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py b/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py index 61351a252e0..2d5e073bec1 100644 --- a/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py +++ b/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py @@ -383,6 +383,7 @@ def toast_dismiss(id: Var[str] | str | None = None) -> EventSpec: dismiss = f"{toast_ref}.dismiss({id!s})" dismiss_var_data = id._get_all_var_data() elif isinstance(id, str): + # Escape marker syntax before the Var decoder sees the expression. serialized_id = json.dumps(id).replace("<", r"\u003c") dismiss = f"{toast_ref}.dismiss({serialized_id})" else: diff --git a/tests/units/reflex_components_sonner/test_toast.py b/tests/units/reflex_components_sonner/test_toast.py index 7e9c9c52285..6b1c42a32f8 100644 --- a/tests/units/reflex_components_sonner/test_toast.py +++ b/tests/units/reflex_components_sonner/test_toast.py @@ -33,24 +33,29 @@ def test_toast_button_styles_use_sonner_prop_names(): [ ( "toast's-id", - '''(() => refs['__toast'].dismiss("toast's-id"))''', + """(() => refs['__toast'].dismiss("toast's-id"))""", ), ( 'toast"-id', - r'''(() => refs['__toast'].dismiss("toast\"-id"))''', + r"""(() => refs['__toast'].dismiss("toast\"-id"))""", ), ( "toast\nid", - r'''(() => refs['__toast'].dismiss("toast\nid"))''', + r"""(() => refs['__toast'].dismiss("toast\nid"))""", ), ( "x", - r'''(() => refs['__toast'].dismiss("\u003creflex.Var>x\u003c/reflex.Var>"))''', + r"""(() => refs['__toast'].dismiss("\u003creflex.Var>x\u003c/reflex.Var>"))""", ), ], ) -def test_toast_dismiss_serializes_string_id(toast_id: str, expected: str): - """Serialize string IDs as opaque JavaScript string literals.""" +def test_toast_dismiss_serializes_string_id(toast_id: str, expected: str) -> None: + """Serialize string IDs as opaque JavaScript string literals. + + Args: + toast_id: The string to pass to the dismissal handler. + expected: The complete generated JavaScript function. + """ event = Toaster.toast_dismiss(toast_id) function = next(value for key, value in event.args if str(key) == "function")