Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Capacitor is not correctly initialized when using a dev server with ssl on Android #3707

Closed
1 task done
Badisi opened this issue Oct 20, 2020 · 4 comments
Closed
1 task done

Comments

@Badisi
Copy link

Badisi commented Oct 20, 2020

Bug Report

Capacitor Version

Latest Dependencies:

  @capacitor/cli: 2.4.2
  @capacitor/core: 2.4.2
  @capacitor/android: 2.4.2
  @capacitor/electron: 2.4.2
  @capacitor/ios: 2.4.2

Installed Dependencies:

  @capacitor/cli 2.4.2
  @capacitor/android 2.4.2
  @capacitor/core 2.4.2
  @capacitor/ios 2.4.2
  @capacitor/electron not installed

[success] Android looking great! 👌
[success] iOS looking great! 👌

Platform(s)

  • Android

Current Behavior

  1. Run capacitor with livereload and ssl on Android : ionic capacitor run android --livereload --external --ssl
  2. Capacitor.isNative is false and Capacitor.platform is web

As a result, plugins are not working and specifically SplashScreen cannot be hidden programmatically when using launchAutoHide: false.

Expected Behavior

Capacitor.isNative should be true and Capacitor.platform should be android

Other Technical Details

npm --version output: 6.14.8

node --version output: v14.10.1

Additional Context

On iOS: everything is working as expected.
On Android: everything is working as expected without ssl

@jcesarmobile
Copy link
Member

can you provide a sample app?

--ssl is an experimental flag, it generates a self signed certificate, and Capacitor won't load urls that use untrusted certificates (including self signed certificates)
I'm curious about how your app can load, I just get a white screen and this message on Logcat
Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

@jcesarmobile jcesarmobile added the needs reply needs reply from the user label Oct 20, 2020
@Badisi
Copy link
Author

Badisi commented Oct 21, 2020

You are right on that point, I forgot to mention that I had to manually patch my MainActivity.java to make ssl work.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
        // Additional plugins you've installed go here
        // Ex: add(TotallyAwesomePlugin.class);
    }});

    if (BuildConfig.DEBUG) {
        this.bridge.getWebView().setWebViewClient(new BridgeWebViewClient(this.bridge) {
            @Override
            public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
    }
}

The reason why I need ssl to be enable is that our app is making calls to https services and uses getUserMedia api which requires https..

@Ionitron Ionitron removed the needs reply needs reply from the user label Oct 21, 2020
@jcesarmobile
Copy link
Member

jcesarmobile commented Oct 29, 2020

The problem is Capacitor is not able to inject the required javascript code because of the bad certificate.

You can add this code to ignore the SSL errors on native connections, but remove it before submitting your app, google will probably reject the app if it contains this code.

    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
      public X509Certificate[] getAcceptedIssuers() {
        return null;
      }

      @Override
      public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        // Not implemented
      }

      @Override
      public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        // Not implemented
      }
    }};

    try {
      SSLContext sc = SSLContext.getInstance("TLS");

      sc.init(null, trustAllCerts, new java.security.SecureRandom());

      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
      HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } });
    } catch (KeyManagementException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }

@ionitron-bot
Copy link

ionitron-bot bot commented Nov 11, 2022

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants