Skip to main content
The 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

Sample Output:

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)
File nodes:
  • 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

Sample Output:

get_file_handler()

Get or create a cached FileHandler 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.
This method validates that the path:
  1. Is relative (not absolute)
  2. Stays within the workspace directory (no .. escapes)
  3. Points to a markdown file (.md or .markdown extension)
  4. Exists on disk
Violations raise ValueError or FileNotFoundError.
Raises:
  • ValueError - If path is absolute, escapes workspace, or is not a markdown file
  • FileNotFoundError - 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

The DirectoryHandler implements several security measures to prevent directory traversal attacks:
  1. Relative path enforcement: Absolute paths are rejected
  2. Boundary checks: Uses Path.is_relative_to() to ensure resolved paths stay within workspace
  3. Path normalization: Converts backslashes to forward slashes, resolves . and ..
  4. Extension validation: Only allows .md and .markdown files

Example Attack Prevention

Handler Caching

The DirectoryHandler caches FileHandler instances by normalized relative path:
Benefits:
  • Reduces overhead of creating multiple handlers for the same file
  • Maintains consistent lock state per file
  • Automatically cleaned up on cleanup()
Example:

Complete Usage Example

Integration with FileHandler

The DirectoryHandler creates and manages FileHandler instances:

Source Reference

See the complete implementation in markdown_os/directory_handler.py.