-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix security issues caused by withProtectedRoutes middleware
- Loading branch information
Showing
3 changed files
with
101 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,21 @@ | ||
import AdminJS, { Router as AdminRouter } from "adminjs"; | ||
import { Router } from "express"; | ||
import { convertToExpressRoute } from "../convertRoutes"; | ||
import { pathToRegexp } from "path-to-regexp"; | ||
|
||
const doesNotRequireAuthentication = ( | ||
url: string, | ||
{ loginPath, logoutPath } | ||
) => { | ||
return ( | ||
isAdminAsset(url) || url.startsWith(loginPath) || url.startsWith(logoutPath) | ||
); | ||
}; | ||
import AdminJS from "adminjs"; | ||
import { Router, RequestHandler } from "express"; | ||
|
||
export const withProtectedRoutesHandler = ( | ||
router: Router, | ||
admin: AdminJS | ||
): void => { | ||
const { rootPath, loginPath, logoutPath } = admin.options; | ||
|
||
router.use((req, res, next) => { | ||
if ( | ||
doesNotRequireAuthentication(req.originalUrl, { loginPath, logoutPath }) | ||
) { | ||
return next(); | ||
} | ||
|
||
if (isAdminRoute(req.originalUrl, rootPath)) { | ||
if (!!req.session.adminUser) { | ||
return next(); | ||
} | ||
return res.redirect(loginPath); | ||
} | ||
|
||
return next(); // custom routes in admin router | ||
}); | ||
}; | ||
|
||
export const isAdminRoute = ( | ||
originalUrl: string, | ||
adminRootPath: string | ||
): boolean => { | ||
const adminRoutes = AdminRouter.routes | ||
.map((route) => convertToExpressRoute(route.path)) | ||
.filter((route) => route !== ""); | ||
|
||
let urlWithoutAdminRootPath = originalUrl.split("?")[0]; | ||
if (adminRootPath !== "/") { | ||
urlWithoutAdminRootPath = urlWithoutAdminRootPath.replace( | ||
adminRootPath, | ||
"" | ||
); | ||
if (!urlWithoutAdminRootPath.startsWith("/")) { | ||
urlWithoutAdminRootPath = `/${urlWithoutAdminRootPath}`; | ||
const { loginPath } = admin.options; | ||
const authorizedRoutesMiddleware: RequestHandler = ( | ||
request, | ||
response, | ||
next | ||
) => { | ||
if (!request.session || !request.session.adminUser) { | ||
return response.redirect(loginPath); | ||
} | ||
} | ||
|
||
const isAdminRootUrl = originalUrl === adminRootPath; | ||
const isUrlUnderRootPath = originalUrl.startsWith(adminRootPath); | ||
return next(); | ||
}; | ||
|
||
return ( | ||
isAdminRootUrl || | ||
(adminRoutes.some((route) => | ||
pathToRegexp(route).test(urlWithoutAdminRootPath) | ||
) && | ||
isUrlUnderRootPath) | ||
); | ||
router.use(authorizedRoutesMiddleware); | ||
}; | ||
|
||
const isAdminAsset = (url: string) => | ||
AdminRouter.assets.find((asset) => url.match(asset.path)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters