openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][C#][csharp] Use RestClient.RemoteCertificateValidationCallback for SSL validation

Open RomanSoloweow opened this issue 3 years ago • 11 comments

The RestSharp library is used to call methods.

The RestClient class has a RemoteCertificateValidationCallback method that works similar to HttpWebRequest.ServerCertificateValidationCallback.

Why can't I use it?

I think it would be logical to be able to process this delegate through IReadableConfiguration

RomanSoloweow avatar Feb 02 '22 12:02 RomanSoloweow

I have seen this problem with both csharp and csharp-netcore generators.

What is more frightening is that as far as I can tell, by default no SSL certificate verification is done, which opens up all C# generated clients to man-in-the-middle attacks.

johnthagen avatar Feb 02 '22 20:02 johnthagen

Another thing I'll note, I wonder if the csharp (not csharp-netcore) generator is effectively deprecated/unmaintained? The first time I used it I ran into multiple glaring issues that have been fixed in csharp-netcore but not backported to csharp:

  • #11488
  • #11489

johnthagen avatar Feb 03 '22 11:02 johnthagen

I think it is not deprecated, but there is a feeling that csharp generators in general are given less time

RomanSoloweow avatar Feb 03 '22 12:02 RomanSoloweow

@RomanSoloweow In your testing, do you also observe that no SSL certificate verification is happening in the RestSharp generated client?

For example if you point your C# REST client at a server with a self-signed certificate, or point it at the IP address of the server rather than its hostname (e.g. https://10.0.0.1) that no SSL verification errors are thrown?

johnthagen avatar Feb 03 '22 12:02 johnthagen

It was I who tried to specify both the URL and the IP. In both cases, I can't control the verification of the certificate

RomanSoloweow avatar Feb 03 '22 12:02 RomanSoloweow

It was I who tried to specify both the URL and the IP. In both cases, I can't control the verification of the certificate

And just to be clear, does it silently succeed without throwing an error, or does it fail and you can't control the verification to allow it to succeed?

I just want to make sure we are seeing the same behavior, thanks.

johnthagen avatar Feb 03 '22 12:02 johnthagen

This causes an error because in my case, need to ignore the verification .

Due to the lack of control over verification, I cannot fully use the generator

RomanSoloweow avatar Feb 03 '22 12:02 RomanSoloweow

Interesting. I see the opposite behavior with (C# 7.3, .NET Framework 4.7.2), all connections succeed even when they shouldn't.

You could try this solution:

  • https://stackoverflow.com/a/56351003

johnthagen avatar Feb 03 '22 12:02 johnthagen

Disregard my comments about validation always succeeding. I dug in deeper and a different framework I'm also using is messing with the global ServerCertificateValidationCallback.

johnthagen avatar Feb 03 '22 12:02 johnthagen

Yes, I also mentioned this callback.

I suggest the following solution: add a callback to IReadableConfiguration for csharp, which will then be added to RestClient. RemoteCertificateValidationCallback.

A little later I can issue this decision in the form of a PR. Or do it you if you have time now

RomanSoloweow avatar Feb 03 '22 13:02 RomanSoloweow

I'm curious to see if there is any activity or a workaround available for this issue. I too need to ignore self-signed certs for testing purposes.

ballcoach12 avatar Oct 18 '22 16:10 ballcoach12

I suggest the following solution: add a callback to IReadableConfiguration for csharp, which will then be added to RestClient. RemoteCertificateValidationCallback.

I did exactly this in my generated client. Im open to make a PR but im never done one here.

Is this a breaking change with fallback if the RemoteCertificateValidationCallback is implementet with a default null? Can go in to the next release (no beaking changes) or does it have to go in the next minor release (breaking changes with fallback)?

Chrigux avatar Mar 10 '23 08:03 Chrigux

@dlange-hima Thank you for adding the property in the IReadableConfiguration. It seems you forgot to pass the callback to the RestClientOptions in the ApiClient.ExecAsync<T>() method.

You only passed the callback in the the synchronous version (ApiClient.Exec<T>()) .

AppyxDaniel avatar Oct 24 '23 13:10 AppyxDaniel

I will try to add it, but I'm not sure how much time I have this and next week. I hope that I can squeeze it in somehow.

dlange-hima avatar Oct 24 '23 13:10 dlange-hima

I just saw that it was already fixed in https://github.com/OpenAPITools/openapi-generator/pull/16886

dlange-hima avatar Oct 24 '23 14:10 dlange-hima