Skip to content

Commit 1a5aa85

Browse files
fix(desktop): keep lone-surrogate scopes in distinct browser partitions
The partition digest hashed `TextEncoder().encode(scope)`, which replaces a lone UTF-16 surrogate with U+FFFD — so two supported ids differing only by such a unit (`"p\ud800"` vs `"p�"`) landed in the same partition and shared every cookie and site storage. Lone surrogates are now escaped to `\uXXXX` before hashing, with a literal backslash doubled so the escape cannot be forged. Every well-formed scope passes through byte-for-byte unchanged, so existing partitions — and the logins in them — stay put. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent fe5a26f commit 1a5aa85

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎apps/desktop/src/preview/BrowserSession.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ describe("BrowserSession", () => {
6363
}).pipe(Effect.provide(layer)),
6464
);
6565

66+
it.effect("keeps scopes that differ only by a lone surrogate in separate partitions", () =>
67+
Effect.gen(function* () {
68+
const browserSessions = yield* BrowserSession.BrowserSession;
69+
70+
// TextEncoder folds a lone surrogate to U+FFFD, so without escaping these
71+
// two supported ids would hash to one partition and share every cookie.
72+
const loneSurrogate = yield* browserSessions.getPartition("p\ud800");
73+
const replacementChar = yield* browserSessions.getPartition("p\ufffd");
74+
assert.notStrictEqual(loneSurrogate, replacementChar);
75+
76+
// The escape can't be forged with a literal backslash either.
77+
const literal = yield* browserSessions.getPartition("p\\ud800");
78+
assert.notStrictEqual(literal, loneSurrogate);
79+
80+
// And a well-formed scope still lands on its historical partition.
81+
assert.strictEqual(
82+
yield* browserSessions.getPartition("scope-a"),
83+
"persist:t3code-preview-f051bb2c68cb7b2fe969",
84+
);
85+
}).pipe(Effect.provide(layer)),
86+
);
87+
6688
it.effect("keeps legacy defaults disjoint from nondefault profile partitions", () =>
6789
Effect.gen(function* () {
6890
const browserSessions = yield* BrowserSession.BrowserSession;

‎apps/desktop/src/preview/BrowserSession.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ const selectSessions = (
141141
([partition]) => partitions === undefined || partitions.includes(partition),
142142
);
143143

144+
/**
145+
* Scope bytes for the partition digest.
146+
*
147+
* `TextEncoder` replaces a lone UTF-16 surrogate with U+FFFD, so `"p\ud800"`
148+
* and `"p\ufffd"` would hash to the same partition and share cookies. Those
149+
* are distinct, supported ids, so lone surrogates are escaped to `\uXXXX`
150+
* first — and a literal backslash is doubled so the escape cannot be forged.
151+
* Every well-formed scope passes through byte-for-byte unchanged, which keeps
152+
* existing partitions (and the logins in them) where they are.
153+
*/
154+
const encodeScopeForDigest = (scope: string): Uint8Array =>
155+
new TextEncoder().encode(
156+
scope
157+
.replace(/\\/g, "\\\\")
158+
.replace(
159+
/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g,
160+
(unit) => `\\u${unit.charCodeAt(0).toString(16).padStart(4, "0")}`,
161+
),
162+
);
163+
144164
export const make = Effect.gen(function* BrowserSessionMake() {
145165
const crypto = yield* Crypto.Crypto;
146166
const sessionsRef = yield* SynchronizedRef.make<ReadonlyMap<string, Session>>(new Map());
@@ -150,7 +170,7 @@ export const make = Effect.gen(function* BrowserSessionMake() {
150170
persistent = true,
151171
namespace?: BrowserSessionPartitionNamespace,
152172
) {
153-
const digest = yield* crypto.digest("SHA-256", new TextEncoder().encode(scope)).pipe(
173+
const digest = yield* crypto.digest("SHA-256", encodeScopeForDigest(scope)).pipe(
154174
Effect.mapError(
155175
(cause) =>
156176
new BrowserSessionPartitionDerivationError({

0 commit comments

Comments
 (0)