2020-08-25 17:04:34 -04:00
|
|
|
const express = require('express');
|
|
|
|
const log4js = require('log4js');
|
|
|
|
const httpLogger = log4js.getLogger('http');
|
|
|
|
const settings = require('../../utils/Settings');
|
|
|
|
const hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks');
|
|
|
|
const ueberStore = require('../../db/SessionStore');
|
|
|
|
const stats = require('ep_etherpad-lite/node/stats');
|
|
|
|
const sessionModule = require('express-session');
|
|
|
|
const cookieParser = require('cookie-parser');
|
2012-02-25 13:38:09 +01:00
|
|
|
|
2020-08-28 01:49:26 -04:00
|
|
|
exports.checkAccess = (req, res, next) => {
|
2020-08-25 17:04:34 -04:00
|
|
|
const hookResultMangle = (cb) => {
|
2020-08-25 16:38:58 -04:00
|
|
|
return (err, data) => {
|
2012-04-19 16:04:03 +02:00
|
|
|
return cb(!err && data.length && data[0]);
|
2020-08-25 16:44:44 -04:00
|
|
|
};
|
|
|
|
};
|
2012-04-19 16:04:03 +02:00
|
|
|
|
2020-08-28 21:03:45 -04:00
|
|
|
// This may be called twice per access: once before authentication is checked and once after (if
|
|
|
|
// settings.requireAuthorization is true).
|
2020-08-25 17:04:34 -04:00
|
|
|
const authorize = (cb) => {
|
2012-12-05 14:04:48 +01:00
|
|
|
// Do not require auth for static paths and the API...this could be a bit brittle
|
|
|
|
if (req.path.match(/^\/(static|javascripts|pluginfw|api)/)) return cb(true);
|
2012-04-19 14:25:12 +02:00
|
|
|
|
2020-08-28 02:00:16 -04:00
|
|
|
if (req.path.toLowerCase().indexOf('/admin') !== 0) {
|
2012-04-19 14:25:12 +02:00
|
|
|
if (!settings.requireAuthentication) return cb(true);
|
|
|
|
if (!settings.requireAuthorization && req.session && req.session.user) return cb(true);
|
2012-02-25 13:38:09 +01:00
|
|
|
}
|
2012-04-19 14:25:12 +02:00
|
|
|
|
|
|
|
if (req.session && req.session.user && req.session.user.is_admin) return cb(true);
|
|
|
|
|
2020-08-26 11:26:53 -04:00
|
|
|
hooks.aCallFirst('authorize', {req, res, next, resource: req.path}, hookResultMangle(cb));
|
2020-08-25 16:44:44 -04:00
|
|
|
};
|
2012-04-19 14:25:12 +02:00
|
|
|
|
2012-04-19 16:04:03 +02:00
|
|
|
/* Authentication OR authorization failed. */
|
2020-08-25 17:04:34 -04:00
|
|
|
const failure = () => {
|
2020-08-26 11:26:53 -04:00
|
|
|
return hooks.aCallFirst('authFailure', {req, res, next}, hookResultMangle((ok) => {
|
2020-08-25 16:55:56 -04:00
|
|
|
if (ok) return;
|
2012-04-19 16:04:03 +02:00
|
|
|
/* No plugin handler for invalid auth. Return Auth required
|
|
|
|
* Headers, delayed for 1 second, if authentication failed
|
|
|
|
* before. */
|
|
|
|
res.header('WWW-Authenticate', 'Basic realm="Protected Area"');
|
|
|
|
if (req.headers.authorization) {
|
2020-08-25 16:38:58 -04:00
|
|
|
setTimeout(() => {
|
2015-04-10 05:52:58 -05:00
|
|
|
res.status(401).send('Authentication required');
|
2012-04-19 16:04:03 +02:00
|
|
|
}, 1000);
|
|
|
|
} else {
|
2015-04-10 05:52:58 -05:00
|
|
|
res.status(401).send('Authentication required');
|
2012-04-19 16:04:03 +02:00
|
|
|
}
|
|
|
|
}));
|
2020-08-25 16:44:44 -04:00
|
|
|
};
|
2012-04-19 14:25:12 +02:00
|
|
|
|
2020-08-28 21:03:45 -04:00
|
|
|
// Access checking is done in three steps:
|
|
|
|
//
|
|
|
|
// 1) Try to just access the thing. If access fails (perhaps authentication has not yet completed,
|
|
|
|
// or maybe different credentials are required), go to the next step.
|
|
|
|
// 2) Try to authenticate. (Or, if already logged in, reauthenticate with different credentials if
|
|
|
|
// supported by the authn scheme.) If authentication fails, give the user a 401 error to
|
|
|
|
// request new credentials. Otherwise, go to the next step.
|
|
|
|
// 3) Try to access the thing again. If this fails, give the user a 401 error.
|
|
|
|
//
|
|
|
|
// Plugins can use the 'next' callback (from the hook's context) to break out at any point (e.g.,
|
|
|
|
// to process an OAuth callback). Plugins can use the authFailure hook to override the default
|
|
|
|
// error handling behavior (e.g., to redirect to a login page).
|
|
|
|
|
|
|
|
let step1PreAuthenticate, step2Authenticate, step3Authorize;
|
|
|
|
|
|
|
|
step1PreAuthenticate = () => {
|
|
|
|
authorize((ok) => {
|
|
|
|
if (ok) return next();
|
|
|
|
step2Authenticate();
|
|
|
|
});
|
|
|
|
};
|
2012-04-19 14:25:12 +02:00
|
|
|
|
2020-08-28 21:03:45 -04:00
|
|
|
step2Authenticate = () => {
|
|
|
|
const ctx = {req, res, next};
|
|
|
|
// If the HTTP basic auth header is present, extract the username and password so it can be
|
|
|
|
// given to authn plugins.
|
|
|
|
const httpBasicAuth =
|
|
|
|
req.headers.authorization && req.headers.authorization.search('Basic ') === 0;
|
|
|
|
if (httpBasicAuth) {
|
|
|
|
const userpass =
|
|
|
|
Buffer.from(req.headers.authorization.split(' ')[1], 'base64').toString().split(':');
|
|
|
|
ctx.username = userpass.shift();
|
|
|
|
ctx.password = userpass.join(':');
|
|
|
|
}
|
|
|
|
hooks.aCallFirst('authenticate', ctx, hookResultMangle((ok) => {
|
|
|
|
if (!ok) {
|
|
|
|
// Fall back to HTTP basic auth.
|
|
|
|
if (!httpBasicAuth) return failure();
|
|
|
|
if (!(ctx.username in settings.users)) {
|
|
|
|
httpLogger.info(`Failed authentication from IP ${req.ip} - no such user`);
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
if (settings.users[ctx.username].password !== ctx.password) {
|
|
|
|
httpLogger.info(`Failed authentication from IP ${req.ip} for user ${ctx.username} - incorrect password`);
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
httpLogger.info(`Successful authentication from IP ${req.ip} for user ${ctx.username}`);
|
|
|
|
settings.users[ctx.username].username = ctx.username;
|
|
|
|
req.session.user = settings.users[ctx.username];
|
|
|
|
}
|
|
|
|
step3Authorize();
|
|
|
|
}));
|
|
|
|
};
|
2015-05-07 18:14:55 +01:00
|
|
|
|
2020-08-28 21:03:45 -04:00
|
|
|
step3Authorize = () => {
|
|
|
|
authorize((ok) => {
|
|
|
|
if (ok) return next();
|
|
|
|
failure();
|
2012-04-19 14:25:12 +02:00
|
|
|
});
|
2020-08-28 21:03:45 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
step1PreAuthenticate();
|
2020-08-25 16:44:44 -04:00
|
|
|
};
|
2012-02-25 13:38:09 +01:00
|
|
|
|
2012-09-22 16:03:40 +02:00
|
|
|
exports.secret = null;
|
2012-07-03 23:30:40 +02:00
|
|
|
|
2020-08-25 16:38:58 -04:00
|
|
|
exports.expressConfigure = (hook_name, args, cb) => {
|
2013-10-27 17:42:55 +01:00
|
|
|
// Measure response time
|
2020-08-25 16:38:58 -04:00
|
|
|
args.app.use((req, res, next) => {
|
2020-08-25 17:04:34 -04:00
|
|
|
const stopWatch = stats.timer('httpRequests').start();
|
|
|
|
const sendFn = res.send;
|
2020-08-25 16:38:58 -04:00
|
|
|
res.send = function() { // function, not arrow, due to use of 'arguments'
|
2020-08-25 16:44:44 -04:00
|
|
|
stopWatch.end();
|
|
|
|
sendFn.apply(res, arguments);
|
|
|
|
};
|
|
|
|
next();
|
|
|
|
});
|
2013-10-27 17:42:55 +01:00
|
|
|
|
2012-02-25 13:38:09 +01:00
|
|
|
// If the log level specified in the config file is WARN or ERROR the application server never starts listening to requests as reported in issue #158.
|
|
|
|
// Not installing the log4js connect logger when the log level has a higher severity than INFO since it would not log at that level anyway.
|
2020-08-28 02:00:16 -04:00
|
|
|
if (!(settings.loglevel === 'WARN' || settings.loglevel === 'ERROR'))
|
2020-08-25 16:55:56 -04:00
|
|
|
args.app.use(log4js.connectLogger(httpLogger, {level: log4js.levels.DEBUG, format: ':status, :method :url'}));
|
2012-04-19 14:25:12 +02:00
|
|
|
|
|
|
|
/* Do not let express create the session, so that we can retain a
|
|
|
|
* reference to it for socket.io to use. Also, set the key (cookie
|
|
|
|
* name) to a javascript identifier compatible string. Makes code
|
|
|
|
* handling it cleaner :) */
|
|
|
|
|
2012-07-03 23:30:40 +02:00
|
|
|
if (!exports.sessionStore) {
|
2013-02-13 01:33:22 +00:00
|
|
|
exports.sessionStore = new ueberStore();
|
2017-01-26 09:59:09 +01:00
|
|
|
exports.secret = settings.sessionKey;
|
2012-07-03 23:30:40 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 17:04:34 -04:00
|
|
|
const sameSite = settings.ssl ? 'Strict' : 'Lax';
|
2020-07-10 08:43:20 +01:00
|
|
|
|
2012-07-03 23:30:40 +02:00
|
|
|
args.app.sessionStore = exports.sessionStore;
|
2019-12-07 04:22:54 +01:00
|
|
|
args.app.use(sessionModule({
|
|
|
|
secret: exports.secret,
|
|
|
|
store: args.app.sessionStore,
|
2020-03-30 14:59:20 +00:00
|
|
|
resave: false,
|
2019-12-07 04:22:54 +01:00
|
|
|
saveUninitialized: true,
|
|
|
|
name: 'express_sid',
|
|
|
|
proxy: true,
|
|
|
|
cookie: {
|
2020-07-10 08:43:20 +01:00
|
|
|
/*
|
|
|
|
* Firefox started enforcing sameSite, see https://github.com/ether/etherpad-lite/issues/3989
|
|
|
|
* for details. In response we set it based on if SSL certs are set in Etherpad. Note that if
|
|
|
|
* You use Nginx or so for reverse proxy this may cause problems. Use Certificate pinning to remedy.
|
|
|
|
*/
|
|
|
|
sameSite: sameSite,
|
2019-12-07 04:36:01 +01:00
|
|
|
/*
|
|
|
|
* The automatic express-session mechanism for determining if the
|
|
|
|
* application is being served over ssl is similar to the one used for
|
|
|
|
* setting the language cookie, which check if one of these conditions is
|
|
|
|
* true:
|
|
|
|
*
|
|
|
|
* 1. we are directly serving the nodejs application over SSL, using the
|
|
|
|
* "ssl" options in settings.json
|
|
|
|
*
|
|
|
|
* 2. we are serving the nodejs application in plaintext, but we are using
|
|
|
|
* a reverse proxy that terminates SSL for us. In this case, the user
|
|
|
|
* has to set trustProxy = true in settings.json, and the information
|
|
|
|
* wheter the application is over SSL or not will be extracted from the
|
|
|
|
* X-Forwarded-Proto HTTP header
|
|
|
|
*
|
|
|
|
* Please note that this will not be compatible with applications being
|
|
|
|
* served over http and https at the same time.
|
|
|
|
*
|
|
|
|
* reference: https://github.com/expressjs/session/blob/v1.17.0/README.md#cookiesecure
|
|
|
|
*/
|
|
|
|
secure: 'auto',
|
2019-12-07 04:22:54 +01:00
|
|
|
}
|
|
|
|
}));
|
2012-04-19 14:25:12 +02:00
|
|
|
|
2015-05-07 18:14:55 +01:00
|
|
|
args.app.use(cookieParser(settings.sessionKey, {}));
|
|
|
|
|
2020-08-28 01:49:26 -04:00
|
|
|
args.app.use(exports.checkAccess);
|
2020-08-25 16:44:44 -04:00
|
|
|
};
|