flutter icon indicating copy to clipboard operation
flutter copied to clipboard

Allow connecting to self-hosted instances with self-signed certs

Open randrej opened this issue 3 years ago • 21 comments

Currently, the app doesn't connect to my local instance because it has a self-signed cert, even though I've imported the CA in Android.

Can you add an option to allow logging with local cer (or an option to disable cert verification) when you select local server?

randrej avatar Dec 09 '21 14:12 randrej

This is definitely something we didn't test. It seems flutter's HTTP client uses the list of CAs from mozilla, but it's possible to add own ones

https://api.flutter.dev/flutter/dart-io/HttpClient-class.html

rolandgeider avatar Dec 09 '21 14:12 rolandgeider

Thanks for the reply.

I seem to have found a way to fix this, but I'm not very familiar with Flutter, being primarily a C++/C programmer.

I found in a StackOverflow post that you can replace the handler for bad certificates in your HttpClient() object: https://stackoverflow.com/a/54359013/1307661

You could add a setting that enables this fix, exposed through a checkbox (called 'self-signed certificate') on the login screen (when you pick an alternative server). Then, in any part of the program where you instantiate a http.Client, replace it with either:

  • a subclass that has the bad cert handler set according to the proposed setting
  • a call to a factory function that produces http.Clients with the bad cert handler set according to the proposed setting

randrej avatar Dec 10 '21 11:12 randrej

Hello @rolandgeider, found a PR on the same topic which might be helpful for you: https://github.com/libre-tube/LibreTube/pull/806. Tested that and it works. TLDR: you just need to alter android manifest appropriately. A drawback of such approach is that you decide that your app trusts user-installed certificates at compile time, and you can't change this behavior in runtime. If it's applicable - looks like changing manifest is the simplest way of implementing desired functionality.

kovdan01 avatar Jul 22 '22 17:07 kovdan01

That sounds great. Just editing the XML would obviously be the fastest solution and I think always trusting user certificates would also be acceptable

rolandgeider avatar Jul 22 '22 17:07 rolandgeider

Hello @rolandgeider, please let me know if some work is planned on the issue and if I can help somehow (unfortunately, I'm not a flutter developer and can't implement the feature myself). Thanks!

kovdan01 avatar Aug 11 '22 12:08 kovdan01

Hi @kovdan01 yes, this is definitely planned. After I finish with the exercise crowd sourcing feature, I'll do this

rolandgeider avatar Aug 11 '22 14:08 rolandgeider

Thanks!

kovdan01 avatar Aug 11 '22 14:08 kovdan01

this took a bit longer, but I have just added the XML allowing user certificates

rolandgeider avatar Dec 06 '22 16:12 rolandgeider

Thanks! Looking forward for the next release including the feature

kovdan01 avatar Dec 06 '22 17:12 kovdan01

it's building right now :)

rolandgeider avatar Dec 06 '22 18:12 rolandgeider

Hello @rolandgeider . Thanks for implementing the feature, but it seems that it does not work as intended. It looks like that you forgot to add a line in AndroidManifest.xml describing that a network_security_config.xml is present (which was introduced in 9db063d65c055be72703f70874e4bf02710f2ce3). At least, I've tested the new version and it behaves the same in terms of connecting to instances with self-signed certs. So, the issue will be possibly resolved after altering AndroidManifest.xml properly. See, for example, https://github.com/Bnyro/LibreTube/blob/aadf6d441a498ac9866c395d29a1da4df1a62fe9/app/src/main/AndroidManifest.xml#L19.

kovdan01 avatar Dec 07 '22 20:12 kovdan01

oh no 😅

rolandgeider avatar Dec 07 '22 20:12 rolandgeider

New release is being reviewed by the Play Store Gods

rolandgeider avatar Feb 10 '23 11:02 rolandgeider

is this live now? can't find any setting

vayan avatar Sep 16 '23 03:09 vayan

@vayan

is this live now? can't find any setting

I might be mistaken, but this is intended to work without any settings, just by altering the manifest in order to allow user certificates.

The last time I checked the issue was still there, but it looks like it's flutter-related and can't be fixed on the wger side. I found some related issues previously, but unfortunately I'm unable to find them right now to leave links here.

I personally switched to using certificates from Let's Encrypt - they are free and do not cause issues. Self-signed ones need to be supported by every single app you want to connect from, so I just gave up.

P.S. Don't take this as an "official" answer, I'm not related to wger development.

kovdan01 avatar Sep 16 '23 09:09 kovdan01

Exactly, this is a flag set during compilation. I haven't been able to test it because I don't have the appropriate setup. If there is anything else that needs to be done from our side, we'll try to fix it, but yeah in general using let's encrypt is much easier and you will avoid all the pain that comes from self signed certificates.

rolandgeider avatar Sep 16 '23 10:09 rolandgeider

Got it! thanks :) yeah my instance is on a private tailscale network so I can't get a let's encrypt cert.

but it's ok I'll switch to http for the mobile app! Just wanted to know if I missed something 😄

Thanks for the great work!

vayan avatar Sep 16 '23 10:09 vayan

Closing the issue now, if there any issues, feel free to repoen

rolandgeider avatar Nov 21 '23 15:11 rolandgeider

Hi guys! I came accross this post when I was trying to connect the Android application to my newly setup server. It appears that there's still an issue with self-signed/untrusted certificates. I use certificates signed by local CA, since it's not viable for me to use Let's Encrypt certificates due to the nature of my environment.

I get the following error when trying to login with HTTPS. Is there anything I can do about this? Thank you very much :)

GameRebellStudio avatar Jan 09 '24 00:01 GameRebellStudio

@rolandgeider I'm using the F-Droid release, v1.6.2 if that helps

GameRebellStudio avatar Jan 09 '24 00:01 GameRebellStudio

so the flag didn't solve it, damn

rolandgeider avatar Jan 09 '24 14:01 rolandgeider