socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

No CORS headers are set on the client javascript file

Open aeons opened this issue 5 years ago • 8 comments

You want to:

  • [x] report a bug
  • [ ] request a feature

Current behaviour

When serving the client via settings the serveClient option, the client is not served with the same CORS headers as the actual socket.io server.

Steps to reproduce (if the current behaviour is a bug)

Start a server with serveClient: true and request /socket.io/socket.io.js with an Origin header set.

Observe that the response does not have any Access-Control-Allow-Origin header set.

Expected behaviour

That the response has the same CORS headers as the normal operation.

Other information (e.g. stacktraces, related issues, suggestions how to fix)

This function (and the corresponding serveMap) should do the same origin checking as this code.

aeons avatar Feb 14 '20 13:02 aeons

Is it me or CORS don't have to be "set" on client side... you have to tell the server which cors will be accepted.

Here's my implementation to allow all in typescript

this.app.use(cors({
    origin: '*',
    optionsSuccessStatus: 200,
}))

the browser is responsible for indicating the origin domain and you have no control over that for security reasons...

I might be mistaken..

Also, even if, this should probably go on the socket.io-client github :)

eltharynd avatar Feb 20 '20 10:02 eltharynd

The Access-Control-Allow-Origin header is a response header from the server, indicating which origins are allowed to use the resource. Browsers respect this header.

If you set the serveClient option in socket.io, it serves the javascript of the client from the socket.io server. However, the function(s) responsible for doing this, does not set the Access-Control-Allow-Origin header, which means that if you try to request the client javascript in a cross-origin environment, the request fails.

The correct headers are applied to the actual socket.io websocket connection, so I'm suggesting that the same logic is applied to the served client sources.

aeons avatar Feb 20 '20 12:02 aeons

I also stumble over the same issue and yes CORS needs to be set by the server also from my knowledge ;-) Can you do a PR? ;-)

Apollon77 avatar Feb 23 '20 18:02 Apollon77

I was able to fix my CORS issues using the handlePreflightRequest referenced in https://github.com/socketio/engine.io/issues/279#issuecomment-585578251 but I couldn't use the latest changes implemented for engine.io v4 in socket.io (2.3.0).

Keepcase avatar Jun 22 '20 03:06 Keepcase

@Keepcase sorry but I do not understand it ... were yiu able to fix it or not? ;-) Or do you mean that you know how/where to fix but in engine.io and noch in socket.io?! Or what? What would be needed to be patched?

Apollon77 avatar Jun 22 '20 07:06 Apollon77

My apologies @Apollon77, let me clarify. Perhaps I'm misunderstanding the issue but I was suggesting to try the change that I referenced on the socket.io server to see if that fixed the issue @aeons was running into.

I recently ran into issues with CORS and the referenced comment fixed it for my use case. I was getting errors on the browser similar to https://github.com/socketio/engine.io/issues/574 but I fixed it with the code referenced in https://github.com/socketio/engine.io/issues/574#issuecomment-476500527 which I implemented on the socket.io server.

I was just mentioning that the latest changes made in https://github.com/socketio/engine.io/issues/279#issuecomment-585578251 referencing cors was not working for socket.io (2.3.0).

Keepcase avatar Jun 22 '20 21:06 Keepcase

Can I work on it @darrachequesne

bilalsha avatar Jan 24 '22 14:01 bilalsha

@bilalsha sure!

darrachequesne avatar Jan 24 '22 22:01 darrachequesne

Has the issue been resolved, if not, I would like to work on it.

pushkarsingh019 avatar Jan 16 '23 09:01 pushkarsingh019

I assume it is still there :-(

Apollon77 avatar Jan 16 '23 10:01 Apollon77

@darrachequesne issue is still there ?

hritik-dubey avatar May 09 '23 13:05 hritik-dubey

This should be fixed by https://github.com/socketio/socket.io/commit/63f181cc12cbbbf94ed40eef52d60f36a1214fbe, included in version 4.7.0.

Please reopen if needed.

darrachequesne avatar Jun 23 '23 07:06 darrachequesne