go/internal/cmd/php/deploy_internal_test.go
Snider 3365bfd5ba
feat(mcp): add workspace root validation to prevent path traversal (#100)
* feat(mcp): add workspace root validation to prevent path traversal

- Add workspaceRoot field to Service for restricting file operations
- Add WithWorkspaceRoot() option for configuring the workspace directory
- Add validatePath() helper to check paths are within workspace
- Apply validation to all file operation handlers
- Default to current working directory for security
- Add comprehensive tests for path validation

Closes #82

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: move CLI commands from pkg/ to internal/cmd/

- Move 18 CLI command packages to internal/cmd/ (not externally importable)
- Keep 16 library packages in pkg/ (externally importable)
- Update all import paths throughout codebase
- Cleaner separation between CLI logic and reusable libraries

CLI commands moved: ai, ci, dev, docs, doctor, gitcmd, go, monitor,
php, pkgcmd, qa, sdk, security, setup, test, updater, vm, workspace

Libraries remaining: agentic, build, cache, cli, container, devops,
errors, framework, git, i18n, io, log, mcp, process, release, repos

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor(mcp): use pkg/io Medium for sandboxed file operations

Replace manual path validation with pkg/io.Medium for all file operations.
This delegates security (path traversal, symlink bypass) to the sandboxed
local.Medium implementation.

Changes:
- Add io.NewSandboxed() for creating sandboxed Medium instances
- Refactor MCP Service to use io.Medium instead of direct os.* calls
- Remove validatePath and resolvePathWithSymlinks functions
- Update tests to verify Medium-based behaviour

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: correct import path and workflow references

- Fix pkg/io/io.go import from core-gui to core
- Update CI workflows to use internal/cmd/updater path

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix(security): address CodeRabbit review issues for path validation

- pkg/io/local: add symlink resolution and boundary-aware containment
  - Reject absolute paths in sandboxed Medium
  - Use filepath.EvalSymlinks to prevent symlink bypass attacks
  - Fix prefix check to prevent /tmp/root matching /tmp/root2

- pkg/mcp: fix resolvePath to validate and return errors
  - Changed resolvePath from (string) to (string, error)
  - Update deleteFile, renameFile, listDirectory, fileExists to handle errors
  - Changed New() to return (*Service, error) instead of *Service
  - Properly propagate option errors instead of silently discarding

- pkg/io: wrap errors with E() helper for consistent context
  - Copy() and MockMedium.Read() now use coreerr.E()

- tests: rename to use _Good/_Bad/_Ugly suffixes per coding guidelines
  - Fix hardcoded /tmp in TestPath to use t.TempDir()
  - Add TestResolvePath_Bad_SymlinkTraversal test

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* style: fix gofmt formatting

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* style: fix gofmt formatting across all files

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 21:59:34 +00:00

221 lines
5.5 KiB
Go

package php
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestConvertDeployment_Good(t *testing.T) {
t.Run("converts all fields", func(t *testing.T) {
now := time.Now()
coolify := &CoolifyDeployment{
ID: "dep-123",
Status: "finished",
CommitSHA: "abc123",
CommitMsg: "Test commit",
Branch: "main",
CreatedAt: now,
FinishedAt: now.Add(5 * time.Minute),
Log: "Build successful",
DeployedURL: "https://app.example.com",
}
status := convertDeployment(coolify)
assert.Equal(t, "dep-123", status.ID)
assert.Equal(t, "finished", status.Status)
assert.Equal(t, "https://app.example.com", status.URL)
assert.Equal(t, "abc123", status.Commit)
assert.Equal(t, "Test commit", status.CommitMessage)
assert.Equal(t, "main", status.Branch)
assert.Equal(t, now, status.StartedAt)
assert.Equal(t, now.Add(5*time.Minute), status.CompletedAt)
assert.Equal(t, "Build successful", status.Log)
})
t.Run("handles empty deployment", func(t *testing.T) {
coolify := &CoolifyDeployment{}
status := convertDeployment(coolify)
assert.Empty(t, status.ID)
assert.Empty(t, status.Status)
})
}
func TestDeploymentStatus_Struct_Good(t *testing.T) {
t.Run("all fields accessible", func(t *testing.T) {
now := time.Now()
status := DeploymentStatus{
ID: "dep-123",
Status: "finished",
URL: "https://app.example.com",
Commit: "abc123",
CommitMessage: "Test commit",
Branch: "main",
StartedAt: now,
CompletedAt: now.Add(5 * time.Minute),
Log: "Build log",
}
assert.Equal(t, "dep-123", status.ID)
assert.Equal(t, "finished", status.Status)
assert.Equal(t, "https://app.example.com", status.URL)
assert.Equal(t, "abc123", status.Commit)
assert.Equal(t, "Test commit", status.CommitMessage)
assert.Equal(t, "main", status.Branch)
assert.Equal(t, "Build log", status.Log)
})
}
func TestDeployOptions_Struct_Good(t *testing.T) {
t.Run("all fields accessible", func(t *testing.T) {
opts := DeployOptions{
Dir: "/project",
Environment: EnvProduction,
Force: true,
Wait: true,
WaitTimeout: 10 * time.Minute,
PollInterval: 5 * time.Second,
}
assert.Equal(t, "/project", opts.Dir)
assert.Equal(t, EnvProduction, opts.Environment)
assert.True(t, opts.Force)
assert.True(t, opts.Wait)
assert.Equal(t, 10*time.Minute, opts.WaitTimeout)
assert.Equal(t, 5*time.Second, opts.PollInterval)
})
}
func TestStatusOptions_Struct_Good(t *testing.T) {
t.Run("all fields accessible", func(t *testing.T) {
opts := StatusOptions{
Dir: "/project",
Environment: EnvStaging,
DeploymentID: "dep-123",
}
assert.Equal(t, "/project", opts.Dir)
assert.Equal(t, EnvStaging, opts.Environment)
assert.Equal(t, "dep-123", opts.DeploymentID)
})
}
func TestRollbackOptions_Struct_Good(t *testing.T) {
t.Run("all fields accessible", func(t *testing.T) {
opts := RollbackOptions{
Dir: "/project",
Environment: EnvProduction,
DeploymentID: "dep-old",
Wait: true,
WaitTimeout: 5 * time.Minute,
}
assert.Equal(t, "/project", opts.Dir)
assert.Equal(t, EnvProduction, opts.Environment)
assert.Equal(t, "dep-old", opts.DeploymentID)
assert.True(t, opts.Wait)
assert.Equal(t, 5*time.Minute, opts.WaitTimeout)
})
}
func TestEnvironment_Constants(t *testing.T) {
t.Run("constants are defined", func(t *testing.T) {
assert.Equal(t, Environment("production"), EnvProduction)
assert.Equal(t, Environment("staging"), EnvStaging)
})
}
func TestGetAppIDForEnvironment_Edge(t *testing.T) {
t.Run("staging without staging ID falls back to production", func(t *testing.T) {
config := &CoolifyConfig{
AppID: "prod-123",
// No StagingAppID set
}
id := getAppIDForEnvironment(config, EnvStaging)
assert.Equal(t, "prod-123", id)
})
t.Run("staging with staging ID uses staging", func(t *testing.T) {
config := &CoolifyConfig{
AppID: "prod-123",
StagingAppID: "staging-456",
}
id := getAppIDForEnvironment(config, EnvStaging)
assert.Equal(t, "staging-456", id)
})
t.Run("production uses production ID", func(t *testing.T) {
config := &CoolifyConfig{
AppID: "prod-123",
StagingAppID: "staging-456",
}
id := getAppIDForEnvironment(config, EnvProduction)
assert.Equal(t, "prod-123", id)
})
t.Run("unknown environment uses production", func(t *testing.T) {
config := &CoolifyConfig{
AppID: "prod-123",
}
id := getAppIDForEnvironment(config, "unknown")
assert.Equal(t, "prod-123", id)
})
}
func TestIsDeploymentComplete_Edge(t *testing.T) {
tests := []struct {
status string
expected bool
}{
{"finished", true},
{"success", true},
{"failed", true},
{"error", true},
{"cancelled", true},
{"queued", false},
{"building", false},
{"deploying", false},
{"pending", false},
{"rolling_back", false},
{"", false},
{"unknown", false},
}
for _, tt := range tests {
t.Run(tt.status, func(t *testing.T) {
result := IsDeploymentComplete(tt.status)
assert.Equal(t, tt.expected, result)
})
}
}
func TestIsDeploymentSuccessful_Edge(t *testing.T) {
tests := []struct {
status string
expected bool
}{
{"finished", true},
{"success", true},
{"failed", false},
{"error", false},
{"cancelled", false},
{"queued", false},
{"building", false},
{"deploying", false},
{"", false},
}
for _, tt := range tests {
t.Run(tt.status, func(t *testing.T) {
result := IsDeploymentSuccessful(tt.status)
assert.Equal(t, tt.expected, result)
})
}
}