You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] 403 "Access denied" in file viewer for relative file paths and global rule files (~/.agents/AGENTS.md) #135
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
Ask the agent to inspect a file (or click a relative file link in chat like package.json or AGENTS.md).
The file viewer on the right opens.
It displays Access denied (status 403).
Root Cause
Relative Path Normalization Bug in app/api/files/[...path]/route.ts:
The route uses:
functionw(a){letb=a.join("/"),c=normalizeFilePathSlashes(b);returnisWindowsAbsolutePath(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.
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
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:
To avoid hardcoded path assumptions and maintain architectural cleanliness across different environments and configurations:
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):
consthome=process.env.HOME||process.env.USERPROFILE;if(home){constconfigDirName=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 directoriesroots.add(normalizeSlashes(path.join(home,".agents")));roots.add(normalizeSlashes(path.join(home,".agent")));}
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:
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.
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.
Activity
onrunun commentedon Sep 25, 2026
Follow-up: Generic & Config-Aware Implementation
To avoid hardcoded path assumptions and maintain architectural cleanliness across different environments and configurations:
Config-Aware Allowed Roots (
lib/file-access.ts):Instead of static strings, resolve the official configuration and agent roots dynamically by honoring
PI_CONFIG_DIRandPI_CODING_AGENT_DIR(matching the rules inlib/omp/paths.tsandskills-service.ts):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 throughallowedRootsto resolve the candidate. Because the candidate is resolved strictly withinallowedRoots, it remains 100% immune to directory traversal attacks while correctly locating project files requested by relative paths: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.fix: address six reported issues in one batch
kahme247 commentedon Oct 2, 2026
Fixed in
acf20c0f.Two separate causes, both confirmed:
Relative paths.
MarkdownBodylinkifies a relative workspace path to/api/files/<path>?type=read, but the catch-all param carries no leading slash, so the route mapped a barepackage.jsonto/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 passesisPathWithinRootsagainst 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.
~/.agentsand~/.agentwere never allowed roots. They are now, along with the config root and the agent dir, built fromgetConfigRoot()/getAgentDir()inlib/omp/paths.tssoPI_CONFIG_DIR/PI_CODING_AGENT_DIRkeep 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) andmodels.ymldownloadable through?type=download, where they previously 403'd. Those two filenames are refused under the config roots specifically — aagent.dbin a workspace still opens normally.Tests in
lib/file-access.test.mjscover relative resolution, traversal denial (../secret.txt,src/../../secret.txt,../../etc/passwdall rejected even though the target exists), Windows-style roots, the end-to-end GET returning 200 for a barepackage.json, and POST staying absolute-only.fix(uploads): show each attached file as a link that opens it (#375)