ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
12.16k stars 1.01k forks source link

bug: Cookie does not become available to other clients / transports #5943

Open WillooWisp opened 2 years ago

WillooWisp commented 2 years ago

Bug Report

Capacitor Version

Capacitor Doctor   

Latest Dependencies:

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

Installed Dependencies:

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

[error] Missing <manifest package=""> attribute in app/src/main

Platform(s)

Android (not tested on iOS yet)

Current Behavior

Using a library like SignalR from Microsoft that out of the box supports cookie authentication when performing web socket calls does not seem to work on devices, but it works in browser on PC. Before Capacitor 4.3 we got CORS problems, but now we get passed this and the problem is the authentication errors when performing the web socket calls.

Error: Failed to invoke 'subscribe' because user is unauthorized
    at _callbacks.<computed> (HubConnection.js:308:36)
    at HubConnection._processIncomingData (HubConnection.js:420:33)

Expected Behavior

Expects authenticated web socket calls to just work, since cookie should be passed on.

Code Reproduction

this.hubConnection = new HubConnectionBuilder()
  .withAutomaticReconnect(new InfiniteRetryPolicy())
  .configureLogging(LogLevel.Information)
  .withUrl(`${this.host.endsWith('/') ? this.host : `${this.host}/`}signalrhub`)
.build();

this.subscribedNodes = [];
await this.subscribeToNodes(nodes);
});

await this.hubConnection.start();
await this.hubConnection.invoke('subscribe'); // Fails on devices when authentication is required on the SignalR Hub in backend

Other Technical Details

"@microsoft/signalr": "~6.0.9"

npm --version output: 8.11.0 yarn version output: v1.22.19 node --version output: v16.16.0

Additional Context

plugins: {
        pushNotifications: {
            presentationOptions: ['badge', 'sound', 'alert'],
        },
        SplashScreen: {
            launchAutoHide: false,
            backgroundColor: '#00000000',
            androidSplashResourceName: 'splash',
            androidScaleType: 'FIT_CENTER',
            showSpinner: true,
            androidSpinnerStyle: 'large',
            iosSpinnerStyle: 'small',
            spinnerColor: '#999999',
            splashFullScreen: false,
            splashImmersive: false,
            layoutName: 'launch_screen',
            useDialog: false
          },
          CapacitorCookies: {
            enabled: true
          },
          CapacitorHttp: {
            enabled: true
          }
    }
nicklasuhde commented 2 years ago

I have the same issue!

ItsChaceD commented 1 year ago

Can you please test with this dev release and see if the problem still occurs: 4.5.1-dev-20221117T024619.0

npm i @capacitor/android@4.5.1-dev-20221117T024619.0
npm i @capacitor/core@4.5.1-dev-20221117T024619.0
npm i @capacitor/ios@4.5.1-dev-20221117T024619.0
WillooWisp commented 1 year ago

On Android I now get this...

WebSocket connection to 'wss://<host>/signalrhub?id=<id>' failed: HTTP Authentication failed; no valid credentials available
(anonymous) @ WebSocketTransport.js:49
Utils.js:155 [2022-11-17T10:48:03.918Z] Information: (WebSockets transport) There was an error with the transport.
Utils.js:149 

       [2022-11-17T10:48:03.919Z] Error: Failed to start the transport 'WebSockets': Error: WebSocket failed to connect. The connection could not be found on the server, either the endpoint may not be a SignalR endpoint, the connection ID is not present on the server, or there is a proxy blocking WebSockets. If you have multiple servers check that sticky sessions are enabled.
log @ Utils.js:149
(anonymous) @ HttpConnection.js:300
notifications:246 native CapacitorHttp.request (#16338023)
notifications:246 native App.addListener (#16338024)
notifications:220 result CapacitorHttp.request (#16338023)
notifications:1 

       Access to resource at 'https://<host>/signalrhub?id=<id>' from origin 'http://192.168.32.85:8100' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Utils.js:149 

       [2022-11-17T10:48:04.120Z] Error: Failed to start the transport 'ServerSentEvents': Error: EventSource failed to connect. The connection could not be found on the server, either the connection ID is not present on the server, or a proxy is refusing/buffering the connection. If you have multiple servers check that sticky sessions are enabled.
ItsChaceD commented 1 year ago

Thanks for all of the information @WillooWisp ! Are you able to provide a project or repo for me to test against? I have a general idea of what may be causing this, but I'd like to do some further discovery & I don't have a server set up for this specific use case.