vexana icon indicating copy to clipboard operation
vexana copied to clipboard

How to append to NetworkManager httpclient setting for skipping SSL sertificate check

Open murat-ti opened this issue 4 years ago • 6 comments

Sometimes when we test network request on localhost and we don't have SSL sertificate, so we can not use Dio requests, because it shows status code 301, but on real site with SSL it works perfectly.

Here is default code:

NetworkManager({
    required BaseOptions options,
    bool? isEnableLogger,
    InterceptorsWrapper? interceptor,
    this.errorModel,
  }) {
    this.options = options;
    _addLoggerInterceptor(isEnableLogger ?? false);
    _addNetworkIntercaptors(interceptor);
    //TODO: Http adapter has come
    httpClientAdapter = DefaultHttpClientAdapter();
  }

I need to add next code to settings for turning off SSL checking and prevent error:

(httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) {
      client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
      return client;
    };

I found next solution on DIO package and it works perfectly, but I don't know how to insert this code to upper code:

var _dio = Dio();
    (_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) {
      client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
      return client;
    };
dio.send(...)

Please help me

murat-ti avatar Jun 17 '21 08:06 murat-ti

Helo I can show your pr and problems right now. I'll check detail immediately

VB10 avatar Aug 30 '21 15:08 VB10

Thanks. The Custom Http Client Adapter is very helpful, especially this days, when on 30th September 2021, the root certificate that Let's Encrypt are currently using, the IdentTrust DST Root CA X3 certificate, expired.

murat-ti avatar Nov 01 '21 12:11 murat-ti

Same error, could you please add the feature that @murat-ti mentioned? @VB10

mfurkanyuceal avatar Feb 02 '22 22:02 mfurkanyuceal

@mfurkanyuceal i don't have required enviorment for instance SSL Pinnig. Https certificate verification There are two ways to verify the https certificate. Suppose the certificate format is PEM, the code like:

String PEM='XXXXX'; // certificate content (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { client.badCertificateCallback=(X509Certificate cert, String host, int port){ if(cert.pem==PEM){ // Verify the certificate return true; } return false; }; }; Another way is creating a SecurityContext when create the HttpClient:

(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { SecurityContext sc = SecurityContext(); //file is the path of certificate sc.setTrustedCertificates(file); HttpClient httpClient = HttpClient(context: sc); return httpClient; }; In this way, the format of certificate must be PEM or PKCS12.

Maybe this can be help to you after if you can send a solution with pr, i'll so happy. thank u

VB10 avatar Feb 03 '22 01:02 VB10

@mfurkanyuceal https://pub.dev/packages/dio#https-certificate-verification

VB10 avatar Feb 03 '22 01:02 VB10

@mfurkanyuceal i don't have required enviorment for instance SSL Pinnig. Https certificate verification There are two ways to verify the https certificate. Suppose the certificate format is PEM, the code like:

String PEM='XXXXX'; // certificate content (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { client.badCertificateCallback=(X509Certificate cert, String host, int port){ if(cert.pem==PEM){ // Verify the certificate return true; } return false; }; }; Another way is creating a SecurityContext when create the HttpClient:

(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { SecurityContext sc = SecurityContext(); //file is the path of certificate sc.setTrustedCertificates(file); HttpClient httpClient = HttpClient(context: sc); return httpClient; }; In this way, the format of certificate must be PEM or PKCS12.

Maybe this can be help to you after if you can send a solution with pr, i'll so happy. thank u

#31 pr

mfurkanyuceal avatar Feb 03 '22 09:02 mfurkanyuceal