Skip to content

[Bug] 403 "Access denied" in file viewer for relative file paths and global rule files (~/.agents/AGENTS.md) #135

Closed

Description

@onrunun

Describe the bug
When opening files in the web UI file viewer (e.g., via markdown file links in chat, the open_file host tool, or relative paths like package.json), the right-side FileViewer fails with HTTP 403:
{"error": "Access denied", "code": "access_denied"}.

Additionally, standard global agent instruction files such as ~/.agents/AGENTS.md are also rejected with 403 because getAllowedFileRoots() only collects roots from recorded sessions.

To Reproduce

  1. Ask the agent to inspect a file (or click a relative file link in chat like package.json or AGENTS.md).
  2. The file viewer on the right opens.
  3. It displays Access denied (status 403).

Root Cause

  1. Relative Path Normalization Bug in app/api/files/[...path]/route.ts:
    The route uses:

    function w(a) {
      let b = a.join("/"), c = normalizeFilePathSlashes(b);
      return isWindowsAbsolutePath(c) ? c : "/" + b.replace(/^\/+/, "");
    }

    When a relative path like package.json or src/app/page.tsx is requested, it becomes "/package.json" (treating it as an absolute path at the root of the OS filesystem).
    isFilePathAllowed("/package.json", allowedRoots) checks whether "/package.json" starts with any session root (e.g. /Users/.../project), which evaluates to false, triggering a 403 response.

  2. Global Agent Paths Missing from getAllowedFileRoots in lib/file-access.ts:
    Allowed roots are only populated from s.cwd and s.projectRoot of existing sessions. Files residing in ~/.agents/ or ~/.omp/ (which omp natively injects as system context) fail the allowed roots check unless an active sessionId is passed AND the path was explicitly mentioned in session text.

Proposed Solution

  1. In app/api/files/[...path]/route.ts, if o = w(n) does not exist or fails isFilePathAllowed(o, allowedRoots), attempt to resolve the relative path against each allowed root:
    if (!fs.existsSync(o) || !isFilePathAllowed(o, allowedRoots)) {
      const raw = n.join("/");
      for (const root of allowedRoots) {
        const candidate = path.resolve(root, raw);
        if (fs.existsSync(candidate)) {
          o = candidate;
          break;
        }
      }
    }
  2. In lib/file-access.ts, include ~/.agents and ~/.omp in getAllowedFileRoots():
    const home = process.env.HOME || process.env.USERPROFILE;
    if (home) {
      roots.add(normalizeSlashes(path.join(home, ".agents")));
      roots.add(normalizeSlashes(path.join(home, ".omp")));
    }

Activity

  1. onrunun commented on Sep 25, 2026

    @onrunun
    Author

    Follow-up: Generic & Config-Aware Implementation

    To avoid hardcoded path assumptions and maintain architectural cleanliness across different environments and configurations:

    1. Config-Aware Allowed Roots (lib/file-access.ts):
      Instead of static strings, resolve the official configuration and agent roots dynamically by honoring PI_CONFIG_DIR and PI_CODING_AGENT_DIR (matching the rules in lib/omp/paths.ts and skills-service.ts):

      const home = process.env.HOME || process.env.USERPROFILE;
      if (home) {
        const configDirName = process.env.PI_CONFIG_DIR || ".omp";
        roots.add(normalizeSlashes(path.join(home, configDirName)));
        roots.add(normalizeSlashes(path.join(home, configDirName, "agent")));
        if (process.env.PI_CODING_AGENT_DIR) {
          roots.add(normalizeSlashes(path.resolve(process.env.PI_CODING_AGENT_DIR)));
        }
        // Official agent rules & skills provider directories
        roots.add(normalizeSlashes(path.join(home, ".agents")));
        roots.add(normalizeSlashes(path.join(home, ".agent")));
      }
    2. Generic Relative Path Resolution (app/api/files/[...path]/route.ts):
      If o = w(n) does not exist on disk or is not within roots, iterate through allowedRoots to resolve the candidate. Because the candidate is resolved strictly within allowedRoots, it remains 100% immune to directory traversal attacks while correctly locating project files requested by relative paths:

      if (!fs.existsSync(o) || !isFilePathAllowed(o, allowedRoots)) {
        const raw = n.join("/");
        for (const root of allowedRoots) {
          const candidate = path.resolve(root, raw);
          if (fs.existsSync(candidate)) {
            o = candidate;
            break;
          }
        }
      }

    Verified locally against running instances: both relative workspace links (package.json, src/...) and agent instruction files (~/.agents/AGENTS.md) open reliably in the FileViewer with 200 OK.

  2. kahme247 commented on Oct 2, 2026

    @kahme247
    Collaborator

    Fixed in acf20c0f.

    Two separate causes, both confirmed:

    Relative paths. MarkdownBody linkifies a relative workspace path to /api/files/<path>?type=read, but the catch-all param carries no leading slash, so the route mapped a bare package.json to /package.json — the filesystem root, which is in no allowed root, hence the 403. resolveRequestedFilePath() now maps a relative request onto an allowed root. A candidate is accepted only when it passes isPathWithinRoots against the one root that produced it and the file exists, so ../.. traversal still fails the same prefix check. Absolute requests keep exactly their previous reading, and every GET type benefits (read, download, meta, preview, watch, list).

    Uploads deliberately do not resolve relative paths: fanning an upload path out over the roots would turn "pick a directory" into "write anywhere".

    Global rule files. ~/.agents and ~/.agent were never allowed roots. They are now, along with the config root and the agent dir, built from getConfigRoot()/getAgentDir() in lib/omp/paths.ts so PI_CONFIG_DIR / PI_CODING_AGENT_DIR keep working. They are appended last, so a bare filename still resolves to a real workspace before falling back to the global dirs.

    One thing worth knowing: because the config root is a prefix, allowing it would also have made agent.db (omp's encrypted credential store) and models.yml downloadable through ?type=download, where they previously 403'd. Those two filenames are refused under the config roots specifically — a agent.db in a workspace still opens normally.

    Tests in lib/file-access.test.mjs cover relative resolution, traversal denial (../secret.txt, src/../../secret.txt, ../../etc/passwd all rejected even though the target exists), Windows-style roots, the end-to-end GET returning 200 for a bare package.json, and POST staying absolute-only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions