Skip to content

fix(codeql): go/path-injection in pkg/server/session_manager.go:542#3758

Merged
Sayt-0 merged 2 commits into
mainfrom
fix/codeql-57-path-injection-workingdir
Jul 20, 2026
Merged

fix(codeql): go/path-injection in pkg/server/session_manager.go:542#3758
Sayt-0 merged 2 commits into
mainfrom
fix/codeql-57-path-injection-workingdir

Conversation

@ronan-thibaut-glitch

Copy link
Copy Markdown
Contributor

The WorkingDir field supplied in the POST /api/sessions request body was resolved with filepath.Abs and passed directly to os.Stat without checking whether it lies within any permitted scope. An authenticated caller could set WorkingDir to /etc, /root, or any other directory and cause the AI agent to operate on arbitrary filesystem locations — a horizontal privilege escalation risk in multi-user deployments (CodeQL rule go/path-injection, alert #57, line 542).

The fix adds two helpers to SessionManager: workingDirRoot (returns the server's configured WorkingDir from runConfig, falling back to the process working directory) and resolveWithinRoot (calls filepath.EvalSymlinks on both the candidate path and the root to defeat symlink escapes, then uses filepath.Rel to verify the result does not escape the root via ../ components or an absolute path on a different drive). The tainted absWd now passes through resolveWithinRoot before reaching os.Stat, breaking the alerted data flow. Symlink-escape, sibling-directory, and dot-dot traversal attacks are each covered by the new pkg/server/session_workingdir_test.go test file.

User-supplied WorkingDir (POST /api/sessions body) was passed through
filepath.Abs and then directly to os.Stat without any containment check,
allowing an authenticated caller to probe or operate on arbitrary
filesystem locations (go/path-injection, CodeQL alert #57).

Add resolveWithinRoot which canonicalises the requested path via
filepath.EvalSymlinks (on both the candidate and the allowed root) and
then rejects any path whose filepath.Rel result escapes the root with
a ".." prefix. The allowed root defaults to the server's configured
WorkingDir (runConfig.WorkingDir), falling back to the process working
directory when none is configured.

Symlink escapes (a link inside root pointing outside), dot-dot traversal,
and sibling-directory attacks are all covered by the new test suite.
@aheritier aheritier added area/api For features/issues/fixes related to the usage of the cagent API area/sessions For features/issues/fixes related to session lifecycle (resume, persistence, export) kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Jul 20, 2026
forbidigo rule disallows context.Background() in tests.
@ronan-thibaut-glitch
ronan-thibaut-glitch marked this pull request as ready for review July 20, 2026 16:03
@ronan-thibaut-glitch
ronan-thibaut-glitch requested a review from a team as a code owner July 20, 2026 16:03
@Sayt-0
Sayt-0 merged commit 9194b2b into main Jul 20, 2026
14 checks passed
@Sayt-0
Sayt-0 deleted the fix/codeql-57-path-injection-workingdir branch July 20, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api For features/issues/fixes related to the usage of the cagent API area/sessions For features/issues/fixes related to session lifecycle (resume, persistence, export) kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants