Skip to content
Merged
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
50 changes: 50 additions & 0 deletions apps/desktop/src/window/DesktopWindow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,45 @@ describe("DesktopWindow", () => {
);
});

it("rejects renderer navigations whose origin only serializes as null", () => {
for (const applicationUrl of ["t3code://app/", "t3code-dev://app/"]) {
assert.isTrue(
DesktopWindow.isSameOriginRendererNavigation({
applicationUrl,
navigationUrl: `${applicationUrl}threads?id=1#latest`,
}),
applicationUrl,
);
}
for (const navigationUrl of [
"t3code://other/",
"t3code://app.example/",
"t3code://app:8080/",
"t3code://user@other/",
"t3code:app",
"t3code-dev://app/",
"file:///etc/passwd",
"data:text/html,<p>hi</p>",
"javascript:alert(1)",
"about:blank",
"blob:t3code://app/0b5e2c4a",
]) {
assert.isFalse(
DesktopWindow.isSameOriginRendererNavigation({
applicationUrl: "t3code://app/",
navigationUrl,
}),
navigationUrl,
);
}
assert.isFalse(
DesktopWindow.isSameOriginRendererNavigation({
applicationUrl: "data:text/html,app",
navigationUrl: "data:text/html,other",
}),
);
});

it.effect(
"opens and reopens the window without backend readiness when local execution is disabled",
() =>
Expand Down Expand Up @@ -973,6 +1012,17 @@ describe("DesktopWindow", () => {
if (!willNavigate) {
return yield* Effect.die("will-navigate listener was not registered");
}
for (const [url, expectedPrevented] of [
["t3code-dev://app/threads?id=1#latest", false],
["t3code-dev://other/", true],
["t3code://app/", true],
] as const) {
const preventDefault = vi.fn();
willNavigate({ preventDefault }, url);
assert.equal(preventDefault.mock.calls.length, expectedPrevented ? 1 : 0, url);
}
assert.deepEqual(openedExternalUrls, []);

let prevented = false;
willNavigate(
{
Expand Down
11 changes: 9 additions & 2 deletions apps/desktop/src/window/DesktopWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,19 @@ function buildConnectingSplashDataUrl(shouldUseDarkColors: boolean): string {
return `data:text/html;charset=utf-8,${encodeURIComponent(html)}`;
}

function isSameOriginRendererNavigation(input: {
export function isSameOriginRendererNavigation(input: {
readonly applicationUrl: string;
readonly navigationUrl: string;
}): boolean {
try {
return new URL(input.applicationUrl).origin === new URL(input.navigationUrl).origin;
const applicationUrl = new URL(input.applicationUrl);
const navigationUrl = new URL(input.navigationUrl);
// Node serializes custom-scheme origins as "null", even for different hosts and schemes.
return (
applicationUrl.host !== "" &&
navigationUrl.protocol === applicationUrl.protocol &&
navigationUrl.host === applicationUrl.host
);
} catch {
return false;
}
Expand Down
Loading