express: Check access before expressConfigure middleware

There are no guarantees about the order of execution of hook
functions, which means that a plugin's `expressConfigure` hook
function could theoretically register a handler/middleware before the
access check middleware is registered. If that happens, the plugin's
handler would run before the access check, which would be bad. Avoid
the problem by explicitly installing the `webaccess.checkAccess`
middleware before running the `expressConfigure` hook.
This commit is contained in:
Richard Hansen 2021-12-18 01:05:31 -05:00
parent 472eddc821
commit 7f3d0e71f7
3 changed files with 8 additions and 9 deletions

View file

@ -50,12 +50,6 @@
"expressCreateServer": "ep_etherpad-lite/node/hooks/express/padurlsanitize" "expressCreateServer": "ep_etherpad-lite/node/hooks/express/padurlsanitize"
} }
}, },
{
"name": "webaccess",
"hooks": {
"expressConfigure": "ep_etherpad-lite/node/hooks/express/webaccess"
}
},
{ {
"name": "apicalls", "name": "apicalls",
"hooks": { "hooks": {

View file

@ -12,6 +12,7 @@ const SessionStore = require('../db/SessionStore');
const settings = require('../utils/Settings'); const settings = require('../utils/Settings');
const stats = require('../stats'); const stats = require('../stats');
const util = require('util'); const util = require('util');
const webaccess = require('./express/webaccess');
const logger = log4js.getLogger('http'); const logger = log4js.getLogger('http');
let serverName; let serverName;
@ -203,6 +204,7 @@ exports.restartServer = async () => {
app.use(exports.sessionMiddleware); app.use(exports.sessionMiddleware);
app.use(cookieParser(settings.sessionKey, {})); app.use(cookieParser(settings.sessionKey, {}));
app.use(webaccess.checkAccess);
await Promise.all([ await Promise.all([
hooks.aCallAll('expressConfigure', {app}), hooks.aCallAll('expressConfigure', {app}),

View file

@ -203,7 +203,10 @@ const checkAccess = async (req, res, next) => {
res.status(403).send('Forbidden'); res.status(403).send('Forbidden');
}; };
exports.expressConfigure = (hookName, args, cb) => { /**
args.app.use((req, res, next) => { checkAccess(req, res, next).catch(next); }); * Express middleware to authenticate the user and check authorization. Must be installed after the
return cb(); * express-session middleware.
*/
exports.checkAccess = (req, res, next) => {
checkAccess(req, res, next).catch((err) => next(err || new Error(err)));
}; };