Skip to content

security: Path traversal risk via $VAR expansion in remote paths #3080

@louisgv

Description

@louisgv

Severity

MEDIUM

Location

packages/cli/src/digitalocean/digitalocean.ts lines 1362-1369, 1402-1409

Description

The remote path validation explicitly allows the $ character to support $HOME expansion:

// uploadFile validation (line 1362-1369)
if (
  \!/^[a-zA-Z0-9/_.~$-]+$/.test(normalizedRemote) ||
  normalizedRemote.includes("..") ||
  normalizedRemote.split("/").some((s) => s.startsWith("-"))
) {
  logError(`Invalid remote path: ${remotePath}`);
  throw new Error("Invalid remote path");
}

// downloadFile uses the same validation (line 1402-1409)
// Then explicitly expands $HOME (line 1412)
const expandedPath = normalizedRemote.replace(/^$HOME/, "~");

Risk

While the code only expands $HOME explicitly (line 1412), the path validation allows ANY $ character. This could enable:

  1. Unintended variable expansion by the shell during scp/ssh operations
  2. Information disclosure if environment variables contain sensitive paths
  3. Potential path traversal via environment variables like $PWD, $OLDPWD, etc.

Example Attack Vector

If an attacker can control the remotePath parameter:

// Passes validation
uploadFile(localFile, "$OLDPWD/../../etc/passwd")
// Shell expands $OLDPWD, potentially writing outside intended directory

Impact

  • Information disclosure via environment variable expansion
  • Potential file write/read outside intended directories
  • Affects uploadFile and downloadFile operations

Recommendation

  1. Strict whitelist: Only allow $HOME prefix, reject all other $ usage

    // Reject paths with $ unless exactly "$HOME/..."
    if (\!/^\$HOME\//.test(remotePath) && remotePath.includes('$')) {
      throw new Error("Only \$HOME variable expansion is allowed");
    }
  2. Pre-expansion validation: Expand $HOME to ~ BEFORE the regex check

    const expandedPath = remotePath.replace(/^\$HOME/, "~");
    if (\!/^[a-zA-Z0-9/_.~-]+$/.test(expandedPath)) { ... }
  3. Remove $ from allowed charset and handle $HOME as special case before validation

  4. Use absolute paths: Require all remote paths to be absolute (start with / or ~) to prevent relative path issues

Similar Issues

This same pattern appears in other cloud providers - recommend auditing:

  • packages/cli/src/gcp/*
  • packages/cli/src/aws/*
  • packages/cli/src/hetzner/*
  • packages/cli/src/sprite/*

Metadata

Metadata

Assignees

No one assigned

    Labels

    in-progressIssue is being actively worked onsafe-to-workSecurity triage: safe for automated processingsecuritySecurity vulnerabilities and concerns

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions