|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import { addError } from 'markdownlint-rule-helpers' |
| 4 | + |
| 5 | +import { getFrontmatter } from '../helpers/utils' |
| 6 | +import type { RuleParams, RuleErrorCallback } from '@/content-linter/types' |
| 7 | + |
| 8 | +interface Frontmatter { |
| 9 | + children?: string[] |
| 10 | + [key: string]: unknown |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Check if a child path is valid. |
| 15 | + * Supports both: |
| 16 | + * - Relative paths (e.g., /local-child) resolved from current directory |
| 17 | + * - Absolute /content/ paths (e.g., /content/actions/workflows) resolved from content root |
| 18 | + */ |
| 19 | +function isValidChildPath(childPath: string, currentFilePath: string): boolean { |
| 20 | + const ROOT = process.env.ROOT || '.' |
| 21 | + const contentDir = path.resolve(ROOT, 'content') |
| 22 | + |
| 23 | + let resolvedPath: string |
| 24 | + |
| 25 | + if (childPath.startsWith('/content/')) { |
| 26 | + // Absolute path from content root - strip /content/ prefix |
| 27 | + const absoluteChildPath = childPath.slice('/content/'.length) |
| 28 | + resolvedPath = path.resolve(contentDir, absoluteChildPath) |
| 29 | + } else { |
| 30 | + // Relative path from current file's directory |
| 31 | + const currentDir: string = path.dirname(currentFilePath) |
| 32 | + const normalizedPath = childPath.startsWith('/') ? childPath.substring(1) : childPath |
| 33 | + resolvedPath = path.resolve(currentDir, normalizedPath) |
| 34 | + } |
| 35 | + |
| 36 | + // Security check: ensure resolved path stays within content directory |
| 37 | + // This prevents path traversal attacks using sequences like '../' |
| 38 | + if (!resolvedPath.startsWith(contentDir + path.sep) && resolvedPath !== contentDir) { |
| 39 | + return false |
| 40 | + } |
| 41 | + |
| 42 | + // Check for direct .md file |
| 43 | + const mdPath = `${resolvedPath}.md` |
| 44 | + if (fs.existsSync(mdPath) && fs.statSync(mdPath).isFile()) { |
| 45 | + return true |
| 46 | + } |
| 47 | + |
| 48 | + // Check for index.md file in directory |
| 49 | + const indexPath = path.join(resolvedPath, 'index.md') |
| 50 | + if (fs.existsSync(indexPath) && fs.statSync(indexPath).isFile()) { |
| 51 | + return true |
| 52 | + } |
| 53 | + |
| 54 | + // Check if the path exists as a directory (may have children) |
| 55 | + if (fs.existsSync(resolvedPath) && fs.statSync(resolvedPath).isDirectory()) { |
| 56 | + return true |
| 57 | + } |
| 58 | + |
| 59 | + return false |
| 60 | +} |
| 61 | + |
| 62 | +export const frontmatterChildren = { |
| 63 | + names: ['GHD063', 'frontmatter-children'], |
| 64 | + description: |
| 65 | + 'Children frontmatter paths must exist. Supports relative paths and absolute /content/ paths for cross-product inclusion.', |
| 66 | + tags: ['frontmatter', 'children'], |
| 67 | + function: (params: RuleParams, onError: RuleErrorCallback) => { |
| 68 | + const fm = getFrontmatter(params.lines) as Frontmatter | null |
| 69 | + if (!fm || !fm.children) return |
| 70 | + |
| 71 | + const childrenLine: string | undefined = params.lines.find((line) => |
| 72 | + line.startsWith('children:'), |
| 73 | + ) |
| 74 | + |
| 75 | + if (!childrenLine) return |
| 76 | + |
| 77 | + const lineNumber: number = params.lines.indexOf(childrenLine) + 1 |
| 78 | + |
| 79 | + if (Array.isArray(fm.children)) { |
| 80 | + const invalidPaths: string[] = [] |
| 81 | + |
| 82 | + for (const child of fm.children) { |
| 83 | + if (!isValidChildPath(child, params.name)) { |
| 84 | + invalidPaths.push(child) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (invalidPaths.length > 0) { |
| 89 | + addError( |
| 90 | + onError, |
| 91 | + lineNumber, |
| 92 | + `Found invalid children paths: ${invalidPaths.join(', ')}. For cross-product paths, use /content/ prefix (e.g., /content/actions/workflows).`, |
| 93 | + childrenLine, |
| 94 | + [1, childrenLine.length], |
| 95 | + null, |
| 96 | + ) |
| 97 | + } |
| 98 | + } |
| 99 | + }, |
| 100 | +} |
0 commit comments