From b911c66bb6d32842788ba0411508d036b69105e0 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 10 May 2022 13:55:19 +0100 Subject: [PATCH] Fixed legacyApiPathMatch not working with subdirs - The recently refactored path matching code forgot to take into account that originalUrl can include the subdir - Added more permutations to tests and ensured that all tests pass - This means we don't have to worry about what sort of path we pass to the function, it'll figure out the version and api info --- .../legacy-api-path-match.js | 12 +++++++----- .../legacy-api-path-match.test.js | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/core/server/services/api-version-compatibility/legacy-api-path-match.js b/core/server/services/api-version-compatibility/legacy-api-path-match.js index 137912185a..70222a5c45 100644 --- a/core/server/services/api-version-compatibility/legacy-api-path-match.js +++ b/core/server/services/api-version-compatibility/legacy-api-path-match.js @@ -1,17 +1,19 @@ const pathMatch = require('path-match')(); module.exports = (url) => { + let basePath = 'ghost/api'; let apiRouteMatcher = '/:version(v2|v3|v4|canary)?/:api(admin|content)/*'; + let urlToMatch = url; - if (url.startsWith('/ghost/api')) { - apiRouteMatcher = `/ghost/api${apiRouteMatcher}`; + if (url.includes(basePath)) { + urlToMatch = url.split(basePath)[1]; } - if (!url.endsWith('/')) { - url += '/'; + if (!urlToMatch.endsWith('/')) { + urlToMatch += '/'; } - let {version, api} = pathMatch(apiRouteMatcher)(url); + let {version, api} = pathMatch(apiRouteMatcher)(urlToMatch); if (version === [null]) { version = null; diff --git a/test/unit/server/services/api-version-compatibility/legacy-api-path-match.test.js b/test/unit/server/services/api-version-compatibility/legacy-api-path-match.test.js index b95271362b..bd00797f21 100644 --- a/test/unit/server/services/api-version-compatibility/legacy-api-path-match.test.js +++ b/test/unit/server/services/api-version-compatibility/legacy-api-path-match.test.js @@ -5,16 +5,22 @@ const legacyApiPathMatch = require('../../../../../core/server/services/api-vers describe('Legacy Path Match', function () { it('returns null, admin for all supported permutations', function () { const permutations = [ + '/subdir/ghost/api/admin/', '/ghost/api/admin/', '/admin/', + '/subdir/ghost/api/admin', '/ghost/api/admin', '/admin', + '/subdir/ghost/api/admin/session/', '/ghost/api/admin/session/', '/admin/session/', + '/subdir/ghost/api/admin/session', '/ghost/api/admin/session', '/admin/session', + '/subdir/ghost/api/admin/session/something/', '/ghost/api/admin/session/something/', '/admin/session/something/', + '/subdir/ghost/api/admin/session/something', '/ghost/api/admin/session/something', '/admin/session/something' ]; @@ -26,16 +32,22 @@ describe('Legacy Path Match', function () { it('returns canary, admin for all supported permutations', function () { const permutations = [ + '/subdir/ghost/api/canary/admin/', '/ghost/api/canary/admin/', '/canary/admin/', + '/subdir/ghost/api/canary/admin', '/ghost/api/canary/admin', '/canary/admin', + '/subdir/ghost/api/canary/admin/session/', '/ghost/api/canary/admin/session/', '/canary/admin/session/', + '/subdir/ghost/api/canary/admin/session', '/ghost/api/canary/admin/session', '/canary/admin/session', + '/subdir/ghost/api/canary/admin/session/something/', '/ghost/api/canary/admin/session/something/', '/canary/admin/session/something/', + '/subdir/ghost/api/canary/admin/session/something', '/ghost/api/canary/admin/session/something', '/canary/admin/session/something' ]; @@ -47,16 +59,22 @@ describe('Legacy Path Match', function () { it('returns v4, admin for all permutations', function () { const permutations = [ + '/subdir/ghost/api/v4/admin/', '/ghost/api/v4/admin/', '/v4/admin/', + '/subdir/ghost/api/v4/admin', '/ghost/api/v4/admin', '/v4/admin', + '/subdir/ghost/api/v4/admin/session/', '/ghost/api/v4/admin/session/', '/v4/admin/session/', + '/subdir/ghost/api/v4/admin/session', '/ghost/api/v4/admin/session', '/v4/admin/session', + '/subdir/ghost/api/v4/admin/session/something/', '/ghost/api/v4/admin/session/something/', '/v4/admin/session/something/', + '/subdir/ghost/api/v4/admin/session/something', '/ghost/api/v4/admin/session/something', '/v4/admin/session/something' ];