Skip to content

Commit

Permalink
Geoblock Rogue States (#2376)
Browse files Browse the repository at this point in the history
* add middleware.ts to app

* fix formatting

* Remove USA
  • Loading branch information
Atmosfearful authored Jul 5, 2024
1 parent 05c4b5e commit cd7e55a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
50 changes: 50 additions & 0 deletions app/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { geolocation } from "@vercel/edge";
import type { NextRequest } from "next/server";

/**
* List of ISO 3166-1 Alpha 2 country codes.
* To compare against X-Vercel-IP-Country header.
* I advise against sharing these across packages, as each domain may have different legal requirements
* */
const BLOCKED_COUNTRY_MAP: { [key: string]: string } = {
IR: "Iran",
SY: "Syria",
KP: "North Korea",
AF: "Afghanistan",
CU: "Cuba",
VE: "Venezuela",
};

export function middleware(request: NextRequest) {
const country = geolocation(request)?.country?.toUpperCase();
const label = country ? BLOCKED_COUNTRY_MAP[country] : null;
const labels = Object.values(BLOCKED_COUNTRY_MAP);
if (!!label) {
return new Response(
`
<h1>Access Denied.</h1>
<p>Pending further regulatory guidance, users located in the following regions are not permitted to access this page:
<ul>
${labels.map((l) => `<li>${l}</li>`).join("")}
</ul>
</p>
`,
{
headers: { "content-type": "text/html" },
}
);
}
}

export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!api|_next/static|_next/image|favicon.ico).*)",
],
};
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@sentry/nextjs": "^7.77.0",
"@tippyjs/react": "^4.2.6",
"@translation/lingui": "^1.0.0",
"@vercel/edge": "^1.1.1",
"ethers": "^5.7.2",
"ethers-v6": "npm:ethers@^6.7.1",
"lodash": "^4.17.21",
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cd7e55a

Please sign in to comment.