Security: support host header validation
This commit is contained in:
parent
e6a821ecc3
commit
ee7be33fa1
17
src/middleware.js
Normal file
17
src/middleware.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
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 allowedHosts = process.env.HOMEPAGE_ALLOWED_HOSTS
|
||||||
|
? process.env.HOMEPAGE_ALLOWED_HOSTS.split(",").concat(["localhost:3000"])
|
||||||
|
: [];
|
||||||
|
if (allowedHosts.length && !(host || allowedHosts.includes(host))) {
|
||||||
|
return new NextResponse("Invalid Host header", { status: 400 });
|
||||||
|
}
|
||||||
|
return NextResponse.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
matcher: "/api/:path*",
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user