Skip to content

feat(loader): add path traversal protection - #20

Merged
robcohen merged 2 commits into
mainfrom
feature/path-security
Jan 10, 2026
Merged

robcohen merged 2 commits into
mainfrom
feature/path-security

Conversation

@robcohen

Copy link
Copy Markdown
Member

Summary

  • Add security feature to prevent malicious ledger files from accessing files outside the ledger directory
  • New with_path_security(bool) method to enable/disable protection
  • New with_root_dir(PathBuf) method to set custom root directory
  • New PathTraversal error variant for security violations

Motivation

This protects against attacks where a crafted .beancount file uses relative paths like include "../../../etc/passwd" to escape the intended directory.

Test plan

  • Unit tests for path traversal detection
  • Test that valid relative includes still work
  • Test custom root directory configuration

🤖 Generated with Claude Code

Add security feature to prevent malicious ledger files from accessing
files outside the ledger directory via include directives.

- Add `with_path_security(bool)` to enable/disable protection
- Add `with_root_dir(PathBuf)` to set custom root directory
- Add `PathTraversal` error variant for security violations
- Includes are validated to stay within the root directory

This protects against attacks where a crafted .beancount file uses
relative paths like `include "../../../etc/passwd"` to escape the
intended directory.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>

// Set root directory for path security if enabled but not explicitly set
if self.enforce_path_security && self.root_dir.is_none() {
self.root_dir = canonical.parent().map(Path::to_path_buf);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Security Issue: Path traversal protection can be bypassed when the main beancount file is located at the filesystem root. When canonical.parent() returns None for root files (e.g., /ledger.beancount), root_dir remains None, causing the security check at line 295 to be skipped entirely

Severity: HIGH
Category: path_traversal
Tool: ClaudeCode AI Security Analysis

Exploit Scenario: An attacker who can control where a beancount file is loaded from could place it at the filesystem root (e.g., /malicious.beancount). When this file contains 'include "/etc/passwd"' and path security is enabled, the protection fails because root_dir is None, allowing arbitrary file reads. This could occur in web services that process user-uploaded beancount files or in CI/CD pipelines that process untrusted ledger files

Recommendation: When canonical.parent() returns None, either: (1) Set root_dir to Path::new("/") to restrict includes to the root directory only, or (2) Fail with an error indicating that files at the filesystem root cannot use includes when security is enabled

@robcohen
robcohen added this pull request to the merge queue Jan 10, 2026
Merged via the queue into main with commit 9e74b58 Jan 10, 2026
@robcohen
robcohen deleted the feature/path-security branch January 10, 2026 21:04
@robcohen
robcohen restored the feature/path-security branch January 13, 2026 15:55
@robcohen
robcohen deleted the feature/path-security branch January 13, 2026 15:55
robcohen added a commit that referenced this pull request Jan 14, 2026
feat(loader): add path traversal protection
robcohen added a commit that referenced this pull request Jan 14, 2026
feat(loader): add path traversal protection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant