Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions apps/desktop/src/preview/BrowserSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,53 @@ describe("BrowserSession", () => {
}).pipe(Effect.provide(layer)),
);

it.effect.each([{ permission: "clipboard-read" }, { permission: "geolocation" }])(
"forgets $permission answers when the profile's cookies are cleared",
({ permission }) =>
Effect.gen(function* () {
const browserSessions = yield* BrowserSession.BrowserSession;
const partition = yield* browserSessions.getPartition("scope-a");
yield* browserSessions.getSession("scope-a");
const browserSession = sessions.get(partition);
assert.isDefined(browserSession);
const requestHandler = browserSession.setPermissionRequestHandler.mock.calls[0]?.[0];
const checkHandler = browserSession.setPermissionCheckHandler.mock.calls[0]?.[0];
const request = () =>
Effect.callback<boolean>((resume) => {
requestHandler(
null,
permission,
(granted: boolean) => resume(Effect.succeed(granted)),
{
requestingUrl: "https://page.test/",
isMainFrame: true,
},
);
});
const check = (): boolean =>
checkHandler(null, permission, "https://page.test", {}) as boolean;

showMessageBox.mockReset();
showMessageBox.mockReturnValueOnce(1).mockReturnValueOnce(0).mockReturnValueOnce(1);

assert.isFalse(yield* request());
assert.isFalse(yield* request());
assert.strictEqual(showMessageBox.mock.calls.length, 1);

// Clearing lets the user reconsider a block...
yield* browserSessions.clearCookies([partition]);
assert.isTrue(yield* request());
assert.isTrue(check());
assert.strictEqual(showMessageBox.mock.calls.length, 2);

// ...and revokes an approval, including one cleared outside clearCookies.
yield* browserSessions.cookiesCleared(browserSession as unknown as Electron.Session);
assert.isFalse(check());
assert.isFalse(yield* request());
assert.strictEqual(showMessageBox.mock.calls.length, 3);
}).pipe(Effect.provide(layer)),
);

it.effect("a reset while a camera prompt is open neither grants nor remembers", () =>
Effect.gen(function* () {
const browserSessions = yield* BrowserSession.BrowserSession;
Expand Down
18 changes: 14 additions & 4 deletions apps/desktop/src/preview/BrowserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ const ensureSystemMediaAccess = async (
/** The camera and microphone answers given in one preview Session. */
interface MediaConsent {
readonly decisions: Map<string, boolean>;
/** Clipboard and location answers, forgotten with the camera ones. */
readonly originDecisions: ReadonlyArray<Map<string, boolean>>;
/** Bumped by each reset, so an answer to an older prompt cannot restore a decision. */
resets: number;
}
Expand Down Expand Up @@ -680,6 +682,7 @@ export const make = Effect.gen(function* BrowserSessionMake() {
if (consent === undefined) return;
consent.resets += 1;
consent.decisions.clear();
for (const decisions of consent.originDecisions) decisions.clear();
};

const sessionsRef = yield* SynchronizedRef.make<ReadonlyMap<string, Session>>(new Map());
Expand Down Expand Up @@ -728,6 +731,13 @@ export const make = Effect.gen(function* BrowserSessionMake() {
"clipboard-read": new Map<string, boolean>(),
geolocation: new Map<string, boolean>(),
};
// Camera and microphone ask once per origin and device, like clipboard reads.
const mediaConsent: MediaConsent = {
decisions: new Map(),
originDecisions: Object.values(permissionDecisions),
resets: 0,
};
mediaConsentBySession.set(browserSession, mediaConsent);
const confirmOriginPermission = (
requester: WebContents,
permission: keyof typeof permissionDecisions,
Expand All @@ -746,6 +756,7 @@ export const make = Effect.gen(function* BrowserSessionMake() {
callback(remembered);
return;
}
const resets = mediaConsent.resets;
promptPermission(
requester,
{
Expand All @@ -763,14 +774,13 @@ export const make = Effect.gen(function* BrowserSessionMake() {
}),
},
(answer) => {
if (answer !== undefined) decisions.set(origin, answer);
if (answer !== undefined && mediaConsent.resets === resets) {
decisions.set(origin, answer);
}
callback(answer === true);
},
);
};
// Camera and microphone ask once per origin and device, like clipboard reads.
const mediaConsent: MediaConsent = { decisions: new Map(), resets: 0 };
mediaConsentBySession.set(browserSession, mediaConsent);
const mediaDecisions = mediaConsent.decisions;
const mediaKey = (origin: string, device: MediaDevice) => `${device} ${origin}`;
const confirmMedia = (
Expand Down
Loading