Merge pull request #3959 from overleaf/jpa-take-note-of-past-url

[UnsupportedBrowserMiddleware] take note of the past URL in redirect

GitOrigin-RevId: d80ed876f87e54c181f00669a11795a2ce44e5a5
This commit is contained in:
Jakob Ackermann 2021-04-28 10:48:58 +02:00 committed by Copybot
parent cf496e1fd2
commit 23c73b9bf1
4 changed files with 78 additions and 7 deletions

View file

@ -1,5 +1,7 @@
const Bowser = require('bowser') const Bowser = require('bowser')
const Settings = require('settings-sharelatex') const Settings = require('settings-sharelatex')
const Url = require('url')
const { getSafeRedirectPath } = require('../Features/Helpers/UrlHelper')
function unsupportedBrowserMiddleware(req, res, next) { function unsupportedBrowserMiddleware(req, res, next) {
if (!Settings.unsupportedBrowsers) return next() if (!Settings.unsupportedBrowsers) return next()
@ -16,10 +18,28 @@ function unsupportedBrowserMiddleware(req, res, next) {
const isUnsupported = parser.satisfies(Settings.unsupportedBrowsers) const isUnsupported = parser.satisfies(Settings.unsupportedBrowsers)
if (isUnsupported) { if (isUnsupported) {
return res.redirect('/unsupported-browser') return res.redirect(
Url.format({
pathname: '/unsupported-browser',
query: { fromURL: req.originalUrl },
})
)
} }
next() next()
} }
module.exports = { unsupportedBrowserMiddleware } 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,
}

View file

@ -50,6 +50,7 @@ const UserMembershipRouter = require('./Features/UserMembership/UserMembershipRo
const SystemMessageController = require('./Features/SystemMessages/SystemMessageController') const SystemMessageController = require('./Features/SystemMessages/SystemMessageController')
const { Joi, validate } = require('./infrastructure/Validation') const { Joi, validate } = require('./infrastructure/Validation')
const { const {
renderUnsupportedBrowserPage,
unsupportedBrowserMiddleware, unsupportedBrowserMiddleware,
} = require('./infrastructure/UnsupportedBrowserMiddleware') } = require('./infrastructure/UnsupportedBrowserMiddleware')
@ -1143,9 +1144,7 @@ function initialize(webRouter, privateApiRouter, publicApiRouter) {
TokenAccessController.grantTokenAccessReadOnly TokenAccessController.grantTokenAccessReadOnly
) )
webRouter.get('/unsupported-browser', (req, res) => { webRouter.get('/unsupported-browser', renderUnsupportedBrowserPage)
res.render('general/unsupported-browser')
})
webRouter.get('*', ErrorController.notFound) webRouter.get('*', ErrorController.notFound)
} }

View file

@ -14,3 +14,9 @@ block body
| Sorry, we don't support your browser anymore. Find out more about #[a(href="https://www.overleaf.com/learn/how-to/What_browsers_do_you_support") what browsers we support]. | Sorry, we don't support your browser anymore. Find out more about #[a(href="https://www.overleaf.com/learn/how-to/What_browsers_do_you_support") what browsers we support].
br br
| If you think you're seeing this message in error, please #[a(href="/contact") let us know]. | If you think you're seeing this message in error, please #[a(href="/contact") let us know].
if fromURL
p
| URL:
|
a(href=fromURL) #{fromURL}

View file

@ -44,9 +44,10 @@ describe('UnsupportedBrowsers', function () {
}) })
it('redirects unsupported browsers to unsupported page', function (done) { it('redirects unsupported browsers to unsupported page', function (done) {
const url = '/project'
this.user.request( this.user.request(
{ {
url: '/project', url,
headers: { headers: {
// IE11 user agent // IE11 user agent
'user-agent': 'user-agent':
@ -56,7 +57,52 @@ describe('UnsupportedBrowsers', function () {
(error, response) => { (error, response) => {
expect(error).to.not.exist expect(error).to.not.exist
expect(response.statusCode).to.equal(302) expect(response.statusCode).to.equal(302)
expect(response.headers.location).to.equal('/unsupported-browser') expect(response.headers.location).to.equal(
'/unsupported-browser?fromURL=' + encodeURIComponent(url)
)
done()
}
)
})
it('shows the previous URL', function (done) {
const url = '/project/60867f47174dfd13f1e00000'
this.user.request(
{
url: '/unsupported-browser?fromURL=' + encodeURIComponent(url),
headers: {
// IE11 user agent
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko',
},
},
(error, response, body) => {
expect(error).to.not.exist
expect(response.statusCode).to.equal(200)
expect(body).to.include('URL:')
expect(body).to.include(url)
done()
}
)
})
it('shows a sanitized URL', function (done) {
const url = 'https://evil.com/the/pathname'
this.user.request(
{
url: '/unsupported-browser?fromURL=' + encodeURIComponent(url),
headers: {
// IE11 user agent
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko',
},
},
(error, response, body) => {
expect(error).to.not.exist
expect(response.statusCode).to.equal(200)
expect(body).to.include('URL:')
expect(body).to.not.include('evil.com')
expect(body).to.include('/the/pathname')
done() done()
} }
) )