mirror of
https://github.com/samerfarida/mcp-ssh-orchestrator.git
synced 2026-11-20 20:33:16 +00:00
feat: Add file size limits for YAML loading (PR4) (#28)
**Security Enhancement:** Prevent resource exhaustion attacks via oversized YAML files. **Implementation:** - Added MAX_YAML_FILE_SIZE constant (10MB) for configuration files - Updated _load_yaml() to check file size before parsing - Returns empty dict and logs security event if file exceeds limit - Size validation prevents loading files into memory if too large **Tests Added:** - test_load_yaml_normal_size - Normal files load correctly - test_load_yaml_oversized_file - Oversized files are rejected - test_load_yaml_at_size_limit - Files at limit still load - test_load_yaml_missing_file - Missing files return empty dict - test_load_yaml_invalid_yaml - Invalid YAML handled gracefully - test_load_yaml_empty_file - Empty files return empty dict **Documentation:** - Updated SECURITY.md with YAML file size limits section - Updated Configuration.md with file size limit details - Added security event logging examples **Security Impact:** Prevents resource exhaustion attacks via maliciously large YAML configuration files while allowing legitimate large configurations. Fixes security vulnerability from CodeQL analysis.
This commit is contained in:
@@ -8,6 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
## [Unreleased] - 2025-11-02
|
||||
|
||||
### Added
|
||||
- **YAML File Size Limits (PR4)**: Added file size validation for YAML configuration files
|
||||
- Maximum file size limit of 10MB per YAML file (`servers.yml`, `credentials.yml`, `policy.yml`)
|
||||
- File size validation before parsing to prevent resource exhaustion attacks
|
||||
- Security event logging for size limit violations
|
||||
- Added `MAX_YAML_FILE_SIZE` constant (10MB) with clear documentation
|
||||
- Updated `_load_yaml()` function with size checking logic
|
||||
- Comprehensive tests for normal, oversized, and edge case file sizes
|
||||
- Updated documentation with file size limit details
|
||||
- **Path Traversal Protection (PR1 & PR2)**: Security enhancements to prevent path traversal attacks
|
||||
- **Secret Path Protection (`_resolve_secret`)**:
|
||||
- Secret name validation: only alphanumeric characters, dashes, and underscores allowed
|
||||
|
||||
@@ -124,6 +124,40 @@ All resolved paths are validated to ensure they are regular files:
|
||||
|
||||
**Effect**: Prevents accessing directories or symlinks that could lead to security vulnerabilities or unauthorized access.
|
||||
|
||||
#### YAML File Size Limits
|
||||
|
||||
All YAML configuration files are validated for size before loading to prevent resource exhaustion attacks:
|
||||
|
||||
1. **Size Limit**: Maximum file size of 10MB per YAML file
|
||||
- Applies to: `servers.yml`, `credentials.yml`, `policy.yml`
|
||||
- Prevents resource exhaustion via oversized configuration files
|
||||
- Files exceeding limit are rejected with security event logging
|
||||
|
||||
2. **Size Validation**: File size is checked using `os.path.getsize()` before parsing
|
||||
- Prevents loading files into memory if they exceed the limit
|
||||
- Returns empty dictionary on size limit violation
|
||||
- No YAML parsing performed if file is too large
|
||||
|
||||
3. **Security Event Logging**: Size limit violations are logged:
|
||||
```json
|
||||
{
|
||||
"level": "error",
|
||||
"kind": "security_event",
|
||||
"type": "file_size_limit_exceeded",
|
||||
"path": "/app/config/servers.yml",
|
||||
"file_size": 10485761,
|
||||
"max_size": 10485760,
|
||||
"reason": "yaml_file_too_large"
|
||||
}
|
||||
```
|
||||
|
||||
4. **Normal Operation**: Files at or below the 10MB limit load normally
|
||||
- 10MB is sufficient for typical configuration files
|
||||
- Large configuration files (multiple thousands of hosts) are supported
|
||||
- Prevents abuse while allowing legitimate use cases
|
||||
|
||||
**Effect**: Prevents resource exhaustion attacks via oversized YAML files that could consume excessive memory or processing time.
|
||||
|
||||
### SSH Key Management
|
||||
|
||||
**Best Practices:**
|
||||
|
||||
@@ -186,6 +186,17 @@ All resolved file paths are validated to ensure security:
|
||||
|
||||
**Security Events**: All path traversal attempts and file validation failures are logged as security events for monitoring and incident response.
|
||||
|
||||
#### YAML File Size Limits
|
||||
|
||||
All YAML configuration files are protected against resource exhaustion attacks:
|
||||
|
||||
- **Maximum File Size**: 10MB per YAML file (applies to `servers.yml`, `credentials.yml`, `policy.yml`)
|
||||
- **Size Validation**: File size is checked before parsing to prevent loading oversized files
|
||||
- **Security Impact**: Prevents resource exhaustion attacks via maliciously large configuration files
|
||||
- **Logging**: Size limit violations are logged as security events
|
||||
|
||||
**Effect**: Protects against memory exhaustion and processing time attacks while allowing legitimate large configurations.
|
||||
|
||||
### File Permissions
|
||||
```bash
|
||||
# Secure configuration files
|
||||
|
||||
+35
-1
@@ -10,10 +10,44 @@ DEFAULT_CONFIG_DIR = os.environ.get("MCP_SSH_CONFIG_DIR", "/app/config")
|
||||
DEFAULT_KEYS_DIR = os.environ.get("MCP_SSH_KEYS_DIR", "/app/keys")
|
||||
DEFAULT_SECRETS_DIR = os.environ.get("MCP_SSH_SECRETS_DIR", "/app/secrets")
|
||||
|
||||
# Maximum file size for YAML config files (10MB)
|
||||
# Prevents resource exhaustion attacks via oversized YAML files
|
||||
MAX_YAML_FILE_SIZE = 10 * 1024 * 1024 # 10MB in bytes
|
||||
|
||||
|
||||
def _load_yaml(path: str) -> dict:
|
||||
"""Load YAML file to dict."""
|
||||
"""Load YAML file to dict with size limit protection.
|
||||
|
||||
Security: Validates file size before loading to prevent resource exhaustion
|
||||
attacks via oversized YAML files.
|
||||
|
||||
Args:
|
||||
path: Path to YAML file to load
|
||||
|
||||
Returns:
|
||||
Dictionary containing parsed YAML data, or empty dict on error/size limit
|
||||
"""
|
||||
try:
|
||||
# Check if file exists
|
||||
if not os.path.exists(path):
|
||||
return {}
|
||||
|
||||
# Check file size before loading to prevent resource exhaustion
|
||||
file_size = os.path.getsize(path)
|
||||
if file_size > MAX_YAML_FILE_SIZE:
|
||||
_log_err(
|
||||
"security_event",
|
||||
{
|
||||
"type": "file_size_limit_exceeded",
|
||||
"path": path,
|
||||
"file_size": file_size,
|
||||
"max_size": MAX_YAML_FILE_SIZE,
|
||||
"reason": "yaml_file_too_large",
|
||||
},
|
||||
)
|
||||
return {}
|
||||
|
||||
# Load YAML file
|
||||
with open(path, encoding="utf-8") as f:
|
||||
return yaml.safe_load(f) or {}
|
||||
except FileNotFoundError:
|
||||
|
||||
@@ -7,7 +7,9 @@ import pytest
|
||||
import yaml
|
||||
|
||||
from mcp_ssh.config import (
|
||||
MAX_YAML_FILE_SIZE,
|
||||
Config,
|
||||
_load_yaml,
|
||||
_resolve_key_path,
|
||||
_resolve_secret,
|
||||
_validate_file_path,
|
||||
@@ -644,3 +646,94 @@ def test_resolve_key_path_regular_file_still_works():
|
||||
# Regular file should still work (absolute path)
|
||||
result2 = _resolve_key_path(key_path, keys_dir=tmpdir)
|
||||
assert result2 == os.path.abspath(key_path)
|
||||
|
||||
|
||||
def test_load_yaml_normal_size():
|
||||
"""Test that normal sized YAML files load correctly."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
yaml_path = os.path.join(tmpdir, "test.yml")
|
||||
# Create a small YAML file (much smaller than limit)
|
||||
yaml_content = {
|
||||
"test": "data",
|
||||
"key": "value",
|
||||
"list": [1, 2, 3],
|
||||
}
|
||||
with open(yaml_path, "w") as f:
|
||||
yaml.dump(yaml_content, f)
|
||||
|
||||
result = _load_yaml(yaml_path)
|
||||
assert result == yaml_content
|
||||
|
||||
|
||||
def test_load_yaml_oversized_file():
|
||||
"""Test that oversized YAML files are rejected."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
yaml_path = os.path.join(tmpdir, "oversized.yml")
|
||||
# Create a file larger than MAX_YAML_FILE_SIZE
|
||||
# We'll create a file that's slightly larger than the limit
|
||||
oversized_size = MAX_YAML_FILE_SIZE + 1024 # 1KB over limit
|
||||
with open(yaml_path, "w") as f:
|
||||
# Write enough data to exceed the limit
|
||||
# YAML files are text, so we'll write a large string
|
||||
large_content = "key: " + "x" * oversized_size
|
||||
f.write(large_content)
|
||||
|
||||
# Verify file size exceeds limit
|
||||
assert os.path.getsize(yaml_path) > MAX_YAML_FILE_SIZE
|
||||
|
||||
# Should return empty dict and log security event
|
||||
result = _load_yaml(yaml_path)
|
||||
assert result == {}
|
||||
|
||||
|
||||
def test_load_yaml_at_size_limit():
|
||||
"""Test that files at exactly the size limit still load."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
yaml_path = os.path.join(tmpdir, "at_limit.yml")
|
||||
# Create a file exactly at the size limit
|
||||
yaml_content = {
|
||||
"test": "x" * (MAX_YAML_FILE_SIZE - 50)
|
||||
} # Leave room for YAML structure
|
||||
with open(yaml_path, "w") as f:
|
||||
yaml.dump(yaml_content, f)
|
||||
|
||||
# If file is at or under limit, it should load
|
||||
# We'll truncate it if needed to be exactly at limit
|
||||
if os.path.getsize(yaml_path) <= MAX_YAML_FILE_SIZE:
|
||||
result = _load_yaml(yaml_path)
|
||||
assert "test" in result
|
||||
else:
|
||||
# File exceeded limit during dump, which is expected
|
||||
pass
|
||||
|
||||
|
||||
def test_load_yaml_missing_file():
|
||||
"""Test that missing files return empty dict."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
missing_path = os.path.join(tmpdir, "nonexistent.yml")
|
||||
result = _load_yaml(missing_path)
|
||||
assert result == {}
|
||||
|
||||
|
||||
def test_load_yaml_invalid_yaml():
|
||||
"""Test that invalid YAML returns empty dict and logs error."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
yaml_path = os.path.join(tmpdir, "invalid.yml")
|
||||
with open(yaml_path, "w") as f:
|
||||
f.write("invalid: yaml: content: [unclosed")
|
||||
|
||||
result = _load_yaml(yaml_path)
|
||||
# Should return empty dict on parse error
|
||||
assert result == {}
|
||||
|
||||
|
||||
def test_load_yaml_empty_file():
|
||||
"""Test that empty YAML files return empty dict."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
yaml_path = os.path.join(tmpdir, "empty.yml")
|
||||
with open(yaml_path, "w") as f:
|
||||
f.write("")
|
||||
|
||||
result = _load_yaml(yaml_path)
|
||||
# Empty YAML should return empty dict
|
||||
assert result == {}
|
||||
|
||||
Reference in New Issue
Block a user