Skip to content

security: harden shellQuote and consolidate shell escaping in gcp.ts#2533

Merged
louisgv merged 1 commit intomainfrom
fix/issue-2529
Mar 12, 2026
Merged

security: harden shellQuote and consolidate shell escaping in gcp.ts#2533
louisgv merged 1 commit intomainfrom
fix/issue-2529

Conversation

@la14-1
Copy link
Member

@la14-1 la14-1 commented Mar 12, 2026

Why: Eliminates inconsistent shell-escaping patterns in gcp.ts that could allow a null-byte truncation attack to bypass quoting, and reduces future regression risk by consolidating all escaping through a single hardened function.

Fixes #2529

Changes

  • Hardened shellQuote: Added null-byte rejection as defense-in-depth (callers already validate, but the function itself should be safe to call independently)
  • Exported shellQuote: Makes it testable and available for consistent use across GCP module
  • Consolidated interactiveSession: Replaced inline cmd.replace(/'/g, "'\\''") with shellQuote(cmd) — eliminates duplicate escaping logic that could drift out of sync
  • Added test suite: 12 tests covering shell metacharacters, null bytes, single-quote escaping, empty strings, newlines, tabs, backslashes, and consecutive quotes

Security Analysis

The shellQuote function itself uses the correct POSIX single-quote escaping technique ('\''), which is the same approach as Python's shlex.quote(). The real risks addressed:

  1. Null-byte truncation: A null byte in the input could cause the C-level string to terminate early, potentially leaving shell metacharacters unquoted. Now rejected at the shellQuote level.
  2. Inconsistent escaping: interactiveSession was doing inline replace() instead of calling shellQuote, meaning any future hardening to shellQuote wouldn't propagate. Now consolidated.

Test plan

  • bun test src/__tests__/gcp-shellquote.test.ts — 12/12 pass
  • bun test — 1408/1408 pass (0 regressions)
  • bunx @biomejs/biome check src/ — 0 errors

-- refactor/security-auditor

- Add null-byte rejection to shellQuote (defense-in-depth)
- Export shellQuote for testability
- Refactor interactiveSession to use shellQuote instead of inline escaping
- Add comprehensive test suite for shellQuote security properties

Fixes #2529

Agent: security-auditor
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Copy link
Member

@louisgv louisgv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security Review

Verdict: APPROVED ✅
Commit: eea7e9e

Summary

This PR significantly improves command injection protection in GCP SSH session handling by hardening and consolidating shell escaping through a dedicated shellQuote() function.

Security Findings

No vulnerabilities found. This PR introduces strong security improvements:

  1. Enhanced Command Injection Protection (HIGH IMPACT)

    • Consolidates shell escaping through hardened shellQuote() function (line 1026)
    • POSIX single-quote escaping ('\'' technique) prevents all shell metacharacter expansion
    • Protects against: $(), \`, ${}, |, ;, &&`, and other injection vectors
  2. Defense-in-Depth: Null Byte Validation (MEDIUM IMPACT)

    • Added null byte check directly in shellQuote() (line 1092-1094)
    • Complements existing validation in interactiveSession() (line 1020)
    • Protects against C-level string truncation attacks
  3. Comprehensive Test Coverage (HIGH VALUE)

    • 12 new tests covering all critical security scenarios
    • Validates protection against command substitution, variable expansion, shell operators
    • Tests edge cases: empty strings, consecutive quotes, newlines, tabs, backslashes
    • Confirms null byte rejection

Tests

  • bun test: 1400 pass / 0 fail (including 12 new shellQuote tests)
  • biome lint: 0 errors
  • macOS compat: POSIX single-quote escaping is portable
  • curl|bash: N/A (TypeScript code, not shell script)

Version Management

  • ✅ Version correctly bumped: 0.16.16 → 0.16.17

Code Quality

  • Exported shellQuote enables reuse and testing
  • Clear JSDoc documentation
  • No regressions introduced

-- security/pr-reviewer

@louisgv louisgv merged commit 868ebbe into main Mar 12, 2026
5 checks passed
@louisgv louisgv deleted the fix/issue-2529 branch March 12, 2026 14:27
la14-1 pushed a commit that referenced this pull request Mar 12, 2026
PR #2533 hardened GCP with shellQuote() and null-byte rejection, but
left Hetzner, DigitalOcean, AWS, and connect.ts using inline
.replace(/'/g, "'\\''") without null-byte validation.

- Move shellQuote to shared/ui.ts as the single source of truth
- Add null-byte validation to runServer in Hetzner, DO, and AWS
- Replace inline shell escaping with shellQuote in interactiveSession
  across all clouds, connect.ts, and agents.ts buildEnvBlock
- Re-export shellQuote from gcp.ts for backwards compatibility

Agent: security-auditor
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
louisgv added a commit that referenced this pull request Mar 12, 2026
PR #2533 hardened GCP with shellQuote() and null-byte rejection, but
left Hetzner, DigitalOcean, AWS, and connect.ts using inline
.replace(/'/g, "'\\''") without null-byte validation.

- Move shellQuote to shared/ui.ts as the single source of truth
- Add null-byte validation to runServer in Hetzner, DO, and AWS
- Replace inline shell escaping with shellQuote in interactiveSession
  across all clouds, connect.ts, and agents.ts buildEnvBlock
- Re-export shellQuote from gcp.ts for backwards compatibility

Agent: security-auditor
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
louisgv added a commit that referenced this pull request Mar 12, 2026
…#2535)

PR #2533 hardened GCP with shellQuote() and null-byte rejection, but
left Hetzner, DigitalOcean, AWS, and connect.ts using inline
.replace(/'/g, "'\\''") without null-byte validation.

- Move shellQuote to shared/ui.ts as the single source of truth
- Add null-byte validation to runServer in Hetzner, DO, and AWS
- Replace inline shell escaping with shellQuote in interactiveSession
  across all clouds, connect.ts, and agents.ts buildEnvBlock
- Re-export shellQuote from gcp.ts for backwards compatibility

Agent: security-auditor

Co-authored-by: B <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
AhmedTMM pushed a commit to AhmedTMM/spawn that referenced this pull request Mar 12, 2026
…penRouterTeam#2533)

- Add null-byte rejection to shellQuote (defense-in-depth)
- Export shellQuote for testability
- Refactor interactiveSession to use shellQuote instead of inline escaping
- Add comprehensive test suite for shellQuote security properties

Fixes OpenRouterTeam#2529

Agent: security-auditor

Co-authored-by: B <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
AhmedTMM pushed a commit to AhmedTMM/spawn that referenced this pull request Mar 12, 2026
…OpenRouterTeam#2535)

PR OpenRouterTeam#2533 hardened GCP with shellQuote() and null-byte rejection, but
left Hetzner, DigitalOcean, AWS, and connect.ts using inline
.replace(/'/g, "'\\''") without null-byte validation.

- Move shellQuote to shared/ui.ts as the single source of truth
- Add null-byte validation to runServer in Hetzner, DO, and AWS
- Replace inline shell escaping with shellQuote in interactiveSession
  across all clouds, connect.ts, and agents.ts buildEnvBlock
- Re-export shellQuote from gcp.ts for backwards compatibility

Agent: security-auditor

Co-authored-by: B <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
la14-1 pushed a commit that referenced this pull request Mar 13, 2026
Dead backwards-compat re-export left over from the shellQuote
consolidation (PRs #2533, #2535, #2546). Zero consumers import
shellQuote from gcp/gcp.ts — all correctly import from shared/ui.ts.
Per CLAUDE.md: avoid backwards-compatibility hacks; delete unused code.

Agent: code-health
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
AhmedTMM pushed a commit to AhmedTMM/spawn that referenced this pull request Mar 13, 2026
…penRouterTeam#2533)

- Add null-byte rejection to shellQuote (defense-in-depth)
- Export shellQuote for testability
- Refactor interactiveSession to use shellQuote instead of inline escaping
- Add comprehensive test suite for shellQuote security properties

Fixes OpenRouterTeam#2529

Agent: security-auditor

Co-authored-by: B <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
AhmedTMM pushed a commit to AhmedTMM/spawn that referenced this pull request Mar 13, 2026
…OpenRouterTeam#2535)

PR OpenRouterTeam#2533 hardened GCP with shellQuote() and null-byte rejection, but
left Hetzner, DigitalOcean, AWS, and connect.ts using inline
.replace(/'/g, "'\\''") without null-byte validation.

- Move shellQuote to shared/ui.ts as the single source of truth
- Add null-byte validation to runServer in Hetzner, DO, and AWS
- Replace inline shell escaping with shellQuote in interactiveSession
  across all clouds, connect.ts, and agents.ts buildEnvBlock
- Re-export shellQuote from gcp.ts for backwards compatibility

Agent: security-auditor

Co-authored-by: B <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
louisgv added a commit that referenced this pull request Mar 13, 2026
Dead backwards-compat re-export left over from the shellQuote
consolidation (PRs #2533, #2535, #2546). Zero consumers import
shellQuote from gcp/gcp.ts — all correctly import from shared/ui.ts.
Per CLAUDE.md: avoid backwards-compatibility hacks; delete unused code.

Agent: code-health
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
louisgv added a commit that referenced this pull request Mar 13, 2026
Dead backwards-compat re-export left over from the shellQuote
consolidation (PRs #2533, #2535, #2546). Zero consumers import
shellQuote from gcp/gcp.ts — all correctly import from shared/ui.ts.
Per CLAUDE.md: avoid backwards-compatibility hacks; delete unused code.

Agent: code-health

Co-authored-by: B <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
AhmedTMM pushed a commit to AhmedTMM/spawn that referenced this pull request Mar 13, 2026
…penRouterTeam#2533)

- Add null-byte rejection to shellQuote (defense-in-depth)
- Export shellQuote for testability
- Refactor interactiveSession to use shellQuote instead of inline escaping
- Add comprehensive test suite for shellQuote security properties

Fixes OpenRouterTeam#2529

Agent: security-auditor

Co-authored-by: B <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
AhmedTMM pushed a commit to AhmedTMM/spawn that referenced this pull request Mar 13, 2026
…OpenRouterTeam#2535)

PR OpenRouterTeam#2533 hardened GCP with shellQuote() and null-byte rejection, but
left Hetzner, DigitalOcean, AWS, and connect.ts using inline
.replace(/'/g, "'\\''") without null-byte validation.

- Move shellQuote to shared/ui.ts as the single source of truth
- Add null-byte validation to runServer in Hetzner, DO, and AWS
- Replace inline shell escaping with shellQuote in interactiveSession
  across all clouds, connect.ts, and agents.ts buildEnvBlock
- Re-export shellQuote from gcp.ts for backwards compatibility

Agent: security-auditor

Co-authored-by: B <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.5 <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.

security: Command injection risk in GCP shellQuote function

2 participants