Skip to content

Commit f72f6f3

Browse files
[fix] allowRequest failures now return 403 Forbidden (#452)
1 parent e144dc1 commit f72f6f3

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/server.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,16 @@ Server.errors = {
7070
UNKNOWN_TRANSPORT: 0,
7171
UNKNOWN_SID: 1,
7272
BAD_HANDSHAKE_METHOD: 2,
73-
BAD_REQUEST: 3
73+
BAD_REQUEST: 3,
74+
FORBIDDEN: 4
7475
};
7576

7677
Server.errorMessages = {
7778
0: 'Transport unknown',
7879
1: 'Session ID unknown',
7980
2: 'Bad handshake method',
80-
3: 'Bad request'
81+
3: 'Bad request',
82+
4: 'Forbidden'
8183
};
8284

8385
/**
@@ -242,6 +244,15 @@ Server.prototype.handleRequest = function (req, res) {
242244
function sendErrorMessage (req, res, code) {
243245
var headers = { 'Content-Type': 'application/json' };
244246

247+
var isForbidden = !Server.errorMessages.hasOwnProperty(code);
248+
if (isForbidden) {
249+
res.writeHead(403, headers);
250+
res.end(JSON.stringify({
251+
code: Server.errors.FORBIDDEN,
252+
message: code || Server.errorMessages[Server.errors.FORBIDDEN]
253+
}));
254+
return;
255+
}
245256
if (req.headers.origin) {
246257
headers['Access-Control-Allow-Credentials'] = 'true';
247258
headers['Access-Control-Allow-Origin'] = req.headers.origin;

test/server.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ describe('server', function () {
7676
});
7777
});
7878
});
79+
80+
it('should disallow requests that are rejected by `allowRequest`', function (done) {
81+
listen({ allowRequest: function (req, fn) { fn('Thou shall not pass', false); } }, function (port) {
82+
request.get('http://localhost:%d/engine.io/default/'.s(port))
83+
.set('Origin', 'http://engine.io')
84+
.query({ transport: 'polling' })
85+
.end(function (res) {
86+
expect(res.status).to.be(403);
87+
expect(res.body.code).to.be(4);
88+
expect(res.body.message).to.be('Thou shall not pass');
89+
expect(res.header['access-control-allow-credentials']).to.be(undefined);
90+
expect(res.header['access-control-allow-origin']).to.be(undefined);
91+
done();
92+
});
93+
});
94+
});
7995
});
8096

8197
describe('handshake', function () {

0 commit comments

Comments
 (0)