Skip to content

Commit

Permalink
refactor(middleware): updated user agent checks and storage
Browse files Browse the repository at this point in the history
  • Loading branch information
hirotomoyamada committed Jun 25, 2024
1 parent 10ea58e commit 3c7c716
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions middleware.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"

export const middleware = ({ headers, ip }: NextRequest) => {
const blockedUserAgents = ["facebookexternalhit", "MJ12bot", "DataForSeoBot"]
const blockedUserAgents = process.env.BLOCKED_USER_AGENTS?.split(",") ?? []

const userAgent = headers.get("user-agent")
ip = headers.get("x-forwarded-for") || ip
Expand All @@ -13,7 +13,9 @@ export const middleware = ({ headers, ip }: NextRequest) => {
if (
userAgent &&
blockedUserAgents.some((blockedUserAgent) =>
userAgent.includes(blockedUserAgent),
userAgent
.toLocaleUpperCase()
.includes(blockedUserAgent.toLocaleUpperCase()),
)
) {
return new NextResponse("Access Denied", { status: 403 })
Expand Down

0 comments on commit 3c7c716

Please sign in to comment.