-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: security headers for apps (#65)
* feat: Add next-middleware library and related files * feat: renames files for middleware * feat: remove unsafe-inline for style-src * chore: include object-src * chore: remove unsafe inline for firefox * chore: IMPORTANT - got A+ but changing the policy to make it work as it is broken now * chore: adding unsafe-inline for style-src * chore: remove font-src, object-src and scriptSrc to have unsafe-inline (for firefox) * refractor: move constants above the function in cspHeader, remove browerName usage as it works on firefox * chore: add 'nonce' support for style-src and commented unsafe-inline * chore: add nonce * chore: remove nonce and add unsafe-inline for style * chore: add api.thegraph.com origin * feat: add middleware to other apps * feat: remove headers from next.config.js for bond & tokenomics app * chore: rename getCspHeader function name * Update libs/common-middleware/src/lib/cspHeader.ts Co-authored-by: Josh Miller <[email protected]> * refactor: Remove duplicate code for address prohibition check * refactor: Remove duplicate code for address prohibition check * feat: Add Vercel links to CSP allowed origins * feat: Add gateway links to CSP allowed origins * feat: Update IPFS gateway links in CSP allowed origins --------- Co-authored-by: Josh Miller <[email protected]>
- Loading branch information
1 parent
895b96b
commit 37375b3
Showing
29 changed files
with
323 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,150 +1,4 @@ | ||
import nextSafe from 'next-safe'; | ||
import { NextRequest, NextResponse, userAgent } from 'next/server'; | ||
import { config, middleware } from 'libs/common-middleware/src'; | ||
|
||
import prohibitedCountries from 'libs/util-prohibited-data/src/lib/prohibited-countries.json'; | ||
|
||
const prohibitedCountriesCode = Object.values(prohibitedCountries); | ||
|
||
const isDev = process.env.NODE_ENV !== 'production'; | ||
|
||
const getCspHeader = (browserName?: string) => { | ||
if (!process.env.NEXT_PUBLIC_AUTONOLAS_SUB_GRAPH_URL) return []; | ||
|
||
const walletconnectSrc = ['https://verify.walletconnect.org', 'https://verify.walletconnect.com']; | ||
|
||
const connectSrc: CSPDirective = [ | ||
"'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', | ||
'https://api.web3modal.com/', | ||
'wss://www.walletlink.org/rpc', | ||
'wss://*.pusher.com/', | ||
process.env.NEXT_PUBLIC_AUTONOLAS_SUB_GRAPH_URL, | ||
]; | ||
|
||
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 nextSafeHeaders = | ||
typeof nextSafe === 'function' | ||
? // TODO | ||
// @ts-expect-error: For some reason, TypeScript is not recognizing the function | ||
nextSafe({ | ||
isDev, | ||
/** | ||
* Content Security Policy | ||
* @see https://content-security-policy.com/ | ||
*/ | ||
contentSecurityPolicy: { | ||
'default-src': "'none'", | ||
'script-src': scriptSrc, | ||
'connect-src': connectSrc, | ||
'img-src': [ | ||
"'self'", | ||
'blob:', | ||
'data:', | ||
'https://*.autonolas.tech/', | ||
'https://explorer-api.walletconnect.com/w3m/', | ||
...walletconnectSrc, | ||
], | ||
'style-src': ["'self'", "'unsafe-inline'", 'https://fonts.googleapis.com/'], | ||
'frame-src': ["'self'", 'https://vercel.live/', ...walletconnectSrc], | ||
}, | ||
permissionsPolicyDirectiveSupport: ['standard'], | ||
}) | ||
: []; | ||
|
||
const headers = [ | ||
...nextSafeHeaders, | ||
{ | ||
key: 'Strict-Transport-Security', | ||
value: 'max-age=31536000; includeSubDomains', | ||
}, | ||
]; | ||
|
||
return headers; | ||
}; | ||
|
||
const getRedirectUrl = (pathName: string, countryName?: string) => { | ||
const isProhibited = countryName ? prohibitedCountriesCode.includes(countryName) : false; | ||
|
||
if (pathName === '/not-legal') { | ||
return isProhibited ? null : '/'; | ||
} | ||
return isProhibited ? '/not-legal' : null; | ||
}; | ||
|
||
export default async function middleware(request: NextRequest) { | ||
const country = request.geo?.country; | ||
const redirectUrl = getRedirectUrl(request.nextUrl.pathname, country); | ||
|
||
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).*)', | ||
], | ||
}; | ||
export default middleware; | ||
export { config }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './addresses'; | ||
export * from './chains'; | ||
export * from './errors'; | ||
export * from './ethers'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { config, middleware } from 'libs/common-middleware/src'; | ||
|
||
export default middleware; | ||
export { config }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { config, middleware } from 'libs/common-middleware/src'; | ||
|
||
export default middleware; | ||
export { config }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { config, middleware } from 'libs/common-middleware/src'; | ||
|
||
export default middleware; | ||
export { config }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './addresses'; | ||
export * from './errors'; | ||
export * from './ethers'; | ||
export * from './time'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { config, middleware } from 'libs/common-middleware/src'; | ||
|
||
export default middleware; | ||
export { config }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.