Skip to content

bug(browser): Fatal SecurityException in Browser.bindService() on Android resume when Custom Tabs provider refuses the bind (Unihertz devices) #2638

Open

Description

@braincomb

Bug Report

Plugin(s)

  • @capacitor/browser (verified on 8.0.5; the same code is present on main as of today)

Capacitor Version

@capacitor/core: 8.5.3
@capacitor/android: 8.5.3
@capacitor/cli: 8.5.3
@capacitor/browser: 8.0.5

Platform(s)

  • Android

Current Behavior

The app crashes with a fatal SecurityException on every Activity.onResume when the resolved Custom Tabs provider refuses the bind.

BrowserPlugin.handleOnResume() calls Browser.bindService(), which calls CustomTabsClient.bindCustomTabsService(). The code assumes this method only fails by returning false (which is logged and ignored), but when the resolved provider (e.g. Brave Browser, or Chrome when disabled/blocked by an intent firewall) refuses the bind, the OS throws a SecurityException from ContextImpl.bindServiceCommon instead. Because this runs inside MainActivity.onResume(), the uncaught exception propagates through BridgeActivity.onResume and kills the process.

The affected user cannot use the app at all: the crash fires on launch and on every return to the foreground, regardless of whether the app ever calls Browser.open().

Fatal exception from production crash reporting:

Fatal Exception: java.lang.RuntimeException
Unable to resume activity {app.pkg/app.pkg.MainActivity}: java.lang.SecurityException:
Not allowed to bind to service Intent { act=android.support.customtabs.action.CustomTabsService pkg=com.brave.browser }

android.app.ActivityThread.performResumeActivity (ActivityThread.java:5352)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:962)
Caused by java.lang.SecurityException
Not allowed to bind to service Intent { act=android.support.customtabs.action.CustomTabsService pkg=com.brave.browser }

android.app.ContextImpl.bindServiceCommon (ContextImpl.java:2188)
androidx.browser.customtabs.CustomTabsClient.bindCustomTabsService (CustomTabsClient.java:90)
com.capacitorjs.plugins.browser.Browser.bindService (Browser.java:144)
com.capacitorjs.plugins.browser.BrowserPlugin.handleOnResume (BrowserPlugin.java:95)
com.getcapacitor.Bridge.onResume (Bridge.java:1359)
com.getcapacitor.BridgeActivity.onResume (BridgeActivity.java:98)
android.app.Instrumentation.callActivityOnResume (Instrumentation.java:1722)

Expected Behavior

A refused Custom Tabs bind should be handled gracefully (log and continue), never crash the app. The plugin's existing if (!implementation.bindService()) error-logging path suggests that was the intent — it just doesn't cover the throwing case.

Code Reproduction

  1. Install the app on a Unihertz device (e.g. Tank 2 Pro, Titan Pocket) with a non-Chrome browser (e.g. Brave) set as the default browser, so CustomTabsClient.getPackageName() resolves to it.
  2. Launch the app (or resume it from the background).
  3. Observe the fatal SecurityException in Browser.bindService() during onResume.

Note that no Browser.open() call is required to trigger the crash — the bind happens on every resume.

Device identified: Unihertz Tank 2 Pro (Android 15). This is a known Unihertz firmware behavior: their ROM's app-launch blocker refuses service binds from third-party apps to any Custom Tabs provider except Chrome. Two precedents:

User-side workaround on Unihertz devices: install/enable Chrome and set it as the default browser so CustomTabsClient.getPackageName() resolves to Chrome.

Other Technical Details

  • This is a follow-up to feat(browser): support other custom tabs providers #1758 ("feat(browser): support other custom tabs providers", merged Aug 2023). That PR is what made the plugin resolve non-Chrome providers such as Brave, which exposed this crash path. The exception handling around the bind itself was never added, and the current main branch still has the unguarded call in Browser.bindService().
  • The same class of issue exists in two other places:
    • Browser.unbindService() (called from handleOnPause()) throws IllegalArgumentException when the service was never successfully bound — i.e. immediately after the bind failure above, the next pause crashes too.
    • BrowserPlugin.open() → activity.open(...) → CustomTabsIntent.launchUrl() can also throw SecurityException (see the stack trace in the feat(browser): support other custom tabs providers #1758 comments).

Proposed Fix

Wrap the affected calls in try/catch so failures degrade to the existing non-crash paths:

// Browser.java
public boolean bindService() {
    String customTabPackageName = CustomTabsClient.getPackageName(context, null);
    if (null == customTabPackageName) {
        customTabPackageName = FALLBACK_CUSTOM_TAB_PACKAGE_NAME;
    }
    try {
        return CustomTabsClient.bindCustomTabsService(context, customTabPackageName, connection);
    } catch (SecurityException | IllegalArgumentException ex) {
        return false;
    }
}

public void unbindService() {
    try {
        context.unbindService(connection);
    } catch (IllegalArgumentException | SecurityException ex) {
        // no-op: service was not bound
    }
}

// BrowserPlugin.java, inside the open() listener
} catch (ActivityNotFoundException | SecurityException ex) {
    Logger.error(getLogTag(), ex.getLocalizedMessage(), null);
    call.reject("Unable to display URL");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions