-
-
Notifications
You must be signed in to change notification settings - Fork 588
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
How to resolve multiple CORS values in socketio requests, Sanic? #205
Comments
I'll have to check this, but it appears that Sanic is reporting the origin sent by the client two times in a row. Not sure if there is some sort of collision with the sanic-cors package or if it is something else, but that seems to be the root cause. |
Hi, thank you for the quick response. It might be obvious in retrospect but the order of placement of the decorator, however, was important in my case as I have The |
+1 |
Thank you @RuWander , your solution worked perfectly |
In sanic, the cors was applied to all connections, even if the socketio connections has there own cors settings set. So there was a duplicate values one from the socketio default and one from the sanic_cors. |
I was just looking at this issue (sorry for taking so long!). Since you guys have CORS set up on the Sanic side, wouldn't it make sense to disable CORS support on the Socket.IO side? You can do that by passing Let me know if that works! |
@miguelgrinberg I tried by setting
I have also tried setting |
I finally spent some time investigating this issue. A working Sanic configuration when using the sanic-cors extension is below: app.config['CORS_AUTOMATIC_OPTIONS'] = True
app.config['CORS_SUPPORTS_CREDENTIALS'] = True On the Socket.IO side, the server must be initialized to not do anything related to CORS: eio = engineio.AsyncServer(async_mode='sanic', cors_allowed_origins=[]) With this configuration, CORS is 100% controlled by Sanic. Hope this helps. |
The above has been added to the documentation. |
@miguelgrinberg I am under the impression that this workaround is broken since the latest release of python-engineio (miguelgrinberg/python-engineio@7548f70). I haven't had time to investigate deeper yet, but this is the only thing that seemed to have changed in the last days |
@salimaboubacar It's not that it broke, python-engineio had to be changed to address security issues with the WebSocket protocol. If you want the old behavior, you can add |
In general yes, unfortunately, doing that reproduces the exact symptoms of this issue (multiple values for Access-Control-Allow-Origin) |
Yes, I just tested it here and you are correct, using |
@salimaboubacar can I ask you to test the master branch of python-engineio? Let me know if the problem is addressed with this fix or not. |
Sure, will do tomorrow :) |
In case others struggle to get things working, I've just had what appears to be exactly this issue using Sanic which was finally solved by specifying
as discussed here. BTW this was with Chrome; FF didn't have these issues. |
(Dropping this here in case this helps others) I was experiencing exactly this symptom using fastapi-socketio - multiple
I applied @dbrnz's |
Hi,
I am having trouble with CORS in python-socketio. I have built a Restful API in Sanic and I am consuming the API with Angular 6. I have added socketio functionality with Python-socketio, however, I am receiving the following error in the browser:
Failed to load http://192.168.33.11:7770/socket.io/?EIO=3&transport=polling&t=MPP6E-6: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:4200, http://localhost:4200', but only one is allowed. Origin 'http://localhost:4200' is therefore not allowed access.
It seems to be adding two origins to the Access-Control-Allow-Origin header.
I have tried setting the cors_allowed_origins to a list containing the http://localhost:4200 but that did not work.
I also had to install the Sanic_Cors (https://github.com/ashleysommer/sanic-cors) module in order to allow CORS for the normal API routes. If I remove the CORS(app) from the below configuration the socket connections seem to be working.
My server currently looks like this:
sio = socketio.AsyncServer(async_mode='sanic', cors_credentials=True)
app = Sanic()
app.config['CORS_AUTOMATIC_OPTIONS'] = True
sio.attach(app)
CORS(app)
Is there a way to have these two modules work in harmony, or to fix the origin header in the Socketio module?
I hope you can help me.
I appreciate any help in advance!
Thanks!
The text was updated successfully, but these errors were encountered: