diff --git a/ghost/express-dynamic-redirects/lib/DynamicRedirectManager.js b/ghost/express-dynamic-redirects/lib/DynamicRedirectManager.js index 50d13acf47..cb45386bbc 100644 --- a/ghost/express-dynamic-redirects/lib/DynamicRedirectManager.js +++ b/ghost/express-dynamic-redirects/lib/DynamicRedirectManager.js @@ -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; + } } /** diff --git a/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js b/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js index 8b23ebb714..0de0078d86 100644 --- a/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js +++ b/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js @@ -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); + }); });