fix: isolate dynamic JavaScript execution environments
Merged
SO
2d ago+220+220+012345678901234567890123456789-78-78-01234567890123456789
All Submissions:
- Have you followed the guidelines stated in the CONTRIBUTING.md file?
- Have you checked to ensure there aren't any other open Pull Requests for the desired change?
Type of change
Please delete options that are not relevant.
- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing functionality not to work as expected)
- This change requires a documentation update
New Feature Submission:
- Does your submission pass the tests?
- Have you linted your code locally before submission?
Changes To Core Features:
- Have you added an explanation of what your changes do and why you'd like us to include them?
- Have you written new tests for your core changes, as applicable?
- Have you successfully run tests with your changes locally?
closes #7860
Server-provided JavaScript (rx.call_script code, rx.call_function functions and their result callbacks) no longer runs through a direct eval in utils/state.js. A direct eval stops the minifier from mangling the chunk that holds it; with frontend_lazy_bundled_libraries on, this shrinks the shared chunk by about 88 KB (11 KB gzipped).
- One
runScripthelper compiles a Function-constructor evaluator on first use, so a CSP withoutunsafe-evalstill loads apps that never send scripts. Its body passes the code toeval, which sees only the Function's parameters and the globals, and runs it as a strict script, so statement lists keep their completion value. - Scripts see every export of
utils/state.js(ReflexEvent,refs,queueEvents,pyMod, ...), the late-boundaddEvents, thesocket,navigateandparamsof the event, the sessiontoken, and the globals. Names the module does not export (its imports, such aseventLoop) are no longer in scope. evalReactComponentuses a nativeimport()of the data URI instead ofeval("import(...)").- Tests:
packages/reflex-frontend/tests/run_script.test.jspins completion values, strict mode, scope, late-boundaddEventsacross event-loop remounts, and lazy compilation.test_state_js_template.pychecks that everyrunScriptcall site declares theaddEventsalias first.