Repository navigation
fix(browser): isolate embedded profiles per session #1526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
27 changes: 26 additions & 1 deletion
27
packages/desktop-electron/src/main/browser/controller-automation.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,30 @@ | ||
| import { join } from "node:path" | ||
| import { app, session } from "electron" | ||
| import { BrowserViewController } from "./controller" | ||
| import { BrowserControllerRegistry } from "./registry" | ||
| import { BrowserProfileRegistry } from "./profile-registry" | ||
| import { BrowserProfileSessions } from "./profile-sessions" | ||
| import { getStore } from "../store" | ||
| import { PAWWORK_RUNTIME } from "../runtime-namespace" | ||
|
|
||
| const PROFILE_MAP_KEY = "profiles" | ||
|
|
||
| export const browserProfiles = new BrowserProfileRegistry({ | ||
| read: () => getStore(PAWWORK_RUNTIME.browserProfilesStore).get(PROFILE_MAP_KEY), | ||
| write: (profiles) => getStore(PAWWORK_RUNTIME.browserProfilesStore).set(PROFILE_MAP_KEY, profiles), | ||
| }) | ||
|
|
||
| export const browserProfileSessions = new BrowserProfileSessions({ | ||
| rootPath: () => join(app.getPath("sessionData"), PAWWORK_RUNTIME.browserProfilesDirectory), | ||
| fromPath: (path) => session.fromPath(path), | ||
| }) | ||
|
|
||
| /** Single main-process instance of the conversation-view registry (see registry.ts). */ | ||
| export const browserControllers = new BrowserControllerRegistry((key: string) => new BrowserViewController(key)) | ||
| export const browserControllers = new BrowserControllerRegistry((key: string) => { | ||
| const profileID = browserProfiles.profileFor(key) | ||
| return new BrowserViewController( | ||
| key, | ||
| browserProfileSessions.sessionFor(profileID), | ||
| (sessionID) => browserProfiles.adopt(sessionID, profileID), | ||
| ) | ||
| }) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,21 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { BROWSER_PARTITION, browserViewWebPreferences } from "./options" | ||
| import type { Session } from "electron" | ||
| import { browserViewWebPreferences } from "./options" | ||
|
|
||
| describe("embedded browser security", () => { | ||
| test("uses a locked-down, preload-free, sandboxed view on a persistent partition", () => { | ||
| const prefs = browserViewWebPreferences() | ||
| test("uses the supplied profile session with locked-down web preferences", () => { | ||
| const profileSession = {} as Session | ||
| const preferences = browserViewWebPreferences(profileSession) | ||
|
|
||
| expect(prefs).toMatchObject({ | ||
| expect(preferences).toMatchObject({ | ||
| session: profileSession, | ||
| sandbox: true, | ||
| contextIsolation: true, | ||
| nodeIntegration: false, | ||
| webSecurity: true, | ||
| }) | ||
| // The embedded page must never receive the app's IPC preload bridge. | ||
| expect(prefs.preload).toBeUndefined() | ||
| expect(prefs.partition).toBe(BROWSER_PARTITION) | ||
| expect(BROWSER_PARTITION.startsWith("persist:")).toBe(true) | ||
| expect(preferences.preload).toBeUndefined() | ||
| expect(preferences.partition).toBeUndefined() | ||
| }) | ||
| }) |
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
64 changes: 64 additions & 0 deletions
64
packages/desktop-electron/src/main/browser/profile-registry.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { draftKey } from "./registry" | ||
| import { BrowserProfileRegistry, type BrowserProfileStorage } from "./profile-registry" | ||
|
|
||
| function memoryStorage(initial: unknown = {}) { | ||
| let value = initial | ||
| const storage: BrowserProfileStorage = { | ||
| read: () => value, | ||
| write: (profiles) => { | ||
| value = profiles | ||
| }, | ||
| } | ||
| return { storage, value: () => value } | ||
| } | ||
|
|
||
| function ids(...values: string[]) { | ||
| let index = 0 | ||
| return () => values[index++] ?? `profile-${index}` | ||
| } | ||
|
|
||
| describe("BrowserProfileRegistry", () => { | ||
| test("gives each session a stable persistent profile", () => { | ||
| const memory = memoryStorage() | ||
| const profiles = new BrowserProfileRegistry(memory.storage, ids("profile-a", "profile-b")) | ||
|
|
||
| expect(profiles.profileFor("ses_a")).toBe("profile-a") | ||
| expect(profiles.profileFor("ses_a")).toBe("profile-a") | ||
| expect(profiles.profileFor("ses_b")).toBe("profile-b") | ||
| expect(memory.value()).toEqual({ ses_a: "profile-a", ses_b: "profile-b" }) | ||
|
|
||
| const restored = new BrowserProfileRegistry(memory.storage, ids("unused")) | ||
| expect(restored.profileFor("ses_a")).toBe("profile-a") | ||
| }) | ||
|
|
||
| test("keeps draft profiles unique without persisting reusable draft owner keys", () => { | ||
| const memory = memoryStorage() | ||
| const profiles = new BrowserProfileRegistry(memory.storage, ids("draft-a", "draft-b")) | ||
|
|
||
| const firstDraft = profiles.profileFor(draftKey(1)) | ||
| const secondDraft = profiles.profileFor(draftKey(2)) | ||
| expect(firstDraft).toBe("draft-a") | ||
| expect(secondDraft).toBe("draft-b") | ||
| expect(memory.value()).toEqual({}) | ||
| }) | ||
|
|
||
| test("binds an adopted draft profile only to its new session", () => { | ||
| const memory = memoryStorage() | ||
| const profiles = new BrowserProfileRegistry(memory.storage, ids("draft-a")) | ||
|
|
||
| const firstDraft = profiles.profileFor(draftKey(1)) | ||
| profiles.adopt("ses_new", firstDraft) | ||
| expect(profiles.profileFor("ses_new")).toBe("draft-a") | ||
| expect(memory.value()).toEqual({ ses_new: "draft-a" }) | ||
| }) | ||
|
|
||
| test("repairs duplicate stored profiles instead of restoring shared session state", () => { | ||
| const memory = memoryStorage({ ses_a: "shared", ses_b: "shared" }) | ||
| const profiles = new BrowserProfileRegistry(memory.storage, ids("profile-b")) | ||
|
|
||
| expect(profiles.profileFor("ses_a")).toBe("shared") | ||
| expect(profiles.profileFor("ses_b")).toBe("profile-b") | ||
| expect(memory.value()).toEqual({ ses_a: "shared", ses_b: "profile-b" }) | ||
| }) | ||
| }) |
62 changes: 62 additions & 0 deletions
62
packages/desktop-electron/src/main/browser/profile-registry.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { randomUUID } from "node:crypto" | ||
| import { draftWindowID } from "./registry" | ||
|
|
||
| export type BrowserProfileStorage = { | ||
| read(): unknown | ||
| write(profiles: Record<string, string>): void | ||
| } | ||
|
|
||
| export class BrowserProfileRegistry { | ||
| private profiles: Record<string, string> | undefined | ||
|
|
||
| constructor( | ||
| private readonly storage: BrowserProfileStorage, | ||
| private readonly createID: () => string = randomUUID, | ||
| ) {} | ||
|
|
||
| private readProfiles(): Record<string, string> { | ||
| if (this.profiles) return this.profiles | ||
| const stored = this.storage.read() | ||
| if (!stored || typeof stored !== "object" || Array.isArray(stored)) return (this.profiles = {}) | ||
| const used = new Set<string>() | ||
| this.profiles = Object.fromEntries( | ||
| Object.entries(stored).filter(([ownerKey, profileID]) => { | ||
| if ( | ||
| draftWindowID(ownerKey) !== null || | ||
| typeof profileID !== "string" || | ||
| profileID.length === 0 || | ||
| used.has(profileID) | ||
| ) | ||
| return false | ||
| used.add(profileID) | ||
| return true | ||
| }), | ||
| ) | ||
| return this.profiles | ||
| } | ||
|
|
||
| private persist() { | ||
| this.storage.write({ ...this.readProfiles() }) | ||
| } | ||
|
|
||
| profileFor(ownerKey: string): string { | ||
| // Home drafts are window-lifetime owners. Never persist by draft:<windowID>: | ||
| // that key is reused after adoption and would make the old conversation and | ||
| // the window's next draft share a Chromium session again. | ||
| if (draftWindowID(ownerKey) !== null) return this.createID() | ||
| const profiles = this.readProfiles() | ||
| const existing = profiles[ownerKey] | ||
| if (existing) return existing | ||
| const created = this.createID() | ||
| profiles[ownerKey] = created | ||
| this.persist() | ||
| return created | ||
| } | ||
|
|
||
| /** Persist a draft controller's immutable profile under its adopted session. */ | ||
| adopt(sessionID: string, profileID: string) { | ||
| if (draftWindowID(sessionID) !== null || !sessionID || !profileID) return | ||
| this.readProfiles()[sessionID] = profileID | ||
| this.persist() | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.