🐛 Fixed redirects with special characters (#15533)

closes: https://github.com/TryGhost/Ghost/issues/15267

- This was because the URLs were not being encoded and matched correctly - it is solved by encoding the URL before adding to the router.
This commit is contained in:
Prathamesh Gawas 2022-10-13 16:11:20 +05:30 committed by GitHub
parent e2124314ed
commit 3424222597
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -94,6 +94,13 @@ class DynamicRedirectManager {
*/
addRedirect(from, to, options = {}) {
try {
// encode "from" only if it's not a regex
try {
new RegExp(from);
} catch (e) {
from = encodeURI(from);
}
const fromRegex = this.buildRegex(from);
const redirectId = from;

View File

@ -363,6 +363,26 @@ describe('DynamicRedirectManager', function () {
should.equal(location, 'https://ghost.org/docs');
});
});
describe('Url with special character redirect', function () {
it('redirects urls with special characters', function () {
const from = '/joloonii-surgaltuud/а-анлал/';
const to = '/joloonii-angilal/а-ангилал';
manager.addRedirect(from, to);
req.url = from;
manager.handleRequest(req, res, function next() {
should.fail(true, false, 'next should NOT have been called');
});
// NOTE: max-age is "0" because it's not a permanent redirect
should.equal(headers['Cache-Control'], 'public, max-age=0');
should.equal(status, 302);
should.equal(location, '/joloonii-angilal/а-ангилал');
});
});
});
describe('with subdirectory configuration', function () {