fix: scope custom domain CORS to public status page routes
Only allow registered custom domain origins on public GET/OPTIONS status page API routes instead of the entire application API surface.
This commit is contained in:
1 parent
0aef8b652f
commit
6a1ab622b3
3 files changed
+58
-8
No files matched your search
@@ -0,0 +1,24 @@
|
||||
const PUBLIC_STATUS_PAGE_API_PREFIX = "/api/v1/status-page";
|
||||
|
||||
export const isPublicStatusPageApiPath = (method: string, path: string): boolean => {
|
||||
if (!path.startsWith(PUBLIC_STATUS_PAGE_API_PREFIX)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (method !== "GET" && method !== "OPTIONS") {
|
||||
return false;
|
||||
}
|
||||
|
||||
const subPath = path.slice(PUBLIC_STATUS_PAGE_API_PREFIX.length).split("?")[0] ?? "";
|
||||
const slug = subPath.replace(/^\//, "");
|
||||
|
||||
if (slug === "resolve") {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!slug || slug === "team" || slug.includes("/")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
+15
-8
@@ -13,6 +13,7 @@ import { InitializedServices } from "@/config/services.js";
|
||||
import { InitializedControllers } from "@/config/controllers.js";
|
||||
import { EnvConfig } from "@/domain/app-settings/app-settings.service.js";
|
||||
import { createStatusPageCorsOrigin } from "@/api/middleware/statusPageCorsOrigin.js";
|
||||
import { isPublicStatusPageApiPath } from "@/api/middleware/statusPagePublicApiPath.js";
|
||||
|
||||
export const createApp = ({
|
||||
services,
|
||||
@@ -29,17 +30,23 @@ export const createApp = ({
|
||||
}) => {
|
||||
const allowedOrigin = envSettings.clientHost;
|
||||
const app = express();
|
||||
const defaultCorsOptions = {
|
||||
origin: allowedOrigin,
|
||||
methods: "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS",
|
||||
allowedHeaders: ["Content-Type", "Authorization", "Accept-Language"],
|
||||
credentials: true,
|
||||
} as const;
|
||||
const publicStatusPageCorsOrigin = createStatusPageCorsOrigin(allowedOrigin, services.statusPagesRepository);
|
||||
|
||||
app.use(generalApiLimiter);
|
||||
|
||||
app.use(
|
||||
cors({
|
||||
origin: createStatusPageCorsOrigin(allowedOrigin, services.statusPagesRepository),
|
||||
methods: "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS",
|
||||
allowedHeaders: ["Content-Type", "Authorization", "Accept-Language"],
|
||||
credentials: true,
|
||||
})
|
||||
);
|
||||
app.use((req, res, next) => {
|
||||
const corsOptions = isPublicStatusPageApiPath(req.method, req.path)
|
||||
? { ...defaultCorsOptions, origin: publicStatusPageCorsOrigin }
|
||||
: defaultCorsOptions;
|
||||
|
||||
return cors(corsOptions)(req, res, next);
|
||||
});
|
||||
|
||||
app.use(express.static(frontendPath));
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { describe, expect, it } from "@jest/globals";
|
||||
import { isPublicStatusPageApiPath } from "../../../src/api/middleware/statusPagePublicApiPath.ts";
|
||||
|
||||
describe("isPublicStatusPageApiPath", () => {
|
||||
it("allows public resolve and slug routes", () => {
|
||||
expect(isPublicStatusPageApiPath("GET", "/api/v1/status-page/resolve")).toBe(true);
|
||||
expect(isPublicStatusPageApiPath("OPTIONS", "/api/v1/status-page/resolve")).toBe(true);
|
||||
expect(isPublicStatusPageApiPath("GET", "/api/v1/status-page/my-status-page")).toBe(true);
|
||||
expect(isPublicStatusPageApiPath("GET", "/api/v1/status-page/my-status-page?type=uptime")).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects authenticated and mutating status page routes", () => {
|
||||
expect(isPublicStatusPageApiPath("GET", "/api/v1/status-page/team")).toBe(false);
|
||||
expect(isPublicStatusPageApiPath("POST", "/api/v1/status-page")).toBe(false);
|
||||
expect(isPublicStatusPageApiPath("PUT", "/api/v1/status-page/64b1f2a3c4d5e6f7a8b9c0d1")).toBe(false);
|
||||
expect(isPublicStatusPageApiPath("DELETE", "/api/v1/status-page/64b1f2a3c4d5e6f7a8b9c0d1")).toBe(false);
|
||||
expect(isPublicStatusPageApiPath("GET", "/api/v1/monitors")).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user