2021-07-07 05:38:56 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-08-11 05:28:29 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2020-01-21 04:33:58 -05:00
|
|
|
|
2021-02-03 07:39:08 -05:00
|
|
|
const botUserAgents = [
|
|
|
|
'kube-probe',
|
|
|
|
'GoogleStackdriverMonitoring',
|
|
|
|
'GoogleHC',
|
|
|
|
'Googlebot',
|
|
|
|
'bingbot',
|
2021-04-27 03:52:58 -04:00
|
|
|
'facebookexternal',
|
2021-02-03 07:39:08 -05:00
|
|
|
].map(agent => {
|
|
|
|
return agent.toLowerCase()
|
|
|
|
})
|
2020-01-21 04:33:58 -05:00
|
|
|
// SessionAutostartMiddleware provides a mechanism to force certain routes not
|
|
|
|
// to get an automatic session where they don't have one already. This allows us
|
|
|
|
// to work around issues where we might overwrite a user's login cookie with one
|
|
|
|
// that is hidden by a `SameSite` setting.
|
|
|
|
//
|
|
|
|
// When registering a route with disableSessionAutostartForRoute, a callback
|
|
|
|
// should be provided that handles the case that a session is not available.
|
|
|
|
// This will be called as a standard middleware with (req, res, next) - calling
|
|
|
|
// next will continue and sett up a session as normal, otherwise the app can
|
|
|
|
// perform a different operation as usual
|
|
|
|
|
|
|
|
class SessionAutostartMiddleware {
|
|
|
|
constructor() {
|
|
|
|
this.middleware = this.middleware.bind(this)
|
|
|
|
this._cookieName = Settings.cookieName
|
|
|
|
this._noAutostartCallbacks = new Map()
|
|
|
|
}
|
|
|
|
|
|
|
|
static applyInitialMiddleware(router) {
|
|
|
|
const middleware = new SessionAutostartMiddleware()
|
|
|
|
router.sessionAutostartMiddleware = middleware
|
|
|
|
router.use(middleware.middleware)
|
|
|
|
}
|
|
|
|
|
|
|
|
disableSessionAutostartForRoute(route, method, callback) {
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new Error('callback not provided when disabling session autostart')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._noAutostartCallbacks[route]) {
|
|
|
|
this._noAutostartCallbacks[route] = new Map()
|
|
|
|
}
|
|
|
|
|
|
|
|
this._noAutostartCallbacks[route][method] = callback
|
|
|
|
}
|
|
|
|
|
2020-01-29 08:54:17 -05:00
|
|
|
applyDefaultPostGatewayForRoute(route) {
|
|
|
|
this.disableSessionAutostartForRoute(
|
|
|
|
route,
|
|
|
|
'POST',
|
|
|
|
SessionAutostartMiddleware.genericPostGatewayMiddleware
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-01-21 04:33:58 -05:00
|
|
|
autostartCallbackForRequest(req) {
|
|
|
|
return (
|
|
|
|
this._noAutostartCallbacks[req.path] &&
|
|
|
|
this._noAutostartCallbacks[req.path][req.method]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-03 07:39:08 -05:00
|
|
|
reqIsBot(req) {
|
|
|
|
const agent = (req.headers['user-agent'] || '').toLowerCase()
|
|
|
|
|
|
|
|
const foundMatch = botUserAgents.find(botAgent => {
|
|
|
|
return agent.includes(botAgent)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (foundMatch) {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
middleware(req, _res, next) {
|
2020-01-21 04:33:58 -05:00
|
|
|
if (!req.signedCookies[this._cookieName]) {
|
|
|
|
const callback = this.autostartCallbackForRequest(req)
|
|
|
|
if (callback) {
|
|
|
|
req.session = {
|
2021-04-27 03:52:58 -04:00
|
|
|
noSessionCallback: callback,
|
2020-01-21 04:33:58 -05:00
|
|
|
}
|
2021-02-03 07:39:08 -05:00
|
|
|
} else if (this.reqIsBot(req)) {
|
|
|
|
req.session = {
|
|
|
|
noSessionCallback: (_req, _res, next) => {
|
|
|
|
next()
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2021-02-03 07:39:08 -05:00
|
|
|
}
|
2020-01-21 04:33:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
|
|
|
|
static invokeCallbackMiddleware(req, res, next) {
|
|
|
|
if (req.session.noSessionCallback) {
|
|
|
|
return req.session.noSessionCallback(req, res, next)
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
}
|
2020-01-29 08:54:17 -05:00
|
|
|
|
|
|
|
static genericPostGatewayMiddleware(req, res, next) {
|
|
|
|
if (req.method !== 'POST') {
|
|
|
|
return next(
|
2020-08-11 05:28:29 -04:00
|
|
|
new OError('post gateway invoked for non-POST request', {
|
|
|
|
path: req.path,
|
2021-04-27 03:52:58 -04:00
|
|
|
method: req.method,
|
2020-01-29 08:54:17 -05:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.body.viaGateway) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
|
|
|
res.render('general/post-gateway', { form_data: req.body })
|
|
|
|
}
|
2020-01-21 04:33:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SessionAutostartMiddleware
|