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

HTTPS support for ionic serve and Cordova livereload (--ssl) #3305

Open
imhoffd opened this issue Jun 13, 2018 · 39 comments
Open

HTTPS support for ionic serve and Cordova livereload (--ssl) #3305

imhoffd opened this issue Jun 13, 2018 · 39 comments
Labels
enhancement project type: angular Ionic Angular 4+ projects project type: react Ionic React projects

Comments

@imhoffd
Copy link
Contributor

imhoffd commented Jun 13, 2018

Known issues:

@bleuscyther
Copy link

GPS wont work outside of safe env. aka: https.

@redplane
Copy link

Any solutions for this ? I'm having the same issue with ionic cordova run browser --livereload

@imhoffd
Copy link
Contributor Author

imhoffd commented Aug 27, 2018

@redplane That command (and the Cordova browser platform) isn't fully supported. See #3043. The solution is forthcoming and this issue will be updated when it arrives.

@redplane
Copy link

Will it be for ionic-3 or ionic 4? Im having a project on ionic 3.9.2, a little big that Im afraid of changing

@imhoffd
Copy link
Contributor Author

imhoffd commented Aug 28, 2018

No plans to add HTTPS to @ionic/app-scripts for Ionic 3.

@kurtommy
Copy link

Updates?

@CJTalbot87
Copy link

Hi any update on this? I'm struggling to get the Geo-location to work on the Ionic Dev App with HTTP so to be able to serve over HTTPS would be great assuming that would fix the issue.

imhoffd added a commit that referenced this issue Nov 15, 2018
By using the Angular CLI's `--ssl` option, the webpack server will
auto-generate an SSL certificate at runtime. Devs will need to confirm
the self-signed certificate in their browsers in order to load the app
using `ionic serve --ssl`.

Known limitations:

* DevApp does not yet support apps on HTTPS (#3748)
* The iOS Web View does not support self-signed certificates yet

references #3305
imhoffd added a commit to ionic-team/angular-toolkit that referenced this issue Nov 15, 2018
@imhoffd imhoffd changed the title HTTPS support for ionic serve and Cordova livereload HTTPS support for ionic serve and Cordova livereload (--ssl) Nov 19, 2018
@imhoffd
Copy link
Contributor Author

imhoffd commented Nov 20, 2018

CLI 4.4.0 is out with an --ssl flag:

ionic serve --ssl

There are a few gotchas, which I've documented in the original issue description: #3305 (comment)

@sebast1219
Copy link

So now livereload should work in https with --ssl ?

@imhoffd
Copy link
Contributor Author

imhoffd commented Nov 21, 2018

@sebast1219 ionic cordova run with --ssl will work, but it seems the iOS wkwebview doesn't accept self-signed certificates. So, it should work with Android.

The --ssl flag is an experimental feature (and is marked as such in ionic serve --help), we expect issues.

@phal0r
Copy link

phal0r commented Nov 26, 2018

@dwieeb
Thanks for this feature, I wanted to try this out and therefor updated to ionic 4.4.0. I can see (and use) ionic ssl commands, but no ionic serve with --ssl flag when performing ionic serve --help.

Using ionic serve --ssl works so far, but still in http mode, so the ssl flag is not acknowledged. Do I need to update something else?

@GuerrillaCoder
Copy link

GuerrillaCoder commented Jun 26, 2019

@dwieeb This is how we do it for angular but ionic appears to ignore these arguments. I specify the correct paths but it always uses a 1 month long cert it generates itself instead of using my 1 year long cert I have in my trusted root.

I am using CLI version 5.1.0

@imhoffd
Copy link
Contributor Author

imhoffd commented Jun 26, 2019

@GuerrillaCoder Did you notice the separator (--) in the command? With the separator, arguments after it are forwarded as-is to the Angular CLI. The command I posted works for me in CLI 5.1.0.

@GuerrillaCoder
Copy link

@dwieeb oooooh no I didn't. Many thanks for that, my apologies for taking your time.

@imedvedev75
Copy link

Dear guys

I still get error trying to use --ssl flag:

ionic serve --ssl -- --ssl-cert ..ionic\ssl\cert.pem --ssl-key ..ionic\ssl\key.pem
Error: spawn EPERM

at ChildProcess.spawn (internal/child_process.js:358:11)
at Object.spawn (child_process.js:533:9)
at spawn (C:\Users\d038471\AppData\Roaming\npm\node_modules\ionic\node_modules\cross-spawn\index.js:12:24)
at spawn
(C:\Users\d038471\AppData\Roaming\npm\node_modules\ionic\node_modules@ionic\utils-subprocess\dist\index.js:169:12)
at Subprocess.spawn
(C:\Users\d038471\AppData\Roaming\npm\node_modules\ionic\node_modules@ionic\utils-subprocess\dist\index.js:142:16)
at Shell.spawn (C:\Users\d038471\AppData\Roaming\npm\node_modules\ionic\lib\shell.js:152:24)

Could you be so kind to help?

Many thanks

@muuvmuuv
Copy link
Contributor

Solution for me for ionic serve is to add cert and key to serve configuration section in angular.json:

// ...
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "sslKey": "src/certificates/dev.key",
            "sslCert": "src/certificates/dev.crt"
          },
