2021-04-16 04:36:46 -04:00
|
|
|
const Bowser = require('bowser')
|
2021-07-07 05:38:56 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2021-04-28 04:48:58 -04:00
|
|
|
const Url = require('url')
|
|
|
|
const { getSafeRedirectPath } = require('../Features/Helpers/UrlHelper')
|
2021-04-16 04:36:46 -04:00
|
|
|
|
|
|
|
function unsupportedBrowserMiddleware(req, res, next) {
|
|
|
|
if (!Settings.unsupportedBrowsers) return next()
|
|
|
|
|
|
|
|
const userAgent = req.headers['user-agent']
|
|
|
|
|
2021-04-19 08:36:29 -04:00
|
|
|
if (!userAgent) return next()
|
|
|
|
|
2021-04-16 04:36:46 -04:00
|
|
|
const parser = Bowser.getParser(userAgent)
|
|
|
|
|
|
|
|
// Allow bots through by only ignoring bots or unrecognised UA strings
|
|
|
|
const isBot = parser.isPlatform('bot') || !parser.getBrowserName()
|
|
|
|
if (isBot) return next()
|
|
|
|
|
|
|
|
const isUnsupported = parser.satisfies(Settings.unsupportedBrowsers)
|
|
|
|
if (isUnsupported) {
|
2021-04-28 04:48:58 -04:00
|
|
|
return res.redirect(
|
|
|
|
Url.format({
|
|
|
|
pathname: '/unsupported-browser',
|
|
|
|
query: { fromURL: req.originalUrl },
|
|
|
|
})
|
|
|
|
)
|
2021-04-16 04:36:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
|
2021-04-28 04:48:58 -04:00
|
|
|
function renderUnsupportedBrowserPage(req, res) {
|
|
|
|
let fromURL
|
|
|
|
if (typeof req.query.fromURL === 'string') {
|
|
|
|
try {
|
|
|
|
fromURL = Settings.siteUrl + getSafeRedirectPath(req.query.fromURL)
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
res.render('general/unsupported-browser', { fromURL })
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
renderUnsupportedBrowserPage,
|
|
|
|
unsupportedBrowserMiddleware,
|
|
|
|
}
|