Skip to content

Commit c31a73f

Browse files
authored
Merge branch 'main' into fix/mobile-lint-missing-tools
2 parents b1b4fdf + 994bd73 commit c31a73f

221 files changed

Lines changed: 7370 additions & 1628 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/ipc/methods/preview.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { it as effectIt } from "@effect/vitest";
2-
import { PreviewAutomationStatus } from "@t3tools/contracts";
2+
import {
3+
DEFAULT_BROWSER_PROFILE_ID,
4+
INCOGNITO_BROWSER_PROFILE_ID,
5+
PreviewAutomationStatus,
6+
} from "@t3tools/contracts";
37
import * as Cause from "effect/Cause";
48
import * as Effect from "effect/Effect";
59
import * as Exit from "effect/Exit";
@@ -38,6 +42,44 @@ describe("preview IPC methods", () => {
3842
expect(fromPartition).not.toHaveBeenCalled();
3943
});
4044

45+
it("derives distinct partition scopes when identifiers contain the delimiter", () => {
46+
const first = PreviewIpc.resolvePartitionScope("a", "b::c");
47+
const second = PreviewIpc.resolvePartitionScope("a::b", "c");
48+
49+
expect(first).toEqual({ scope: '["a","b::c"]', persistent: true, namespace: "profile" });
50+
expect(second).toEqual({ scope: '["a::b","c"]', persistent: true, namespace: "profile" });
51+
expect(first.scope).not.toBe(second.scope);
52+
});
53+
54+
it("preserves lone surrogates without collapsing them to replacement characters", () => {
55+
const highSurrogate = PreviewIpc.resolvePartitionScope("environment", "profile-\ud800");
56+
const lowSurrogate = PreviewIpc.resolvePartitionScope("environment", "profile-\udc00");
57+
const replacement = PreviewIpc.resolvePartitionScope("environment", "profile-�");
58+
59+
expect(highSurrogate.scope).toBe('["environment","profile-\\ud800"]');
60+
expect(lowSurrogate.scope).toBe('["environment","profile-\\udc00"]');
61+
expect(highSurrogate.scope).not.toBe(lowSurrogate.scope);
62+
expect(highSurrogate.scope).not.toBe(replacement.scope);
63+
expect(lowSurrogate.scope).not.toBe(replacement.scope);
64+
});
65+
66+
it("keeps the legacy default partition scope and incognito persistence", () => {
67+
expect(PreviewIpc.resolvePartitionScope("environment::legacy", undefined)).toEqual({
68+
scope: "environment::legacy",
69+
persistent: true,
70+
});
71+
expect(
72+
PreviewIpc.resolvePartitionScope("environment::legacy", DEFAULT_BROWSER_PROFILE_ID),
73+
).toEqual({ scope: "environment::legacy", persistent: true });
74+
expect(
75+
PreviewIpc.resolvePartitionScope("environment::legacy", INCOGNITO_BROWSER_PROFILE_ID),
76+
).toEqual({
77+
scope: '["environment::legacy","incognito"]',
78+
persistent: false,
79+
namespace: "profile",
80+
});
81+
});
82+
4183
effectIt.effect("rejects invalid webContents ids before resolving the preview service", () =>
4284
Effect.map(
4385
PreviewIpc.registerWebview

‎apps/desktop/src/ipc/methods/preview.ts‎

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ import {
1616
DesktopPreviewScreenshotArtifactSchema,
1717
DesktopPreviewSetAudioMutedInputSchema,
1818
DesktopPreviewSetColorSchemeInputSchema,
19+
DesktopPreviewClearDataInputSchema,
1920
DesktopPreviewCreateTabInputSchema,
2021
DesktopPreviewTabInputSchema,
2122
DesktopPreviewWebviewConfigSchema,
2223
PreviewAnnotationSubmissionResultSchema,
2324
PreviewAutomationSnapshot,
25+
DEFAULT_BROWSER_PROFILE_ID,
26+
INCOGNITO_BROWSER_PROFILE_ID,
2427
} from "@t3tools/contracts";
2528
import * as Effect from "effect/Effect";
2629
import * as Schema from "effect/Schema";
@@ -196,33 +199,85 @@ export const closePictureInPicture = tabMethod(
196199

197200
export const clearCookies = DesktopIpc.makeIpcMethod({
198201
channel: IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL,
199-
payload: Schema.Void,
202+
payload: DesktopPreviewClearDataInputSchema,
200203
result: Schema.Void,
201-
handler: Effect.fn("desktop.ipc.preview.clearCookies")(function* () {
204+
handler: Effect.fn("desktop.ipc.preview.clearCookies")(function* ({ environmentId, profileId }) {
202205
const manager = yield* PreviewManager.PreviewManager;
203-
yield* manager.clearCookies();
206+
yield* manager.clearCookies(yield* resolveClearPartitions(manager, environmentId, profileId));
204207
}),
205208
});
206209

207210
export const clearCache = DesktopIpc.makeIpcMethod({
208211
channel: IpcChannels.PREVIEW_CLEAR_CACHE_CHANNEL,
209-
payload: Schema.Void,
212+
payload: DesktopPreviewClearDataInputSchema,
210213
result: Schema.Void,
211-
handler: Effect.fn("desktop.ipc.preview.clearCache")(function* () {
214+
handler: Effect.fn("desktop.ipc.preview.clearCache")(function* ({ environmentId, profileId }) {
212215
const manager = yield* PreviewManager.PreviewManager;
213-
yield* manager.clearCache();
216+
yield* manager.clearCache(yield* resolveClearPartitions(manager, environmentId, profileId));
214217
}),
215218
});
216219

220+
/**
221+
* Partition scope for an (environment, profile) pair.
222+
*
223+
* The default profile keeps the bare environment id it used before profiles
224+
* existed, so upgrading does not strand anyone's existing logins in an
225+
* orphaned partition. Incognito derives a non-persistent partition.
226+
*/
227+
export function resolvePartitionScope(
228+
environmentId: string,
229+
profileId: string | undefined,
230+
): {
231+
readonly scope: string;
232+
readonly persistent: boolean;
233+
readonly namespace?: "profile";
234+
} {
235+
if (profileId === undefined || profileId === DEFAULT_BROWSER_PROFILE_ID) {
236+
return { scope: environmentId, persistent: true };
237+
}
238+
// JSON's tuple framing is injective for strings, including lone UTF-16
239+
// surrogates (which it escapes). URI encoding throws on those supported ids,
240+
// while replacing them with U+FFFD would collapse distinct identities.
241+
return {
242+
scope: JSON.stringify([environmentId, profileId]),
243+
persistent: profileId !== INCOGNITO_BROWSER_PROFILE_ID,
244+
namespace: "profile" as const,
245+
};
246+
}
247+
248+
/**
249+
* Clearing without a profile keeps the historical "everything" behaviour for
250+
* an explicit all-profiles action; naming a profile confines it to that
251+
* profile's partition so one profile's sign-out cannot reach the others.
252+
*/
253+
const resolveClearPartitions = Effect.fn("desktop.ipc.preview.resolveClearPartitions")(function* (
254+
manager: PreviewManager.PreviewManager["Service"],
255+
environmentId: string,
256+
profileId: string | undefined,
257+
) {
258+
if (profileId === undefined) return undefined;
259+
const { scope, persistent, namespace } = resolvePartitionScope(environmentId, profileId);
260+
// Loading the session is what puts the partition in the map the clear walks.
261+
// Deriving the partition string alone leaves nothing to match, so clearing a
262+
// profile with no tab open this run — after a restart, or when deleting a
263+
// profile — would report success and delete nothing.
264+
yield* manager.getBrowserSession(scope, persistent, namespace);
265+
return [yield* manager.getBrowserPartition(scope, persistent, namespace)];
266+
});
267+
217268
export const getPreviewConfig = DesktopIpc.makeIpcMethod({
218269
channel: IpcChannels.PREVIEW_GET_CONFIG_CHANNEL,
219270
payload: DesktopPreviewConfigInputSchema,
220271
result: DesktopPreviewWebviewConfigSchema,
221-
handler: Effect.fn("desktop.ipc.preview.getConfig")(function* ({ environmentId }) {
272+
handler: Effect.fn("desktop.ipc.preview.getConfig")(function* ({ environmentId, profileId }) {
222273
const manager = yield* PreviewManager.PreviewManager;
223-
yield* manager.getBrowserSession(environmentId);
274+
const { scope, persistent, namespace } = resolvePartitionScope(environmentId, profileId);
275+
// Creating the session first is what installs the UA rewrite and permission
276+
// handlers; a guest that attached to an untouched partition would run with
277+
// Electron's default UA and Chromium's default permission behaviour.
278+
yield* manager.getBrowserSession(scope, persistent, namespace);
224279
return {
225-
partition: yield* manager.getBrowserPartition(environmentId),
280+
partition: yield* manager.getBrowserPartition(scope, persistent, namespace),
226281
webPreferences: PREVIEW_WEBVIEW_PREFERENCES,
227282
preloadUrl: NodeURL.pathToFileURL(`${__dirname}/preview-pick-preload.cjs`).href,
228283
};

‎apps/desktop/src/preload.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,12 @@ contextBridge.exposeInMainWorld("desktopBridge", {
222222
ipcRenderer.invoke(IpcChannels.PREVIEW_SET_AUDIO_MUTED_CHANNEL, { tabId, audioMuted }),
223223
openDevTools: (tabId) =>
224224
ipcRenderer.invoke(IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL, { tabId }),
225-
clearCookies: () => ipcRenderer.invoke(IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL),
226-
clearCache: () => ipcRenderer.invoke(IpcChannels.PREVIEW_CLEAR_CACHE_CHANNEL),
227-
getPreviewConfig: (environmentId) =>
228-
ipcRenderer.invoke(IpcChannels.PREVIEW_GET_CONFIG_CHANNEL, { environmentId }),
225+
clearCookies: (environmentId, profileId) =>
226+
ipcRenderer.invoke(IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL, { environmentId, profileId }),
227+
clearCache: (environmentId, profileId) =>
228+
ipcRenderer.invoke(IpcChannels.PREVIEW_CLEAR_CACHE_CHANNEL, { environmentId, profileId }),
229+
getPreviewConfig: (environmentId, profileId) =>
230+
ipcRenderer.invoke(IpcChannels.PREVIEW_GET_CONFIG_CHANNEL, { environmentId, profileId }),
229231
setAnnotationTheme: (theme) =>
230232
ipcRenderer.invoke(IpcChannels.PREVIEW_SET_ANNOTATION_THEME_CHANNEL, { theme }),
231233
pickElement: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_PICK_ELEMENT_CHANNEL, { tabId }),

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,45 @@ 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+
88+
it.effect("keeps legacy defaults disjoint from nondefault profile partitions", () =>
89+
Effect.gen(function* () {
90+
const browserSessions = yield* BrowserSession.BrowserSession;
91+
92+
// These share the same scope string: default environment `a::b`, and
93+
// environment `a` with nondefault profile `b`.
94+
const legacyDefault = yield* browserSessions.getPartition("a::b");
95+
const nondefaultProfile = yield* browserSessions.getPartition("a::b", true, "profile");
96+
97+
assert.strictEqual(legacyDefault, "persist:t3code-preview-78f0be89237d77f7a70e");
98+
assert.strictEqual(nondefaultProfile, "persist:t3code-preview-profile-78f0be89237d77f7a70e");
99+
assert.notStrictEqual(nondefaultProfile, legacyDefault);
100+
assert.isTrue(browserSessions.isPartition(legacyDefault));
101+
assert.isTrue(browserSessions.isPartition(nondefaultProfile));
102+
}).pipe(Effect.provide(layer)),
103+
);
104+
66105
it.effect("grants clipboard-sanitized-write through both the request and check handlers", () =>
67106
Effect.gen(function* () {
68107
const browserSessions = yield* BrowserSession.BrowserSession;
@@ -192,6 +231,28 @@ describe("BrowserSession", () => {
192231
}).pipe(Effect.provide(layer)),
193232
);
194233

234+
it.effect("clears a partition whose session has not been opened yet", () =>
235+
Effect.gen(function* () {
236+
const browserSessions = yield* BrowserSession.BrowserSession;
237+
const partition = yield* browserSessions.getPartition("scope-untouched");
238+
239+
// Deriving the partition string does not create the session, and the
240+
// clear only walks sessions it already holds. Without loading it first
241+
// this reports success and deletes nothing — which is what a user
242+
// clearing a profile after a restart would get.
243+
assert.isUndefined(sessions.get(partition));
244+
yield* browserSessions.clearCookies([partition]);
245+
assert.isUndefined(sessions.get(partition));
246+
247+
yield* browserSessions.getSession("scope-untouched");
248+
yield* browserSessions.clearCookies([partition]);
249+
250+
const created = sessions.get(partition);
251+
assert.isDefined(created);
252+
assert.strictEqual(created.clearStorageData.mock.calls.length, 1);
253+
}).pipe(Effect.provide(layer)),
254+
);
255+
195256
it.effect("correlates clear failures while still attempting every session", () =>
196257
Effect.gen(function* () {
197258
const browserSessions = yield* BrowserSession.BrowserSession;

0 commit comments

Comments
 (0)