2025-02-11 21:30:00 -08:00
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
|
|
|
export function middleware(req) {
|
|
|
|
// Check the Host header, if HOMEPAGE_ALLOWED_HOSTS is set
|
|
|
|
const host = req.headers.get("host");
|
|
|
|
const port = process.env.PORT || 3000;
|
2025-03-14 22:51:07 -07:00
|
|
|
let allowedHosts = [`localhost:${port}`, `127.0.0.1:${port}`];
|
2025-02-11 21:30:00 -08:00
|
|
|
if (process.env.HOMEPAGE_ALLOWED_HOSTS) {
|
|
|
|
allowedHosts = allowedHosts.concat(process.env.HOMEPAGE_ALLOWED_HOSTS.split(","));
|
|
|
|
}
|
|
|
|
if (!host || !allowedHosts.includes(host)) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(
|
2025-03-14 11:58:44 -07:00
|
|
|
`Host validation failed for: ${host}. Hint: Set the HOMEPAGE_ALLOWED_HOSTS environment variable to allow requests from this host / port.`,
|
2025-02-11 21:30:00 -08:00
|
|
|
);
|
|
|
|
return NextResponse.json({ error: "Host validation failed. See logs for more details." }, { status: 400 });
|
|
|
|
}
|
|
|
|
return NextResponse.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
export const config = {
|
|
|
|
matcher: "/api/:path*",
|
|
|
|
};
|