From 7fabf8ec6a2bfb4eaddb7109dab6eef22da79f4a Mon Sep 17 00:00:00 2001 From: Daniel Salazar Date: Wed, 25 Nov 2026 17:05:14 -0700 Subject: [PATCH] fix(gui): AuthMe refuses redirects to any host under the deployment domain (#4777) Hosts are compared by name, ignoring scheme, port and a trailing dot, so the GUI, API and WebDAV hosts and anything else under the domain are refused. Configured relays (authmeRelayOrigins) are still delivered to on their exact origin, and loopback destinations other than the GUI itself are unchanged. --- src/gui/src/initgui.js | 8 +++- src/gui/src/util/authmeGrant.js | 45 ++++++++++++++++--- src/gui/src/util/authmeGrant.test.js | 67 +++++++++++++++++++++------- 3 files changed, 97 insertions(+), 23 deletions(-) diff --git a/src/gui/src/initgui.js b/src/gui/src/initgui.js index 842846f59..72cc621b3 100644 --- a/src/gui/src/initgui.js +++ b/src/gui/src/initgui.js @@ -74,7 +74,7 @@ import { forwardedDestination, fullTokenAllowed, isDeliverableRedirect, - isGuiRedirect, + isDeploymentRedirect, shouldUseRemoteAuthme, urlTokenParam, wantsFullToken, @@ -210,7 +210,11 @@ const postAuthActions = async (action) => { await UIAlert({ message: i18n('authme_bad_redirect_url') }); return; } - if ( redirectURL && isGuiRedirect(redirectURL, [window.location.origin, window.gui_origin]) ) { + if ( redirectURL && isDeploymentRedirect(redirectURL, { + domain: window.gui_params?.domain, + guiOrigins: [window.location.origin, window.gui_origin], + relayOrigins: window.gui_params?.authmeRelayOrigins, + }) ) { await UIAlert({ message: i18n('authme_gui_redirect_url') }); return; } diff --git a/src/gui/src/util/authmeGrant.js b/src/gui/src/util/authmeGrant.js index 9077d7a21..5da648742 100644 --- a/src/gui/src/util/authmeGrant.js +++ b/src/gui/src/util/authmeGrant.js @@ -137,19 +137,52 @@ export const forwardedDestination = (redirectURL, relayOrigins) => { return named === url.host.toLowerCase() ? null : named; }; +const hostnameOf = (value) => { + try { + return new URL(value).hostname.toLowerCase() + .replace(/^\[/, '').replace(/\]$/, '') + .replace(/\.+$/, ''); + } catch { + return null; + } +}; + /** - * Does the redirect land back on the GUI itself? + * Does the redirect land on a host this deployment serves? * * GUI pages pass their query on to what they open (`/app/` hands it to - * that app), so a grant delivered there reaches whatever the URL opens rather - * than the page the dialog names. A grant is never delivered to the GUI. + * that app), and the API and WebDAV hosts serve users' files inline, so a + * grant delivered to any of them reaches content the dialog doesn't name. + * Hosts are compared by name, ignoring scheme, port and a trailing dot, and + * every host under the deployment's domain counts. The configured relays are + * the exception, and a loopback destination other than the GUI itself is the + * user's own machine. * * @param {string} redirectURL - * @param {string[]} guiOrigins - The origins this GUI is served on. + * @param {object} deployment + * @param {string} [deployment.domain] - The deployment's domain (`gui_params.domain`). + * @param {string[]} [deployment.guiOrigins] - The origins this GUI is served on. + * @param {string[]} [deployment.relayOrigins] - Exact origins of the configured relays. * @returns {boolean} */ -export const isGuiRedirect = (redirectURL, guiOrigins) => - isListedOrigin(redirectURL, guiOrigins); +export const isDeploymentRedirect = ( + redirectURL, + { domain, guiOrigins = [], relayOrigins = [] } = {}, +) => { + if ( isListedOrigin(redirectURL, guiOrigins) ) return true; + if ( isListedOrigin(redirectURL, relayOrigins) ) return false; + if ( isLoopbackTarget(redirectURL) ) return false; + + const host = hostnameOf(redirectURL); + if ( ! host ) return false; + const ownHosts = guiOrigins.map(hostnameOf).filter(Boolean); + if ( ownHosts.includes(host) ) return true; + + const ownDomain = typeof domain === 'string' + ? domain.toLowerCase().replace(/\.+$/, '') + : ''; + return !!ownDomain && (host === ownDomain || host.endsWith(`.${ownDomain}`)); +}; /** * Was a full account session explicitly requested? diff --git a/src/gui/src/util/authmeGrant.test.js b/src/gui/src/util/authmeGrant.test.js index b9210c98a..716507d81 100644 --- a/src/gui/src/util/authmeGrant.test.js +++ b/src/gui/src/util/authmeGrant.test.js @@ -27,7 +27,7 @@ import { forwardedDestination, fullTokenAllowed, isDeliverableRedirect, - isGuiRedirect, + isDeploymentRedirect, isLoopbackTarget, isRemoteBackendGui, shouldUseRemoteAuthme, @@ -240,26 +240,63 @@ describe('forwardedDestination', () => { }); }); -describe('isGuiRedirect', () => { - const GUI = ['https://puter.com', 'https://www.puter.com']; +describe('isDeploymentRedirect', () => { + const PROD = { + domain: 'puter.com', + guiOrigins: ['https://puter.com', 'https://puter.com'], + relayOrigins: ['https://mcp.puter.com'], + }; it('is true for any page on the GUI origin', () => { - expect(isGuiRedirect('https://puter.com/app/some-app?forwards_to=client.example', GUI)).toBe(true); - expect(isGuiRedirect('https://www.puter.com/app/some-app', GUI)).toBe(true); - expect(isGuiRedirect('https://puter.com/desktop/app/some-app', GUI)).toBe(true); - expect(isGuiRedirect('https://PUTER.com/', GUI)).toBe(true); + expect(isDeploymentRedirect('https://puter.com/app/some-app?forwards_to=client.example', PROD)).toBe(true); + expect(isDeploymentRedirect('https://puter.com/desktop/app/some-app', PROD)).toBe(true); + expect(isDeploymentRedirect('https://PUTER.com/', PROD)).toBe(true); }); - it('is false for any other origin, including a local GUI and a relay', () => { - expect(isGuiRedirect('https://mcp.puter.com/oauth/callback', GUI)).toBe(false); - expect(isGuiRedirect('http://localhost:4100/', GUI)).toBe(false); - expect(isGuiRedirect('http://puter.com/app/x', GUI)).toBe(false); - expect(isGuiRedirect('https://app.example/cb', GUI)).toBe(false); + it('ignores scheme, port and a trailing dot on the GUI host', () => { + expect(isDeploymentRedirect('http://puter.com/app/x', PROD)).toBe(true); + expect(isDeploymentRedirect('https://puter.com:8443/app/x', PROD)).toBe(true); + expect(isDeploymentRedirect('https://puter.com./app/x', PROD)).toBe(true); }); - it('is false for an unparseable redirect or a missing origin', () => { - expect(isGuiRedirect('not a url', GUI)).toBe(false); - expect(isGuiRedirect('https://puter.com/', [undefined])).toBe(false); + it('is true for other hosts under the deployment domain', () => { + expect(isDeploymentRedirect('https://api.puter.com/token-read?uid=x&token=y', PROD)).toBe(true); + expect(isDeploymentRedirect('https://dav.puter.com/someone/page.html?auth_token=x', PROD)).toBe(true); + expect(isDeploymentRedirect('https://www.puter.com/app/x', PROD)).toBe(true); + expect(isDeploymentRedirect('https://desktop.puter.com/', PROD)).toBe(true); + }); + + it('is false for a configured relay, on its exact origin only', () => { + expect(isDeploymentRedirect('https://mcp.puter.com/oauth/callback', PROD)).toBe(false); + expect(isDeploymentRedirect('http://mcp.puter.com/oauth/callback', PROD)).toBe(true); + expect(isDeploymentRedirect('https://mcp.puter.com./oauth/callback', PROD)).toBe(true); + expect(isDeploymentRedirect('https://mcp.puter.com/oauth/callback', { ...PROD, relayOrigins: [] })).toBe(true); + }); + + it('is false for loopback and for other sites', () => { + expect(isDeploymentRedirect('http://localhost:4100/', PROD)).toBe(false); + expect(isDeploymentRedirect('http://127.0.0.1:33418/callback', PROD)).toBe(false); + expect(isDeploymentRedirect('https://app.example/cb', PROD)).toBe(false); + expect(isDeploymentRedirect('https://someone.puter.site/cb', PROD)).toBe(false); + expect(isDeploymentRedirect('https://notputer.com/cb', PROD)).toBe(false); + }); + + it('still refuses a loopback GUI on its own origin', () => { + const local = { domain: 'puter.localhost', guiOrigins: ['http://puter.localhost:4100'] }; + expect(isDeploymentRedirect('http://puter.localhost:4100/app/x', local)).toBe(true); + expect(isDeploymentRedirect('http://localhost:4000/', local)).toBe(false); + }); + + it('falls back to the GUI hosts when no domain is configured', () => { + const noDomain = { guiOrigins: ['https://puter.com'] }; + expect(isDeploymentRedirect('http://puter.com/app/x', noDomain)).toBe(true); + expect(isDeploymentRedirect('https://api.puter.com/token-read', noDomain)).toBe(false); + }); + + it('is false for an unparseable redirect or missing settings', () => { + expect(isDeploymentRedirect('not a url', PROD)).toBe(false); + expect(isDeploymentRedirect('https://puter.com/', { guiOrigins: [undefined] })).toBe(false); + expect(isDeploymentRedirect('https://puter.com/')).toBe(false); }); });