// ...

BUT this won't work for ionic capacitor run ios -l --ssl...

@imhoffd
Copy link
Contributor Author

imhoffd commented May 18, 2020

@muuvmuuv You can make a feature request for that in this repo: https://github.com/ionic-team/angular-toolkit

Basically, the ionic-cordova-serve needs to forward sslKey and sslCert options to the Angular serve builder.

@jayenashar
Copy link

is this supposed to work with react, or just angular? is DevApp still a thing, or can #3748 be closed?

@imhoffd imhoffd added the project type: react Ionic React projects label Jun 15, 2020
@imhoffd
Copy link
Contributor Author

imhoffd commented Jun 15, 2020

The goal would to get it working with react-scripts too, yes. I closed the other issue, thanks!

@Laurence-Walpole
Copy link

Laurence-Walpole commented Jun 24, 2020

I'm currently unable to get the following boot params to work: ionic capacitor run android -l --ssl --external - it launches the app correctly but on the emulator I just get a white screen with no url in the webview. I believe it's linked to this issue because it only arises when I add the --ssl flag to the command. This is on Windows 10, using Ionic 5 and running on an emulated Pixel C API 29.

@kamranayub
Copy link

FYI for React apps, ionic serve --https does work, I use it for testing my PWA 👍

name: 'https',
summary: 'Use HTTPS for the dev server',
type: Boolean,

@appaulmac
Copy link

FYI for React apps, ionic serve --https does work, I use it for testing my PWA 👍

name: 'https',
summary: 'Use HTTPS for the dev server',
type: Boolean,

Fantastic! Thanks for pointing this out; I couldn't find any other reference to this flag

@imhoffd
Copy link
Contributor Author

imhoffd commented Jul 2, 2020

Unfortunately our online CLI documentation is still specific to Angular. For React, you can use the --help flag in your terminal: ionic serve --help.

The --https flag for React is a bit different than what this issue was originally trying to accomplish. It uses a webpack plugin that automatically generates an SSL certificate at runtime, but when I last tried to use it there were trust issues.

@zengchuan
Copy link

I'm currently unable to get the following boot params to work: ionic capacitor run android -l --ssl --external - it launches the app correctly but on the emulator I just get a white screen with no url in the webview. I believe it's linked to this issue because it only arises when I add the --ssl flag to the command. This is on Windows 10, using Ionic 5 and running on an emulated Pixel C API 29.

I have the same problem. How to resolve it. command: ionic cap run android -l --external --ssl -- --ssl-cert .ionic/ssl/cert.pem --ssl-key .ionic/ssl/key.pem

@JesusSPC
Copy link

I have the same problem. How to resolve it. command: ionic cap run android -l --external --ssl -- --ssl-cert .ionic/ssl/cert.pem --ssl-key .ionic/ssl/key.pem

Same problem but I couldn't resolve it. I tried to generate a ssl certificate and run that command, still a white screen on --ssl. I don't know what I'm doing wrong here.

@JesusSPC
Copy link

I managed to make the --ssl flag work by including another override in:
node_modules/@capacitor/android/capacitor/src/main/java/com/getcapacitor/BridgeWebViewClient.java

 @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
      handler.proceed();
      return;
    }

Also import there:

import android.webkit.SslErrorHandler;
import android.net.http.SslError;

But this is a nuisance since you need to document this in your project or inform other people that is going to pull it to follow this steps with their own module.

@Schwankenson
Copy link

Schwankenson commented Nov 28, 2020

