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

[Bug]: Android ERR_CONNECTION_REFUSED when redirecting from external url back to https://localhost #7367

Open
1 of 3 tasks
atremel opened this issue Mar 25, 2024 · 8 comments
Labels

Comments

@atremel
Copy link

atremel commented Mar 25, 2024

Capacitor Version

Latest Dependencies:

@capacitor/cli: 5.7.4
@capacitor/core: 5.7.4
@capacitor/android: 5.7.4
@capacitor/ios: 5.7.4

Installed Dependencies:

@capacitor/cli: 5.7.3
@capacitor/core: 5.7.3
@capacitor/android: 5.7.3
@capacitor/ios: 5.7.3

[success] Android looking great! 👌

Other API Details

npm --version -> 10.2.1
node --version -> v18.18.0

Platforms Affected

  • iOS
  • Android
  • Web

Current Behavior

The app is redirecting to an external auth service. From there the user is redirected 302 back to https://localhost, where the browser blocks the request saying ERR_CONNECTION_REFUSED:

image

HTTPS is encouraged: "we would strongly recommend that you change your androidScheme to use https."

capacitor.config.ts looks like this:

import { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
  appId: 'com.somedomain.app',
  appName: 'App',
  webDir: 'dist/prod',
  server: {
    androidScheme: 'https',
    allowNavigation: [
      "login.somedomain.com"
    ]
  },
  plugins: {
    CapacitorHttp: {
      enabled: true,
    },
  },
};

export default config;

Expected Behavior

Redirect to https://localhost is successful.

Project Reproduction

https://github.com/atremel/redir

Additional Information

Real device has this issue, too.

@atremel atremel added the triage label Mar 25, 2024
@atremel
Copy link
Author

atremel commented Apr 19, 2024

Just posting an update - Ionic 6 now requires https by default:

image

Any updates on this issue? It really blocks us from migrating our native apps to Ionic / Capacitor...

@Benno08
Copy link

Benno08 commented May 16, 2024

We've got the same issue (with Capacitor 5/6). #5818 is about the same issue but was closed because set as duplicate of #4240 (by mistake to me, because is does not fix the issue at all as said by this comment #4240 (comment)).
Redirecting from an external URL to localhost on Android doesn't work. PR here about the same issue was not considered either: #5872

Might be related to this one (with Capacitor 6 example) as well: #7454

@terrencebob
Copy link

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    Uri url = request.getUrl();
     if(url.getHost().equalsIgnoreCase(bridge.getHost())){
      view.loadUrl(urlString);
      return false;
    }
    return bridge.launchIntent(url);
}
you may edit shouldOverrideUrlLoading like this,view.loadUrl(urlString)can reload url as expected behavior

@atremel
Copy link
Author

atremel commented Aug 13, 2024

Hi @terrencebob,

Thanks for getting back to me. You are referring to overload android/capacitor/src/main/java/com/getcapacitor/BridgeWebViewClient.java, is that correct?

Is there any way to do this without forking the repo? Or, would it be possible to integrate this into the release? I'm not sure if we are able maintain a custom fork...

Thank you!

Best,
Andreas

@nirhu
Copy link

nirhu commented Sep 5, 2024

I just spent a couple of hours trying to resolve this, I found this #2391 where it seems like a lot of people have struggled with it only for it to be closed by this #7199 which did not seem to fix it and yet somehow nobody complained, until I found this bug.

So I am a little confused, was it working at any point in time and is this a regression or people just moved on to something else?

@atremel
Copy link
Author

atremel commented Sep 9, 2024

This really blocks us - we have not moved away but will continue to use our existing native app.

@wallstudios
Copy link

I'm facing the same issue. Any updates?
I've tried a lot of combinations but nothing will let the redirect reload the app. If I do a Oauth request it then fails on the ERR_CONNECTION_REFUSED. If I reload the app it then works. So the Oauth data/cookies etc are being set just the page is being stopped from loading.

I've tried setting
hostname
androidScheme

But no difference.

@wallstudios
Copy link

wallstudios commented Sep 24, 2024

I can confirm that the suggestion by @terrencebob works.

change below in BridgeWebViewClient.

From:
@OverRide
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Uri url = request.getUrl();
return bridge.launchIntent(url);
}

To:
@OverRide
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Uri url = request.getUrl();
if(url.getHost().equalsIgnoreCase(bridge.getHost())){
view.loadUrl(url.toString());
return false;
}
return bridge.launchIntent(url);
}

Can we get this change implemented ASAP?

FYI.
Ionic:

Ionic CLI : 7.2.0
Ionic Framework : @ionic/angular 8.3.1
@angular-devkit/build-angular : 17.3.9
@angular-devkit/schematics : 16.2.16
@angular/cli : 17.3.9
@ionic/angular-toolkit : 9.0.0

Capacitor:

Capacitor CLI : 6.1.2
@capacitor/android : 6.1.2
@capacitor/core : 6.1.2
@capacitor/ios : 6.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants