Skip to content

Fix legacy WebView same-origin URL routing#7043

Open
himu-gupta wants to merge 2 commits into
home-assistant:mainfrom
himu-gupta:fix-legacy-webview-same-origin-routing
Open

Fix legacy WebView same-origin URL routing#7043
himu-gupta wants to merge 2 commits into
home-assistant:mainfrom
himu-gupta:fix-legacy-webview-same-origin-routing

Conversation

@himu-gupta

@himu-gupta himu-gupta commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Potentially addresses part of #7027.

The legacy WebViewActivity URL override path used a substring check against the current WebView URL to decide whether a URL should open externally. That can incorrectly classify same-origin Home Assistant URLs with a different path as external.

This updates the legacy override path to compare origins instead. Same-origin Home Assistant URLs stay in the app, different origins still open externally, and non-HTTP(S)/hostless URLs continue to be handed to Android without calling the origin helper.

Checklist

  • New or updated tests have been added to cover the changes following the testing guidelines.
  • The code follows the project's code style and best_practices.
  • The changes have been thoroughly tested, and edge cases have been considered.
  • Changes are backward compatible whenever feasible. Any breaking changes are documented in the changelog for users and/or in the code for developers depending on the relevance.

Screenshots

Not applicable; this changes URL routing behavior only.

Any other notes

Verification performed:

  • git diff --check
  • ./gradlew --no-daemon --console=plain :app:testFullDebugUnitTest --tests io.homeassistant.companion.android.webview.WebViewUrlOverrideTest
  • ./gradlew --no-daemon --console=plain :app:ktlintCheck

@himu-gupta himu-gupta marked this pull request as ready for review June 17, 2026 20:07
Copilot AI review requested due to automatic review settings June 17, 2026 20:07

Copilot AI 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.

⚠️ Not ready to approve

The new origin-based helper call can throw for non-HTTP(S)/hostless URIs (e.g., mailto:) and the added Robolectric test is missing the module-standard @Config(application = HiltTestApplication::class) configuration.

Pull request overview

This PR refactors the legacy WebViewActivity URL override logic to use the shared origin-comparison helper (hasSameOrigin) instead of a substring check, so same-origin Home Assistant links don’t get misrouted to an external browser (related to #7027 behavior).

Changes:

  • Introduce shouldOpenInExternalBrowser(...) to centralize the origin-based external-link decision.
  • Update WebViewActivity.shouldOverrideUrlLoading to use the new helper instead of contains(...).
  • Add Robolectric unit tests covering same-origin vs different-origin routing (and extendable to scheme edge cases).
File summaries
File Description
app/src/main/kotlin/io/homeassistant/companion/android/webview/WebViewActivity.kt Switch legacy URL override decision from substring matching to origin-based helper usage
app/src/main/kotlin/io/homeassistant/companion/android/webview/WebViewUrlOverride.kt New helper function encapsulating “open externally?” decision using hasSameOrigin
app/src/test/kotlin/io/homeassistant/companion/android/webview/WebViewUrlOverrideTest.kt New Robolectric unit tests validating URL routing decisions

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 4

Note

Your feedback helps us improve the quality of this feature.
Please use 👍 or 👎 to tell us whether this assessment is correct.

Comment thread app/src/main/kotlin/io/homeassistant/companion/android/webview/WebViewActivity.kt Outdated
@jpelgrom

Copy link
Copy Markdown
Member

Potentially addresses #7027.

This doesn't explain why navigation would happen on closing the app, and linked to the last update. The line you're referring to hasn't changed in months.

@himu-gupta himu-gupta force-pushed the fix-legacy-webview-same-origin-routing branch from 9463402 to 5298fec Compare June 18, 2026 09:33
@himu-gupta

Copy link
Copy Markdown
Contributor Author

Thanks, agreed. I checked the attached issue log and it shows the WebView renderer going away first, followed by Launching browser shortly after, so this PR does not explain the renderer crash or prove the app-update timing by itself.

I narrowed the PR wording to "potentially addresses part of #7027" and kept the code scoped to one legacy routing issue: same-origin URLs with a different path no longer get treated as external. I also addressed the review findings by parsing the target URL once, guarding non-HTTP(S)/hostless targets before hasSameOrigin, adding mailto:, tel:, and relative URL tests, and configuring the Robolectric test with HiltTestApplication.

@TimoPtr

TimoPtr commented Jun 18, 2026

Copy link
Copy Markdown
Member

Could you describe how to reproduce the initial issue? I never managed to end up into this situation.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants