mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00: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:
parent
a532e35dc4
commit
0d89acd910
3 changed files with 35 additions and 14 deletions
|
@ -22,13 +22,6 @@ _private.registerRoutes = () => {
|
|||
validation.validateRedirects(redirects);
|
||||
|
||||
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
|
||||
* / ... /i
|
||||
|
@ -56,17 +49,18 @@ _private.registerRoutes = () => {
|
|||
|
||||
debug('register', redirect.from);
|
||||
customRedirectsRouter.get(new RegExp(redirect.from, options), function (req, res) {
|
||||
const maxAge = redirect.permanent ? config.get('caching:customRedirects:maxAge') : 0,
|
||||
parsedUrl = url.parse(req.originalUrl);
|
||||
const maxAge = redirect.permanent ? config.get('caching:customRedirects:maxAge') : 0;
|
||||
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({
|
||||
'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),
|
||||
search: parsedUrl.search,
|
||||
hash: parsedTo.hash
|
||||
}));
|
||||
|
||||
res.redirect(redirect.permanent ? 301 : 302, url.format(toURL));
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
|
|
|
@ -673,6 +673,7 @@ describe('Frontend Routing', function () {
|
|||
});
|
||||
});
|
||||
|
||||
// TODO: convert to unit tests
|
||||
describe('Redirects (use redirects.json from test/utils/fixtures/data)', function () {
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -42,5 +42,9 @@
|
|||
{
|
||||
"from": "^\\/Default-Sensitive",
|
||||
"to": "/redirected-default"
|
||||
},
|
||||
{
|
||||
"from": "/external-url",
|
||||
"to": "https://ghost.org"
|
||||
}
|
||||
]
|
||||
|
|
Loading…
Add table
Reference in a new issue