Repository navigation
Fix sandboxed widget startup in WebKit - #10
Conversation
fdussert
left a comment
There was a problem hiding this comment.
Thanks a lot for tracking this down — the handshake timeout on the native dashboard is real, and the issue write-up made it easy to reproduce.
I reproduced it on WebKit 26.5, and the root cause looks a little different from the description. In a document with an opaque origin (sandbox allow-scripts without allow-same-origin, whether it comes from the CSP directive or the iframe attribute), WebKit's CSP matches 'self' against nothing. So it is not only the bridge request that gets refused: every subresource gated by 'self' is too. With this branch, weather (wmo.js) and pomodoro (phases.js) still fail on WebKit with "does not appear in the script-src directive", and so would any marketplace widget that ships its own script file.
A smaller fix that covers all of them: list the server's own origin next to 'self' in WIDGET_CSP (server/src/widgets/routes.ts), for script, style, img, font and media. I checked that script-src 'self' http://127.0.0.1:<port> 'unsafe-inline' makes WebKit load both /fremkit.js and relative widget scripts, with the sandbox untouched. The CSP would have to be built per request (from a loopback-filtered Host) or from a fixed list of loopback origins, since pnpm dev serves widgets through Vite on another port. With that in place the bridge can stay a <script src="/fremkit.js">, and the "read once at server start" note in AGENTS.md stays accurate. Would you be up for reworking the PR this way?
If you'd rather keep the inline bridge as well, please add a test that serves a widget with a relative <script src> and asserts it still loads, plus unit tests for injectBridge with a </script> in the bridge and with no <head>. The current test runs the extracted bridge against a fake window: it shows the bridge is inline, not that WebKit loads it.
Nit: the CHANGELOG line sits as a loose bullet above ### Changed. Could it go under a ### Fixed heading in Unreleased, and describe what the user saw (widgets stuck empty on the native dashboard)? #11–#13 add their line at the same spot, so whichever lands second will need a small rebase.
fdussert
left a comment
There was a problem hiding this comment.
Thanks a lot for reworking this, Florian — this is the fix we were after. I checked it in WebKit against a test server: weather now loads wmo.js and pomodoro loads phases.js, the bridge stays a plain /fremkit.js, and typecheck plus the full test suite pass.
One small thing before merging: please drop http://[::1]:<port> from the origin list. WebKit rejects a bracketed IPv6 host as a CSP source ("contains an invalid source: 'http://[::1]:4301'. It will be ignored."), which logs five warnings per widget, one per directive, and the server only listens on 127.0.0.1 anyway. With that removed, this is good to go.
Affected WebKit versions refuse the separate bridge script in an opaque widget frame. Embed the trusted local bridge in served widget HTML while retaining the iframe sandbox and CSP, and test its handshake. Co-Authored-By: Claude <noreply@anthropic.com>
WebKit does not resolve self in sandboxed opaque documents. Keep the external bridge script and allow guarded loopback origins for widget assets, including the development proxy and second checkout port. Co-Authored-By: Claude <noreply@anthropic.com>
WebKit rejects bracketed IPv6 authorities in this directive, and Fremkit serves widgets on IPv4 loopback. Co-Authored-By: Claude <noreply@anthropic.com>
04f3d5e to
13b0269
Compare
fdussert
left a comment
There was a problem hiding this comment.
Thanks Florian — the IPv6 source is gone, and WebKit now loads the bridge, wmo.js and phases.js with a clean console. Merging as a squash so the intermediate inline-bridge commit doesn't stay in history.
What this changes
On affected WebKit versions, sandboxed widgets load without their bridge and stay empty. The server now embeds its trusted local bridge in widget HTML while retaining the widget sandbox and CSP.
Related fork issue: FlorianBruniaux#18
How to see it
Open a widget on the XENEON WebKit dashboard. It should complete the bridge handshake and render its data. The route test executes the bridge extracted from the served HTML and checks the handshake.
Checks
pnpm typecheck,pnpm testandpnpm buildpass on this branch.pnpm build; no UI source changed.