Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Fix desktop clipboard writes and prepare 0.1.1

Regan Bell5d9664fb1db7
diff --git a/desktop/main.mjs b/desktop/main.mjs
index ce594669f..da40d8c7e 100644
--- a/desktop/main.mjs
+++ b/desktop/main.mjs
@@ -154,8 +154,14 @@ async function showInstance(url) {
mainWindow = window;
const origin = new URL(url).origin;
const session = window.webContents.session;
- session.setPermissionRequestHandler((_contents, _permission, callback) => callback(false));
- session.setPermissionCheckHandler(() => false);
+ const canWriteClipboard = (permission, requestingUrl) =>
+ permission === "clipboard-sanitized-write" && requestingUrl !== "about:blank" && internalUrl(requestingUrl, origin);
+ session.setPermissionRequestHandler((_contents, permission, callback, details) =>
+ callback(canWriteClipboard(permission, details.requestingUrl)),
+ );
+ session.setPermissionCheckHandler((_contents, permission, requestingOrigin) =>
+ canWriteClipboard(permission, requestingOrigin),
+ );
let handedOff = false;
const children = new Set();
const configureNavigation = (page) => {
diff --git a/desktop/package-lock.json b/desktop/package-lock.json
index 805f3eed9..9f25785d9 100644
--- a/desktop/package-lock.json
+++ b/desktop/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "qm-desktop",
- "version": "0.1.0",
+ "version": "0.1.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "qm-desktop",
- "version": "0.1.0",
+ "version": "0.1.1",
"devDependencies": {
"@electron/packager": "20.3.0",
"electron": "44.4.3",
diff --git a/desktop/package.json b/desktop/package.json
index 11d599a4b..7ab5c8697 100644
--- a/desktop/package.json
+++ b/desktop/package.json
@@ -1,7 +1,7 @@
{
"name": "qm-desktop",
"productName": "QM",
- "version": "0.1.0",
+ "version": "0.1.1",
"private": true,
"type": "module",
"main": "main.mjs",
diff --git a/desktop/test/main.test.mjs b/desktop/test/main.test.mjs
index 6bd09175d..f4bb7e451 100644
--- a/desktop/test/main.test.mjs
+++ b/desktop/test/main.test.mjs
@@ -29,8 +29,12 @@ test(
const errors = [];
let flushes = 0;
const session = {
- setPermissionRequestHandler() {},
- setPermissionCheckHandler() {},
+ setPermissionRequestHandler(handler) {
+ this.requestPermission = handler;
+ },
+ setPermissionCheckHandler(handler) {
+ this.checkPermission = handler;
+ },
cookies: {
flushStore: async () => {
flushes++;
@@ -94,6 +98,23 @@ test(
await import("../main.mjs");
await initialLoad.promise;
assert.equal(windows[0].url, "https://old.example/");
+ for (const [permission, requestingUrl, allowed] of [
+ ["clipboard-sanitized-write", "https://old.example/", true],
+ ["clipboard-sanitized-write", "https://old.example/share/internal/test", true],
+ ["clipboard-sanitized-write", "https://evil.example/", false],
+ ["clipboard-sanitized-write", "about:blank", false],
+ ["clipboard-sanitized-write", undefined, false],
+ ["clipboard-read", "https://old.example/", false],
+ ["media", "https://old.example/", false],
+ ["notifications", "https://old.example/", false],
+ ]) {
+ assert.equal(session.checkPermission(windows[0].webContents, permission, requestingUrl), allowed);
+ let granted;
+ session.requestPermission(windows[0].webContents, permission, (value) => (granted = value), {
+ requestingUrl,
+ });
+ assert.equal(granted, allowed);
+ }
for (const destination of ["about:blank", "https://old.example/file.pdf", "blob:https://old.example/id"]) {
const popup = windows[0].popup({ url: destination });
assert.equal(popup.action, "allow");
diff --git a/desktop/test/navigation.electron.mjs b/desktop/test/navigation.electron.mjs
index 6c4f3cd47..a219a818a 100644
--- a/desktop/test/navigation.electron.mjs
+++ b/desktop/test/navigation.electron.mjs
@@ -3,7 +3,7 @@ import { mkdtemp, rm } from "node:fs/promises";
import { createServer } from "node:http";
import { tmpdir } from "node:os";
import path from "node:path";
-import { app, BrowserWindow, shell } from "electron";
+import { app, BrowserWindow, clipboard, shell } from "electron";
const tick = () => new Promise((resolve) => setTimeout(resolve, 25));
async function waitFor(read) {
for (let i = 0; i < 200; i++) {
@@ -45,6 +45,18 @@ async function waitFor(read) {
await preview.webContents.executeJavaScript('document.querySelector("main").textContent'),
"GET:session",
);
+ for (const page of [main, preview]) {
+ page.show();
+ page.focus();
+ await waitFor(() => page.webContents.executeJavaScript("document.hasFocus()"));
+ const link = `${page.webContents.getURL()}?session=copy-link-test`;
+ await page.webContents.executeJavaScript(`navigator.clipboard.writeText(${JSON.stringify(link)})`, true);
+ await waitFor(async () => (await clipboard.readText()) === link);
+ assert.equal(
+ await page.webContents.executeJavaScript("navigator.clipboard.readText().then(() => false, () => true)", true),
+ true,
+ );
+ }
assert.equal(main.webContents.getURL(), origin + "/");
assert.equal(await preview.webContents.executeJavaScript("typeof require"), "undefined");
assert.equal(preview.webContents.getLastWebPreferences().sandbox, true);
@@ -74,7 +86,7 @@ async function waitFor(read) {
main.destroy();
assert.equal(BrowserWindow.getAllWindows().length, 0);
console.log(
- "PASS: separate previews, cookie isolation, blob URLs, blank POST popups, sandbox, browser bridge and child cleanup",
+ "PASS: separate previews, cookie isolation, blob URLs, blank POST popups, sandbox, clipboard writes and read denial, browser bridge and child cleanup",
);
} finally {
for (const window of BrowserWindow.getAllWindows()) window.destroy();