0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

🐛 Fixed redirects to external URL

closes #10623

- The ability to redirect to external URLs was broken with 7e211a307c
- Added test coverage for external URL case
This commit is contained in:
Nazar Gargol 2019-04-01 12:11:04 +08:00
parent a532e35dc4
commit 0d89acd910
3 changed files with 35 additions and 14 deletions

View file

@ -22,13 +22,6 @@ _private.registerRoutes = () => {
validation.validateRedirects(redirects); validation.validateRedirects(redirects);
redirects.forEach((redirect) => { redirects.forEach((redirect) => {
/**
* Extract target info, such as hash.
*
* Required to re-formulate the correct endpoint.
*/
const parsedTo = url.parse(redirect.to);
/** /**
* Detect case insensitive modifier when regex is enclosed by * Detect case insensitive modifier when regex is enclosed by
* / ... /i * / ... /i
@ -56,17 +49,18 @@ _private.registerRoutes = () => {
debug('register', redirect.from); debug('register', redirect.from);
customRedirectsRouter.get(new RegExp(redirect.from, options), function (req, res) { customRedirectsRouter.get(new RegExp(redirect.from, options), function (req, res) {
const maxAge = redirect.permanent ? config.get('caching:customRedirects:maxAge') : 0, const maxAge = redirect.permanent ? config.get('caching:customRedirects:maxAge') : 0;
parsedUrl = url.parse(req.originalUrl); const fromURL = url.parse(req.originalUrl);
const toURL = url.parse(redirect.to);
toURL.pathname = fromURL.pathname.replace(new RegExp(redirect.from, options), toURL.pathname),
toURL.search = fromURL.search;
res.set({ res.set({
'Cache-Control': `public, max-age=${maxAge}` 'Cache-Control': `public, max-age=${maxAge}`
}); });
res.redirect(redirect.permanent ? 301 : 302, url.format({
pathname: parsedUrl.pathname.replace(new RegExp(redirect.from, options), parsedTo.pathname), res.redirect(redirect.permanent ? 301 : 302, url.format(toURL));
search: parsedUrl.search,
hash: parsedTo.hash
}));
}); });
}); });
} catch (err) { } catch (err) {

View file

@ -673,6 +673,7 @@ describe('Frontend Routing', function () {
}); });
}); });
// TODO: convert to unit tests
describe('Redirects (use redirects.json from test/utils/fixtures/data)', function () { describe('Redirects (use redirects.json from test/utils/fixtures/data)', function () {
var ghostServer; var ghostServer;
@ -923,5 +924,27 @@ describe('Frontend Routing', function () {
}); });
}); });
}); });
describe('external url redirect', function () {
it('with trailing slash', function (done) {
request.get('/external-url/')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('https://ghost.org/');
doEnd(done)(err, res);
});
});
it('without trailing slash', function (done) {
request.get('/external-url')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('https://ghost.org/');
doEnd(done)(err, res);
});
});
});
}); });
}); });

View file

@ -42,5 +42,9 @@
{ {
"from": "^\\/Default-Sensitive", "from": "^\\/Default-Sensitive",
"to": "/redirected-default" "to": "/redirected-default"
},
{
"from": "/external-url",
"to": "https://ghost.org"
} }
] ]