-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
jsonp-polling.js
116 lines (105 loc) · 2.79 KB
/
jsonp-polling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
const ResponseReceiver = require('./response-receiver');
const Session = require('../session');
const middleware = require('../middleware');
class JsonpReceiver extends ResponseReceiver {
constructor(req, res, options, callback) {
super(req, res, options);
this.protocol = 'jsonp-polling';
this.max_response_size = 1;
this.callback = callback;
}
sendFrame(payload) {
// Yes, JSONed twice, there isn't a a better way, we must pass
// a string back, and the script, will be evaled() by the
// browser.
// prepend comment to avoid SWF exploit #163
return super.sendFrame(`/**/${this.callback}(${JSON.stringify(payload)});\r\n`);
}
}
function jsonp(req, res, _head, next) {
if (!('c' in req.query || 'callback' in req.query)) {
return next({
status: 500,
message: '"callback" parameter required'
});
}
const callback = 'c' in req.query ? req.query['c'] : req.query['callback'];
if (/[^a-zA-Z0-9-_.]/.test(callback) || callback.length > 32) {
return next({
status: 500,
message: 'invalid "callback" parameter'
});
}
// protect against SWF JSONP exploit - #163
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('Content-Type', 'application/javascript; charset=UTF-8');
res.writeHead(200);
Session.register(req, this, new JsonpReceiver(req, res, this.options, callback));
next();
}
function jsonp_send(req, res, _head, next) {
if (!req.body) {
return next({
status: 500,
message: 'Payload expected.'
});
}
let d;
if (typeof req.body === 'string') {
try {
d = JSON.parse(req.body);
} catch (x) {
return next({
status: 500,
message: 'Broken JSON encoding.'
});
}
} else {
d = req.body.d;
}
if (typeof d === 'string' && d) {
try {
d = JSON.parse(d);
} catch (x) {
return next({
status: 500,
message: 'Broken JSON encoding.'
});
}
}
if (!d || d.__proto__.constructor !== Array) {
return next({
status: 500,
message: 'Payload expected.'
});
}
const jsonp = Session.bySessionId(req.session);
if (jsonp === null) {
return next({ status: 404, message: 'session not found' });
}
for (const message of d) {
jsonp.didMessage(message);
}
res.setHeader('Content-Length', '2');
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
res.writeHead(200);
res.end('ok');
next();
}
module.exports = {
routes: [
{
method: 'GET',
path: '/jsonp',
handlers: [middleware.h_sid, middleware.h_no_cache, jsonp],
transport: true
},
{
method: 'POST',
path: '/jsonp_send',
handlers: [middleware.h_sid, middleware.h_no_cache, middleware.expect_form, jsonp_send],
transport: true
}
]
};