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()
|
|
|
|
|
2021-09-08 08:14:25 -04:00
|
|
|
// Prevent redirect loop
|
2021-10-20 06:17:59 -04:00
|
|
|
const path = req.path
|
2021-09-08 08:14:25 -04:00
|
|
|
if (path === '/unsupported-browser') return next()
|
|
|
|
|
2021-04-16 04:36:46 -04:00
|
|
|
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 {
|
2021-09-08 08:14:25 -04:00
|
|
|
fromURL =
|
|
|
|
Settings.siteUrl + (getSafeRedirectPath(req.query.fromURL) || '/')
|
2021-04-28 04:48:58 -04:00
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
res.render('general/unsupported-browser', { fromURL })
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
renderUnsupportedBrowserPage,
|
|
|
|
unsupportedBrowserMiddleware,
|
|
|
|
}
|