fix: 修复 API 前缀下的管理端鉴权绕过
This commit is contained in:
1 parent
3ae7b462f5
commit
476e3d93dd
2 files changed
+37
-6
No files matched your search
@@ -3,20 +3,25 @@ import type { CanActivate, ExecutionContext } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { AdminAccessService } from "./admin-access.service.js";
|
||||
|
||||
const PATH_METADATA = "path";
|
||||
|
||||
@Injectable()
|
||||
export class AdminMembershipGuard implements CanActivate {
|
||||
constructor(private readonly access: AdminAccessService) {}
|
||||
|
||||
canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest<FastifyRequest>();
|
||||
if (request.url.startsWith("/admin/session/login")) return Promise.resolve(true);
|
||||
if (request.url.startsWith("/admin/session/change-password")) return Promise.resolve(true);
|
||||
if (!request.url.startsWith("/admin/")) return Promise.resolve(true);
|
||||
const handlerPath = Reflect.getMetadata(PATH_METADATA, context.getHandler());
|
||||
const controllerPath = Reflect.getMetadata(PATH_METADATA, context.getClass());
|
||||
const routePath = `${controllerPath ?? ""}/${handlerPath ?? ""}`.replace(/^\/+|\/+$/g, "");
|
||||
if (routePath === "admin/session/login") return Promise.resolve(true);
|
||||
if (routePath === "admin/session/change-password") return Promise.resolve(true);
|
||||
if (!routePath.startsWith("admin/")) return Promise.resolve(true);
|
||||
if (
|
||||
/^\/admin\/workspace\/(install|capabilities(?:\/|$)|template(?:\/|$)|fields(?:\/|$))/.test(
|
||||
request.url,
|
||||
/^admin\/workspace\/(install|capabilities(?:\/|$)|template(?:\/|$)|fields(?:\/|$))/.test(
|
||||
routePath,
|
||||
) ||
|
||||
/^\/admin\/config\/visual-template(?:\/|$)/.test(request.url)
|
||||
/^admin\/config\/visual-template(?:\/|$)/.test(routePath)
|
||||
) {
|
||||
return this.access.requireRole(request, ["owner", "admin", "platform-admin"]);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,32 @@ before(async () => {
|
||||
await app.init();
|
||||
});
|
||||
|
||||
test("admin routes remain guarded when an API prefix is configured", async () => {
|
||||
process.env.API_PREFIX = "api";
|
||||
try {
|
||||
const prefixedApp = await createApp();
|
||||
await prefixedApp.init();
|
||||
const injected = prefixedApp.getHttpAdapter().getInstance();
|
||||
const missingAuth = await injected.inject({ method: "GET", url: "/api/admin/bookings" });
|
||||
assert.equal(missingAuth.statusCode, 401);
|
||||
const customerAuth = await injected.inject({
|
||||
method: "GET",
|
||||
url: "/api/admin/bookings",
|
||||
headers: { authorization: `Bearer ${customerToken}` },
|
||||
});
|
||||
assert.equal(customerAuth.statusCode, 403);
|
||||
const login = await injected.inject({
|
||||
method: "POST",
|
||||
url: "/api/admin/session/login",
|
||||
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
||||
});
|
||||
assert.equal(login.statusCode, 201);
|
||||
await prefixedApp.close();
|
||||
} finally {
|
||||
process.env.API_PREFIX = "";
|
||||
}
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
await prisma.brandRelease.deleteMany({ where: { brandId } });
|
||||
|
||||
Reference in new issue
Block a user