-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
CORS works even if it is not enabled #5148
Comments
Hi! CORS only applies to HTTP long-polling, but you use WebSocket first: Note: the "flashsocket" transport does not exist anymore. |
Hi, thank you for your answer. When I used the default transport method, the connection failed even though I defined the origin correctly. From what I read, it was caused by "the default transportation method is not always allowed by all servers": Stackoverflow Can you confirm that the error was caused by an unsupported transport method and not an error with the cors configuration? |
The Stackoverflow post uses an old version of the const io = new Server({
cors: {
origin: ["http://localhost:8000"]
}
}); Reference: https://socket.io/docs/v4/handling-cors/ |
Thats the problem. It doesnt work even though I set this origin correctly. Access` to XMLHttpRequest at 'http://localhost:3000/socket.io/?EIO=4&transport=polling&t=P3kAbNS' from origin 'http://localhost:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. const io = new Server({ i tried it also without http |
Hmm, that's weird... Could you please check if you are able to reach the Socket.IO server with:
It should return something like:
Reference: https://socket.io/docs/v4/troubleshooting-connection-issues/ |
My server is running on port 3000 and I'm trying to connect from 8000, so I modified the command to include those addresses.
it returned me this:
|
and it returns the same thing when I try to set the origin to not allowed in the command |
Also I dont see any cors header in network tab in devtools |
@darrachequesne I tried adding the cors header manually and it works then, could there be a problem with adding the headers? Can you please check my nuxt socket.io plugin code to see if I have a misconfigured server? Or is it possible that there is a bug in socket.io itself?
|
To summarize, while both configurations handle CORS, they do so at different stages and for different purposes. The origin option in the Server constructor controls whether the server will accept connections from a given origin, and the Access-Control-Allow-Origin header in the response ensures that the browser will accept the response from the server. So they are not same things and socket.io initaly not offering to send header to browser when you give origin |
Describe the bug
I am using socket.io in nuxt. Even though I didn't enable cors, it is possible to connect from a different origin than the one nuxt is running on. E.g. it runs on localhost:3000 and I can connect from localhost:8000. Is this a bug or is it my fault?
To Reproduce
Please fill the following code example:
Socket.IO server version:
^4.7.5
Server
Socket.IO client version:
^4.7.5
Client
Expected behavior
It will not be possible to connect from an origin other than the one defined or on which nuxt is running.
Platform:
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: