Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fet-1490: basic CSP #85

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions blog/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { NextRequest, NextResponse } from 'next/server';

const PUBLIC_FILE = /\.(.*)$/;

const resources = [
'https://*.googletagmanager.com',
'plausible.io',
'static.cloudflareinsights.com',
'*.ens-app-v3.pages.dev',
'https://app.intercom.io',
'https://widget.intercom.io',
'https://js.intercomcdn.com',
].join(' ');

const frameAncestors = `frame-ancestors 'self' https://app.safe.global;`;

function cspMiddleware(req: NextRequest) {
const res = NextResponse.next();
const userAgent = req.headers.get('user-agent')?.toLowerCase() ?? '';

const isFirefox =
userAgent.includes('gecko/20100101') && userAgent.includes('firefox/');

res.headers.set(
'Content-Security-Policy',
isFirefox
? frameAncestors
: `worker-src 'self'; script-src 'self' 'sha256-UyYcl+sKCF/ROFZPHBlozJrndwfNiC5KT5ZZfup/pPc=' ${resources} 'wasm-unsafe-eval'; ${frameAncestors}`
);

return res;
}

export default async function middleware(req: NextRequest) {
const pathname = req.nextUrl.pathname;

if (pathname.startsWith('/_next') || PUBLIC_FILE.test(pathname)) {
return;
}

return cspMiddleware(req);
}
2 changes: 1 addition & 1 deletion blog/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const mdxOptions = {

/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'export',
// output: 'export',
reactStrictMode: true,
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'mdx'],
experimental: {
Expand Down
Loading