DirectoryHandler class manages a directory of markdown files, providing file tree navigation, cached FileHandler instances, and path validation to prevent directory traversal attacks.
Class Definition
Constructor
Path
required
Root directory path containing markdown files. The path will be expanded and resolved automatically.
Example
Properties
directory
Exposes the root directory path.Path
The resolved absolute path to the workspace directory.
Example
Methods
list_files()
List all markdown files in the directory recursively.set[str] | None
File extensions to include (case-insensitive). Defaults to
{".md", ".markdown"} if not specified.list[Path]
Sorted list of markdown file paths relative to the root directory, ordered by POSIX path (case-insensitive).
The returned paths are relative to the directory root and use forward slashes (POSIX format) regardless of platform.
Example
get_file_tree()
Build a nested tree structure of markdown files and folders.dict[str, Any]
Nested dictionary representing folder/file structure with the following schema:Folder nodes:
type:"folder"name: Folder name (string)path: Relative POSIX path from root (string)children: List of child nodes (folders and files)
type:"file"name: File name with extension (string)path: Relative POSIX path from root (string)
The tree is sorted with folders first (alphabetically), then files (alphabetically). All sorting is case-insensitive.
Example
get_file_handler()
Get or create a cachedFileHandler for a specific markdown file.
str
required
File path relative to the root directory (e.g.,
"guides/intro.md"). Can use forward or backslashes - they will be normalized.FileHandler
A cached or newly created
FileHandler instance for the specified file.ValueError- If path is absolute, escapes workspace, or is not a markdown fileFileNotFoundError- If the file does not exist
Example
validate_file_path()
Check if a relative path points to a valid markdown file in the directory.str
required
File path relative to the root directory to validate.
bool
True if file exists and is a markdown file within the directory boundaries, False otherwise.This method never raises exceptions - it returns
False for any invalid path, including directory traversal attempts.Example
cleanup()
Clean up lock files for all cached file handlers.This method has best-effort semantics and never raises exceptions. It iterates over all
FileHandler instances created during the session and calls their cleanup() methods.Example
Constants
MARKDOWN_EXTENSIONS
Default set of markdown file extensions recognized by the handler.Security Features
Path Validation
TheDirectoryHandler implements several security measures to prevent directory traversal attacks:
- Relative path enforcement: Absolute paths are rejected
- Boundary checks: Uses
Path.is_relative_to()to ensure resolved paths stay within workspace - Path normalization: Converts backslashes to forward slashes, resolves
.and.. - Extension validation: Only allows
.mdand.markdownfiles
Example Attack Prevention
Handler Caching
TheDirectoryHandler caches FileHandler instances by normalized relative path:
- Reduces overhead of creating multiple handlers for the same file
- Maintains consistent lock state per file
- Automatically cleaned up on
cleanup()
Complete Usage Example
Integration with FileHandler
TheDirectoryHandler creates and manages FileHandler instances:
Source Reference
See the complete implementation inmarkdown_os/directory_handler.py.