Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: open another site's sign-in page from a link in the browser

Tw93368ded2fed1a
diff --git a/src-tauri/src/inject/auth.js b/src-tauri/src/inject/auth.js
index 2973508f..73a64903 100644
--- a/src-tauri/src/inject/auth.js
+++ b/src-tauri/src/inject/auth.js
@@ -1,7 +1,64 @@
// OAuth and Authentication Logic
+// link_policy.js runs first in every frame and defines getRootDomain.
// Check if URL matches OAuth/authentication patterns
function matchesAuthUrl(url, baseUrl = window.location.href) {
+ // A sign-in path on this site is this site's sign-in. On another site it is
+ // one only when the request says so: an OAuth, OpenID, SAML or WS-Federation
+ // parameter, or a return address on this site. A link in a mail or a post to
+ // another site's /login, /auth/verify or /signin page carries neither and
+ // opens in the browser like any other link (Outlook, 2026-11-23). The
+ // helpers stay inside this function so a page never sees or replaces them.
+ const signInParams = [
+ "client_id",
+ "redirect_uri",
+ "response_type",
+ "code_challenge",
+ "samlrequest",
+ "samlresponse",
+ "relaystate",
+ "wtrealm",
+ "wreply",
+ "logintorp",
+ "openid.mode",
+ "openid.return_to",
+ ];
+ const safeDecode = (value) => {
+ try {
+ return decodeURIComponent(value);
+ } catch (e) {
+ return value;
+ }
+ };
+ const asAddress = (value) => {
+ for (const text of [value, safeDecode(value)]) {
+ try {
+ const address = new URL(text);
+ if (/^https?:$/.test(address.protocol)) return address;
+ } catch (e) {
+ // Not an address.
+ }
+ }
+ return null;
+ };
+ const signsInToThisSite = (target) => {
+ const sites = new Set();
+ for (const address of [baseUrl, window.pakeConfig?.url]) {
+ try {
+ if (address) sites.add(getRootDomain(new URL(address).hostname));
+ } catch (e) {
+ // An unreadable address names no site.
+ }
+ }
+ if (sites.has(getRootDomain(target.hostname))) return true;
+ for (const [key, value] of target.searchParams) {
+ if (signInParams.includes(key.toLowerCase())) return true;
+ const address = asAddress(value);
+ if (address && sites.has(getRootDomain(address.hostname))) return true;
+ }
+ return false;
+ };
+
try {
const urlObj = new URL(url, baseUrl);
const hostname = urlObj.hostname.toLowerCase();
@@ -10,35 +67,44 @@ function matchesAuthUrl(url, baseUrl = window.location.href) {
const oauthHostPatterns = [
/^accounts\.google\.(?:com|[a-z]{2}|com\.[a-z]{2}|co\.[a-z]{2})$/,
/^login\.microsoftonline\.com$/,
- /^appleid\.apple\.com$/,
];
// Match complete path segments, including nested identity-provider routes.
// /login-tips is content; /tenant/login remains an authentication endpoint.
const oauthPathPatterns = [
/\/(?:oauth2?|auth|authorize|signin|login|servicelogin)(?:\/|$)/,
];
+ // Apple's and Facebook's sign-in live under one path on hosts that also
+ // serve account pages and share dialogs, which mails link to.
const providerEndpoint =
- /^(?:www\.)?facebook\.com$/.test(hostname) &&
- /^\/(?:[^/]+\/)?dialog(?:\/|$)/.test(pathname);
+ (hostname === "appleid.apple.com" && /^\/auth(?:\/|$)/.test(pathname)) ||
+ (/^(?:www\.)?facebook\.com$/.test(hostname) &&
+ /^\/(?:[^/]+\/)?dialog\/oauth(?:\/|$)/.test(pathname));
// Enterprise SSO. Match identity providers on the host, and SAML/SSO/ADFS on
// the pathname with endpoint-shaped patterns only, so ordinary pages such as
// /settings/sso/providers (or a query string carrying an SSO URL) are not
// misread as authentication.
- const enterpriseHostPatterns = [/(^|\.)okta\.com$/, /(^|\.)onelogin\.com$/];
+ // A tenant's own subdomain, never the vendors' own websites.
+ const enterpriseHostPatterns = [
+ /^(?!www\.)[a-z0-9-]+\.okta\.com$/,
+ /^(?!www\.)[a-z0-9-]+\.onelogin\.com$/,
+ ];
const enterprisePathPatterns = [
/\/saml2?\/(sso|acs|login|metadata|consume|redirect|callback|continue)/,
/\/sso\/(saml|oidc|oauth|login|authorize|redirect|callback|acs|start|continue|metadata)/,
/\/adfs\/ls\b/,
];
- const isMatch =
+ const providerHost =
oauthHostPatterns.some((pattern) => pattern.test(hostname)) ||
- oauthPathPatterns.some((pattern) => pattern.test(pathname)) ||
providerEndpoint ||
- enterpriseHostPatterns.some((pattern) => pattern.test(hostname)) ||
+ enterpriseHostPatterns.some((pattern) => pattern.test(hostname));
+ const signInPath =
+ oauthPathPatterns.some((pattern) => pattern.test(pathname)) ||
enterprisePathPatterns.some((pattern) => pattern.test(pathname));
+ const isMatch = providerHost || (signInPath && signsInToThisSite(urlObj));
+
if (isMatch) {
console.log("[Pake] OAuth URL detected:", url);
}
diff --git a/tests/unit/auth-sso-patterns.test.js b/tests/unit/auth-sso-patterns.test.js
index 9bd6ab0e..20fe1569 100644
--- a/tests/unit/auth-sso-patterns.test.js
+++ b/tests/unit/auth-sso-patterns.test.js
@@ -3,11 +3,13 @@ import path from "path";
import { runInNewContext } from "node:vm";
import { describe, expect, it } from "vitest";
+// In the order window.rs injects them: link_policy.js defines getRootDomain.
function loadAuthHelpers() {
- const source = fs.readFileSync(
- path.join(process.cwd(), "src-tauri/src/inject/auth.js"),
- "utf-8",
- );
+ const read = (name) =>
+ fs.readFileSync(
+ path.join(process.cwd(), "src-tauri/src/inject", name),
+ "utf-8",
+ );
const context = {
console,
@@ -17,7 +19,8 @@ function loadAuthHelpers() {
},
};
- runInNewContext(source, context);
+ context.globalThis = context;
+ runInNewContext(read("link_policy.js") + "\n" + read("auth.js"), context);
return context.window;
}
@@ -87,18 +90,71 @@ describe("auth SSO patterns", () => {
it.each([
"https://slack.com/openid/connect/authorize?client_id=123",
- "https://identity.example/connect/authorize",
- "https://tenant.auth0.com/u/login/identifier",
- "https://www.amazon.com/ap/signin",
+ "https://identity.example/connect/authorize?client_id=pake&redirect_uri=https%3A%2F%2Fexample.com%2Fcallback",
+ "https://tenant.auth0.com/u/login/identifier?returnTo=https%3A%2F%2Fapp.example.com%2Fhome",
+ "https://www.amazon.com/ap/signin?openid.mode=checkid_setup&openid.return_to=https%3A%2F%2Fexample.com%2F",
"https://example.com/tenant/oauth/authorize",
"https://example.com/login?next=/dashboard",
"https://example.com/signin/",
"https://example.com/oauth/authorize",
"https://example.com/api/auth/signin/google",
"https://accounts.google.co.uk/ServiceLogin",
- "https://github.com/login/oauth/authorize",
+ "https://github.com/login/oauth/authorize?client_id=1",
+ "https://idp.example/saml/sso/login?SAMLRequest=abc&RelayState=1",
+ "https://xyz.supabase.co/auth/v1/authorize?provider=github&code_challenge=x&code_challenge_method=s256",
+ "https://fs.corp.test/adfs/ls/idpinitiatedsignon.aspx?loginToRp=urn:app",
+ "https://idp.corp.test/adfs/ls/?wa=wsignin1.0&wreply=https%253A%252F%252Fapp.example.com%252F",
+ "https://www.facebook.com/v18.0/dialog/oauth?app_id=1",
"https://appleid.apple.com/auth/authorize",
])("preserves authentication endpoints: %s", (url) => {
expect(isAuthLink(url)).toBe(true);
});
+
+ // Outlook opens every link in a mail through window.open with noopener; a
+ // link to another site's sign-in page went on in the app (2026-11-23).
+ it.each([
+ "https://github.com/login",
+ "https://identity.example/connect/authorize",
+ "https://tenant.auth0.com/u/login/identifier",
+ "https://www.amazon.com/ap/signin",
+ "https://idp.corp.test/saml/sso",
+ "https://www.okta.com/blog/",
+ "https://okta.com/login",
+ "https://www.onelogin.com/login",
+ "https://appleid.apple.com/",
+ "https://appleid.apple.com/account/manage",
+ "https://www.facebook.com/dialog/share?app_id=1&href=https%3A%2F%2Fnews.test%2F",
+ "https://service.example/auth/verify?token=abc",
+ "https://service.example/login?next=/settings",
+ "https://service.example/signin?email=reader%40example.org",
+ "https://service.example/oauth/authorize?return=https%3A%2F%2Fother.example%2F",
+ ])(
+ "sends another site's sign-in page in a link to the browser: %s",
+ (url) => {
+ expect(isAuthLink(url)).toBe(false);
+ expect(isAuthPopup(url, "_blank")).toBe(false);
+ },
+ );
+
+ it("compares sites by the same root domain as the link policy", () => {
+ const helpers = loadAuthHelpers();
+ helpers.location.href = "https://app.example.co.uk/home";
+ expect(helpers.isAuthLink("https://other.co.uk/login")).toBe(false);
+ expect(helpers.isAuthLink("https://id.example.co.uk/login")).toBe(true);
+ });
+
+ it("counts the configured site as this site", () => {
+ const helpers = loadAuthHelpers();
+ helpers.location.href = "https://outlook.live.com/mail/0/";
+ helpers.pakeConfig = { url: "https://outlook.live.com/" };
+ expect(helpers.isAuthLink("https://account.live.com/auth/verify")).toBe(
+ true,
+ );
+ expect(helpers.isAuthLink("https://github.com/login")).toBe(false);
+ expect(
+ helpers.isAuthLink(
+ "https://idp.example/login?return_to=https%3A%2F%2Foutlook.live.com%2Fmail",
+ ),
+ ).toBe(true);
+ });
});
diff --git a/tests/unit/event-link-guard.test.js b/tests/unit/event-link-guard.test.js
index 34f63e4f..bbda0b7f 100644
--- a/tests/unit/event-link-guard.test.js
+++ b/tests/unit/event-link-guard.test.js
@@ -1479,3 +1479,53 @@ describe("getFilenameFromUrl percent-encoded path", () => {
]);
});
});
+
+// Outlook opens every link in a mail with window.open and noopener. A link to
+// another site's sign-in page replaced the mail page (2026-11-23).
+describe("links in a web mail app", () => {
+ const loadOutlook = (userAgent) => {
+ const context = loadEventHelpers({ withTauri: true, userAgent });
+ runInNewContext(
+ fs.readFileSync(
+ path.join(process.cwd(), "src-tauri/src/inject/auth.js"),
+ "utf-8",
+ ),
+ context,
+ );
+ context.window.location.href = "https://outlook.live.com/mail/0/";
+ context.window.location.origin = "https://outlook.live.com";
+ context.window.location.pathname = "/mail/0/";
+ context.window.pakeConfig = { url: "https://outlook.live.com/" };
+ runDomReady(context);
+ return context;
+ };
+
+ it.each([
+ ["macOS", "Mozilla/5.0 (Macintosh; Intel Mac OS X 15_5)"],
+ ["Windows", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"],
+ ])("opens another site's sign-in page in the browser on %s", (_os, ua) => {
+ const fresh = loadOutlook(ua);
+ const result = fresh.window.open(
+ "https://github.com/login",
+ "_blank",
+ "noopener noreferrer",
+ );
+ expect(result).toBeNull();
+ expect(fresh.window.location.href).toBe("https://outlook.live.com/mail/0/");
+ expect(
+ fresh.invokeCalls.filter(([command]) => command === "plugin:shell|open"),
+ ).toEqual([["plugin:shell|open", { path: "https://github.com/login" }]]);
+ });
+
+ it("keeps a sign-in request with an OAuth parameter in the app", () => {
+ const context = loadOutlook("Mozilla/5.0 (Macintosh; Intel Mac OS X 15_5)");
+ const url = "https://github.com/login/oauth/authorize?client_id=1";
+ context.window.open(url, "_blank", "noopener noreferrer");
+ expect(context.window.location.href).toBe(url);
+ expect(
+ context.invokeCalls.filter(
+ ([command]) => command === "plugin:shell|open",
+ ),
+ ).toEqual([]);
+ });
+});