I managed to make the --ssl flag work by including another override in:
node_modules/@capacitor/android/capacitor/src/main/java/com/getcapacitor/BridgeWebViewClient.java

 @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
      handler.proceed();
      return;
    }

Also import there:

import android.webkit.SslErrorHandler;
import android.net.http.SslError;

But this is a nuisance since you need to document this in your project or inform other people that is going to pull it to follow this steps with their own module.

Thank you, this works for me!
But it is of course not a good solution to make changes in a node module. Would be great to have a fix in a clean way.

@Patric
Copy link

Patric commented Jan 10, 2021

I managed to make the --ssl flag work by including another override in:
node_modules/@capacitor/android/capacitor/src/main/java/com/getcapacitor/BridgeWebViewClient.java

 @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
      handler.proceed();
      return;
    }

Also import there:

import android.webkit.SslErrorHandler;
import android.net.http.SslError;

But this is a nuisance since you need to document this in your project or inform other people that is going to pull it to follow this steps with their own module.

Using this solution changed platforms array deleting cordova parameter. That means everything is working as if it was in a browser preventing cordova plugins to be loaded. I'm getting:

Msg: Native: tried calling BluetoothLE.isScanning, but Cordova is not available. Make sure to include cordova.js or run in a                  device/simulator

Is there any solution to actually run ssl on android?

@Schwankenson
Copy link

Schwankenson commented Nov 3, 2021

Any news on this? I need to run the livereload with ssl. And the hack works, but is really annoying.

@Schwankenson
Copy link

Schwankenson commented Nov 4, 2021

I managed to get it runnning for ios with generating mkcerts: https://github.com/FiloSottile/mkcert

Run livereload:

ionic cap run ios --external --livereload-url=capacitor://mytestdomain.test:8100

And then run a server with a self signed certificate in another terminal

ng run app:serve --host=0.0.0.0 --port=8100 --ssl --public-host=rg-eat.test --ssl-cert=/host_system_mkcert_folder/${RG_MKCERT_CERT_NAME} --ssl-key=/host_system_mkcert_folder/${RG_MKCERT_KEY_NAME}

In iOs simulator it's easy to install the certificate (just show path witk mkcert -CAROOT) and drag it to the simulator.
In Android it can be installed and works in browser, but it is not compiled in Android Studio without the hack from @JesusSPC

@reslear
Copy link

reslear commented May 9, 2022

thx @Schwankenson )
Adopting for vue3 vite and walletconnect web3 provider

  1. install and configure https://github.com/liuweiGL/vite-plugin-mkcert

  2. run vue dev server

pnpm dev --host="0.0.0.0" --port=8100
  1. run capacitor server
npx ionic cap run ios --external --livereload-url="https://192.168.100.3:8100/"

@graemeenglish
Copy link

This might help someone...

For Android create a self signed cert for the host address you are using for live reload e.g.

IP="192.168.1.215"
NAME="live_reload"
openssl req \
-x509 \
-newkey rsa:4096 \
-sha256 \
-days 365 \
-nodes \
-keyout ssl/$NAME.key \
-out ssl/$NAME.crt \
-subj "/CN=${IP}" \
-extensions v3_ca \
-extensions v3_req \
-config <( \
  echo '[req]'; \
  echo 'default_bits= 4096'; \
  echo 'distinguished_name=req'; \
  echo 'x509_extension = v3_ca'; \
  echo 'req_extensions = v3_req'; \
  echo '[v3_req]'; \
  echo 'basicConstraints = CA:FALSE'; \
  echo 'keyUsage = nonRepudiation, digitalSignature, keyEncipherment'; \
  echo 'subjectAltName = @alt_names'; \
  echo '[ alt_names ]'; \
  echo "IP.1 = ${IP}"; \
  echo '[ v3_ca ]'; \
  echo 'subjectKeyIdentifier=hash'; \
  echo 'authorityKeyIdentifier=keyid:always,issuer'; \
  echo 'basicConstraints = critical, CA:TRUE, pathlen:0'; \
  echo 'keyUsage = critical, cRLSign, keyCertSign'; \
  echo 'extendedKeyUsage = serverAuth, clientAuth')

Use this when running ionic cap run e.g.
ionic cap run android -l --external --ssl -- --ssl-cert ssl\live_reload.crt --ssl-key ssl\live_reload.key

At this point you still get a blank screen when running the emulator because Android doesn't trust this certificate.

