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: CapacitorCookieManager (Android) is initialized incorrectly since capacitor version 4.7.3 #6467

Closed
chrste90 opened this issue Apr 5, 2023 · 7 comments
Labels
needs reproduction needs reproducible example to illustrate the issue platform: android

Comments

@chrste90
Copy link

chrste90 commented Apr 5, 2023

Bug Report

Capacitor Version

Latest Dependencies:

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

Installed Dependencies:

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

Platform(s)

Android

Current Behavior

When the app starts, CapacitorCookieManager is initialized by CapacitorCookies-Plugin. Since @capacitor/[email protected], the localUrl and serverUrl are set in the constructor of CapacitorCookieManager

public CapacitorCookieManager(CookieStore store, CookiePolicy policy, Bridge bridge) {
    super(store, policy);
    webkitCookieManager = android.webkit.CookieManager.getInstance();
    this.localUrl = bridge.getLocalUrl();
    this.serverUrl = bridge.getServerUrl();
}

The problem is, that the bridge returns null for localUrl at this point, so when setCookie(getDomainFromCookieString(headerValue), headerValue); inside the put-Method is called, getDomainFromCookieString(headerValue) returns null if the cookie doesn't set a domain.

Expected Behavior

getDomainFromCookieString(headerValue) should return http://localhost instead of null.

Code Reproduction

Create simple ionic app with https://github.com/silkimen/cordova-plugin-advanced-http and call a url which sets a cookie similar to: sessionid=XXX; Expires=Wed Apr 05 2023 10:51:33 GMT+0200 (CEST); SameSite=Lax; Secure; HttpOnly

Other Technical Details

The bug was introduced by this PR: #6448

Additional Context

As a workaround, it's possible to add this to the MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CapacitorCookieManager cookieManager = new CapacitorCookieManager(null, java.net.CookiePolicy.ACCEPT_ALL, this.bridge);
    CookieHandler.setDefault(cookieManager);
}

It looks like the bridge returns the correct URL if called later.

@EzeFSpa
Copy link

EzeFSpa commented Apr 7, 2023

I can confirm this behavior when i need to call an API returning a "set-cookie" header, then this happens:

E/Capacitor/Plugin: null
    java.lang.NullPointerException
        at java.util.Objects.requireNonNull(Objects.java:220)
        at uw0.<init>(Unknown Source:3)
        at Co.a(Unknown Source:2)
        at Co.setCookie(chromium-TrichromeWebViewGoogle6432.apk-stable-463807437:2)
        at com.getcapacitor.plugin.CapacitorCookieManager.setCookie(CapacitorCookieManager.java:123)
        at com.getcapacitor.plugin.CapacitorCookieManager.put(CapacitorCookieManager.java:176)
        at com.android.okhttp.internal.http.HttpEngine.receiveHeaders(HttpEngine.java:891)
        at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:625)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:475)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
        at com.getcapacitor.plugin.util.CapacitorHttpUrlConnection.getResponseCode(CapacitorHttpUrlConnection.java:251)
        at com.getcapacitor.plugin.util.HttpRequestHandler.buildResponse(HttpRequestHandler.java:206)
        at com.getcapacitor.plugin.util.HttpRequestHandler.request(HttpRequestHandler.java:414)
        at com.getcapacitor.plugin.CapacitorHttp$1.run(CapacitorHttp.java:34)
        at java.lang.Thread.run(Thread.java:920)

@EzeFSpa
Copy link

EzeFSpa commented Apr 11, 2023

Still no news on this? I will have to avoid the use of cookies for now until this is solved :(

@jcesarmobile jcesarmobile added the needs reproduction needs reproducible example to illustrate the issue label Apr 11, 2023
@Ionitron
Copy link
Collaborator

This issue may need more information before it can be addressed. In particular, it will need a reliable Code Reproduction that demonstrates the issue.

Please see the Contributing Guide for how to create a Code Reproduction.

Thanks!
Ionitron 💙

@Ionitron Ionitron added the needs reply needs reply from the user label Apr 11, 2023
@Vansinnesvisor
Copy link

Looks like a related issue #6479
image

There is no error when using this property in the capacitor.config.ts

server: {
    url: "http://192.168.0.10:3000",
    cleartext: true,
    androidScheme: "https",
    iosScheme: "https",
    allowNavigation: ["*"]
},

But for production I've to comment out this code. And after that simple POST requests stop working.

@Ionitron Ionitron removed the needs reply needs reply from the user label Apr 12, 2023
@jcesarmobile jcesarmobile added the needs reply needs reply from the user label Apr 12, 2023
@chrste90
Copy link
Author

Check out https://github.com/chrste90/capacitor-http-cookie-demo

npm i

npm start

Click "call backend" on an android device

You can see the following error via logcat:

Capacitor/Plugin        com.example.app                      E  Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
                                                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
                                                                                                    	at cy0.<init>(chromium-TrichromeWebViewGoogle6432.aab-stable-556311634:4)
                                                                                                    	at com.android.webview.chromium.a.setCookie(chromium-TrichromeWebViewGoogle6432.aab-stable-556311634:4)
                                                                                                    	at com.getcapacitor.plugin.CapacitorCookieManager.setCookie(CapacitorCookieManager.java:123)
                                                                                                    	at com.getcapacitor.plugin.CapacitorCookieManager.put(CapacitorCookieManager.java:176)
                                                                                                    	at com.android.okhttp.internal.http.HttpEngine.receiveHeaders(HttpEngine.java:891)
                                                                                                    	at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:625)
                                                                                                    	at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:475)
                                                                                                    	at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
                                                                                                    	at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
                                                                                                    	at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
                                                                                                    	at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
                                                                                                    	at com.getcapacitor.plugin.util.CapacitorHttpUrlConnection.getResponseCode(CapacitorHttpUrlConnection.java:251)
                                                                                                    	at com.getcapacitor.plugin.util.HttpRequestHandler.buildResponse(HttpRequestHandler.java:206)
                                                                                                    	at com.getcapacitor.plugin.util.HttpRequestHandler.request(HttpRequestHandler.java:411)
                                                                                                    	at com.getcapacitor.plugin.CapacitorHttp$1.run(CapacitorHttp.java:34)
                                                                                                    	at java.lang.Thread.run(Thread.java:1012)

I wrote already a detailed description why this is happening in my initial post.

@jcesarmobile
Copy link
Member

fixed in #6509

@ionitron-bot
Copy link

ionitron-bot bot commented May 18, 2023

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators May 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs reproduction needs reproducible example to illustrate the issue platform: android
Projects
None yet
Development

No branches or pull requests

5 participants