Skip to content

fix(server): restrict HTTP file read/list to allowed paths#46

Open
y4sol wants to merge 1 commit intoAgentFlocks:mainfrom
y4sol:fix/http-file-read-guard
Open

fix(server): restrict HTTP file read/list to allowed paths#46
y4sol wants to merge 1 commit intoAgentFlocks:mainfrom
y4sol:fix/http-file-read-guard

Conversation

@y4sol
Copy link
Copy Markdown

@y4sol y4sol commented Apr 3, 2026

Summary

Restricts filesystem access through GET /api/file/content and GET /api/file/list by validating paths only in the HTTP routes. Internal File.read() callers (e.g. memory under user data directories) are unchanged.

Problem

Clients that could reach the API could read arbitrary local files the server process could open.

Solution

  • Add flocks/utils/http_file_read_guard.py with resolve_path_for_http_file_access():
    • Allowed roots: Flocks project directory (ancestor containing .flocks/, without falling back to cwd as a project root when none exists), configured data directory, workspace directory, optional allowReadPaths entries (validated), and a small built-in allowlist for common safe host files (e.g. /etc/hosts when they exist).
    • Denied: Flocks config tree, ~/.ssh, and on POSIX /proc, /sys, /dev.
    • Uses assert_sandbox_path for containment and symlink checks.
  • Wire the guard into flocks/server/routes/file.py for /content and /list; PermissionError → HTTP 403.
  • Add ConfigInfo.allow_read_paths with Pydantic alias allowReadPaths for JSON config.
  • Add find_flocks_project_root() (returns None if no .flocks ancestor); find_project_root() keeps backward-compatible behavior for non-security callers.

Configuration

Optional extra paths in user config, e.g. ~/.flocks/config/flocks.json:

{
  "allowReadPaths": ["/opt/myapp/config"]
}

Validate paths only at GET /api/file/content and /api/file/list via resolve_path_for_http_file_access.

- Add http_file_read_guard: project root (no unsafe cwd fallback), data, workspace, allowReadPaths, small safe system file allowlist; block config, ~/.ssh, /proc|/sys|/dev; use assert_sandbox_path for symlink-safe containment.
- ConfigInfo.allow_read_paths with alias allowReadPaths.
- find_flocks_project_root() for strict discovery; find_project_root() delegates for backward compat.
- Map PermissionError to HTTP 403.
@y4sol y4sol force-pushed the fix/http-file-read-guard branch from 972b351 to e8a493b Compare April 3, 2026 11:46
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