2019-05-29 05:21:06 -04:00
|
|
|
const Path = require('path')
|
|
|
|
const express = require('express')
|
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const metrics = require('metrics-sharelatex')
|
|
|
|
const crawlerLogger = require('./CrawlerLogger')
|
|
|
|
const expressLocals = require('./ExpressLocals')
|
|
|
|
const Router = require('../router')
|
|
|
|
const helmet = require('helmet')
|
|
|
|
const UserSessionsRedis = require('../Features/User/UserSessionsRedis')
|
|
|
|
const Csrf = require('./Csrf')
|
|
|
|
|
|
|
|
const sessionsRedisClient = UserSessionsRedis.client()
|
|
|
|
|
|
|
|
const session = require('express-session')
|
|
|
|
const RedisStore = require('connect-redis')(session)
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const methodOverride = require('method-override')
|
|
|
|
const cookieParser = require('cookie-parser')
|
|
|
|
const bearerToken = require('express-bearer-token')
|
|
|
|
|
|
|
|
const passport = require('passport')
|
|
|
|
const LocalStrategy = require('passport-local').Strategy
|
|
|
|
|
|
|
|
const oneDayInMilliseconds = 86400000
|
|
|
|
const ReferalConnect = require('../Features/Referal/ReferalConnect')
|
|
|
|
const RedirectManager = require('./RedirectManager')
|
|
|
|
const ProxyManager = require('./ProxyManager')
|
|
|
|
const translations = require('translations-sharelatex').setup(Settings.i18n)
|
|
|
|
const Modules = require('./Modules')
|
|
|
|
|
|
|
|
const ErrorController = require('../Features/Errors/ErrorController')
|
2019-07-19 05:40:23 -04:00
|
|
|
const HttpErrorController = require('../Features/Errors/HttpErrorController')
|
2019-05-29 05:21:06 -04:00
|
|
|
const UserSessionsManager = require('../Features/User/UserSessionsManager')
|
|
|
|
const AuthenticationController = require('../Features/Authentication/AuthenticationController')
|
|
|
|
|
2019-08-06 08:20:52 -04:00
|
|
|
const STATIC_CACHE_AGE = Settings.cacheStaticAssets
|
|
|
|
? oneDayInMilliseconds * 365
|
|
|
|
: 0
|
|
|
|
|
|
|
|
// Init the session store
|
|
|
|
const sessionStore = new RedisStore({ client: sessionsRedisClient })
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
if (metrics.event_loop != null) {
|
|
|
|
metrics.event_loop.monitor(logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
const app = express()
|
|
|
|
|
|
|
|
const webRouter = express.Router()
|
|
|
|
const privateApiRouter = express.Router()
|
|
|
|
const publicApiRouter = express.Router()
|
|
|
|
|
|
|
|
if (Settings.behindProxy) {
|
2019-08-06 08:20:52 -04:00
|
|
|
app.set('trust proxy', Settings.trustedProxyIps || true)
|
|
|
|
/**
|
|
|
|
* Handle the X-Original-Forwarded-For header.
|
|
|
|
*
|
|
|
|
* The nginx ingress sends us the contents of X-Forwarded-For it received in
|
|
|
|
* X-Original-Forwarded-For. Express expects all proxy IPs to be in a comma
|
|
|
|
* separated list in X-Forwarded-For.
|
|
|
|
*/
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
if (
|
|
|
|
req.headers['x-original-forwarded-for'] &&
|
|
|
|
req.headers['x-forwarded-for']
|
|
|
|
) {
|
|
|
|
req.headers['x-forwarded-for'] =
|
|
|
|
req.headers['x-original-forwarded-for'] +
|
|
|
|
', ' +
|
|
|
|
req.headers['x-forwarded-for']
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
webRouter.use(
|
2019-08-06 08:20:52 -04:00
|
|
|
express.static(Path.join(__dirname, '/../../../public'), {
|
|
|
|
maxAge: STATIC_CACHE_AGE
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-08-06 08:20:52 -04:00
|
|
|
app.set('views', Path.join(__dirname, '/../../views'))
|
2019-05-29 05:21:06 -04:00
|
|
|
app.set('view engine', 'pug')
|
|
|
|
Modules.loadViewIncludes(app)
|
|
|
|
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true, limit: '2mb' }))
|
|
|
|
// Make sure we can process twice the max doc length, to allow for
|
|
|
|
// - the doc content
|
|
|
|
// - text ranges spanning the whole doc
|
|
|
|
// Also allow some overhead for JSON encoding
|
|
|
|
app.use(bodyParser.json({ limit: 2 * Settings.max_doc_length + 64 * 1024 })) // 64kb overhead
|
|
|
|
app.use(methodOverride())
|
|
|
|
app.use(bearerToken())
|
|
|
|
|
|
|
|
app.use(metrics.http.monitor(logger))
|
|
|
|
RedirectManager.apply(webRouter)
|
|
|
|
ProxyManager.apply(publicApiRouter)
|
|
|
|
|
|
|
|
webRouter.use(cookieParser(Settings.security.sessionSecret))
|
|
|
|
webRouter.use(
|
|
|
|
session({
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false,
|
|
|
|
secret: Settings.security.sessionSecret,
|
|
|
|
proxy: Settings.behindProxy,
|
|
|
|
cookie: {
|
|
|
|
domain: Settings.cookieDomain,
|
2019-09-12 04:04:06 -04:00
|
|
|
maxAge: Settings.cookieSessionLength, // in milliseconds, see https://github.com/expressjs/session#cookiemaxage
|
2019-05-29 05:21:06 -04:00
|
|
|
secure: Settings.secureCookie
|
|
|
|
},
|
|
|
|
store: sessionStore,
|
|
|
|
key: Settings.cookieName,
|
|
|
|
rolling: true
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// passport
|
|
|
|
webRouter.use(passport.initialize())
|
|
|
|
webRouter.use(passport.session())
|
|
|
|
|
|
|
|
passport.use(
|
|
|
|
new LocalStrategy(
|
|
|
|
{
|
|
|
|
passReqToCallback: true,
|
|
|
|
usernameField: 'email',
|
|
|
|
passwordField: 'password'
|
|
|
|
},
|
|
|
|
AuthenticationController.doPassportLogin
|
|
|
|
)
|
|
|
|
)
|
|
|
|
passport.serializeUser(AuthenticationController.serializeUser)
|
|
|
|
passport.deserializeUser(AuthenticationController.deserializeUser)
|
|
|
|
|
|
|
|
Modules.hooks.fire('passportSetup', passport, function(err) {
|
|
|
|
if (err != null) {
|
2019-08-06 08:20:52 -04:00
|
|
|
logger.err({ err }, 'error setting up passport in modules')
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Modules.applyNonCsrfRouter(webRouter, privateApiRouter, publicApiRouter)
|
|
|
|
|
|
|
|
webRouter.csrf = new Csrf()
|
|
|
|
webRouter.use(webRouter.csrf.middleware)
|
|
|
|
webRouter.use(translations.expressMiddlewear)
|
|
|
|
webRouter.use(translations.setLangBasedOnDomainMiddlewear)
|
|
|
|
|
|
|
|
// Measure expiry from last request, not last login
|
|
|
|
webRouter.use(function(req, res, next) {
|
|
|
|
req.session.touch()
|
|
|
|
if (AuthenticationController.isUserLoggedIn(req)) {
|
|
|
|
UserSessionsManager.touch(
|
|
|
|
AuthenticationController.getSessionUser(req),
|
2019-08-06 08:20:52 -04:00
|
|
|
err => {
|
2019-08-07 04:45:39 -04:00
|
|
|
if (err) {
|
|
|
|
logger.err({ err }, 'error extending user session')
|
|
|
|
}
|
2019-08-06 08:20:52 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
}
|
2019-08-06 08:20:52 -04:00
|
|
|
next()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
webRouter.use(ReferalConnect.use)
|
|
|
|
expressLocals(app, webRouter, privateApiRouter, publicApiRouter)
|
|
|
|
|
|
|
|
if (app.get('env') === 'production') {
|
|
|
|
logger.info('Production Enviroment')
|
|
|
|
app.enable('view cache')
|
|
|
|
}
|
|
|
|
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
metrics.inc('http-request')
|
|
|
|
crawlerLogger.log(req)
|
2019-08-06 08:20:52 -04:00
|
|
|
next()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
webRouter.use(function(req, res, next) {
|
|
|
|
if (Settings.siteIsOpen) {
|
2019-08-06 08:20:52 -04:00
|
|
|
next()
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
res.status(503)
|
2019-08-06 08:20:52 -04:00
|
|
|
res.render('general/closed', { title: 'maintenance' })
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
webRouter.use(function(req, res, next) {
|
|
|
|
if (Settings.editorIsOpen) {
|
2019-08-06 08:20:52 -04:00
|
|
|
next()
|
2019-05-29 05:21:06 -04:00
|
|
|
} else if (req.url.indexOf('/admin') === 0) {
|
2019-08-06 08:20:52 -04:00
|
|
|
next()
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
res.status(503)
|
2019-08-06 08:20:52 -04:00
|
|
|
res.render('general/closed', { title: 'maintenance' })
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// add security headers using Helmet
|
|
|
|
webRouter.use(function(req, res, next) {
|
|
|
|
const isLoggedIn = AuthenticationController.isUserLoggedIn(req)
|
|
|
|
const isProjectPage = !!req.path.match('^/project/[a-f0-9]{24}$')
|
|
|
|
|
2019-08-06 08:20:52 -04:00
|
|
|
helmet({
|
2019-05-29 05:21:06 -04:00
|
|
|
// note that more headers are added by default
|
|
|
|
dnsPrefetchControl: false,
|
|
|
|
referrerPolicy: { policy: 'origin-when-cross-origin' },
|
|
|
|
noCache: isLoggedIn || isProjectPage,
|
2019-09-18 06:29:43 -04:00
|
|
|
hsts: false
|
2019-05-29 05:21:06 -04:00
|
|
|
})(req, res, next)
|
|
|
|
})
|
|
|
|
|
|
|
|
const profiler = require('v8-profiler-node8')
|
|
|
|
privateApiRouter.get('/profile', function(req, res) {
|
|
|
|
const time = parseInt(req.query.time || '1000')
|
|
|
|
profiler.startProfiling('test')
|
2019-08-06 08:20:52 -04:00
|
|
|
setTimeout(function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
const profile = profiler.stopProfiling('test')
|
2019-08-06 08:20:52 -04:00
|
|
|
res.json(profile)
|
2019-05-29 05:21:06 -04:00
|
|
|
}, time)
|
|
|
|
})
|
|
|
|
|
2019-08-06 08:20:52 -04:00
|
|
|
privateApiRouter.get('/heapdump', (req, res, next) =>
|
2019-05-29 05:21:06 -04:00
|
|
|
require('heapdump').writeSnapshot(
|
|
|
|
`/tmp/${Date.now()}.web.heapsnapshot`,
|
2019-08-06 08:20:52 -04:00
|
|
|
(err, filename) => {
|
|
|
|
if (err != null) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
res.send(filename)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.info('creating HTTP server'.yellow)
|
|
|
|
const server = require('http').createServer(app)
|
|
|
|
|
|
|
|
// provide settings for separate web and api processes
|
|
|
|
// if enableApiRouter and enableWebRouter are not defined they default
|
|
|
|
// to true.
|
|
|
|
const notDefined = x => x == null
|
|
|
|
const enableApiRouter =
|
|
|
|
Settings.web != null ? Settings.web.enableApiRouter : undefined
|
|
|
|
if (enableApiRouter || notDefined(enableApiRouter)) {
|
|
|
|
logger.info('providing api router')
|
|
|
|
app.use(privateApiRouter)
|
2019-07-19 05:40:23 -04:00
|
|
|
app.use(HttpErrorController.handleError)
|
2019-05-29 05:21:06 -04:00
|
|
|
app.use(ErrorController.handleApiError)
|
|
|
|
}
|
|
|
|
|
|
|
|
const enableWebRouter =
|
|
|
|
Settings.web != null ? Settings.web.enableWebRouter : undefined
|
|
|
|
if (enableWebRouter || notDefined(enableWebRouter)) {
|
|
|
|
logger.info('providing web router')
|
|
|
|
app.use(publicApiRouter) // public API goes with web router for public access
|
2019-07-19 05:40:23 -04:00
|
|
|
app.use(HttpErrorController.handleError)
|
2019-05-29 05:21:06 -04:00
|
|
|
app.use(ErrorController.handleApiError)
|
|
|
|
app.use(webRouter)
|
2019-07-19 05:40:23 -04:00
|
|
|
app.use(HttpErrorController.handleError)
|
2019-05-29 05:21:06 -04:00
|
|
|
app.use(ErrorController.handleError)
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.injectMetricsRoute(webRouter)
|
2019-08-06 08:20:52 -04:00
|
|
|
Router.initialize(webRouter, privateApiRouter, publicApiRouter)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
app,
|
|
|
|
server
|
|
|
|
}
|