Skip to content
Closed
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
16 changes: 7 additions & 9 deletions src/main/apps/appsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
} from './localComfy'
import { localBuilds, openInComfy } from './openInComfy'

const APPS_URL = appsUrl(process.env, app.isPackaged)
const APPS_URL = appsUrl(process.env, app.isPackaged, CLOUD_CONFIG.issuer)
const APPS_ORIGIN = appsOrigin(APPS_URL)
const APPS_PARTITION = 'persist:comfy-apps'

Expand Down Expand Up @@ -113,8 +113,8 @@ function secureAppsSession(): void {
securedSession = appsSession
}

function keepOnAppsOrigin(event: { preventDefault(): void }, url: string): void {
if (isAppsFrameUrl(url, APPS_ORIGIN)) return
function keepInApps(event: { preventDefault(): void }, url: string): void {
if (isAppsFrameUrl(url, APPS_URL)) return
event.preventDefault()
openOutside(url)
}
Expand All @@ -132,9 +132,9 @@ function createAppsView(key: number): WebContentsView {
})
view.setBorderRadius(14)
const wc = view.webContents
wc.on('will-navigate', keepOnAppsOrigin)
wc.on('will-navigate', keepInApps)
wc.on('will-redirect', (event, url, _isInPlace, isMainFrame) => {
if (isMainFrame) keepOnAppsOrigin(event, url)
if (isMainFrame) keepInApps(event, url)
})
wc.setWindowOpenHandler(({ url }) => {
openOutside(url)
Expand Down Expand Up @@ -209,9 +209,7 @@ function hideApps(event: IpcMainEvent): void {
function fromAppsPage(event: IpcMainInvokeEvent): boolean {
const ours = [...views.values()].some((v) => v.webContents === event.sender)
const frame = event.senderFrame
return (
ours && !!frame && frame === event.sender.mainFrame && isAppsFrameUrl(frame.url, APPS_ORIGIN)
)
return ours && !!frame && frame === event.sender.mainFrame && isAppsFrameUrl(frame.url, APPS_URL)
}

function authState(): AppsAuthState {
Expand Down Expand Up @@ -567,7 +565,7 @@ function handleFromApps<A extends unknown[], R>(
function appsPages(): WebContents[] {
return [...views.values()]
.map((v) => v.webContents)
.filter((wc) => !wc.isDestroyed() && isAppsFrameUrl(wc.getURL(), APPS_ORIGIN))
.filter((wc) => !wc.isDestroyed() && isAppsFrameUrl(wc.getURL(), APPS_URL))
}

export function registerAppsIpc(): void {
Expand Down
70 changes: 59 additions & 11 deletions src/main/apps/guards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,37 @@ import {
} from './guards'

describe('appsUrl', () => {
it('defaults to the hosted site and takes an override in a development build', () => {
expect(appsUrl({}, false)).toBe(DEFAULT_APPS_URL)
expect(appsUrl({ COMFY_APPS_URL: 'http://127.0.0.1:5310/' }, false)).toBe(
'http://127.0.0.1:5310/'
)
const prod = 'https://cloud.comfy.org'
const staging = 'https://stagingcloud.comfy.org'
const stagingApps = 'https://comfy-website-preview-pr-384.vercel.app/apps/'

it('opens the comfy.org apps page for production Cloud', () => {
expect(DEFAULT_APPS_URL).toBe('https://comfy.org/apps/')
expect(appsUrl({}, false, prod)).toBe(DEFAULT_APPS_URL)
expect(appsUrl({}, true, `${prod}/`)).toBe(DEFAULT_APPS_URL)
})

it('opens the staging website apps page for staging Cloud, packaged or not', () => {
expect(appsUrl({}, false, staging)).toBe(stagingApps)
expect(appsUrl({}, true, staging)).toBe(stagingApps)
})

it('falls back to the comfy.org apps page for any other Cloud', () => {
expect(appsUrl({}, false, 'https://testcloud.comfy.org')).toBe(DEFAULT_APPS_URL)
expect(appsUrl({}, false, 'http://stagingcloud.comfy.org')).toBe(DEFAULT_APPS_URL)
expect(appsUrl({}, false, 'not a url')).toBe(DEFAULT_APPS_URL)
})

it('takes an override in a development build over either Cloud', () => {
const env = { COMFY_APPS_URL: 'http://127.0.0.1:5310/' }
expect(appsUrl(env, false, prod)).toBe('http://127.0.0.1:5310/')
expect(appsUrl(env, false, staging)).toBe('http://127.0.0.1:5310/')
})

it('ignores the override in a packaged build', () => {
expect(appsUrl({ COMFY_APPS_URL: 'https://evil.example/' }, true)).toBe(DEFAULT_APPS_URL)
const env = { COMFY_APPS_URL: 'https://evil.example/' }
expect(appsUrl(env, true, prod)).toBe(DEFAULT_APPS_URL)
expect(appsUrl(env, true, staging)).toBe(stagingApps)
})
})

Expand Down Expand Up @@ -104,11 +126,37 @@ describe('isAllowedAppsPermission', () => {

describe('isAppsFrameUrl', () => {
it('matches the exact origin', () => {
const origin = 'https://apps.comfy.org'
expect(isAppsFrameUrl('https://apps.comfy.org/upscale', origin)).toBe(true)
expect(isAppsFrameUrl('https://apps.comfy.org.evil.com/', origin)).toBe(false)
expect(isAppsFrameUrl('https://evil.com/?https://apps.comfy.org', origin)).toBe(false)
expect(isAppsFrameUrl('https://apps.comfy.org/', null)).toBe(false)
const apps = 'https://apps.comfy.org/'
expect(isAppsFrameUrl('https://apps.comfy.org/upscale', apps)).toBe(true)
expect(isAppsFrameUrl('https://apps.comfy.org.evil.com/', apps)).toBe(false)
expect(isAppsFrameUrl('https://evil.com/?https://apps.comfy.org', apps)).toBe(false)
expect(isAppsFrameUrl('http://apps.comfy.org/', 'http://apps.comfy.org/')).toBe(false)
expect(isAppsFrameUrl('https://apps.comfy.org/', 'not a url')).toBe(false)
})

it.each([DEFAULT_APPS_URL, 'https://comfy-website-preview-pr-384.vercel.app/apps/'])(
'keeps %s to its apps pages',
(apps) => {
const site = new URL(apps).origin
expect(isAppsFrameUrl(`${site}/apps/`, apps)).toBe(true)
expect(isAppsFrameUrl(`${site}/apps`, apps)).toBe(true)
expect(isAppsFrameUrl(`${site}/apps/some-app`, apps)).toBe(true)
expect(isAppsFrameUrl(`${site}/apps/text-to-image/desktop.html#/run`, apps)).toBe(true)
expect(isAppsFrameUrl(`${site}/hub/`, apps)).toBe(false)
expect(isAppsFrameUrl(`${site}/`, apps)).toBe(false)
expect(isAppsFrameUrl(`${site}/appsfoo`, apps)).toBe(false)
expect(isAppsFrameUrl(`${site}/apps-evil/`, apps)).toBe(false)
expect(isAppsFrameUrl(`${site}/apps/../hub/`, apps)).toBe(false)
expect(isAppsFrameUrl(`${site}/apps/%2e%2e/hub/`, apps)).toBe(false)
expect(isAppsFrameUrl(`${site}/hub/?next=/apps/`, apps)).toBe(false)
}
)

it('lets a development site at its root use the whole origin', () => {
const apps = 'http://localhost:5173/'
expect(isAppsFrameUrl('http://localhost:5173/', apps)).toBe(true)
expect(isAppsFrameUrl('http://localhost:5173/some-app/desktop.html', apps)).toBe(true)
expect(isAppsFrameUrl('http://localhost:5174/', apps)).toBe(false)
})
})

Expand Down
27 changes: 22 additions & 5 deletions src/main/apps/guards.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
export const DEFAULT_APPS_URL = 'https://apps.comfy.org/'
export const DEFAULT_APPS_URL = 'https://comfy.org/apps/'

export function appsUrl(env: NodeJS.ProcessEnv, packaged: boolean): string {
return (!packaged && env['COMFY_APPS_URL']) || DEFAULT_APPS_URL
const APPS_URL_BY_CLOUD: ReadonlyMap<string, string> = new Map([
['https://cloud.comfy.org', DEFAULT_APPS_URL],
['https://stagingcloud.comfy.org', 'https://comfy-website-preview-pr-384.vercel.app/apps/']
])

function cloudAppsUrl(issuer: string): string {
try {
return APPS_URL_BY_CLOUD.get(new URL(issuer).origin) ?? DEFAULT_APPS_URL
} catch {
return DEFAULT_APPS_URL
}
}

export function appsUrl(env: NodeJS.ProcessEnv, packaged: boolean, issuer: string): string {
return (!packaged && env['COMFY_APPS_URL']) || cloudAppsUrl(issuer)
}

export function appsOrigin(url: string): string | null {
Expand All @@ -25,10 +38,14 @@ export function isAllowedAppsPermission(permission: string): boolean {
return ALLOWED_APPS_PERMISSIONS.has(permission)
}

export function isAppsFrameUrl(frameUrl: string, origin: string | null): boolean {
export function isAppsFrameUrl(frameUrl: string, appsPage: string): boolean {
const origin = appsOrigin(appsPage)
if (!origin) return false
try {
return new URL(frameUrl).origin === origin
const frame = new URL(frameUrl)
const base = new URL(appsPage).pathname.replace(/\/+$/, '')
if (frame.origin !== origin) return false
return frame.pathname === base || frame.pathname.startsWith(`${base}/`)
} catch {
return false
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/apps/wiring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ describe('Apps bridge wiring', () => {
expect(main).not.toMatch(/ipcMain\.handle\('comfy-apps:/)
})

it('every apps-page check is held to the Apps URL path, not just its origin', () => {
const checks = all(main, /isAppsFrameUrl\([^,]+, (\w+)\)/g)
expect(checks).toHaveLength(3)
expect(new Set(checks)).toEqual(new Set(['APPS_URL']))
})

it('local events go out on the channel the preload listens to', () => {
const sent = local.match(/LOCAL_EVENT_CHANNEL = '([\w:-]+)'/)![1]
expect(preload).toContain(`ipcRenderer.on('${sent}'`)
Expand Down
Loading