Fix cookie JSON parsing by disabling automatic parsing (ENG-6818)#5616
Conversation
- Add true parameter to cookies.get() calls in state.js to prevent universal-cookies from automatically parsing JSON strings - Add comprehensive integration tests for JSON cookie values including: - Dictionary objects serialized as JSON - Arrays serialized as JSON - Complex nested objects - JSON with special characters and escaping - Empty JSON objects and arrays Fixes ENG-6818: Cookie handling bug where JSON-formatted values were being parsed into dictionaries instead of preserved as strings Co-Authored-By: masen@reflex.dev <masen@reflex.dev>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
There was a problem hiding this comment.
Greptile Summary
This PR fixes a critical cookie handling bug in Reflex's client-side state management where JSON-formatted cookie values were being automatically parsed into JavaScript objects by the universal-cookies library instead of preserved as strings, causing type validation errors in the backend.
The core fix modifies the hydrateClientStorage function in reflex/.templates/web/utils/state.js by adding a true parameter to both cookies.get() calls on lines 794 and 796. This second parameter disables automatic JSON parsing in the universal-cookies library, ensuring that JSON strings stored in cookies remain as strings when sent to the Reflex backend. The change integrates seamlessly with the existing client storage hydration process, which reads cookie values and populates the client-side state during app initialization.
To validate the fix, a comprehensive test suite was added to tests/integration/test_client_storage.py with the test_json_cookie_values function. The test covers various JSON scenarios including dictionaries, arrays, nested objects with mixed types, escaped characters, and empty JSON structures to ensure the fix works across all edge cases.
Confidence score: 3/5
- This fix addresses a specific documented bug but requires verification of the universal-cookies API behavior
- The author was unable to test locally due to environment issues, creating uncertainty about actual test execution
- The
reflex/.templates/web/utils/state.jsfile needs careful review as it's part of the core client-side state management system
2 files reviewed, 1 comment
CodSpeed Performance ReportMerging #5616 will not alter performanceComparing Summary
|
- Wrap long JSON string with escapes to meet line length requirements - Use consistent double quotes for empty JSON strings - Apply formatting changes as required by pre-commit hooks Co-Authored-By: masen@reflex.dev <masen@reflex.dev>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
- Fix pre-commit ruff check failure F841 (unused variable) - The test doesn't require authentication to verify JSON string preservation Co-Authored-By: masen@reflex.dev <masen@reflex.dev>
- Add back poll_for_token() call before accessing DOM elements - Follows the same pattern as the existing working test - Ensures app is fully loaded before test execution Co-Authored-By: masen@reflex.dev <masen@reflex.dev>
- Refresh browser after setting each JSON cookie value - Re-acquire DOM elements after refresh to test cookie hydration - Ensures test properly exercises the universal-cookies JSON parsing bug - Addresses feedback from masenf to test the actual bug scenario Co-Authored-By: masen@reflex.dev <masen@reflex.dev>
- Remove trailing whitespace from empty lines - Address ruff formatting requirements for browser refresh test enhancement Co-Authored-By: masen@reflex.dev <masen@reflex.dev>
- Create helper function test_json_cookie_with_refresh() as requested by masenf - Encapsulates full test cycle: poll token, get element, set value, assert, refresh, poll token, assert - Reduces test from ~60 lines of duplicated logic to ~20 lines using helper function - Maintains identical functionality and test coverage - Addresses GitHub comment feedback for cleaner code organization Co-Authored-By: masen@reflex.dev <masen@reflex.dev>
Summary
Fixes ENG-6818: Cookie handling bug where JSON-formatted cookie values were being automatically parsed into dictionaries by the universal-cookies library instead of preserved as strings, causing type validation errors in the Reflex backend.
Changes Made
Core Fix
reflex/.templates/web/utils/state.js: Addedtrueparameter to bothcookies.get()calls (lines 794, 796) to disable automatic JSON parsing in the universal-cookies libraryTest Coverage
test_json_cookie_valuestotests/integration/test_client_storage.py: Comprehensive test cases covering:Type of Change
Critical Review Items⚠️
Environment Issue: Due to uv version mismatch in development environment (requires >=0.7.0, have 0.6.17), I was unable to run tests or pre-commit hooks locally. Human reviewers must verify:
cookies.get(name, true)actually disables JSON parsing as expectedtest_json_cookie_valuestest to ensure it passesExample of Fixed Issue
Before (broken):
After (fixed):
Link to Devin run: https://app.devin.ai/sessions/de4e8b1de1564c53a30ea5f6b7c22d06
Requested by: masen@reflex.dev
Checklist