fix: escape toast dismissal IDs

Merged
HA
5d ago
+76-14

Summary

Closes #8017.

rx.toast.dismiss built its call by putting a string id inside '...' with no escaping. A quote in the id broke the generated JavaScript. An untrusted id returned from a backend handler could run script in the browser. An id built from an f-string with a state Var compiled to a literal string, so it never matched the toast that rx.toast(id=...) created.

toast_dismiss now builds the call as send_toast does: refs['__toast']?.["dismiss"](id) through FunctionVar.call. So dismiss serializes an id exactly as rx.toast(id=...) does:

  • a string id becomes an escaped JavaScript string literal;
  • an f-string with a state Var becomes ("toast-"+…) and keeps its state hook;
  • a Var id and None use the same call.

Tests

  • tests/units/reflex_components_sonner/test_toast.py checks the complete generated function and its var data for None, a Var, an f-string with a Var, and ids with an apostrophe, ", newline, backtick, ${, U+2028 and </script>. It also checks that dismiss and rx.toast(id=...) emit the same id expression.
  • Live in an example app (dev server, and the prod export compiles): each dismiss removes only its own toast and leaves an unrelated toast on screen. This covers string, apostrophe, f-string and Var ids from on_click, and a quote-injection id from a backend handler. The injected script did not run, and the console shows no errors.

Known limit, not changed here: a literal id that contains <reflex.Var>…</reflex.Var> with a non-numeric body hangs LiteralStringVar.create. rx.toast(id=...) and rx.toast(message) already hang on that input, so this is a separate framework bug.