Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(desktop): honor scheme ancestors and test preview frame header routing

jeffrey4e0001a84693
diff --git a/apps/desktop/src/main/navigation.test.ts b/apps/desktop/src/main/navigation.test.ts
index c3818638..588483f9 100644
--- a/apps/desktop/src/main/navigation.test.ts
+++ b/apps/desktop/src/main/navigation.test.ts
@@ -139,24 +139,26 @@ describe("framableHeaders", () => {
describe("refusesFraming", () => {
it("reads X-Frame-Options and lets CSP frame-ancestors override it", () => {
- expect(refusesFraming({ "X-Frame-Options": ["SAMEORIGIN"] })).toBe(true);
- expect(refusesFraming({ "x-frame-options": ["deny"] })).toBe(true);
- expect(refusesFraming({ "X-Frame-Options": ["ALLOWALL"] })).toBe(false);
+ const refuses = (headers: Record<string, string[]>) =>
+ refusesFraming(headers, "https:");
+ expect(refuses({ "X-Frame-Options": ["SAMEORIGIN"] })).toBe(true);
+ expect(refuses({ "x-frame-options": ["deny"] })).toBe(true);
+ expect(refuses({ "X-Frame-Options": ["ALLOWALL"] })).toBe(false);
expect(
- refusesFraming({
+ refuses({
"Content-Security-Policy": [
"default-src 'self'; frame-ancestors 'self'",
],
}),
).toBe(true);
expect(
- refusesFraming({
+ refuses({
"X-Frame-Options": ["DENY"],
"Content-Security-Policy": ["frame-ancestors *"],
}),
).toBe(false);
expect(
- refusesFraming({
+ refuses({
"Content-Security-Policy": [
"frame-ancestors *",
"frame-ancestors 'none'",
@@ -164,12 +166,23 @@ describe("refusesFraming", () => {
}),
).toBe(true);
expect(
- refusesFraming({
+ refuses({
"Content-Security-Policy": ["default-src 'self'"],
"Content-Type": ["text/html"],
}),
).toBe(false);
});
+
+ it("lets a scheme source allow an app served over that scheme", () => {
+ const policy = (sources: string) => ({
+ "Content-Security-Policy": [`frame-ancestors ${sources}`],
+ });
+ expect(refusesFraming(policy("https:"), "https:")).toBe(false);
+ expect(refusesFraming(policy("http:"), "https:")).toBe(false);
+ expect(refusesFraming(policy("http:"), "http:")).toBe(false);
+ expect(refusesFraming(policy("https:"), "http:")).toBe(true);
+ expect(refusesFraming(policy("https://example.com"), "https:")).toBe(true);
+ });
});
describe("windowLoadDecision", () => {
diff --git a/apps/desktop/src/main/navigation.ts b/apps/desktop/src/main/navigation.ts
index 73eb147c..191e321b 100644
--- a/apps/desktop/src/main/navigation.ts
+++ b/apps/desktop/src/main/navigation.ts
@@ -199,21 +199,33 @@ const frameAncestorSources = (policy: string): string[] | undefined => {
return undefined;
};
-// Whether a response refuses to be framed by a page of another origin. CSP
-// frame-ancestors, when present, overrides X-Frame-Options, as in Chromium.
-// A source list other than * is taken as a refusal: a site never lists the
-// Linger server among its ancestors.
-export const refusesFraming = (headers: Record<string, string[]>): boolean => {
+// Whether a response refuses to be framed by the app's page at
+// ancestorProtocol. CSP frame-ancestors, when present, overrides
+// X-Frame-Options, as in Chromium. Only * or a scheme source matching the
+// app's scheme (http: also matches https:) allows the frame; a host source
+// never names the Linger server.
+export const refusesFraming = (
+ headers: Record<string, string[]>,
+ ancestorProtocol: string,
+): boolean => {
const values = (wanted: string): string[] =>
Object.entries(headers).flatMap(([name, value]) =>
name.toLowerCase() === wanted ? value : [],
);
+ const allowing = new Set([
+ "*",
+ ancestorProtocol,
+ ...(ancestorProtocol === "https:" ? ["http:"] : []),
+ ]);
const ancestors = values("content-security-policy").flatMap((policy) => {
const sources = frameAncestorSources(policy);
return sources ? [sources] : [];
});
if (ancestors.length)
- return ancestors.some((sources) => !sources.includes("*"));
+ return ancestors.some(
+ (sources) =>
+ !sources.some((source) => allowing.has(source.toLowerCase())),
+ );
return values("x-frame-options").some((value) =>
value
.split(",")
diff --git a/apps/desktop/src/main/window-manager.test.ts b/apps/desktop/src/main/window-manager.test.ts
index 03ae98d4..db0ebcf4 100644
--- a/apps/desktop/src/main/window-manager.test.ts
+++ b/apps/desktop/src/main/window-manager.test.ts
@@ -129,6 +129,9 @@ vi.mock("../../resources/icon.png", () => ({
vi.mock("../shell/offline.html", () => ({ default: "<html>offline</html>" }));
vi.mock("../shell/titlebar.html", () => ({ default: "<html>title</html>" }));
+import { session } from "electron";
+
+import { IPC } from "../shared/api.js";
import { ConfigStore } from "./config.js";
import { EventBus, ModuleHost, type Module } from "./modules.js";
import { tempDirectory } from "./test-support.js";
@@ -481,4 +484,60 @@ describe("WindowManagerModule", () => {
vi.useRealTimers();
}
});
+
+ it("keeps framing rules for the preview frame and reports a refusal", async () => {
+ const local = "http://localhost:3000";
+ const { host } = setup({ serverUrl: local });
+ await host.start();
+ const content = contentView().webContents;
+ const onHeaders = vi.mocked(
+ session.defaultSession.webRequest.onHeadersReceived,
+ );
+ const handler = onHeaders.mock.calls.at(-1)![0] as unknown as (
+ details: Record<string, unknown>,
+ done: (response: Record<string, unknown>) => void,
+ ) => void;
+ const respond = (details: Record<string, unknown>) => {
+ const done = vi.fn();
+ handler(
+ {
+ resourceType: "subFrame",
+ webContentsId: content.id,
+ statusCode: 200,
+ url: "https://linear.app/a",
+ responseHeaders: {
+ "X-Frame-Options": ["DENY"],
+ "Content-Type": ["text/html"],
+ },
+ ...details,
+ },
+ done,
+ );
+ return done.mock.calls[0]![0];
+ };
+
+ expect(respond({ frame: { name: "" } })).toEqual({
+ responseHeaders: { "Content-Type": ["text/html"] },
+ });
+ expect(content.send).not.toHaveBeenCalled();
+
+ expect(
+ respond({ frame: { name: "linger-url-preview:a" }, statusCode: 302 }),
+ ).toEqual({});
+ expect(content.send).not.toHaveBeenCalled();
+
+ expect(respond({ frame: { name: "linger-url-preview:a" } })).toEqual({});
+ expect(content.send).toHaveBeenCalledExactlyOnceWith(
+ IPC.previewFrameRefused,
+ "linger-url-preview:a",
+ "https://linear.app/a",
+ );
+
+ expect(
+ respond({ frame: { name: "linger-url-preview:a" }, webContentsId: 99 }),
+ ).toEqual({});
+ expect(respond({ resourceType: "mainFrame" })).toEqual({});
+ expect(content.send).toHaveBeenCalledTimes(1);
+ await host.stop("quit");
+ });
});
diff --git a/apps/desktop/src/main/window-manager.ts b/apps/desktop/src/main/window-manager.ts
index e22fddb4..e1c7f704 100644
--- a/apps/desktop/src/main/window-manager.ts
+++ b/apps/desktop/src/main/window-manager.ts
@@ -753,7 +753,10 @@ export class WindowManagerModule implements Module {
}
// Chromium applies framing rules to the final response only.
const redirect = details.statusCode >= 300 && details.statusCode < 400;
- if (!redirect && refusesFraming(details.responseHeaders))
+ if (
+ !redirect &&
+ refusesFraming(details.responseHeaders, new URL(server).protocol)
+ )
contents.send(IPC.previewFrameRefused, name, details.url);
done({});
});