fix(web): sync home iframe theme and language - #5917
Conversation
WalkthroughThe Home page component adds logic to synchronize theme mode and language preferences with an embedded iframe, using postMessage calls triggered on iframe load and when the iframe URL becomes available. ChangesIframe Preference Sync
Estimated code review effort: 1 (Trivial) | ~5 minutes Sequence Diagram(s)sequenceDiagram
participant Home
participant Iframe
Home->>Home: useEffect detects isUrl set
Home->>Iframe: postMessage(themeMode)
Home->>Iframe: postMessage(lang)
Iframe-->>Home: onLoad triggers syncIframePreferences
Home->>Iframe: postMessage(themeMode, lang) again
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
web/default/src/features/home/index.tsx (1)
40-53: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick winAvoid wildcard
postMessagetarget origin.Both
postMessagecalls use'*'as targetOrigin, broadcasting theme/language to whatever origin the iframe currently holds — including after a same-window navigation (sandbox permitsallow-popups-to-escape-sandbox/allow-scripts). Prefer deriving the target origin from thecontentURL instead of'*'.🔒 Proposed fix using explicit origin
const syncIframePreferences = useCallback(() => { try { + const targetOrigin = new URL(content).origin iframeRef.current?.contentWindow?.postMessage( { themeMode: resolvedTheme }, - '*' + targetOrigin ) iframeRef.current?.contentWindow?.postMessage( { lang: i18n.language }, - '*' + targetOrigin ) } catch { // Cross-origin frames may reject access while navigating. } - }, [i18n.language, resolvedTheme]) + }, [content, i18n.language, resolvedTheme])🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@web/default/src/features/home/index.tsx` around lines 40 - 53, The iframe sync in syncIframePreferences uses wildcard targetOrigin in both postMessage calls, which should be replaced with an explicit origin. Derive the allowed target origin from the iframe/content URL or a known constant associated with the iframe source, then pass that value instead of '*'. Keep the existing useCallback and iframeRef/contentWindow logic, but ensure both themeMode and lang messages use the same validated origin.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@web/default/src/features/home/index.tsx`:
- Around line 40-53: The iframe sync in syncIframePreferences uses wildcard
targetOrigin in both postMessage calls, which should be replaced with an
explicit origin. Derive the allowed target origin from the iframe/content URL or
a known constant associated with the iframe source, then pass that value instead
of '*'. Keep the existing useCallback and iframeRef/contentWindow logic, but
ensure both themeMode and lang messages use the same validated origin.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 11e573dc-58b6-42cb-9b98-8cdfb7c50c23
📒 Files selected for processing (1)
web/default/src/features/home/index.tsx
…codes * origin/main: (180 commits) fix(billing): extend quantity validation and saturating conversions to remaining paths fix(billing): validate quantity parameters and harden quota calculations Fix/build date dns error (QuantumNous#5945) fix: avoid stale stream writes after client disconnect (QuantumNous#5710) feat(group): enhance group ratio editor with improved visibility rules and JSON parsing feat: optimize legacy top-up warning banner copy (QuantumNous#5851) (QuantumNous#5855) fix(web): redirect authenticated users away from sign-up page (QuantumNous#5910) feat(ssrf): implement SSRF protection in HTTP clients and validation functions feat(user): better messages for redeem failures fix(html): 修复 Shadow DOM 隔离渲染下深浅色模式无法自动切换的问题 (QuantumNous#5890) fix(web): sync home iframe theme and language (QuantumNous#5917) fix: 任务差额结算后 quota 和阿里视频时长优化 (QuantumNous#5923) fix(web): refine mobile user cards test(user): cover self-service password update guard feat(session): support opt-in Secure session cookies fix(auth): allow read-only access for non-disabled tokens fix(user): harden account email and password handling fix: align dynamic pricing style with log details dialog sections fix(channels): show field passthrough controls for Codex (QuantumNous#5902) fix(user): trim whitespace from username and validate input ... # Conflicts: # controller/redemption.go # controller/user.go # model/option.go # model/redemption.go # model/user.go # web/default/src/features/auth/api.ts # web/default/src/features/redemption-codes/api.ts # web/default/src/features/redemption-codes/components/redemptions-provider.tsx # web/default/src/features/redemption-codes/components/redemptions-table.tsx # web/default/src/features/redemption-codes/constants.ts # web/default/src/hooks/use-sidebar-data.ts # web/default/src/i18n/static-keys.ts # web/default/src/routes/__root.tsx
(cherry picked from commit 2f91d8c)
📝 变更描述 / Description
旧版前端在使用 URL 形式的自定义首页时,会把当前主题和语言状态传递给 iframe 页面,因此自定义 HTML 可以跟随系统的深色模式和语言切换。
新版默认前端重构后,这部分同步逻辑没有保留下来。当前自定义首页仍然可以通过 iframe 加载,但 iframe 内页面无法感知父页面的主题和语言变化,导致嵌入页面和主界面状态不一致。
本次修改补回这部分逻辑:在自定义首页 iframe 加载完成后,以及主题或语言变化时,向 iframe 发送当前的
themeMode和lang。嵌入的自定义 HTML 页面可以通过postMessage接收这些状态,并自行同步深色模式和语言显示。The previous frontend passed the current theme and language state to URL-based custom home page iframes, allowing embedded custom HTML pages to follow the app's dark mode and language changes.
After the new default frontend refactor, this synchronization logic was not carried over. The custom home page can still be loaded through an iframe, but the iframe page cannot detect theme or language changes from the parent page, which may cause inconsistent UI state.
This change restores that behavior by sending the current
themeModeandlangto the custom home iframe on iframe load and when theme or language changes. Embedded custom HTML pages can receive these values throughpostMessageand update their own theme and language accordingly.🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
已使用自定义 iframe 页面进行验证。
iframe 页面可以正确接收到父页面传入的两个状态:
themeMode:用于同步浅色 / 深色主题lang:用于同步当前选择的界面语言验证结果:嵌入的自定义首页可以跟随主页面的主题和语言切换保持一致。
Verified with a custom iframe page.
The iframe page correctly receives both values from the parent page:
themeMode: used to sync light / dark themelang: used to sync the currently selected interface languageResult: the embedded custom home page stays in sync with the parent frontend when theme or language changes.
截图 / Screenshot:
Summary by CodeRabbit
New Features
Bug Fixes