fix(storage): write strings raw so current_tenant does not double-encode - #237
Open
sonegillis1 wants to merge 1 commit into
Open
fix(storage): write strings raw so current_tenant does not double-encode#237sonegillis1 wants to merge 1 commit into
sonegillis1 wants to merge 1 commit into
Conversation
getItem reads raw but setItem JSON-stringified, so every string written through the SDK came back double-encoded. The SDK's cookie sync then parsed current_tenant to a string rather than an object, read key as undefined, treated every poll as a cross-SPA change, wrote it back and refreshed the page — 120 times in under two minutes when a mentor embed was on a different tenant. Strings are now written as-is; non-strings are still serialised so object callers are unaffected. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
LocalStorageServicewas asymmetric —getItemread raw,setItemJSON-stringified:So every string written through the SDK came back double-encoded.
That fed an infinite refresh loop in the SDK's
syncCookiesToLocalStorage. From a console capture of a mentor embed on a different tenant — 120 iterations in 112 seconds:Identical values, reported as different, forever:
setItemstringifies that string → stored double-encodedJSON.parseyields the JSON string, not an object, so.keyisundefined"gillis" !== undefined→ "changed" → write → refresh → back to 2, foreverIt also fired 118
interval redirecting to auth spaattempts, stopped only by the login-timestamp guard.The fix
Strings are written as-is, so they round-trip through
getItem. Non-strings are still serialised, so object callers are unaffected.This matches how the other SPAs implement the same interface (mentorai, apps/auth both write raw).
Verified
tsc --noEmitcleansetItem→getItem→JSON.parsegives the object back); the existing non-string case still asserts stringificationRelated
iblai/iblai-web-frontend#2059 hardens the SDK's comparison so no host adapter can spin this loop again, and fixes a second issue in the same capture where a tenant switch offered to the host was never taken.
🤖 Generated with Claude Code