mirror of
https://github.com/xlionjuan/wastebin-mcp-go.git
synced 2026-11-17 04:17:54 +00:00
* chore(deps): update dependency go to v1.27.1 * Fixed lints; `just verify` passes. Co-authored-by: xlionjuan <[email protected]> * Re-enabled exhaustruct_v5; just verify passes. Co-authored-by: xlionjuan <[email protected]> * Fixed lint config; verify passes. Co-authored-by: xlionjuan <[email protected]> * Explicit DisableBuiltinBlocklist; verify passes 0 issues. Co-authored-by: xlionjuan <[email protected]> --------- Co-authored-by: xlion-bot-renovate[bot] <218815870+xlion-bot-renovate[bot]@users.noreply.github.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: xlionjuan <[email protected]>
262 lines
6.9 KiB
Go
262 lines
6.9 KiB
Go
package wastebin
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Sentinel errors are defined in errors.go.
|
|
|
|
const (
|
|
defaultExpirySeconds = 31536000 // 1 year
|
|
defaultMaxContentSize = 1048576 // 1 MB
|
|
|
|
// MaxContentSizeLimit is the largest accepted value of
|
|
// WASTEBIN_MCP_MAX_CONTENT_SIZE (256 MiB). It bounds the MCP first-message
|
|
// transport gate that is derived from the content size, keeping the gate's
|
|
// int arithmetic (conversion plus the 64 KiB envelope allowance plus the
|
|
// trailing-newline byte) safe on 32-bit targets and preventing a
|
|
// configuration mistake from triggering a large startup allocation.
|
|
MaxContentSizeLimit int64 = 256 << 20
|
|
)
|
|
|
|
// DefaultConfig returns Config with safe defaults.
|
|
func DefaultConfig() *Config {
|
|
return &Config{
|
|
ServerURL: "",
|
|
DefaultExpires: defaultExpirySeconds, // 1 year
|
|
FileReadEnabled: true,
|
|
AllowedPaths: nil,
|
|
BlockedPaths: nil,
|
|
MaxContentSize: defaultMaxContentSize, // 1 MB
|
|
SandboxMounts: nil,
|
|
SandboxTransparent: false,
|
|
AllowInsecurePassword: false,
|
|
DisableBuiltinBlocklist: false,
|
|
Debug: false,
|
|
}
|
|
}
|
|
|
|
// ConfigFromEnv reads and validates config from environment variables.
|
|
// Returns validated Config or error.
|
|
//
|
|
//nolint:cyclop,funlen,gocognit,gocyclo // Sequential env-var parsing with per-variable validation branches
|
|
func ConfigFromEnv() (*Config, error) {
|
|
cfg := DefaultConfig()
|
|
|
|
// Server URL (required).
|
|
cfg.ServerURL = os.Getenv("WASTEBIN_SERVER_URL")
|
|
if cfg.ServerURL == "" {
|
|
return nil, errServerURLRequired
|
|
}
|
|
|
|
// Default expires.
|
|
if v := os.Getenv("WASTEBIN_MCP_DEFAULT_EXPIRES"); v != "" {
|
|
num, err := strconv.Atoi(v)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid WASTEBIN_MCP_DEFAULT_EXPIRES: %w", err)
|
|
}
|
|
|
|
if num < 0 {
|
|
return nil, errNegativeDefaultExpires
|
|
}
|
|
|
|
cfg.DefaultExpires = num
|
|
}
|
|
|
|
// File read enabled.
|
|
if v := os.Getenv("WASTEBIN_MCP_FILE_READ_ENABLED"); v != "" {
|
|
b, err := strconv.ParseBool(v)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid WASTEBIN_MCP_FILE_READ_ENABLED: %w", err)
|
|
}
|
|
|
|
cfg.FileReadEnabled = b
|
|
}
|
|
|
|
// Allowed paths (comma-separated absolute paths, resolved with EvalSymlinks + Clean).
|
|
if v := os.Getenv("WASTEBIN_MCP_ALLOWED_PATHS"); v != "" {
|
|
parts := strings.SplitSeq(v, ",")
|
|
for part := range parts {
|
|
part = strings.TrimSpace(part)
|
|
if part == "" {
|
|
continue
|
|
}
|
|
|
|
resolved, err := resolveConfiguredPath("WASTEBIN_MCP_ALLOWED_PATHS", part, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cfg.AllowedPaths = append(cfg.AllowedPaths, resolved)
|
|
}
|
|
}
|
|
|
|
// Blocked paths (comma-separated absolute paths; empty by default — the
|
|
// built-in blocklist handles system directories /etc,/proc,/sys,/dev).
|
|
// Each entry stores both the operator's original lexical form and the
|
|
// resolved (symlink-canonical) form. The lexical form enables
|
|
// pre-resolution matching against late-created symlinks; the resolved
|
|
// form matches what EvalSymlinks produces at request time.
|
|
if v := os.Getenv("WASTEBIN_MCP_BLOCKED_PATHS"); v != "" {
|
|
cfg.BlockedPaths = nil
|
|
|
|
parts := strings.SplitSeq(v, ",")
|
|
for part := range parts {
|
|
part = strings.TrimSpace(part)
|
|
if part == "" {
|
|
continue
|
|
}
|
|
|
|
lexical := filepath.Clean(part)
|
|
if !filepath.IsAbs(lexical) {
|
|
return nil, fmt.Errorf("%w: WASTEBIN_MCP_BLOCKED_PATHS entry %q", errConfiguredPathNotAbsolute, part)
|
|
}
|
|
|
|
resolved, err := filepath.EvalSymlinks(lexical)
|
|
if err != nil {
|
|
cfg.BlockedPaths = append(cfg.BlockedPaths, blockedPathEntry{
|
|
Lexical: lexical,
|
|
Resolved: lexical,
|
|
})
|
|
} else {
|
|
cfg.BlockedPaths = append(cfg.BlockedPaths, blockedPathEntry{
|
|
Lexical: lexical,
|
|
Resolved: filepath.Clean(resolved),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// Max content size.
|
|
if v := os.Getenv("WASTEBIN_MCP_MAX_CONTENT_SIZE"); v != "" {
|
|
num, err := strconv.ParseInt(v, 10, 64)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid WASTEBIN_MCP_MAX_CONTENT_SIZE: %w", err)
|
|
}
|
|
|
|
if num < 1 {
|
|
return nil, errMaxContentSizeTooSmall
|
|
}
|
|
|
|
if num > MaxContentSizeLimit {
|
|
return nil, fmt.Errorf(
|
|
"%w (%d exceeds the maximum of %d)",
|
|
errMaxContentSizeTooLarge, num, MaxContentSizeLimit,
|
|
)
|
|
}
|
|
|
|
cfg.MaxContentSize = num
|
|
}
|
|
|
|
// Sandbox mounts.
|
|
if v := os.Getenv("WASTEBIN_MCP_SANDBOX_MOUNTS"); v != "" {
|
|
mounts, err := ParseSandboxMounts(v)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid WASTEBIN_MCP_SANDBOX_MOUNTS: %w", err)
|
|
}
|
|
|
|
cfg.SandboxMounts, err = resolveAndValidateMounts(mounts, cfg.AllowedPaths)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
// Sandbox transparent.
|
|
if v := os.Getenv("WASTEBIN_MCP_SANDBOX_TRANSPARENT"); v != "" {
|
|
b, err := strconv.ParseBool(v)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid WASTEBIN_MCP_SANDBOX_TRANSPARENT: %w", err)
|
|
}
|
|
|
|
cfg.SandboxTransparent = b
|
|
}
|
|
|
|
// Disable built-in blocklist.
|
|
if v := os.Getenv("WASTEBIN_MCP_DISABLE_BUILTIN_BLOCKLIST"); v != "" {
|
|
b, err := strconv.ParseBool(v)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %s", errInvalidDisableBuiltinBlocklist, v)
|
|
}
|
|
|
|
cfg.DisableBuiltinBlocklist = b
|
|
}
|
|
|
|
// Allow insecure password over HTTP.
|
|
if v := os.Getenv("WASTEBIN_MCP_ALLOW_INSECURE_PASSWORD"); v != "" {
|
|
b, err := strconv.ParseBool(v)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid WASTEBIN_MCP_ALLOW_INSECURE_PASSWORD: %w", err)
|
|
}
|
|
|
|
cfg.AllowInsecurePassword = b
|
|
}
|
|
|
|
// Debug.
|
|
if v := os.Getenv("DEBUG"); v != "" {
|
|
b, err := strconv.ParseBool(v)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid DEBUG: %w", err)
|
|
}
|
|
|
|
cfg.Debug = b
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func resolveConfiguredPath(envName, rawPath string, allowMissing bool) (string, error) {
|
|
if !filepath.IsAbs(rawPath) {
|
|
return "", fmt.Errorf("%w: %s entry %q", errConfiguredPathNotAbsolute, envName, rawPath)
|
|
}
|
|
|
|
resolved, err := filepath.EvalSymlinks(rawPath)
|
|
if err != nil {
|
|
if allowMissing {
|
|
return filepath.Clean(rawPath), nil
|
|
}
|
|
|
|
return "", fmt.Errorf("failed to resolve %s path %q: %w", envName, rawPath, err)
|
|
}
|
|
|
|
return filepath.Clean(resolved), nil
|
|
}
|
|
|
|
// resolveAndValidateMounts resolves mount host paths with EvalSymlinks and
|
|
// validates them against allowed paths.
|
|
func resolveAndValidateMounts(
|
|
mounts []SandboxMount,
|
|
allowedPaths []string,
|
|
) ([]SandboxMount, error) {
|
|
for idx := range mounts {
|
|
resolved, err := filepath.EvalSymlinks(mounts[idx].HostPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf(
|
|
"failed to resolve sandbox mount host path %q: %w",
|
|
mounts[idx].HostPath, err,
|
|
)
|
|
}
|
|
|
|
mounts[idx].HostPath = filepath.Clean(resolved)
|
|
}
|
|
|
|
validated := make([]SandboxMount, 0, len(mounts))
|
|
for _, mount := range mounts {
|
|
if len(allowedPaths) > 0 && !isAllowedPath(mount.HostPath, allowedPaths) {
|
|
slog.Warn("sandbox mount host_path not under any allowed path; skipping mount",
|
|
"host_path", mount.HostPath,
|
|
"sandbox_path", mount.SandboxPath,
|
|
)
|
|
|
|
continue
|
|
}
|
|
|
|
validated = append(validated, mount)
|
|
}
|
|
|
|
return validated, nil
|
|
}
|