This repository has been archived by the owner on May 10, 2019. It is now read-only.
/
wsapi.js
353 lines (302 loc) · 12.6 KB
/
wsapi.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// an abstraction that implements all of the cookie handling, CSRF protection,
// etc of the wsapi. This module also routes request to the approriate handlers
// underneath wsapi/
//
// each handler under wsapi/ supports the following exports:
// exports.process - function(req, res) - process a request
// exports.writes_db - must be true if the processing causes a database write
// exports.method - either 'get' or 'post'
// exports.authed - whether the wsapi requires authentication
// exports.args - an array of arguments that should be verified
// exports.i18n - boolean, does this operation display user facing strings
const
sessions = require('client-sessions'),
express = require('express');
secrets = require('./secrets'),
config = require('./configuration'),
logger = require('./logging.js').logger,
httputils = require('./httputils.js'),
forward = require('./http_forward.js'),
url = require('url'),
fs = require('fs'),
path = require('path'),
validate = require('./validate'),
statsd = require('./statsd');
bcrypt = require('./bcrypt');
i18n = require('./i18n');
var abide = i18n.abide({
supported_languages: config.get('supported_languages'),
default_lang: config.get('default_lang'),
locale_directory: config.get('locale_directory')
});
const COOKIE_SECRET = secrets.hydrateSecret('browserid_cookie', config.get('var_path'));
var COOKIE_KEY = 'browserid_state';
// to support testing of browserid, we'll add a hash fragment to the cookie name for
// sites other than browserid.org. This is to address a bug in IE, see issue #296
if (config.get('public_url').indexOf('https://browserid.org') !== 0) {
const crypto = require('crypto');
var hash = crypto.createHash('md5');
hash.update(config.get('public_url'));
COOKIE_KEY += "_" + hash.digest('hex').slice(0, 6);
}
logger.info('session cookie name is: ' + COOKIE_KEY);
function clearAuthenticatedUser(session) {
session.reset(['csrf']);
}
function isAuthed(req, requiredLevel) {
if (req.session && req.session.userid && req.session.auth_level) {
// 'password' authentication allows access to all apis.
// 'assertion' authentication, grants access to only those apis
// that don't require 'password'
if (requiredLevel === 'assertion' || req.session.auth_level === 'password') {
return true;
}
}
return false;
}
function bcryptPassword(password, cb) {
var startTime = new Date();
bcrypt.encrypt(config.get('bcrypt_work_factor'), password, function() {
var reqTime = new Date - startTime;
statsd.timing('bcrypt.encrypt_time', reqTime);
cb.apply(null, arguments);
});
};
function authenticateSession(session, uid, level) {
if (['assertion', 'password'].indexOf(level) === -1)
throw "invalid authentication level: " + level;
// if the user is *already* authenticated as this uid with an equal or better
// level of auth, let's not lower them. Issue #1049
if (session.userid === uid && session.auth_level === 'password' &&
session.auth_level !== level) {
logger.info("not resetting cookies to 'assertion' authenticate a user who is already password authenticated");
} else {
session.userid = uid;
session.auth_level = level;
}
}
function checkPassword(pass) {
if (!pass || pass.length < 8 || pass.length > 80) {
return "valid passwords are between 8 and 80 chars";
}
}
function langContext(req) {
return {
lang: req.lang,
locale: req.locale,
gettext: req.gettext,
ngettext: req.ngettext,
format: req.format
};
}
function databaseDown(res, err) {
logger.warn('database is down, cannot process request: ' + err);
httputils.serviceUnavailable(res, "database unavailable");
}
// common functions exported, for use by different api calls
exports.clearAuthenticatedUser = clearAuthenticatedUser;
exports.isAuthed = isAuthed;
exports.bcryptPassword = bcryptPassword;
exports.authenticateSession = authenticateSession;
exports.checkPassword = checkPassword;
exports.fowardWritesTo = undefined;
exports.langContext = langContext;
exports.databaseDown = databaseDown;
exports.setup = function(options, app) {
const WSAPI_PREFIX = '/wsapi/';
// all operations that are being forwarded
var forwardedOperations = [];
// If externally we're serving content over SSL we can enable things
// like strict transport security and change the way cookies are set
const overSSL = (config.get('scheme') == 'https');
var cookieParser = express.cookieParser();
var bodyParser = express.bodyParser();
// stash our forward-to url so different wsapi handlers can use it
exports.fowardWritesTo = options.forward_writes;
var cookieSessionMiddleware = sessions({
secret: COOKIE_SECRET,
cookieName: COOKIE_KEY,
duration: 7 * 24 * 60 * 60 * 1000, // 1 week
cookie: {
path: '/wsapi',
httpOnly: true,
// IMPORTANT: we allow users to go 1 weeks on the same device
// without entering their password again
maxAge: config.get('authentication_duration_ms'),
secure: overSSL
}
});
app.use(function(req, resp, next) {
var purl = url.parse(req.url);
// cookie sessions are only applied to calls to /wsapi
// as all other resources can be aggressively cached
// by layers higher up based on cache control headers.
// the fallout is that all code that interacts with sessions
// should be under /wsapi
if (purl.pathname.substr(0, WSAPI_PREFIX.length) === WSAPI_PREFIX)
{
// explicitly disallow caching on all /wsapi calls (issue #294)
resp.setHeader('Cache-Control', 'no-cache, max-age=0');
// we set this parameter so the connect-cookie-session
// sends the cookie even though the local connection is HTTP
// (the load balancer does SSL)
if (overSSL)
req.connection.proxySecure = true;
const operation = purl.pathname.substr(WSAPI_PREFIX.length);
// count the number of WSAPI operation
statsd.increment("wsapi." + operation);
// check to see if the api is known here, before spending more time with
// the request.
if (!wsapis.hasOwnProperty(operation) ||
wsapis[operation].method.toLowerCase() !== req.method.toLowerCase())
{
// if the fake verification api is enabled (for load testing),
// then let this request fall through
if (operation !== 'fake_verification' || !process.env['BROWSERID_FAKE_VERIFICATION'])
return httputils.badRequest(resp, "no such api");
}
// if this request is to be forwarded, we will not perform request validation,
// cookie parsing, nor body parsing - leaving that up to the process we're forwarding
// to.
if (-1 !== forwardedOperations.indexOf(operation)) {
// queue up the body here on and forward a single unchunked request onto the
// writer
return bodyParser(req, resp, function() {
next();
});
} else {
// this is not a forwarded operation, perform full parsing and validation
return cookieParser(req, resp, function() {
bodyParser(req, resp, function() {
cookieSessionMiddleware(req, resp, function() {
// only on POSTs
if (req.method === "POST") {
if (req.session === undefined || typeof req.session.csrf !== 'string') { // there must be a session
logger.warn("POST calls to /wsapi require a cookie to be sent, this user may have cookies disabled");
return httputils.forbidden(resp, "no cookie");
}
// and the token must match what is sent in the post body
else if (!req.body || !req.session || !req.session.csrf || req.body.csrf != req.session.csrf) {
// if any of these things are false, then we'll block the request
var b = req.body ? req.body.csrf : "<none>";
var s = req.session ? req.session.csrf : "<none>";
logger.warn("CSRF validation failure, token mismatch. got:" + b + " want:" + s);
return httputils.badRequest(resp, "CSRF violation");
}
}
return next();
});
});
});
}
} else {
return next();
}
});
// load all of the APIs supported by this process
var wsapis = { };
function describeOperation(name, op) {
var str = " " + name + " (";
str += op.method.toUpperCase() + " - ";
str += (op.authed ? "" : "not ") + "authed";
if (op.args) {
str += " - " + op.args.join(", ");
}
str += ")";
logger.debug(str);
}
fs.readdirSync(path.join(__dirname, 'wsapi')).forEach(function (f) {
// skip files that don't have a .js suffix or start with a dot
if (f.length <= 3 || f.substr(-3) !== '.js' || f.substr(0,1) === '.') return;
var operation = f.substr(0, f.length - 3);
try {
var api = require(path.join(__dirname, 'wsapi', f));
// don't register read apis if we are configured as a writer, except for
// have_email which we'll register on dbwriter as well (see issue #1258).
// We explicitly allow have_email on the dbwriter
// so that monitoring software has a hook to test db connectivity
// (this will change in dev to an API specifically for this purpose).
if (options.only_write_apis && !api.writes_db && operation !== 'have_email') return;
wsapis[operation] = api;
// forward writes if options.forward_writes is defined
if (options.forward_writes && wsapis[operation].writes_db &&
!wsapis[operation].disallow_forward)
{
forwardedOperations.push(operation);
var forward_url = options.forward_writes + "/wsapi/" + operation;
wsapis[operation].process = function(req, res) {
forward(forward_url, req, res, function(err) {
if (err) {
logger.error("error forwarding '"+ operation +
"' request to '" + options.forward_writes + ":" + err);
httputils.serverError(res, "internal request forwarding error");
}
});
};
// XXX: disable validation on forwarded requests
// (we cannot perform this validation because we don't parse cookies
// nor post bodies on forwarded requests)
//
// at some point we'll want to improve our cookie parser and
// fully validate forwarded requests both at the intermediate
// hop (webhead) AND final destination (secure webhead)
delete api.args; // deleting args will cause arg validation to be skipped
api.authed = false; // authed=false will prevent us from checking auth status
}
// set up the argument validator
if (api.args) {
if (!Array.isArray(api.args)) throw "exports.args must be an array of strings";
wsapis[operation].validate = validate(api.args);
} else {
wsapis[operation].validate = function(req,res,next) { next(); };
}
} catch(e) {
var msg = "error registering " + operation + " api: " + e;
logger.error(msg);
throw msg;
}
});
// debug output - all supported apis
logger.debug("WSAPIs:");
Object.keys(wsapis).forEach(function(api) {
if (options.forward_writes && wsapis[api].writes_db) return;
describeOperation(api, wsapis[api]);
});
if (options.forward_writes) {
logger.debug("forwarded WSAPIs (to " + options.forward_writes + "):");
Object.keys(wsapis).forEach(function(api) {
if (wsapis[api].writes_db) {
describeOperation(api, wsapis[api]);
}
});
}
app.use(function(req, resp, next) {
var purl = url.parse(req.url);
if (purl.pathname.substr(0, WSAPI_PREFIX.length) === WSAPI_PREFIX) {
const operation = purl.pathname.substr(WSAPI_PREFIX.length);
// the fake_verification wsapi is implemented elsewhere.
if (operation == 'fake_verification') return next();
// at this point, we *know* 'operation' is valid API, give checks performed
// above
// does the request require authentication?
if (wsapis[operation].authed && !isAuthed(req, wsapis[operation].authed)) {
return httputils.badRequest(resp, "requires authentication");
}
// validate the arguments of the request
wsapis[operation].validate(req, resp, function() {
if (wsapis[operation].i18n) {
abide(req, resp, function () {
wsapis[operation].process(req, resp);
});
} else {
wsapis[operation].process(req, resp);
}
});
} else {
next();
}
});
};