const { readFileSync } = require('node:fs') const { join } = require('node:path') const vm = require('node:vm') const { getProxiedPathPatterns } = require('../lib/security-headers.js') const SUPABASE_ORIGIN = 'https://supabase.com' // Evaluated as production: the /docs rewrite only exists there, and /docs links must be flagged in every environment. function loadProductionRewrites() { const rewritesModule = { exports: [] } const source = readFileSync(join(__dirname, '../lib/rewrites.js'), 'utf8') vm.runInNewContext(source, { module: rewritesModule, process: { env: { NEXT_PUBLIC_VERCEL_ENV: 'production' } }, }) return rewritesModule.exports } // next.js path matching, so a regex source flags only the paths that it proxies const crossZonePatterns = getProxiedPathPatterns(loadProductionRewrites()).map( (pattern) => new RegExp(`^(?:${pattern})$`, 'i') ) function parseHref(href) { try { return new URL(href, SUPABASE_ORIGIN) } catch { return null } } function isCrossZonePath(href) { const url = parseHref(href) if (url?.origin !== SUPABASE_ORIGIN) return false return crossZonePatterns.some((pattern) => pattern.test(url.pathname.slice(1))) } function staticHrefPrefix(value) { if (!value) return null if (value.type === 'Literal') return typeof value.value === 'string' ? value.value : null if (value.type !== 'JSXExpressionContainer') return null const { expression } = value if (expression.type === 'Literal' && typeof expression.value === 'string') return expression.value if (expression.type === 'TemplateLiteral') return expression.quasis[0].value.cooked return null } /** @type {import('eslint').Rule.RuleModule} */ module.exports = { meta: { type: 'problem', messages: { crossZone: "'{{href}}' is served by another zone. Use a plain : Next.js can't prefetch or client-navigate across zones, so 404s its prefetch.", }, schema: [], }, create(context) { const linkNames = new Set() return { ImportDeclaration(node) { if (node.source.value !== 'next/link') return for (const specifier of node.specifiers) { if (specifier.type === 'ImportDefaultSpecifier') linkNames.add(specifier.local.name) } }, JSXOpeningElement(node) { if (node.name.type !== 'JSXIdentifier' || !linkNames.has(node.name.name)) return const hrefAttribute = node.attributes.find( (attribute) => attribute.type === 'JSXAttribute' && attribute.name.name === 'href' ) const href = staticHrefPrefix(hrefAttribute?.value) if (!href || !isCrossZonePath(href)) return context.report({ node: hrefAttribute, messageId: 'crossZone', data: { href } }) }, } }, }