Skip to content

Commit

Permalink
Security: support host header validation
Browse files Browse the repository at this point in the history
  • Loading branch information
shamoon committed Jan 27, 2025
1 parent e6a821e commit 2e6d205
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NextResponse } from "next/server";

export function middleware(req) {
// Ensure the request is coming from localhost
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*",
};

0 comments on commit 2e6d205

Please sign in to comment.