Files
puter/doc/embedding-app-browser.md
T
Nariman Jelveh 9a86803418
Backend Tests / changes (push) Canceled after 0s
Backend Tests / typecheck (push) Canceled after 0s
Backend Tests / test shard 1/4 (push) Canceled after 0s
Backend Tests / test shard 2/4 (push) Canceled after 0s
Backend Tests / test shard 3/4 (push) Canceled after 0s
Backend Tests / test shard 4/4 (push) Canceled after 0s
Backend Tests / test (main) (push) Canceled after 0s
Backend Tests / coverage (push) Canceled after 0s
release-please / release-please (push) Canceled after 0s
Maintain Release Merge PR / update-release-pr (push) Canceled after 0s
Notify HeyPuter / notify (push) Canceled after 0s
Show the upgrade in a frame on first-party pages (#4541)
* Show the upgrade in a frame on first-party pages

puter.ui.requestUpgrade() on a signed-in page one label under the Puter
origin now frames /embed/upgrade over the page and resolves { upgraded }
once it closes. The usage limit dialog's Upgrade button does the same on
those pages instead of opening the dashboard in a new tab. Apps, the
desktop and every other page keep their current behavior.

/embed/upgrade is served like the sign-in and picker frames: the GUI
shell with the same framing rules, query and exact-path redirects and
no-store. The GUI shows the deployment's upgrade dialog
(window.UIUpgradeAccount) only for the framing page's own user, with
nothing left to verify, and only when the dialog says it reports its
close (UIUpgradeAccount.embeddable). Otherwise it reports unavailable and
the page opens the dashboard upgrade in a tab, through a consent dialog
when the click has expired. The app it credits is resolved from the
framing page's origin, never read from the fragment.

The frame covers the page with a transparent GUI page so the dialog's own
scrim shows, allows payment, and leaves Escape to the dialog. A call made
while it shows settles with it. If every dialog and window in the frame
goes without a close being reported, the frame closes itself rather than
staying over the page.

* Open the framed upgrade only once the frame has a viewport

The page keeps a loading frame hidden, which Chromium and WebKit lay out at
0x0. The team-seat alert, a GUI window that centers itself when it opens,
landed at (-175, 0) with its message and OK button cut off. A hidden frame
now tells the page it's ready first, and opens the dialog on the resize that
follows, or after a second if none comes.

* Document when the framed upgrade posts ready

* Retry a framed popup's focus() that the frame ignored

Firefox and WebKit ignore focus() inside a frame for a moment after the
page shows it, and Firefox sends no focus event afterwards, so the framed
upgrade dialog and its team-seat alert opened with nothing focused there:
Enter didn't press OK and screen readers didn't land in the dialog. A
focus() that didn't take is now retried briefly, until the user presses a
key or the pointer.

* Keep the framed upgrade's GUI in the light color scheme

The transparent upgrade frame put `color-scheme: light dark` on body as
well as the root, so in dark mode form fields the GUI leaves at browser
defaults turned dark. The email confirmation code digits showed slate
text on a dark grey field inside a white window. Only the root needs to
match the frame's scheme to stay transparent; body now keeps the light
scheme the GUI uses everywhere else.

* Fall back to the upgrade tab when the framed dialog fails to open

If the deployment's upgrade dialog threw while opening, the frame reported
a close, so requestUpgrade() resolved { upgraded: false } and the user's
click did nothing. It now reports itself unavailable, and the page opens
the dashboard's upgrade page as it does for any frame that can't show it.

* Say which plan changes count as upgraded in UpgradeResult

The upgrade dialog sets `upgraded` only for a subscribe or an upgrade;
a downgrade, cancellation or resume closes with false. The type said any
plan change counted.

* Stop the page scrolling behind a framed GUI embed

Firefox and WebKit pass a wheel or touch scroll over a frame on to the
page behind it, whatever the dialog's overscroll-behavior, so scrolling
over the framed upgrade, sign-in or a picker scrolled the page under the
scrim. The page's root now stops scrolling while an embed shows, taking
a classic scrollbar's width as padding so its content doesn't shift, and
gets its own styles back once the last embed closes.

* Skip the usage limit dialog while the framed upgrade shows

A refused call while the upgrade shows in a frame over a first-party
page opened the usage limit dialog on top of it, which made the upgrade
dialog inert until the user closed it. The refusal still rejects.

* Restore the page's root styles after an embed the page removed

An embed dialog the page removed itself never unlocked the page scroll,
and the next embed read the locked styles as the page's own, so the
page stayed unscrollable once it closed. The page's styles now go back
before the next lock reads them.

* Open the upgrade tab when the page can no longer frame it

The usage limit dialog decided to frame the upgrade when it opened, so
an Upgrade click after the page signed out did nothing. onUpgrade now
returns false when the page can't frame it, and the dialog opens the
dashboard tab as it does without onUpgrade.

* Keep the upgrade showing until its tab consent is answered

When the framed upgrade fell back to a consent dialog, requestUpgrade
settled as soon as the dialog showed. A refusal meanwhile opened a usage
limit dialog over it, and a second call asked again. The upgrade now
counts as showing until the user answers, so both join it.

* Say where requestUpgrade never settles

Its JSDoc named only apps, though on any website that can't frame the
upgrade the promise doesn't settle either.

* Treat a GUI embed the page removes as dismissed

A page that re-renders its body took the embed's dialog with it, and the
embed's promise never settled. For the upgrade that left it counted as
showing for good: later requestUpgrade() calls joined the dead one, no
refused call opened the usage limit dialog again, and the page stayed
unscrollable.

* Let the framed upgrade credit the remembered prompt when it knows none

When the framing page's origin resolved to no app and the call named no
reason or method, the frame handed the dialog an empty attribution. The
dialog then remembered that as the latest prompt, erasing another app's
credit, and sent the purchase with no attribution at all. It now gets
null, as from every other entry point.

* Pad the page by the width its scrollbar gives back behind an embed

A page with scrollbar-gutter: stable keeps its gutter once the root stops
scrolling, so padding it by the scrollbar's width as well shifted its
content left while any GUI embed showed. The lock now measures how much
width the root actually gets back.

* Keep the page's scroll position behind an embed when its body scrolls it

A page whose body carries the viewport's overflow (html and body at 100%
height, body with overflow-x hidden) jumped to the top while an embed
showed, and stayed there after: hiding the root's overflow made the body
a scroller of its own. The lock now hides the overflow of whichever
element the viewport uses, and restores a single-axis inline overflow.
2026-11-08 14:27:41 -07:00