Copy the live_reload.crt you created to the android raw resource folder

Create a network_security_config.xml file with debug-overrides as explained https://developer.android.com/training/articles/security-config?msclkid=44f9be68d04d11eca3865cb4143ff1d6#TrustingDebugCa

e.g.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <debug-overrides>
    <trust-anchors>
        <certificates src="@raw/live_reload"/>
    </trust-anchors>
  </debug-overrides>
</network-security-config>

Also if you are working with a local api service for your app it is worth considering using a proxy.conf.json to avoid any similar issues with addressability/trust for your api.

@AlvaroLarumbe
Copy link

This might help someone...

For Android create a self signed cert for the host address you are using for live reload e.g.

IP="192.168.1.215"
NAME="live_reload"
openssl req \
-x509 \
-newkey rsa:4096 \
-sha256 \
-days 365 \
-nodes \
-keyout ssl/$NAME.key \
-out ssl/$NAME.crt \
-subj "/CN=${IP}" \
-extensions v3_ca \
-extensions v3_req \
-config <( \
  echo '[req]'; \
  echo 'default_bits= 4096'; \
  echo 'distinguished_name=req'; \
  echo 'x509_extension = v3_ca'; \
  echo 'req_extensions = v3_req'; \
  echo '[v3_req]'; \
  echo 'basicConstraints = CA:FALSE'; \
  echo 'keyUsage = nonRepudiation, digitalSignature, keyEncipherment'; \
  echo 'subjectAltName = @alt_names'; \
  echo '[ alt_names ]'; \
  echo "IP.1 = ${IP}"; \
  echo '[ v3_ca ]'; \
  echo 'subjectKeyIdentifier=hash'; \
  echo 'authorityKeyIdentifier=keyid:always,issuer'; \
  echo 'basicConstraints = critical, CA:TRUE, pathlen:0'; \
  echo 'keyUsage = critical, cRLSign, keyCertSign'; \
  echo 'extendedKeyUsage = serverAuth, clientAuth')

Use this when running ionic cap run e.g. ionic cap run android -l --external --ssl -- --ssl-cert ssl\live_reload.crt --ssl-key ssl\live_reload.key

At this point you still get a blank screen when running the emulator because Android doesn't trust this certificate.

Copy the live_reload.crt you created to the android raw resource folder

Create a network_security_config.xml file with debug-overrides as explained https://developer.android.com/training/articles/security-config?msclkid=44f9be68d04d11eca3865cb4143ff1d6#TrustingDebugCa

e.g.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <debug-overrides>
    <trust-anchors>
        <certificates src="@raw/live_reload"/>
    </trust-anchors>
  </debug-overrides>
</network-security-config>

Also if you are working with a local api service for your app it is worth considering using a proxy.conf.json to avoid any similar issues with addressability/trust for your api.

Thanks @graemeenglish

I'm using mkcert 192.168.1.153 for example and also works great if you want a simplified cert generation.

For people following this approach, you also need to add this to your AndroidManifest.xml into application node:

android:networkSecurityConfig="@xml/network_security_config"

Reference: https://developer.android.com/training/articles/security-config?msclkid=44f9be68d04d11eca3865cb4143ff1d6#manifest

wand1252 added a commit to wand1252/angular-toolkit-develop that referenced this issue Aug 31, 2022
@avalanche1
Copy link

Ionic 6 here. Still no out-of-the-box solution for running live-reload over https on a physical device...

@dpa99c
Copy link

dpa99c commented May 31, 2023

This got me 95% of the way: #3305 (comment)

To make it work with Vue/Ionic/Capacitor, the command I had to run is:

ionic cap run android -l --external -- --https

which in turn runs:

vue-cli-service.cmd serve --https 

so the Vue server runs in SSL mode.

I then had to create a vue.config.js to configure the certificate:

const fs = require('fs')

module.exports = {
    devServer: {
        https: {
            key: fs.readFileSync('./ssl/live_reload.key'),
            cert: fs.readFileSync('./ssl/live_reload.crt'),
        }
    }
}

@joaogabriel-ar
Copy link

hey, im still having the same problem trying to run ionic --ssl on IOS. Some features need the secure context (https), and i can't manage to bypass the certificate verification on ios. Anyone with the same issue still ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement project type: angular Ionic Angular 4+ projects project type: react Ionic React projects
Projects
None yet
Development

No branches or pull requests