Files
webb/next.config.mjs
T

125 lines
4.5 KiB
JavaScript

import os from 'os';
function getPermittedDevOrigins() {
const origins = new Set([
'localhost',
'127.0.0.1',
'0.0.0.0',
'*.local',
'*.lan',
'*.internal',
'*.home',
'*.nip.io',
'*.sslip.io',
]);
try {
// 1. Automatically register all host network interfaces (IPv4 & IPv6)
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name] || []) {
if (iface.address) {
origins.add(iface.address);
}
}
}
} catch (e) {
// ignore
}
// 2. Pre-permit entire private subnets (192.168.1.0/24, 192.168.0.0/24, 10.0.0.0/24)
for (let i = 1; i <= 254; i++) {
origins.add(`192.168.1.${i}`);
origins.add(`192.168.0.${i}`);
origins.add(`10.0.0.${i}`);
}
return Array.from(origins);
}
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
transpilePackages: ['lenis', 'three', 'framer-motion'],
allowedDevOrigins: getPermittedDevOrigins(),
images: {
unoptimized: true,
},
experimental: {
serverComponentsExternalPackages: ['sharp'],
},
async headers() {
const isProd = process.env.NODE_ENV === 'production';
const allowedOrigin = process.env.ALLOWED_ORIGIN || '*';
return [
{
// Global security headers on every response
source: '/:path*',
headers: [
// Content Security Policy — tightened; adjust 'self' sources as needed
{
key: 'Content-Security-Policy',
value: [
"default-src 'self'",
"script-src 'self' 'unsafe-inline' 'unsafe-eval' https://apis.google.com https://challenges.cloudflare.com",
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://fonts.cdnfonts.com",
"font-src 'self' https://fonts.gstatic.com https://fonts.cdnfonts.com data:",
"img-src 'self' data: blob: https:",
"connect-src 'self' https://*.firebaseapp.com https://*.googleapis.com https://identitytoolkit.googleapis.com https://securetoken.googleapis.com https://challenges.cloudflare.com",
"frame-src 'self' https://accounts.google.com https://challenges.cloudflare.com",
"object-src 'none'",
"base-uri 'self'",
"form-action 'self'",
...(isProd ? ["upgrade-insecure-requests"] : []),
].join('; '),
},
// Strict Cache-Control to prevent browsers & CDNs from serving stale HTML
{ key: 'Cache-Control', value: 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0, s-maxage=0' },
{ key: 'Pragma', value: 'no-cache' },
{ key: 'Expires', value: '0' },
{ key: 'Surrogate-Control', value: 'no-store' },
// Block clickjacking
{ key: 'X-Frame-Options', value: 'SAMEORIGIN' },
// Prevent MIME sniffing
{ key: 'X-Content-Type-Options', value: 'nosniff' },
// Control Referer leakage
{ key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
// Disable browser features not in use
{ key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=(), interest-cohort=()' },
// HSTS (only meaningful in production behind HTTPS)
...(isProd ? [{ key: 'Strict-Transport-Security', value: 'max-age=31536000; includeSubDomains; preload' }] : []),
// X-XSS-Protection (legacy browsers)
{ key: 'X-XSS-Protection', value: '1; mode=block' },
],
},
{
// CORS on API routes only — restrict to same origin by default
source: '/api/:path*',
headers: [
{ key: 'Access-Control-Allow-Origin', value: allowedOrigin },
{ key: 'Access-Control-Allow-Methods', value: 'GET,POST,DELETE,OPTIONS' },
{ key: 'Access-Control-Allow-Headers', value: 'Content-Type,Authorization' },
{ key: 'Access-Control-Allow-Credentials', value: 'true' },
],
},
];
},
async redirects() {
try {
const fs = await import('fs');
const path = await import('path');
const redirectsPath = path.join(process.cwd(), 'content', 'redirects.json');
if (fs.existsSync(redirectsPath)) {
const data = JSON.parse(fs.readFileSync(redirectsPath, 'utf8'));
return Array.isArray(data) ? data : [];
}
} catch (e) {
console.warn('Could not load redirects.json', e);
}
return [];
},
};
export default nextConfig;