Skip to content

Build your personal knowledge base with Trilium Notes

fix(website): detect Apple Silicon Macs when downloading

Elian Doran916318b3eb8a
diff --git a/apps/website/src/download-helper.spec.ts b/apps/website/src/download-helper.spec.ts
new file mode 100644
index 0000000000..e792deac38
--- /dev/null
+++ b/apps/website/src/download-helper.spec.ts
@@ -0,0 +1,96 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { getArchitecture, getPlatform } from "./download-helper";
+
+const MAC_UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15";
+const WINDOWS_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36";
+
+function mockBrowser({ userAgent, architecture, renderer }: { userAgent: string, architecture?: string, renderer?: string }) {
+ vi.stubGlobal("navigator", {
+ userAgent,
+ userAgentData: architecture !== undefined
+ ? { getHighEntropyValues: () => Promise.resolve({ architecture }) }
+ : undefined
+ });
+
+ vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockImplementation(() => (renderer !== undefined ? {
+ RENDERER: 0x1f01,
+ getExtension: () => null,
+ getParameter: () => renderer
+ } : null) as never);
+}
+
+describe("getPlatform", () => {
+ afterEach(() => vi.unstubAllGlobals());
+
+ it("detects the platform from the user agent", () => {
+ mockBrowser({ userAgent: MAC_UA });
+ expect(getPlatform()).toBe("macos");
+
+ mockBrowser({ userAgent: WINDOWS_UA });
+ expect(getPlatform()).toBe("windows");
+
+ mockBrowser({ userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0" });
+ expect(getPlatform()).toBe("linux");
+ });
+});
+
+describe("getArchitecture", () => {
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+ });
+
+ it("detects Apple Silicon on browsers without client hints", async () => {
+ // Safari and Firefox report an Intel user agent on every Mac.
+ mockBrowser({ userAgent: MAC_UA, renderer: "Apple GPU" });
+ expect(await getArchitecture()).toBe("arm64");
+
+ mockBrowser({ userAgent: MAC_UA, renderer: "ANGLE (Apple, ANGLE Metal Renderer: Apple M2 Pro, Unspecified Version)" });
+ expect(await getArchitecture()).toBe("arm64");
+ });
+
+ it("detects Apple Silicon even when the browser runs under Rosetta", async () => {
+ mockBrowser({ userAgent: MAC_UA, architecture: "x86", renderer: "Apple M1" });
+ expect(await getArchitecture()).toBe("arm64");
+ });
+
+ it("detects Intel Macs", async () => {
+ mockBrowser({ userAgent: MAC_UA, renderer: "Intel(R) Iris(TM) Plus Graphics 640" });
+ expect(await getArchitecture()).toBe("x64");
+
+ mockBrowser({ userAgent: MAC_UA, architecture: "x86", renderer: "AMD Radeon Pro 5500M OpenGL Engine" });
+ expect(await getArchitecture()).toBe("x64");
+ });
+
+ it("falls back to client hints and the user agent", async () => {
+ mockBrowser({ userAgent: WINDOWS_UA, architecture: "arm" });
+ expect(await getArchitecture()).toBe("arm64");
+
+ mockBrowser({ userAgent: WINDOWS_UA, architecture: "x86" });
+ expect(await getArchitecture()).toBe("x64");
+
+ mockBrowser({ userAgent: "Mozilla/5.0 (X11; Linux aarch64; rv:126.0) Gecko/20100101 Firefox/126.0" });
+ expect(await getArchitecture()).toBe("arm64");
+
+ mockBrowser({ userAgent: WINDOWS_UA });
+ expect(await getArchitecture()).toBe("x64");
+ });
+
+ it("assumes Apple Silicon for a Mac it cannot identify", async () => {
+ // WebGL unavailable, no client hints.
+ mockBrowser({ userAgent: MAC_UA });
+ expect(await getArchitecture()).toBe("arm64");
+
+ // A virtualised GPU names neither an Apple nor an Intel chip.
+ mockBrowser({ userAgent: MAC_UA, renderer: "Apple Paravirtual device" });
+ expect(await getArchitecture()).toBe("arm64");
+
+ // Client hints rejected by permissions policy.
+ vi.stubGlobal("navigator", {
+ userAgent: MAC_UA,
+ userAgentData: { getHighEntropyValues: () => Promise.reject(new Error("NotAllowedError")) }
+ });
+ vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue(null);
+ expect(await getArchitecture()).toBe("arm64");
+ });
+});
diff --git a/apps/website/src/download-helper.ts b/apps/website/src/download-helper.ts
index a8a11c7c3f..227f09abf1 100644
--- a/apps/website/src/download-helper.ts
+++ b/apps/website/src/download-helper.ts
@@ -195,17 +195,72 @@ export function buildDownloadUrl(t: TFunction<"translation", undefined>, app: Ap
export async function getArchitecture(): Promise<Architecture | null> {
if (typeof window === "undefined") return null;
- if (navigator.userAgentData) {
- const { architecture } = await navigator.userAgentData.getHighEntropyValues(["architecture"]);
- return architecture?.startsWith("arm") ? "arm64" : "x64";
+ if (getPlatform() === "macos") {
+ return getMacArchitecture();
}
- const userAgent = navigator.userAgent.toLowerCase();
- if (userAgent.includes('arm64') || userAgent.includes('aarch64')) {
- return 'arm64';
+ return await getArchitectureFromClientHints()
+ ?? getArchitectureFromUserAgent()
+ ?? "x64";
+}
+
+/**
+ * macOS never reveals the architecture through the user agent: Safari and Firefox hardcode
+ * "Intel Mac OS X" on Apple Silicon as well, and even Chromium reports "x86" when the browser itself
+ * is being translated by Rosetta. The GPU name describes the actual machine, which is what the
+ * download has to match, so it takes precedence over both.
+ */
+async function getMacArchitecture(): Promise<Architecture> {
+ return getArchitectureFromGpu()
+ ?? await getArchitectureFromClientHints()
+ // Apple stopped selling Intel Macs in 2023, so a Mac we cannot identify (WebGL blocked for
+ // fingerprinting reasons, a virtualised GPU) is far more likely to be Apple Silicon. Guessing
+ // that way round is also the safer failure: the x64 build merely runs through Rosetta on
+ // Apple Silicon, whereas the arm64 build does not start at all on an Intel Mac.
+ ?? "arm64";
+}
+
+/**
+ * Reads the architecture off the GPU name via WebGL, the only signal exposed to every macOS browser
+ * that distinguishes an Apple Silicon Mac from an Intel one.
+ *
+ * Apple Silicon renders as `Apple GPU` (Safari), `Apple M1 Pro` or
+ * `ANGLE (Apple, ANGLE Metal Renderer: Apple M2, Unspecified Version)` (Chromium, Firefox), whereas
+ * Intel Macs name their Intel, AMD or NVIDIA GPU instead. Anything else (a paravirtualised GPU, no
+ * WebGL at all) is inconclusive rather than Intel.
+ */
+function getArchitectureFromGpu(): Architecture | null {
+ try {
+ const gl = document.createElement("canvas").getContext("webgl");
+ if (!gl) return null;
+
+ const debugInfo = gl.getExtension("WEBGL_debug_renderer_info");
+ const renderer = String(gl.getParameter(debugInfo ? debugInfo.UNMASKED_RENDERER_WEBGL : gl.RENDERER));
+ if (/\bapple\s+(gpu|m\d)/i.test(renderer)) return "arm64";
+ if (/\b(intel|amd|radeon|nvidia|geforce)\b/i.test(renderer)) return "x64";
+ return null;
+ } catch {
+ // WebGL can be unavailable or blocked for fingerprinting reasons.
+ return null;
}
+}
+
+async function getArchitectureFromClientHints(): Promise<Architecture | null> {
+ if (!navigator.userAgentData) return null;
- return "x64";
+ try {
+ const { architecture } = await navigator.userAgentData.getHighEntropyValues(["architecture"]);
+ if (!architecture) return null;
+ return architecture.startsWith("arm") ? "arm64" : "x64";
+ } catch {
+ // Permissions policy may reject high entropy values.
+ return null;
+ }
+}
+
+function getArchitectureFromUserAgent(): Architecture | null {
+ const userAgent = navigator.userAgent.toLowerCase();
+ return (userAgent.includes("arm64") || userAgent.includes("aarch64")) ? "arm64" : null;
}
export function getPlatform(): Platform | null {