Skip to content

fix(security): harden sandbox — shell escape bypass, symlink TOCTOU, SSRF, working_dir validation#331

Open
AlexanderGalkin95 wants to merge 6 commits intosipeed:mainfrom
AlexanderGalkin95:main
Open

fix(security): harden sandbox — shell escape bypass, symlink TOCTOU, SSRF, working_dir validation#331
AlexanderGalkin95 wants to merge 6 commits intosipeed:mainfrom
AlexanderGalkin95:main

Conversation

@AlexanderGalkin95
Copy link

Summary

Security audit of the tool sandbox revealed several vulnerabilities that could allow workspace escape or unauthorized access. This PR fixes 6 issues across shell, filesystem, and web tools, adding 637 lines with comprehensive tests.

Changes

1. Shell denylist hardening (pkg/tools/shell.go)

  • Expanded dangerous command denylist with patterns for rm --recursive/--force, base64 -d | sh, python/perl/ruby -c/-e, eval, curl/wget | sh, find -exec rm, xargs rm, and disk tools
  • Added deny-by-default ACL via allowlist support

2. Shell escape sequence bypass (pkg/tools/shell.go)

  • Added 5 new regex patterns to block ANSI-C quoting ($'...'), locale quoting ($"..."), hex escapes (\xNN), octal escapes (\NNN), and escaped metacharacters (\`, \$) that bypassed existing shellMetaRe checks in restricted mode

3. Symlink TOCTOU race condition (pkg/tools/filesystem.go, pkg/tools/edit.go)

  • Added safeReadFile/safeWriteFile/safeOpenFile wrappers that re-verify symlink targets via Lstat immediately before I/O operations
  • Closes the time-of-check-to-time-of-use window between validatePath() and actual file operations
  • Applied to ReadFileTool, WriteFileTool, EditFileTool, and AppendFileTool

4. working_dir escape (pkg/tools/shell.go)

  • Validates working_dir parameter is within workspace when restrict_to_workspace=true
  • Previously an attacker could pass working_dir="/etc" to escape the sandbox

5. SSRF protection (pkg/tools/web.go)

  • web_fetch now blocks requests to loopback, link-local, and RFC 1918 private addresses
  • Resolves hostnames before checking to prevent DNS rebinding attacks

6. TLS warning for providers (pkg/providers/http_provider.go)

  • Logs a warning when an LLM provider is configured with plain http:// API base URL

Docs

  • Added security sections to README: Shell Escape Sequence Protection, Working Directory Validation, Symlink TOCTOU Protection, SSRF Protection, TLS Warning
  • Added .claude/ to .gitignore

Test plan

  • go vet ./pkg/tools/... — passes
  • go test ./pkg/tools/... -count=1 — all tests pass
  • TestShellTool_DenylistBypassTechniques — 18 dangerous command patterns blocked
  • TestShellTool_EscapeSequenceBlocking — 5 escape sequence patterns blocked
  • TestShellTool_WorkingDirRestriction — outside workspace blocked, inside allowed
  • TestShellTool_WorkspaceMetacharacterBlocking — backticks, $(), ${}, $VAR, cd / blocked
  • TestShellTool_WorkspaceAllowedCommands — safe commands still work in restricted mode
  • TestFilesystemTool_{Read,Write,Edit,Append}File_RejectsSymlinkEscape — symlink escape blocked
  • TestWebFetchTool_SSRFBlocking — loopback, localhost, metadata, private IPs blocked

Breaking changes

None. All changes are additive security hardening. Existing behavior is preserved for non-malicious inputs.

🤖 Generated with Claude Code

AlexanderGalkin95 and others added 6 commits February 16, 2026 16:47
…-default ACL

Close critical attack chain: empty allow_from → any user → prompt injection →
exec denylist bypass → full system access.

- Expand shell denylist with 10 new patterns (rm long flags, base64→shell,
  python/perl/ruby -c/-e, eval, curl/wget→shell, find -exec rm, xargs rm,
  fdisk/parted/wipefs)
- Block shell metacharacters ($(), ${}, backticks), $VAR expansion and
  cd /absolute in workspace-restricted mode
- Change empty allow_from from allow-all to deny-all (deny-by-default)
- Add logger.WarnCF at all block points and rejected messages
- Add tests for 18 bypass techniques, 6 metacharacter escapes, and
  5 safe-command allowance checks

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… HTTP in provider

Block requests to internal/private networks (loopback, link-local, RFC1918, IPv6 ULA)
in WebFetchTool to prevent SSRF attacks targeting cloud metadata and internal services.
Log a warning when HTTPProvider is configured with plain http:// API base, as API keys
may be transmitted without encryption.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ng_dir escape

- Add 5 regex patterns to block ANSI-C/locale quoting, hex/octal escapes,
  and escaped metacharacters that bypassed shell denylist in restricted mode
- Add safeReadFile/safeWriteFile/safeOpenFile wrappers that re-verify
  symlink targets right before I/O to close TOCTOU race window
- Validate working_dir parameter stays within workspace when restricted
- Document all three protections in README security section

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ink-workdir

fix(security): block shell escape bypasses, symlink TOCTOU, and working_dir escape
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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