Skip to content

fix(android): read CapacitorCookies/CapacitorHttp isEnabled from injected global - #8745

Open
basnijholt wants to merge 2 commits into
ionic-team:mainfrom
basnijholt:fix/android-native-config-js-interfaces
Open

basnijholt wants to merge 2 commits into
ionic-team:mainfrom
basnijholt:fix/android-native-config-js-interfaces

Conversation

@basnijholt

Copy link
Copy Markdown

Description

Extend the window.CapacitorNativeConfig mechanism (added for iOS in #8688, #8689 on next) to Android so CapacitorHttp and CapacitorCookies do not unconditionally register addJavascriptInterface objects just to report isEnabled():

  • Add a package-private 5-argument JSExport.getGlobalJS(context, loggingEnabled, isDebug, cookiesEnabled, httpEnabled) overload emitting window.CapacitorNativeConfig = { cookiesEnabled, httpEnabled }; (leaving the 3-argument overload unchanged so custom callers keep using the fallback), and pass plugin flags from Bridge.getJSInjector().
  • Update core/native-bridge.ts (and regenerate both android/ and ios/ native-bridge.js) to read window.CapacitorNativeConfig first on Android, fall back to CapacitorHttpAndroidInterface.isEnabled() / CapacitorCookiesAndroidInterface.isEnabled() when absent, and avoid patching document.cookie on Android if CapacitorCookiesAndroidInterface is absent.
  • Gate addJavascriptInterface in both CapacitorHttp.load() and CapacitorCookies.load() behind if (isEnabled()) so apps that do not enable either plugin do not expose frame-unaware @JavascriptInterface objects.
  • Update ARCHITECTURE.md to reflect the config-first check and gated fallback interfaces.

Change Type

  • Fix
  • Feature
  • Refactor
  • Breaking Change
  • Documentation
  • Other (CI, chores, etc.)

Rationale / Problems Fixed

WebView.addJavascriptInterface exposes methods to every frame in the WebView without frame or origin identification. Previously both CapacitorHttpAndroidInterface and CapacitorCookiesAndroidInterface were registered unconditionally even when both plugins were disabled. Reading the static enabled flags from window.CapacitorNativeConfig matches iOS (#8688) and registers neither interface unless its plugin is enabled.

No related issue. Happy to move this to a discussion first if you prefer.

Tests or Reproductions

Added tests:

  • core/src/tests/bridge.spec.ts: Android CapacitorNativeConfig.httpEnabled (true without interface, false taking precedence over interface) and browser document.cookie round-trip when CapacitorCookiesAndroidInterface is absent.
  • android/capacitor/src/test/java/com/getcapacitor/JSExportTest.java
  • android/capacitor/src/test/java/com/getcapacitor/plugin/CapacitorHttpTest.java
  • android/capacitor/src/test/java/com/getcapacitor/plugin/CapacitorCookiesTest.java

Ran locally:

  • npm run build:nativebridge — passed (regenerated both native-bridge.js copies)
  • npm run build -w core — passed
  • npm test -w core — passed (6 suites, 59 tests)
  • npm run eslint — passed
  • npm run prettier -- --check — passed

Not run locally:

  • Android JUnit (npm run verify -w android): no JDK 21 / Android SDK here, left to CI.
  • npm run verify -w ios / SwiftLint: macOS only; no Swift sources changed (only the regenerated iOS native-bridge.js).

Screenshots / Media

N/A

Platforms Affected

  • Android
  • iOS (regenerated native-bridge.js only; behaviour unchanged)
  • Web

Notes / Comments

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant