0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Added handling for invalid redirect regexes

refs https://linear.app/tryghost/issue/CORE-84/have-a-look-at-the-eggs-redirects-refactor-branch
refs 8f5186995d
refs 260a47da83

- The router should not stop working when an invalid redirect definition is added
- Referenced commits solve this exact problem before this module was introduced
This commit is contained in:
Naz 2021-10-12 14:32:38 +02:00
parent 80f2a001ec
commit ba2a5df493
2 changed files with 55 additions and 10 deletions

View file

@ -92,19 +92,27 @@ class DynamicRedirectManager {
* @returns {string} The redirect ID
*/
addRedirect(from, to, options) {
const fromRegex = this.buildRegex(from);
const redirectId = from;
try {
const fromRegex = this.buildRegex(from);
const redirectId = from;
this.redirectIds.push(redirectId);
this.redirects[redirectId] = {
fromRegex,
to,
options
};
this.redirectIds.push(redirectId);
this.redirects[redirectId] = {
fromRegex,
to,
options
};
this.setupRedirect(redirectId);
this.setupRedirect(redirectId);
return redirectId;
return redirectId;
} catch (error) {
if (error.message.match(/Invalid regular expression/gi)) {
return null;
}
throw error;
}
}
/**

View file

@ -76,4 +76,41 @@ describe('DynamicRedirectManager', function () {
should.equal(status, null);
should.equal(location, null);
});
it('The routing works when passed an invalid regexp for the from parameter', function () {
const manager = new DynamicRedirectManager({permanentMaxAge: 100, urlUtils});
const from = '/invalid_regex/(/size/[a-zA-Z0-9_-.]*/[a-zA-Z0-9_-.]*/[0-9]*/[0-9]*/)([a-zA-Z0-9_-.]*)';
const to = '/';
manager.addRedirect(from , to, {
permanent: false
});
const req = {
method: 'GET',
url: '/test-params/'
};
let headers = null;
let status = null;
let location = null;
const res = {
set(_headers) {
headers = _headers;
},
redirect(_status, _location) {
status = _status;
location = _location;
}
};
manager.handleRequest(req, res, function next() {
should.ok(true, 'next should have been called');
});
should.equal(headers, null);
should.equal(status, null);
should.equal(location, null);
});
});