Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Open in browser: per-user folder, Windows device names, UTF-8 mark

AdityaUmale76da3a323964
diff --git a/electron/main.mjs b/electron/main.mjs
index c0f127ec14..4b0c2b481c 100644
--- a/electron/main.mjs
+++ b/electron/main.mjs
@@ -2330,7 +2330,7 @@ ipcMain.handle("desktop:reveal-file", localOnly("desktop:reveal-file", async (_e
if (desktopRemoteAccess) throw new Error("That file is on the computer this app is connected to");
return revealInFolder(rawPath, { reveal: (target) => shell.showItemInFolder(target) });
}));
-ipcMain.handle("desktop:open-html", localOnly("desktop:open-html", (_event, source, name) => openHtmlCopy(source, name, { dir: path.join(app.getPath("temp"), "openmausbot-open"), open: (file) => shell.openPath(file) })));
+ipcMain.handle("desktop:open-html", localOnly("desktop:open-html", (_event, source, name) => openHtmlCopy(source, name, { dir: path.join(app.getPath("userData"), "open-html"), open: (file) => shell.openPath(file) })));
ipcMain.handle("desktop:open-logs", localOrHome("desktop:open-logs", () => openLogsFolder(LOG_DIR, { open: (folder) => shell.openPath(folder) })));
ipcMain.handle("desktop:skin", (event, skin) => {
if (!isKnownSkin(skin)) return false;
diff --git a/electron/save-file.mjs b/electron/save-file.mjs
index 4494204596..aa8bc30d20 100644
--- a/electron/save-file.mjs
+++ b/electron/save-file.mjs
@@ -142,12 +142,13 @@ export async function openLogsFolder(folder, { open, fsp = fs.promises }) {
export const OPEN_HTML_MAX_BYTES = 2 * 1024 * 1024;
-/** A file name safe to write in our own folder: no folders, control or reserved characters, and a .html/.htm end. */
+/** A file name safe to write in our own folder: no folders, control or reserved characters, no Windows device name
+ * (CON, NUL, COM1…, which would open the device, not a file), and a .html/.htm end. */
export function htmlCopyName(name) {
const kept = [...String(name ?? "").split(/[\\/]/).pop()].filter((ch) => ch > "\u001f" && ch !== "\u007f" && !'<>:"|?*'.includes(ch));
const base = kept.join("").replace(/^[.\s]+|[.\s]+$/g, "");
const stem = (base.replace(/\.html?$/i, "") || "page").slice(0, 80);
- return `${stem}${/\.htm$/i.test(base) ? ".htm" : ".html"}`;
+ return `${/^(con|prn|aux|nul|com\d|lpt\d)(\.|$)/i.test(stem) ? "_" : ""}${stem}${/\.htm$/i.test(base) ? ".htm" : ".html"}`;
}
/** Opens HTML the page already showed in the default browser: a fresh copy in our own temp folder, never a path a page
@@ -163,7 +164,8 @@ export async function openHtmlCopy(source, name, { dir, open, fsp = fs.promises,
if (stat && now - stat.mtimeMs > 24 * 60 * 60 * 1000) await fsp.rm(old, { recursive: true, force: true }).catch(() => undefined);
}
const file = path.join(await fsp.mkdtemp(path.join(dir, "p-")), htmlCopyName(name));
- await fsp.writeFile(file, source, { encoding: "utf8", flag: "wx", mode: 0o600 });
+ // The byte order mark tells every browser (Safari reads a bare file: page as Latin-1) that the copy is UTF-8.
+ await fsp.writeFile(file, `\ufeff${source}`, { encoding: "utf8", flag: "wx", mode: 0o600 });
const error = await open(file);
if (error) throw new Error(String(error).slice(0, 300));
return true;
diff --git a/electron/save-file.node-test.mjs b/electron/save-file.node-test.mjs
index 01c9659d26..d84d041810 100644
--- a/electron/save-file.node-test.mjs
+++ b/electron/save-file.node-test.mjs
@@ -300,7 +300,7 @@ describe("openHtmlCopy (#4406)", () => {
assert.equal(opened.length, 1);
assert.equal(path.dirname(path.dirname(opened[0])), dir);
assert.equal(path.basename(opened[0]), "report.html");
- assert.equal(fs.readFileSync(opened[0], "utf8"), "<h1>hi</h1>");
+ assert.equal(fs.readFileSync(opened[0], "utf8"), "\ufeff<h1>hi</h1>");
if (process.platform !== "win32") assert.equal(fs.statSync(opened[0]).mode & 0o777, 0o600);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
@@ -337,10 +337,11 @@ describe("openHtmlCopy (#4406)", () => {
assert.equal(htmlCopyName("a<b>:c?.txt"), "abc.txt.html");
assert.equal(htmlCopyName(".."), "page.html");
assert.equal(htmlCopyName(undefined), "page.html");
+ assert.deepEqual(["CON.html", "nul", "com1.report.htm", "Lpt9.HTML", "console.html"].map(htmlCopyName), ["_CON.html", "_nul.html", "_com1.report.htm", "_Lpt9.html", "console.html"]);
});
it("is wired in main for this computer's page only, with main's own temp folder", () => {
const main = fs.readFileSync(new URL("./main.mjs", import.meta.url), "utf8");
- assert.match(main, /ipcMain\.handle\("desktop:open-html", localOnly\("desktop:open-html", \(_event, source, name\) =>\s*openHtmlCopy\(source, name, \{ dir: path\.join\(app\.getPath\("temp"\), "openmausbot-open"\)/);
+ assert.match(main, /ipcMain\.handle\("desktop:open-html", localOnly\("desktop:open-html", \(_event, source, name\) =>\s*openHtmlCopy\(source, name, \{ dir: path\.join\(app\.getPath\("userData"\), "open-html"\)/);
});
});
diff --git a/src/components/OpenInBrowserButton.test.ts b/src/components/OpenInBrowserButton.test.ts
new file mode 100644
index 0000000000..79bfaef29b
--- /dev/null
+++ b/src/components/OpenInBrowserButton.test.ts
@@ -0,0 +1,31 @@
+// @vitest-environment happy-dom
+import { createElement } from "react";
+import { flushSync } from "react-dom";
+import { createRoot } from "react-dom/client";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { OpenInBrowserButton } from "./HtmlFilePreview";
+
+describe("OpenInBrowserButton click (#4406)", () => {
+ afterEach(() => { document.body.innerHTML = ""; vi.unstubAllGlobals(); });
+ const click = (openHtmlInBrowser: (source: string, name: string) => Promise<unknown>, onError: (reason: string) => void) => {
+ vi.stubGlobal("ogb", { openHtmlInBrowser });
+ const host = document.body.appendChild(document.createElement("div"));
+ const root = createRoot(host);
+ flushSync(() => root.render(createElement(OpenInBrowserButton, { source: "<p>hi</p>", name: "report.html", onError })));
+ flushSync(() => host.querySelector("button")?.click());
+ return root;
+ };
+
+ it("hands the page and its name to the desktop app", () => {
+ const open = vi.fn(async () => true), onError = vi.fn();
+ click(open, onError).unmount();
+ expect(open).toHaveBeenCalledExactlyOnceWith("<p>hi</p>", "report.html");
+ expect(onError).toHaveBeenCalledExactlyOnceWith("");
+ });
+
+ it("shows why the desktop app could not open it", async () => {
+ const onError = vi.fn();
+ click(async () => { throw new Error("No application is set to open .html files"); }, onError).unmount();
+ await expect.poll(() => onError.mock.calls.at(-1)?.[0]).toBe("No application is set to open .html files");
+ });
+});