Skip to content

Commit f103f9c

Browse files
committed
fix: Routes protection
1 parent 4eb615d commit f103f9c

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

src/authentication/protected-routes.handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ export const isAdminRoute = (url: string, adminRootUrl: string): boolean => {
4343
.map((route) => convertToExpressRoute(route.path))
4444
.filter((route) => route !== "");
4545
const isAdminRootUrl = url === adminRootUrl;
46+
const urlWithoutRoot = url.substring(adminRootUrl?.length ?? 0, url.length);
4647

4748
return (
4849
isAdminRootUrl ||
49-
!!adminRoutes.find((route) => pathToRegexp(route).test(url))
50+
adminRoutes.some((route) => pathToRegexp(route).test(urlWithoutRoot))
5051
);
5152
};
5253

src/buildAuthenticatedRouter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const buildAuthenticatedRouter = (
5555
const router = predefinedRouter || express.Router();
5656

5757
router.use((req, _, next) => {
58+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5859
if ((req as any)._body) {
5960
next(new OldBodyParserUsedError());
6061
}

test/protected-routes.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,11 @@ describe("Protected routes", () => {
3232
it("should detect non-admin routes", () => {
3333
expect(isAdminRoute("/api/my-endpoint", "/")).toBeFalsy();
3434
});
35+
36+
it("should detect admin routes with base url", () => {
37+
expect(
38+
isAdminRoute("/admin/resources/someResource/actions/new", "/admin")
39+
).toBeTruthy();
40+
});
3541
});
3642
});

0 commit comments

Comments
 (0)