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

Abierto
#2,638 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
55/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
android, java

Línea de trabajo

Start with Browser.java at bindService() and unbindService(), then trace BrowserPlugin.java through handleOnResume() and open(). Check the existing tests for browser service binding and URL launching before changing these paths. Done means refused binds, unbinding after a failed bind, and launch failures do not crash the app, while successful Custom Tabs behavior remains intact.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

platform: android

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 #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 #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");
}
Lenguaje dominante
Java
Estrellas
688
Forks
690
Merge medio
16 h 15 min
PR fusionados (30 d)
7

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de ionic-team/capacitor-plugins

Todos los issues de ionic-team/capacitor-plugins

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.