This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
middleware.js
149 lines (132 loc) · 4.39 KB
/
middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { NextResponse, userAgent } from 'next/server';
import nextSafe from 'next-safe';
import prohibitedCountries from './data/prohibited-countries.json';
const prohibitedCountriesCode = Object.values(prohibitedCountries);
const isDev = process.env.NODE_ENV !== 'production';
const getCspHeader = (browserName) => {
const walletconnectSrc = [
'https://verify.walletconnect.org',
'https://verify.walletconnect.com',
];
const connectSrc = [
"'self'",
...walletconnectSrc,
'https://*.olas.network/',
'https://*.autonolas.tech/',
'https://rpc.walletconnect.com/',
'wss://relay.walletconnect.org/',
'wss://relay.walletconnect.com/',
'https://explorer-api.walletconnect.com/',
'https://eth-mainnet.g.alchemy.com/v2/',
'https://eth-goerli.g.alchemy.com/v2/',
'https://gno.getblock.io/',
'https://polygon-mainnet.g.alchemy.com/v2/',
'https://polygon-mumbai-bor.publicnode.com/',
'https://rpc.chiado.gnosis.gateway.fm/',
'https://safe-transaction-mainnet.safe.global/api/v1/',
'https://safe-transaction-goerli.safe.global/api/',
'https://safe-transaction-gnosis-chain.safe.global/api/',
'https://safe-transaction-polygon.safe.global/api/',
'https://vercel.live/',
'https://api.devnet.solana.com/',
'wss://api.devnet.solana.com/',
'https://api.mainnet-beta.solana.com/',
'wss://api.mainnet-beta.solana.com/',
'https://holy-convincing-bird.solana-mainnet.quiknode.pro/',
'wss://holy-convincing-bird.solana-mainnet.quiknode.pro/',
'https://arb1.arbitrum.io/rpc/',
'https://sepolia-rollup.arbitrum.io/rpc',
'https://rpc.gnosischain.com/',
'https://mainnet.base.org/',
'https://sepolia.base.org/',
'https://mainnet.optimism.io',
'https://sepolia.optimism.io/',
'https://forno.celo.org',
'https://alfajores-forno.celo-testnet.org',
];
if (isDev) {
connectSrc.push('http://localhost');
connectSrc.push('ws://localhost');
}
const scriptSrc = ["'self'", 'https://vercel.live/', 'https://fonts.googleapis.com/'];
// Firefox blocks inline scripts by default and it's an issue with Metamask
// reference: https://github.com/MetaMask/metamask-extension/issues/3133
if (browserName === 'Firefox') {
scriptSrc.push("'unsafe-inline'");
}
const headers = [
...nextSafe({
isDev,
/**
* Content Security Policy
* @see https://content-security-policy.com/
*/
contentSecurityPolicy: {
'default-src': "'none'",
'script-src': scriptSrc,
'connect-src': connectSrc,
'img-src': [
"'self'",
'data:',
'https://*.autonolas.tech/',
'https://explorer-api.walletconnect.com/w3m/',
...walletconnectSrc,
],
'style-src': ["'self'", "'unsafe-inline'"],
'frame-src': ["'self'", ...walletconnectSrc],
},
permissionsPolicyDirectiveSupport: ['standard'],
}),
{
key: 'Strict-Transport-Security',
value: 'max-age=31536000; includeSubDomains',
},
];
return headers;
};
/**
*
* @param {string} countryName
* @param {string} pathName
* @returns {string} redirectUrl
*/
const getRedirectUrl = (countryName, pathName) => {
const isProhibited = prohibitedCountriesCode.includes(countryName);
if (pathName === '/not-legal') {
return isProhibited ? null : '/';
}
return isProhibited ? '/not-legal' : null;
};
/**
* common middleware
*
* @param {NextRequest} request
*/
export default async function middleware(request) {
const country = request.geo?.country;
const redirectUrl = getRedirectUrl(country, request.nextUrl.pathname);
const response = redirectUrl
? NextResponse.redirect(new URL(redirectUrl, request.nextUrl))
: NextResponse.next();
const browserName = userAgent(request)?.browser.name;
const cspHeaders = getCspHeader(browserName);
// apply CSP headers
// https://nextjs.org/docs/app/building-your-application/routing/middleware#setting-headers
cspHeaders.forEach((header) => {
const { key, value } = header;
response.headers.set(key, value);
});
return response;
}
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).*)',
],
};