|
| 1 | +import { ERRORS } from '@internal/errors' |
| 2 | + |
| 3 | +const SIMPLE_RULES = [ |
| 4 | + 'all', |
| 5 | + 'noindex', |
| 6 | + 'nofollow', |
| 7 | + 'none', |
| 8 | + 'nosnippet', |
| 9 | + 'indexifembedded', |
| 10 | + 'notranslate', |
| 11 | + 'noimageindex', |
| 12 | +] as const |
| 13 | + |
| 14 | +const PARAMETRIC_RULES = [ |
| 15 | + 'max-snippet', |
| 16 | + 'max-image-preview', |
| 17 | + 'max-video-preview', |
| 18 | + 'unavailable_after', |
| 19 | +] as const |
| 20 | + |
| 21 | +const simpleRulesPattern = SIMPLE_RULES.join('|') |
| 22 | +const parametricRulesPattern = PARAMETRIC_RULES.join('|') |
| 23 | +const SIMPLE_RULE_REGEX = new RegExp(`^(${simpleRulesPattern})$`) |
| 24 | +const PARAMETRIC_RULE_REGEX = new RegExp(`^(${parametricRulesPattern}):\\s*(.*)$`) |
| 25 | +const PARAMETRIC_RULE_START_REGEX = new RegExp(`^(${parametricRulesPattern}):`) |
| 26 | +const VALID_IMAGE_PREVIEW_VALUES = new Set(['none', 'standard', 'large']) |
| 27 | + |
| 28 | +/** |
| 29 | + * Validates the X-Robots-Tag header value according to MDN specification |
| 30 | + * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Robots-Tag |
| 31 | + * |
| 32 | + * @param value - The X-Robots-Tag header value to validate |
| 33 | + * @throws {Error} If the header value is invalid |
| 34 | + */ |
| 35 | +export function validateXRobotsTag(value: string): void { |
| 36 | + if (!value || typeof value !== 'string') { |
| 37 | + throw ERRORS.InvalidXRobotsTag('X-Robots-Tag header value must be a non-empty string') |
| 38 | + } |
| 39 | + |
| 40 | + const trimmedValue = value.trim() |
| 41 | + if (!trimmedValue) { |
| 42 | + throw ERRORS.InvalidXRobotsTag('X-Robots-Tag header value must be a non-empty string') |
| 43 | + } |
| 44 | + |
| 45 | + const parts = splitRules(trimmedValue) |
| 46 | + |
| 47 | + for (const part of parts) { |
| 48 | + if (!part) { |
| 49 | + throw ERRORS.InvalidXRobotsTag('X-Robots-Tag header contains empty rule') |
| 50 | + } |
| 51 | + |
| 52 | + // Check if this is a parametric rule |
| 53 | + const parametricMatch = part.match(PARAMETRIC_RULE_REGEX) |
| 54 | + if (parametricMatch) { |
| 55 | + const [, ruleName, ruleValue] = parametricMatch |
| 56 | + validateParametricRule(ruleName, ruleValue.trim(), VALID_IMAGE_PREVIEW_VALUES) |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + // Check if this is a simple rule |
| 61 | + if (SIMPLE_RULE_REGEX.test(part)) { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + // Check if this has a colon (could be user agent prefix) |
| 66 | + const colonIndex = part.indexOf(':') |
| 67 | + if (colonIndex !== -1) { |
| 68 | + const beforeColon = part.substring(0, colonIndex).trim() |
| 69 | + const afterColon = part.substring(colonIndex + 1).trim() |
| 70 | + |
| 71 | + if (!afterColon) { |
| 72 | + throw ERRORS.InvalidXRobotsTag( |
| 73 | + `X-Robots-Tag user agent "${beforeColon}" has no rules specified` |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + // Recursively validate user agent rules |
| 78 | + validateXRobotsTag(afterColon) |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + throw ERRORS.InvalidXRobotsTag(`Invalid X-Robots-Tag rule: "${part}"`) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Splits rules by comma, handling parametric rules with dates that contain commas |
| 88 | + */ |
| 89 | +function splitRules(value: string): string[] { |
| 90 | + const parts: string[] = [] |
| 91 | + let remaining = value |
| 92 | + |
| 93 | + while (remaining) { |
| 94 | + remaining = remaining.trim() |
| 95 | + if (!remaining) break |
| 96 | + |
| 97 | + const match = remaining.match(PARAMETRIC_RULE_START_REGEX) |
| 98 | + if (match) { |
| 99 | + const ruleName = match[1] |
| 100 | + |
| 101 | + // For unavailable_after, extract date value (may contain commas) |
| 102 | + if (ruleName === 'unavailable_after') { |
| 103 | + // Build regex to find end of date by looking for comma + known rule or user agent |
| 104 | + const endPattern = new RegExp( |
| 105 | + `unavailable_after:\\s*(.+?)(?:,\\s*(?:${simpleRulesPattern}|${parametricRulesPattern}|[a-zA-Z0-9_-]+:)|$)` |
| 106 | + ) |
| 107 | + const dateEndMatch = remaining.match(endPattern) |
| 108 | + |
| 109 | + if (dateEndMatch) { |
| 110 | + const fullRule = `unavailable_after: ${dateEndMatch[1].trim()}` |
| 111 | + parts.push(fullRule) |
| 112 | + remaining = remaining.substring(fullRule.length).replace(/^,\s*/, '').trim() |
| 113 | + } else { |
| 114 | + parts.push(remaining) |
| 115 | + remaining = '' |
| 116 | + } |
| 117 | + continue |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Default: split by comma (for other parametric rules and simple rules) |
| 122 | + const nextComma = remaining.indexOf(',') |
| 123 | + if (nextComma === -1) { |
| 124 | + parts.push(remaining) |
| 125 | + remaining = '' |
| 126 | + } else { |
| 127 | + parts.push(remaining.substring(0, nextComma).trim()) |
| 128 | + remaining = remaining.substring(nextComma + 1).trim() |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return parts |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Validates a parametric rule value |
| 137 | + */ |
| 138 | +function validateParametricRule( |
| 139 | + ruleName: string, |
| 140 | + ruleValue: string, |
| 141 | + validImagePreviewValues: Set<string> |
| 142 | +): void { |
| 143 | + if (!ruleValue) { |
| 144 | + throw ERRORS.InvalidXRobotsTag(`X-Robots-Tag rule "${ruleName}" requires a value`) |
| 145 | + } |
| 146 | + |
| 147 | + switch (ruleName) { |
| 148 | + case 'max-snippet': { |
| 149 | + const num = parseInt(ruleValue, 10) |
| 150 | + if (isNaN(num) || num < 0) { |
| 151 | + throw ERRORS.InvalidXRobotsTag( |
| 152 | + `X-Robots-Tag "max-snippet" value must be a non-negative number, got: "${ruleValue}"` |
| 153 | + ) |
| 154 | + } |
| 155 | + break |
| 156 | + } |
| 157 | + |
| 158 | + case 'max-image-preview': { |
| 159 | + if (!validImagePreviewValues.has(ruleValue)) { |
| 160 | + throw ERRORS.InvalidXRobotsTag( |
| 161 | + `X-Robots-Tag "max-image-preview" value must be one of: none, standard, large, got: "${ruleValue}"` |
| 162 | + ) |
| 163 | + } |
| 164 | + break |
| 165 | + } |
| 166 | + |
| 167 | + case 'max-video-preview': { |
| 168 | + const num = parseInt(ruleValue, 10) |
| 169 | + if (isNaN(num) || num < -1) { |
| 170 | + throw ERRORS.InvalidXRobotsTag( |
| 171 | + `X-Robots-Tag "max-video-preview" value must be a number >= -1, got: "${ruleValue}"` |
| 172 | + ) |
| 173 | + } |
| 174 | + break |
| 175 | + } |
| 176 | + |
| 177 | + case 'unavailable_after': { |
| 178 | + // Check if it's a valid date string (try parsing it) |
| 179 | + const date = new Date(ruleValue) |
| 180 | + if (isNaN(date.getTime())) { |
| 181 | + throw ERRORS.InvalidXRobotsTag( |
| 182 | + `X-Robots-Tag "unavailable_after" value must be a valid date, got: "${ruleValue}"` |
| 183 | + ) |
| 184 | + } |
| 185 | + break |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments