const PERMANENT_REDIRECT_STATUSES = new Set([301, 308]); const BOUNDED_CACHE_CONTROL_PATTERN = /\b(max-age\s*=\s*\d+|s-maxage\s*=\s*\d+|no-store)\b/i; const MESSAGE = "GitBook-generated permanent redirects (301/308) should set a bounded Cache-Control header (e.g. 'private, max-age=86400' or 'private, no-store') so browsers and CDNs do not cache redirect loops indefinitely. User-defined permanent redirects may omit it."; function getPropertyName(node) { if (!node) { return null; } if (node.type === 'Identifier') { return node.name; } if (node.type === 'Literal' && typeof node.value === 'string') { return node.value; } return null; } function unwrapExpression(node) { let current = node; while (current) { if ( current.type === 'ChainExpression' || current.type === 'ParenthesizedExpression' || current.type === 'TSAsExpression' || current.type === 'TSTypeAssertion' || current.type === 'TSNonNullExpression' || current.type === 'TSSatisfiesExpression' ) { current = current.expression; continue; } return current; } return current; } function expressionContainsPermanentStatus(node) { const current = unwrapExpression(node); if (!current) { return false; } if (current.type === 'Literal' && typeof current.value === 'number') { return PERMANENT_REDIRECT_STATUSES.has(current.value); } if (current.type === 'ConditionalExpression') { return ( expressionContainsPermanentStatus(current.consequent) || expressionContainsPermanentStatus(current.alternate) ); } return false; } function getObjectProperty(objectNode, targetName) { if (!objectNode || objectNode.type !== 'ObjectExpression') { return null; } const lowerTarget = targetName.toLowerCase(); for (const prop of objectNode.properties) { if (prop.type !== 'Property') { continue; } const name = getPropertyName(prop.key); if (name && name.toLowerCase() === lowerTarget) { return prop; } } return null; } function isBoundedCacheControlValue(valueNode) { const current = unwrapExpression(valueNode); if (!current) { return false; } if (current.type === 'Literal') { return ( typeof current.value === 'string' && BOUNDED_CACHE_CONTROL_PATTERN.test(current.value) ); } if (current.type === 'TemplateLiteral') { const raw = current.quasis.map((q) => q.value.raw).join('0'); return BOUNDED_CACHE_CONTROL_PATTERN.test(raw); } // Dynamic variables or helpers are allowed when an explicit Cache-Control entry is present. return true; } function objectExpressionHasBoundedCacheControl(objectNode) { for (const prop of objectNode.properties) { if (prop.type === 'SpreadElement') { return true; } if (prop.type !== 'Property') { continue; } const name = getPropertyName(prop.key); if (name && name.toLowerCase() === 'cache-control') { return isBoundedCacheControlValue(prop.value); } } return false; } function headersObjectHasCacheControl(headersNode) { const current = unwrapExpression(headersNode); if (!current) { return false; } if (current.type === 'Identifier' && current.name === 'undefined') { return false; } if (current.type === 'Literal' && current.value === null) { return false; } if (current.type === 'ObjectExpression') { return objectExpressionHasBoundedCacheControl(current); } if ( current.type === 'NewExpression' && current.callee.type === 'Identifier' && current.callee.name === 'Headers' ) { if (current.arguments.length === 0) { return false; } return headersObjectHasCacheControl(current.arguments[0]); } // Non-literal Headers instances or variables may set Cache-Control dynamically. return true; } function checkResponseInit(context, reportNode, initNode) { if (!initNode || initNode.type !== 'ObjectExpression') { return; } const statusProp = getObjectProperty(initNode, 'status'); if (!statusProp || !expressionContainsPermanentStatus(statusProp.value)) { return; } const headersProp = getObjectProperty(initNode, 'headers'); if (!headersProp || !headersObjectHasCacheControl(headersProp.value)) { context.report({ node: reportNode, message: MESSAGE }); } } export default { meta: { name: 'gitbook-cache', }, rules: { 'no-unbounded-permanent-redirect': { create(context) { return { NewExpression(node) { if (node.callee.type !== 'Identifier') { return; } if ( node.callee.name !== 'Response' && node.callee.name !== 'NextResponse' ) { return; } checkResponseInit(context, node, node.arguments[1]); }, CallExpression(node) { if ( node.callee.type !== 'MemberExpression' || node.callee.computed || node.callee.object.type !== 'Identifier' ) { return; } const objName = node.callee.object.name; const propName = getPropertyName(node.callee.property); if ( propName !== 'redirect' || (objName !== 'Response' && objName !== 'NextResponse') ) { return; } const secondArg = node.arguments[1]; if (expressionContainsPermanentStatus(secondArg)) { context.report({ node, message: MESSAGE }); return; } checkResponseInit(context, node, secondArg); }, }; }, }, }, };