16 KiB

Embedded app browser

GET /embed/apps on the deployment's main GUI origin renders the same app browser as the dashboard's Apps tab, without the desktop or dashboard navigation.

It supports search, paging, saved folders, and saved ordering. It uses the same installed-app and recommendation data as the dashboard, including the user's removed recommendations. Editing, adding, reordering, renaming, and uninstalling are disabled. Selecting an app opens it in a new browser tab at its own URL, the same way the dashboard does, and the host page stays open. The app's token isn't put in the URL: the app's SDK asks the embed for it once the tab loads, so the embed must still be open then. If the iframe is removed first, the app falls back to its normal sign-in. Apps that need the desktop (built-in and godmode apps, private apps the user can't open, or a failed token request) open /app/<name> on the GUI origin, cut off from the embed. Website shortcuts open their external destination with no opener. The embed follows the OS's light or dark setting. A host with a fixed theme adds theme=dark or theme=light to the fragment (#user=<uuid>&theme=dark), since Chromium doesn't pass an iframe's color-scheme on to the page inside it. Apps opened from the embed carry utm_source=puter.com&utm_medium=referral&utm_campaign=app_browser_embed; the desktop and dashboard send utm_campaign=desktop and utm_campaign=dashboard.

Session and framing

The embed shows the GUI session only to the user the host page is signed in as. The host names that user's uuid (from puter.auth.getUser()) in the iframe URL's fragment, as /embed/apps#user=<uuid>. The embed reads the session, asks /whoami which user it belongs to, and shows the signed-out view unless the two match. So a host that names no user, a malformed one, or a user other than the session's, gets the signed-out view, and so does a session the API refuses. A failed /whoami check shows the error state. The fragment never reaches the server and can only narrow what the embed shows: naming the session's own user gives the same view as a direct visit to the GUI. Changing the fragment of a loaded embed reloads it.

The embed reads the existing GUI session from local storage on its own origin. If that storage holds no session, the embed gets the session from the GUI origin's session cookie through /get-gui-token. This matters in Safari, which gives a framed origin separate local storage under each top-level origin, so even a sibling subdomain's iframe can't see the GUI's stored token. The session cookie is still sent, because the host is on the same site. The embed doesn't write that token back to storage. It does not create accounts, show a login dialog, or accept a token from its parent. Being signed in on a subdomain alone does not create a session on the main GUI origin.

Without a session it can show, the embed shows the recommended apps (RecommendedAppsService) that the server put in the page, and nothing else: no installed apps, folders, or saved order, and no puter.js. Selecting one opens it at the same URL a signed-in launch uses, with no opener and no token on offer, so the app signs in on its own. Apps that need the desktop open /app/<name>. If the page carries no recommended apps, or a load fails, the embed displays an unavailable/error state.

The route's Content-Security-Policy: frame-ancestors allows the GUI origin and the embedding page's origin when it is exactly one label under config.domain with the scheme and port from config.origin. Subdomains the server answers itself (api, dav, js, www, the native-app hosts, and the host of api_base_url) and the user-content hosting domains (static_hosting_domain, private_app_hosting_domain, and their _alt variants) are refused. For example, https://puter.com permits https://spreadsheet.puter.com but not https://api.puter.com, and http://puter.localhost:4100 permits http://spreadsheet.puter.localhost:4100 but not http://someone.site.puter.localhost:4100. The embedding page is read from the iframe request's referrer, so the host must not suppress it (the browser default strict-origin-when-cross-origin is enough). All ancestors in a nested iframe chain must be allowed. Other GUI routes retain X-Frame-Options: SAMEORIGIN.

The embed accepts no query parameters. Requests with a query string redirect to the same embed path so SDK bootstrap parameters cannot override the session or API origin. The host needs to allow the GUI origin in its own frame-src CSP if it sets one. If sandboxing the iframe, allow scripts, same-origin storage, popups, and popups to escape the sandbox.

Host integration

A first-party app that signs in with puter.js can place <puter-user-menu> from the SDK in its header instead (see src/puter-js/src/ui/components.md). It opens this embed with the signed-in user named and closes it when the app's sign-in changes. The rest of this section is for a host that builds the iframe itself.

The iframe can be shown whether or not the user is signed in. Create it when “More apps” is opened, naming the signed-in user in its fragment, and remove it on close so reopening loads current apps and preferences. Remove it when the host's own sign-in changes too, such as on sign-out, so an open embed doesn't keep the previous user. The host owns the modal and close control; there is no parent messaging API and no app list or credential is sent to it. The app browser fills its iframe and responds to its size. It shows a spinner until its apps load.

This example assumes puterOrigin comes from the host app's deployment settings. Translate the button and frame labels using the host app's localization system.

<button id="more-apps" type="button">More apps</button>
<dialog id="apps-dialog" aria-label="More apps">
  <button id="close-apps" type="button">Close</button>
  <div id="apps-frame"></div>
</dialog>
<style>
  #apps-dialog { padding: 12px; width: min(900px, 90vw); }
  #apps-frame iframe { width: 100%; height: min(650px, 75vh); border: 0; }
</style>
<script type="module">
  const puterOrigin = 'https://puter.com'; // Use this deployment's configured origin.
  const dialog = document.querySelector('#apps-dialog');
  const container = document.querySelector('#apps-frame');
  const trigger = document.querySelector('#more-apps');

  trigger.addEventListener('click', async () => {
    const url = new URL('/embed/apps', puterOrigin);
    // Signed out, the embed shows the recommended apps.
    const user = puter.auth.isSignedIn() ? await puter.auth.getUser().catch(() => null) : null;
    if (user) url.hash = `user=${encodeURIComponent(user.uuid)}`;
    const frame = document.createElement('iframe');
    frame.title = 'Your apps';
    frame.src = url.href;
    container.replaceChildren(frame);
    dialog.showModal();
  });
  document.querySelector('#close-apps').addEventListener('click', () => dialog.close());
  dialog.addEventListener('close', () => {
    container.replaceChildren();
    trigger.focus();
  });
</script>

Implementation

createAppBrowser in src/gui/src/UI/Dashboard/AppBrowser.js owns the shared UI and data behavior; its guestApps option is the signed-out list. TabApps.js injects desktop actions. appsEmbed.js supplies new-tab launching through launchAppInTab (signed in) and guestAppLaunchUrl (signed out) in src/gui/src/helpers/appTabLaunch.js, which build the same app URL as launch_app, and loads only the embed entry, its SDK, and shared styles. embedHostUser in src/gui/src/helpers/embedHostUser.js reads the host's user from the fragment, and embedHostTheme.js beside it applies the host's theme. The normal GUI webpack build emits bundle.min.js, apps-embed.min.js, and feedback-embed.min.js. The /embed/apps route puts the recommended list in the page's config as guestApps. No puter.js API or app-list endpoint changes are needed.

Feedback dialog

GET /embed/feedback is the app feedback dialog (puter.ui.showFeedbackDialog()) for a first-party page, with the same framing rules, query redirect, and host-user binding as /embed/apps. It sends the feedback for the framing page's origin, which the server reads from the referrer and pins with frame-ancestors; opened top-level, it does nothing. Its page is transparent so the dialog's own scrim covers the host.

puter.js uses it on a page one label under the GUI origin, other than the subdomains the server refuses, when the page is signed in, framing it in a modal <dialog> that stays hidden until the embed is ready. A session check that takes longer than 200ms shows a loading scrim with a spinner in its place; Escape or a click on it cancels. The embed posts { msg: 'feedbackEmbed', state } to its parent (loading, then ready or unavailable) and finally { msg: 'feedbackDialogClosed', sent }. It reports unavailable without the named user's session, or while that user's email still needs confirming, and puter.js then uses the /action/send-feedback popup, which can sign in. So does a frame that never answers, such as one the host's CSP blocks. feedbackEmbed.js is the entry, sharing embedSession in src/gui/src/helpers/embedSession.js with the app browser; showFeedbackEmbed in src/puter-js/src/lib/guiEmbed.js is the host side.

Sign-in and file pickers

GET /embed/sign-in, /embed/show-open-file-picker, /embed/show-save-file-picker and /embed/show-directory-picker serve the same GUI popups as /action/<action> for a first-party page, with the same framing rules, query redirect, and no-store as /embed/apps. Any other spelling of the path (another case, a trailing slash) redirects to it, since the shell boots the desktop for it. They render the normal GUI shell, which names the framing page (read from the referrer, pinned with frame-ancestors) as gui_params.embed_host_origin. The GUI then runs the popup boot unchanged, with the framing page as window.opener, and window.close() asks the page to remove the frame (src/gui/src/helpers/popupFrame.js). Without a framing page to answer (opened top-level, or framed by a refused page) it does nothing. The popup's parameters come from the fragment instead of the query: msg_id, options and request_auth, plus user for a picker.

The frame posts { msg: 'puterPopupFrame', state } to its parent: loading as soon as the shell boots, ready once it shows a window or dialog, unavailable when the page has to use the popup, provider when the user picks a sign-in provider (below), and closed where a popup would close. Its answers (puter.token, fileOpenPicked, directoryPicked, fileSaved, the cancel messages, sendMeFileData) are the popup's.

puter.js uses them on a page one label under the GUI origin, other than the subdomains the server refuses, unless the page is cross-origin isolated or in a frame of another site (which frame-ancestors refuses too). showPopupFrame in src/puter-js/src/lib/guiEmbed.js shows the frame in a box the size of the popup window, over a scrim, hidden until ready and behind the loading spinner after 200ms. Escape closes it, on the page or in the frame. One shows per popup, as each popup has its own window name: a second picker request of the same kind replaces the one showing, and a signIn() call made while a framed sign-in is open settles with it. On screens up to 600px wide the box fills the screen. A frame that is refused, never answers, or reports unavailable falls back to the popup, opened through a consent dialog when the click that asked for it has expired. A reload or navigation the frame starts itself (logging out, a sign-out in another tab) names the frame as its referrer, so the server refuses it; the page then shows its spinner and opens the frame again, once per ready.

  • Sign-in (puter.auth.signIn()) has no host-user binding: it is how the page gets a user. The box has a close button, which rejects with auth_window_closed as closing the popup does. Providers refuse to be framed, so choosing one posts { state: 'provider', url } with the provider's start URL for this sign-in's msg_id, then unavailable. The page opens the popup straight at that URL, once it checks the URL is the GUI's /auth/oidc/<provider>/start for this msg_id, and the provider returns it as this sign-in's popup. attempt_temp_user_creation keeps the popup, since only the popup creates a temporary account.
  • Pickers frame only for a signed-in page. The GUI reports unavailable unless its session belongs to the user named in user, and the page then uses the popup, which can sign in and switch accounts. The token the GUI sends before a picker's answer isn't adopted, since it is for the page's own user. Dismissing the loading scrim settles the picker as cancelled.

Upgrade

GET /embed/upgrade is the deployment's upgrade dialog for a first-party page, served like the popups above: the GUI shell with the same framing rules, query redirect, exact-path redirect and no-store, naming the framing page as gui_params.embed_host_origin. It runs only when framed by an allowed page.

puter.js frames it on a signed-in first-party page (the same pages as the pickers) for puter.ui.requestUpgrade(details), and for the Upgrade button of the usage limit dialog a refused call shows there. Elsewhere both keep their behavior: inside an app requestUpgrade() asks the desktop, and on any other page the usage limit dialog opens the dashboard's upgrade page in a new tab.

The frame covers the page with no box or close button, and the GUI page is transparent (html.popup-frame-transparent) so the dialog's own scrim covers the host. The frame allows payment as well as otp-credentials. Escape on the page is left to the dialog, since one there shouldn't drop a payment in progress. Its fragment names user (the page's signed-in user) and, when the refusal gave them, reason (funds, subscription or storage) and method.

The GUI reports unavailable unless its session belongs to user, the account has no verification or password change waiting (which the frame couldn't be closed from), and the deployment defines window.UIUpgradeAccount with UIUpgradeAccount.embeddable === true. Otherwise it runs:

new UIUpgradeAccount({ attribution: { appUid, reason, method }, onClose }).open_as_window();

appUid is the app the framing page's origin resolves to, never one the fragment names. open_as_window() resolves once the dialog shows, and the GUI then posts ready. While the page still hides the frame, which Chromium and WebKit lay out at 0x0, the GUI posts ready first and opens the dialog once the frame has a viewport, so a window it opens doesn't center on 0x0. onClose({ upgraded }) must run exactly once, when the dialog closes or when it declines to open for the account; the GUI then posts { msg: 'puterPopupFrame', state: 'closed', upgraded } and the page removes the frame. If open_as_window() rejects, the GUI posts unavailable instead. requestUpgrade() resolves { upgraded }. A call made while the frame shows settles with it rather than replacing it, and a call refused while it shows still rejects but opens no usage limit dialog over it.

When the frame can't show the upgrade (refused, never ready, unavailable, or no user to name), puter.js opens the dashboard's upgrade page in a new tab with what was refused, through a consent dialog when the click has expired or the tab is blocked, and requestUpgrade() resolves { upgraded: false }.

Checks

Run the GUI component/browser tests (with the SDK bundle built and Playwright Chromium installed), and the backend route tests:

npx vitest run --config src/gui/vitest.config.js src/gui/src/UI/Dashboard/AppBrowser src/feedbackEmbed
npx vitest run src/puter-js/src/lib/guiEmbed.test.js src/puter-js/src/modules/UI.showFeedbackDialog.test.js src/puter-js/src/modules/Auth.signInFrame.test.js src/puter-js/src/modules/UI.pickerFrame.test.js src/puter-js/src/modules/UI.requestUpgrade.test.js src/puter-js/src/lib/upgradePrompt.test.js src/puter-js/src/modules/UsageLimitDialog.upgrade.test.js
npx vitest run --config src/gui/vitest.config.js src/gui/src/helpers/popupFrame.test.js
npx vitest run --config src/backend/vitest.config.ts src/backend/server.test.ts