-
Notifications
You must be signed in to change notification settings - Fork 569
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 with ready fix!) "OPTIONS" request returns 400 (Bad Request) and fails to respond proper CORS headers #574
Comments
Hi! I'm not sure what's the best way to fix that. Couldn't you use the const io = require('socket.io')(server, {
handlePreflightRequest: (req, res) => {
res.writeHead(200, {
'Access-Control-Allow-Headers': 'Authorization',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Origin': 'null', // served from file system
'Access-Control-Allow-Credentials': true
});
res.end();
}
}); |
This worked perfectly, thanks! |
options requests should be allowed before the handshake since the purpose of the options request is to ask if I am allowed to perform the handshake request |
Please note that the const { Server } = require('engine.io');
// before
new Server({
handlePreflightRequest: (req, res) => {
res.writeHead(200, {
"Access-Control-Allow-Origin": 'https://example.com',
"Access-Control-Allow-Methods": 'GET,POST',
"Access-Control-Allow-Headers": 'Authorization',
"Access-Control-Allow-Credentials": true
});
res.end();
}
});
// after
new Server({
cors: {
origin: "https://example.com",
methods: ["GET","POST"],
allowedHeaders: ["Authorization"],
credentials: true
}
}); |
Thank you, this saved my life. |
You want to:
Current behaviour
My situation is this:
Thus, the browser performs an "OPTIONS" pre-flight request.
The "OPTIONS" pre-flight request fails in two ways:
while it should have taken the header's value from the request-header "Access-Control-Request-Headers"
This relates to bug #279.
Steps to reproduce
$ npm install
$ npm start
Just open (double-click) the file "client-demo.html" in any modern browser
Expected behaviour
Socket.io should connect...
Setup
Other information (e.g. stacktraces, related issues, suggestions how to fix)
I have made a fix, and tested it. There are two fixes in engine.io:
Fix 1: engine.io/lib/transports/polling-xhr.js in XHR.prototype.onRequest
Replace this line:
headers['Access-Control-Allow-Headers'] = 'Content-Type';
with this:
const accessControlRequestHeaders = req.headers['access-control-request-headers'];
if (accessControlRequestHeaders) headers['Access-Control-Allow-Headers'] = accessControlRequestHeaders;
Fix 2: engine.io/lib/server.js in Server.prototype.verify
Replace this line:
if ('GET' !== req.method) return fn(Server.errors.BAD_HANDSHAKE_METHOD, false);
with this:
if (('GET' !== req.method) && ('OPTIONS' !== req.method)) return fn(Server.errors.BAD_HANDSHAKE_METHOD, false);
The text was updated successfully, but these errors were encountered: