Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cordova "release" behaves differently to "debug" regarding SSL

I have very difficult and totally ungoogleable problem with cordova.

A program, working perfectly being compiled in --debug mode, ceases working after compilation in --release mode. I made sure the source is identical, and the effect is constant.

The only difference between --debug build and --release build is that the --release build fails to open any SSL connections.

This problem is localized very narrow, in my case it is the following line:

Socket = new WebSocket('wss://376.su/');

a friend of mine has reported the same error occurrence in the line:

<img src="https://blabla" />;

UPD: the problem is solved see the answers.

like image 635
Eugene Panferov Avatar asked Aug 15 '15 05:08

Eugene Panferov


People also ask

What is the difference between release mode and debug mode?

By default, Debug includes debug information in the compiled files (allowing easy debugging) while Release usually has optimizations enabled. As far as conditional compilation goes, they each define different symbols that can be checked in your program, but they are language-specific macros.

Which is faster debug or release?

Lots of your code could be completely removed or rewritten in Release mode. The resulting executable will most likely not match up with your written code. Because of this release mode will run faster than debug mode due to the optimizations.

What is the difference between debug and release build in Visual Studio?

Visual Studio projects have separate release and debug configurations for your program. You build the debug version for debugging and the release version for the final release distribution. In debug configuration, your program compiles with full symbolic debug information and no optimization.

How much faster is release than debug C++?

Release CRT makes the code faster in 2x times, given that inlining is disabled. Release CRT and inlining have major synergy, providing impressive x14 boost together when Runtime Checks are disabled. Default Debug build is x240 times slower than default Release build.


3 Answers

Problem

I have identified the exact source of the problem and i have found the perfect solution. It turned out to be a superposition of two separate issues each of which is seriously misleading:

  1. My SSL certificate from Thawte (despite its cost) is not recognized by Android 5.1.1 as a valid one (while being recognized by all desktop browsers)

  2. The --debug flag in cordova build simply ignores certificate "errors" (silently).

Solution

Go to your project's directory and find the following file:

platforms/android/CordovaLib/src/org/apache/cordova/engine/SystemWebViewClient.java

Locate the method definition (onReceivedSslError) and the following condition:

(appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0

This is what makes --debug and --release different. In order to ignore certificate "errors" the following code should be executed:

handler.proceed();
return;

This file persists through the build process. Don't forget to ignore those quasi-errors next time you add a platform to your project.

like image 145
Eugene Panferov Avatar answered Oct 05 '22 00:10

Eugene Panferov


Issue

Android does not recognise the certificate authority (CA) of that certificate. It is a common issue, specially with older devices, and it affects every device every time a new CA appears.

Solutions

A. Configure intermediate certificates.

Look for a detailed setup for your platform. Here are some examples:

  • Microsoft IIS and Exchange: https://knowledge.digicert.com/solution/SO16219.html

  • Apache on RedHat (and related): https://access.redhat.com/solutions/43575

You can read more about it in this Q&A at StackExchange's Unix.

B. Use the trust hierarchy chaining certs.

Taking advantage of the trust hierarchy feature, you can chain certs.

You can leverage the effort using a tool like: https://whatsmychaincert.com/

Or you can do it by yourself, as it is just a concatenation of text files (certs):

Example steps for Linux / macOS

  1. Concat the authority's certs with your cert. That way you'll send your CA's certificates first to ensure that the device trust your CA before your domain's certificate.

    If you have separated certs, this shell command does the trick:

    $ cat authority1.cert authority2.cert authority3.cert your_domain.cert >> your_domain_bundle.cert
    

    Or if you have a ca-bundle file, that is a concatenation of certificates, just run:

    $ cat authority.ca-bundle your_domain.cert >> your_domain_bundle.cert
    
  2. Add that your_domain_bundle.cert to the server.

Problem solved for any ssl protocol, https, wss, etc.

like image 31
Ignacio Lago Avatar answered Oct 04 '22 23:10

Ignacio Lago


I had the same problem but the main source isn't the code SystemWebViewClient.java. Your post helped me a lot to find the exact source. Actually the main source is that the https site you are trying to reach is missing the certificate authority (CA) that is needed by Cordova to connect to a secured site. Actually I'm using Siberian CMS which is built over Ionic/Cordova.

You can check the site with https://www.sslshopper.com/ssl-checker.html#hostname=

like image 37
Brueire Avatar answered Oct 04 '22 23:10

Brueire