diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b60491e..5edf864 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26" + go-version-file: go.mod - name: Run tests run: go test -v -race -count=1 ./... @@ -43,7 +43,7 @@ jobs: - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26" + go-version-file: go.mod - name: Build iron-proxy run: go build -o iron-proxy ./cmd/iron-proxy @@ -131,7 +131,7 @@ jobs: - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26" + go-version-file: go.mod - name: go vet run: go vet ./... @@ -150,7 +150,7 @@ jobs: - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26" + go-version-file: go.mod - uses: golangci/golangci-lint-action@ba0d7d2ec06a0ea1cb5fa41b2e4a3ab91d21278a # v9.3.0 with: @@ -171,7 +171,7 @@ jobs: - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26" + go-version-file: go.mod - name: Check go mod tidy run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6be19b1..bc266e9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -26,7 +26,7 @@ jobs: - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26" + go-version-file: go.mod - name: Set up QEMU uses: docker/setup-qemu-action@96fe6ef7f33517b61c61be40b68a1882f3264fb8 # v4.2.0 diff --git a/AGENTS.md b/AGENTS.md index 8c79f2d..442ce9c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -6,8 +6,8 @@ tools. ## Repo at a glance -- Single Go module: `github.com/ironsh/iron-proxy` (Go 1.26+). -- One binary: `cmd/iron-proxy` (the MITM egress proxy). +- Main Go module: `github.com/ironsh/iron-proxy` (version in `go.mod`). +- One binary: `cmd/iron-proxy`, with an opt-in SSH listener in `internal/sshproxy/`. - Core packages live under `internal/`. The transform pipeline lives in `internal/transform/` with one subpackage per transform (`allowlist`, `secrets`, `bodycapture`, `headerallowlist`, `annotate`, diff --git a/README.md b/README.md index 9a5e1e2..10bfce5 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,14 @@ Single binary. Single YAML config. - **PostgreSQL MITM proxy.** Optional listener that authenticates clients against proxy-managed credentials, injects `SET ROLE` on the upstream session, and rejects client attempts to mutate the role (`SET ROLE`, - `set_config('role', ...)`, DO blocks, etc.) via a SQL AST walk. Pairs with + any `set_config` call, DO blocks, unsafe function definitions, definitions + that bind functions as callbacks such as `CREATE AGGREGATE`, etc.) via a + SQL AST walk. SQL and PL/pgSQL routine bodies are inspected recursively; + dynamic PL/pgSQL and unsupported routine languages are rejected. As defense + in depth, revoke `EXECUTE` on `pg_catalog.set_config` and unneeded + `TEMPORARY` / `CREATE` privileges from the injected role (e.g. from + `PUBLIC`), granting `set_config` back only to the login user that the proxy + uses for session setup. Pairs with PostgreSQL row-level security to give per-tenant data isolation when the application connects as a shared service-account user. **Requires PgBouncer (if used) to run in `pool_mode = session`** — transaction or @@ -1096,3 +1103,10 @@ To verify a specific binary against the signed checksum list (example: `iron-pro ```bash shasum -a 256 iron-proxy-linux-amd64 | grep -F "$(grep -F 'iron-proxy-linux-amd64' checksums.txt | awk '{print $1}')" ``` + +### SSH credential proxy (local development) + +The opt-in [SSH listener](internal/sshproxy/README.md) maps a sandbox's disposable identity +onto a separately held upstream key. It supports non-interactive +exec, SFTP, and wstunnel v11 WSS endpoints, with pinned SSH host identities and +renewable authorization leases. HTTP and SSH share one executable and process. diff --git a/cmd/iron-proxy/main.go b/cmd/iron-proxy/main.go index a06534a..3b8c894 100644 --- a/cmd/iron-proxy/main.go +++ b/cmd/iron-proxy/main.go @@ -1,4 +1,4 @@ -// Command iron-proxy runs the MITM HTTP/HTTPS proxy with built-in DNS server. +// Command iron-proxy runs HTTP/HTTPS, DNS, and optional Postgres and SSH listeners. package main import ( @@ -29,6 +29,7 @@ import ( "github.com/ironsh/iron-proxy/internal/postgres" "github.com/ironsh/iron-proxy/internal/proxy" "github.com/ironsh/iron-proxy/internal/responseretry" + "github.com/ironsh/iron-proxy/internal/sshproxy" "github.com/ironsh/iron-proxy/internal/transform" "github.com/ironsh/iron-proxy/internal/version" @@ -60,6 +61,7 @@ func main() { } configPath := flag.String("config", "", "path to iron-proxy YAML config file") + requireSSH := flag.Bool("ssh", false, "require SSH configuration at startup") tokenFlag := flag.String("token", "", "control plane bearer token (managed mode)") flag.Parse() @@ -108,7 +110,7 @@ func main() { // errc collects fatal errors from all background goroutines: servers // and (in managed mode) the config poller. - errc := make(chan error, 5) + errc := make(chan error, 6) // The postgres listener comes from the local YAML postgres: block // (self-managed proxies, inline DSNs) and, in managed mode, has additional @@ -214,6 +216,22 @@ func main() { ) } + sshServer, sshListener, err := sshproxy.LoadFromNode(ctx, cfg.SSH, guard, auditFunc, logger) + if err == nil && *requireSSH && sshServer == nil { + err = fmt.Errorf("SSH configuration required") + } + if err != nil { + logger.Error("initializing SSH listener", slog.String("error", err.Error())) + os.Exit(1) + } + if sshListener != nil { + defer func() { + if err := sshListener.Close(); err != nil && !errors.Is(err, net.ErrClosed) { + logger.Error("closing SSH listener", slog.String("error", err.Error())) + } + }() + } + responseRetryHandler, responseRetryStatuses, err := responseRetryHandlerFromEnv( os.Getenv, time.Duration(cfg.Proxy.UpstreamResponseHeaderTimeout), @@ -272,6 +290,17 @@ func main() { mgmtServer = management.New(mgmtOpts) } + var sshDone chan struct{} + if sshServer != nil { + sshDone = make(chan struct{}) + go func() { + defer close(sshDone) + if err := sshServer.Serve(ctx, sshListener); err != nil { + errc <- fmt.Errorf("ssh: %w", err) + } + }() + } + // Start services. if dnsServer != nil { go func() { errc <- fmt.Errorf("dns: %w", dnsServer.ListenAndServe()) }() @@ -343,6 +372,14 @@ func main() { logger.Error("postgres server shutdown error", slog.String("error", err.Error())) } + if sshDone != nil { + select { + case <-sshDone: + case <-shutdownCtx.Done(): + logger.Error("SSH shutdown timed out") + } + } + logger.Info("iron-proxy stopped") } diff --git a/examples/docker-compose/Dockerfile b/examples/docker-compose/Dockerfile index c4a402b..ab67610 100644 --- a/examples/docker-compose/Dockerfile +++ b/examples/docker-compose/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.27-alpine AS build +FROM golang:1.27.3-alpine AS build RUN apk add --no-cache git WORKDIR /src COPY go.mod go.sum ./ diff --git a/examples/nftables/Dockerfile b/examples/nftables/Dockerfile index 750e8c1..7a4aabd 100644 --- a/examples/nftables/Dockerfile +++ b/examples/nftables/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.27-alpine AS build +FROM golang:1.27.3-alpine AS build RUN apk add --no-cache git WORKDIR /src COPY go.mod go.sum ./ diff --git a/go.mod b/go.mod index 558425f..f585d9e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/ironsh/iron-proxy -go 1.26.1 +go 1.27.3 require ( github.com/1Password/connect-sdk-go v1.5.3 @@ -11,11 +11,13 @@ require ( github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.46.1 github.com/aws/aws-sdk-go-v2/service/ssm v1.75.1 github.com/cenkalti/backoff/v5 v5.0.3 + github.com/gobwas/ws v1.4.0 github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/hashicorp/vault/api v1.23.0 github.com/jackc/pgx/v5 v5.10.0 github.com/miekg/dns v1.1.72 github.com/pganalyze/pg_query_go/v6 v6.2.2 + github.com/pkg/sftp v1.13.12 github.com/stretchr/testify v1.11.1 github.com/testcontainers/testcontainers-go v0.44.0 github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 @@ -27,6 +29,7 @@ require ( go.opentelemetry.io/otel/log v0.20.0 go.opentelemetry.io/otel/sdk v1.44.0 go.opentelemetry.io/otel/sdk/log v0.20.0 + golang.org/x/crypto v0.55.0 golang.org/x/net v0.58.0 golang.org/x/oauth2 v0.36.0 google.golang.org/grpc v1.83.0 @@ -74,6 +77,8 @@ require ( github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.3.0 // indirect github.com/gobwas/glob v0.2.3 // indirect + github.com/gobwas/httphead v0.1.0 // indirect + github.com/gobwas/pool v0.2.1 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect @@ -90,6 +95,7 @@ require ( github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/klauspost/compress v1.18.6 // indirect github.com/klauspost/cpuid/v2 v2.0.12 // indirect + github.com/kr/fs v0.1.0 // indirect github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e // indirect github.com/magiconair/properties v1.8.10 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect @@ -126,7 +132,6 @@ require ( go.opentelemetry.io/otel/trace v1.44.0 // indirect go.opentelemetry.io/proto/otlp v1.10.0 // indirect go.uber.org/atomic v1.9.0 // indirect - golang.org/x/crypto v0.55.0 // indirect golang.org/x/mod v0.38.0 // indirect golang.org/x/sync v0.22.0 // indirect golang.org/x/sys v0.47.0 // indirect diff --git a/go.sum b/go.sum index ce57f25..373d77e 100644 --- a/go.sum +++ b/go.sum @@ -103,6 +103,12 @@ github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U= github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= +github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= +github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= +github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= +github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs= +github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= @@ -152,6 +158,8 @@ github.com/klauspost/compress v1.18.6 h1:2jupLlAwFm95+YDR+NwD2MEfFO9d4z4Prjl1XXD github.com/klauspost/compress v1.18.6/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE= github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= +github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= +github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -202,6 +210,8 @@ github.com/pganalyze/pg_query_go/v6 v6.2.2 h1:O0L6zMC226R82RF3X5n0Ki6HjytDsoAzuz github.com/pganalyze/pg_query_go/v6 v6.2.2/go.mod h1:Cn6+j4870kJz3iYNsb0VsNG04vpSWgEvBwc590J4qD0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/sftp v1.13.12 h1:Myhzy/ZzLPnCtE2F2WtDHsgs8RfgMnH2gfbHuCJhkMU= +github.com/pkg/sftp v1.13.12/go.mod h1:uNkH9roSXglNJqM+glJJi+TQXQUm0fXFWqCFmT8hsN0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU= @@ -291,6 +301,7 @@ golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= golang.org/x/term v0.45.0 h1:NwWyBmoJCbfTHpxrWoZ9C6/VxOf7ic219I8xZZFdrf0= diff --git a/integration_test/postgres_test.go b/integration_test/postgres_test.go index 21cca18..f707932 100644 --- a/integration_test/postgres_test.go +++ b/integration_test/postgres_test.go @@ -307,13 +307,31 @@ func TestPostgresPolicy(t *testing.T) { require.Equal(t, pgRole, role) }) - t.Run("set_config for other gucs passes through", func(t *testing.T) { - // Only role-mutating GUCs trigger the rejection; set_config for benign - // parameters should pass through. - conn := dial(t, pgClientPassword) - results, err := conn.Exec(context.Background(), "SELECT set_config('application_name', 'iron-test', false)").ReadAll() + t.Run("parameterized set_config target is rejected", func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + conn, err := pgx.Connect(ctx, connStr(pgClientPassword)) require.NoError(t, err) - require.Len(t, results, 1) + t.Cleanup(func() { require.NoError(t, conn.Close(context.Background())) }) + + _, err = conn.Exec(ctx, "SELECT set_config($1, $2, false)", "role", "other_role") + require.Error(t, err) + var pgErr *pgconn.PgError + require.True(t, errors.As(err, &pgErr)) + require.Contains(t, pgErr.Message, "set_config is not supported") + + var role string + require.NoError(t, conn.QueryRow(ctx, "SELECT current_role").Scan(&role)) + require.Equal(t, pgRole, role) + }) + + t.Run("set_config for other gucs is rejected", func(t *testing.T) { + conn := dial(t, pgClientPassword) + _, err := conn.Exec(context.Background(), "SELECT set_config('application_name', 'iron-test', false)").ReadAll() + require.Error(t, err) + var pgErr *pgconn.PgError + require.True(t, errors.As(err, &pgErr)) + require.Contains(t, pgErr.Message, "set_config is not supported") }) t.Run("session setting is injected at session start", func(t *testing.T) { @@ -355,6 +373,34 @@ func TestPostgresPolicy(t *testing.T) { require.Equal(t, pgRole, currentRole(t, conn)) }) + t.Run("pg_settings writes are rejected", func(t *testing.T) { + // pg_settings' update rule calls set_config, and an auto-updatable + // view over it forwards updates, so both would bypass pinned settings. + conn := dial(t, pgClientPassword) + for _, sql := range []string{ + "UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'", + "CREATE TEMP VIEW settings_view AS SELECT * FROM pg_settings", + } { + err := exec(t, conn, sql) + require.Error(t, err) + var pgErr *pgconn.PgError + require.True(t, errors.As(err, &pgErr)) + require.Contains(t, pgErr.Message, "writing to pg_settings") + } + }) + + t.Run("callback definition aliasing set_config is rejected", func(t *testing.T) { + // An aggregate whose SFUNC is set_config would let the client invoke + // set_config under another name, bypassing the FuncCall check. + conn := dial(t, pgClientPassword) + err := exec(t, conn, "CREATE AGGREGATE pg_temp.cfg(text, boolean) (SFUNC = pg_catalog.set_config, STYPE = text, INITCOND = 'role')") + require.Error(t, err) + var pgErr *pgconn.PgError + require.True(t, errors.As(err, &pgErr)) + require.Contains(t, pgErr.Message, "bind functions as callbacks") + require.Equal(t, pgRole, currentRole(t, conn)) + }) + t.Run("do block is rejected", func(t *testing.T) { conn := dial(t, pgClientPassword) err := exec(t, conn, "DO $$ BEGIN PERFORM 1; END $$") diff --git a/internal/config/config.go b/internal/config/config.go index 31d7d1c..4efc442 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -52,6 +52,7 @@ func (d *Duration) UnmarshalYAML(value *yaml.Node) error { // package stays decoupled from the consumer's typed schema, and validation // errors surface where the value is actually used. type Config struct { + SSH yaml.Node `yaml:"ssh"` DNS DNS `yaml:"dns"` Proxy Proxy `yaml:"proxy"` TLS TLS `yaml:"tls"` diff --git a/internal/config/env.go b/internal/config/env.go index 9365034..9fbdba3 100644 --- a/internal/config/env.go +++ b/internal/config/env.go @@ -7,12 +7,20 @@ import ( "strings" "time" + "gopkg.in/yaml.v3" + "github.com/ironsh/iron-proxy/internal/dnsguard" ) // applyEnvOverrides layers IRON_* environment variables on top of an existing // Config. Only non-empty environment variables override the corresponding field. func applyEnvOverrides(cfg *Config) error { + if raw := os.Getenv("IRON_SSH_CONFIG_JSON"); raw != "" { + if yaml.Unmarshal([]byte(raw), &cfg.SSH) != nil { + return fmt.Errorf("IRON_SSH_CONFIG_JSON is invalid") + } + } + if v := os.Getenv("IRON_DNS_ENABLED"); v != "" { b, err := strconv.ParseBool(v) if err != nil { diff --git a/internal/config/env_test.go b/internal/config/env_test.go index e9d2b08..a0cbab7 100644 --- a/internal/config/env_test.go +++ b/internal/config/env_test.go @@ -428,6 +428,7 @@ func setEnvs(t *testing.T, envs map[string]string) { // Clear all IRON_* env vars to prevent cross-test leakage. ironKeys := []string{ + "IRON_SSH_CONFIG_JSON", "IRON_DNS_ENABLED", "IRON_DNS_LISTEN", "IRON_DNS_PROXY_IP", @@ -454,3 +455,17 @@ func setEnvs(t *testing.T, envs map[string]string) { t.Setenv(k, v) } } + +func TestSSHEnvironmentConfig(t *testing.T) { + setEnvs(t, map[string]string{"IRON_SSH_CONFIG_JSON": `{"listen":"127.0.0.1:2222"}`}) + cfg, err := LoadConfig("") + require.NoError(t, err) + var ssh struct { + Listen string `yaml:"listen"` + } + require.NoError(t, cfg.SSH.Decode(&ssh)) + require.Equal(t, "127.0.0.1:2222", ssh.Listen) + t.Setenv("IRON_SSH_CONFIG_JSON", "sensitive-value: [") + _, err = LoadConfig("") + require.EqualError(t, err, "IRON_SSH_CONFIG_JSON is invalid") +} diff --git a/internal/postgres/config.go b/internal/postgres/config.go index 3323f71..e40edd3 100644 --- a/internal/postgres/config.go +++ b/internal/postgres/config.go @@ -17,12 +17,19 @@ // While the relay is running the proxy is mostly transparent. It rejects // client-issued role-changing statements (`SET ROLE`, `RESET ROLE`, // `SET SESSION AUTHORIZATION`, `RESET SESSION AUTHORIZATION`) and their -// function-call equivalents (`set_config('role', ...)`); any `SET`, `RESET`, or -// `set_config` of a setting the upstream pins; the reset-everything statements +// function-call equivalents; all calls to `set_config`; any `SET` or `RESET` +// of a setting the upstream pins; the reset-everything statements // `RESET ALL` and `DISCARD ALL` (which would clear the managed role and pinned -// settings); and DO blocks. Multi-statement Simple Queries are allowed as long -// as every statement passes; a batch is rejected if any statement is rejected. -// Extended Query, COPY, and prepared statements pass through unchanged. +// settings); UPDATE or INSERT on pg_settings and view or rule definitions over +// it; DO blocks; uninspectable function and procedure definitions; +// definitions that bind functions as implicit callbacks (CREATE AGGREGATE, +// CREATE OPERATOR, CREATE TYPE, CREATE CAST, and similar); and +// the legacy wire-level FunctionCall protocol. SQL and PL/pgSQL routine bodies +// are inspected recursively, while dynamic PL/pgSQL and other languages are +// rejected. +// Multi-statement Simple Queries are allowed as long as every statement passes; +// a batch is rejected if any statement is rejected. Extended Query, COPY, and +// prepared statements pass through unchanged. // // The proxy runs a single postgres listener fronting multiple upstream // databases: the top-level postgres: block is one object with a listen address diff --git a/internal/postgres/policy.go b/internal/postgres/policy.go index 414a5b0..e1ed529 100644 --- a/internal/postgres/policy.go +++ b/internal/postgres/policy.go @@ -14,10 +14,9 @@ const ( // The plpgsql body is opaque to our SQL-level AST walker; rejecting // avoids the risk of an embedded role change. RejectDoBlock - // RejectPinnedSetting — the client tried to SET / RESET / set_config a - // session variable the upstream pins. The proxy sets these at session start - // and forbids overrides so a setting used as a security boundary can't be - // changed. + // RejectPinnedSetting — the client tried to SET or RESET a session variable + // the upstream pins. The proxy sets these at session start and forbids + // overrides so a setting used as a security boundary can't be changed. RejectPinnedSetting // RejectResetAll — the client tried RESET ALL, which would reset the // proxy-managed role and every pinned setting. @@ -25,6 +24,24 @@ const ( // RejectDiscardAll — the client tried DISCARD ALL, which (like RESET ALL) // resets the proxy-managed role and every pinned setting. RejectDiscardAll + // RejectSetConfig — the client called set_config. Dynamic targets cannot be + // inspected safely, so all calls are rejected regardless of the GUC name. + RejectSetConfig + // RejectFunctionCall — the client used the legacy wire-level FunctionCall + // protocol, which invokes a function by OID without inspectable SQL text. + RejectFunctionCall + // RejectUninspectableRoutine — the client tried to define a function or + // procedure whose body cannot be inspected safely. + RejectUninspectableRoutine + // RejectSettingsCatalogWrite — the client tried to UPDATE or INSERT into + // pg_settings, or define a view or rule over it. Writes through pg_settings + // call set_config and would bypass pinned settings. + RejectSettingsCatalogWrite + // RejectCallbackDefinition — the client tried to define an aggregate, + // operator, type, cast, or other object that binds existing functions as + // implicitly invoked callbacks. These create executable aliases (e.g. an + // aggregate whose SFUNC is set_config) the classifier cannot see. + RejectCallbackDefinition ) // ClassifyClientStatement inspects sql and returns whether the relay should @@ -32,8 +49,10 @@ const ( // the lowercased set of setting names this upstream forbids the client from // mutating; pass nil when the upstream pins nothing. // -// Beyond the role policy, the proxy rejects any SET / RESET / set_config of a -// pinned setting, and any RESET ALL / DISCARD ALL (which would reset the +// Beyond the role policy, the proxy rejects every set_config call, writes to +// pg_settings (directly or via a client-defined view or rule), definitions that +// bind functions as implicit callbacks (CREATE AGGREGATE and friends), any SET or RESET +// of a pinned setting, and any RESET ALL / DISCARD ALL (which would reset the // managed role and every pinned setting at once). // // Multi-statement Simple Queries are allowed when every statement passes; @@ -47,11 +66,19 @@ func ClassifyClientStatement(sql string, pinned map[string]struct{}) (allowed bo switch op.Kind { case OpSetRole, OpSetSessionAuthorization, OpResetRole, OpResetSessionAuthorization: return false, RejectClientRoleChange + case OpSetConfig: + return false, RejectSetConfig case OpDoBlock: // DO blocks contain opaque plpgsql we can't introspect from the SQL // AST. Rather than risk an embedded role change slipping through, // we reject them outright. return false, RejectDoBlock + case OpUninspectableRoutine: + return false, RejectUninspectableRoutine + case OpSettingsCatalogWrite: + return false, RejectSettingsCatalogWrite + case OpCallbackDefinition: + return false, RejectCallbackDefinition } // RESET ALL / DISCARD ALL reset the proxy-managed role regardless of which // settings the upstream pins, so they are always rejected. diff --git a/internal/postgres/policy_test.go b/internal/postgres/policy_test.go index 555e966..c8c3cf2 100644 --- a/internal/postgres/policy_test.go +++ b/internal/postgres/policy_test.go @@ -9,7 +9,10 @@ import ( func TestClassifyClientStatement(t *testing.T) { // pinned models an upstream that pins one custom setting; nil means an // upstream that pins nothing beyond the always-on role policy. - pinned := map[string]struct{}{"centaur.slack_channel_id": {}} + pinned := map[string]struct{}{ + "centaur.slack_channel_id": {}, + "centaur.slack_user": {}, + } tests := []struct { name string @@ -54,7 +57,7 @@ func TestClassifyClientStatement(t *testing.T) { {name: "set pinned rejected", sql: "SET centaur.slack_channel_id = 'C999'", pinned: pinned, reason: RejectPinnedSetting}, {name: "set local pinned rejected", sql: "SET LOCAL centaur.slack_channel_id = 'C999'", pinned: pinned, reason: RejectPinnedSetting}, {name: "reset pinned rejected", sql: "RESET centaur.slack_channel_id", pinned: pinned, reason: RejectPinnedSetting}, - {name: "set_config pinned rejected", sql: "SELECT set_config('centaur.slack_channel_id', 'C999', false)", pinned: pinned, reason: RejectPinnedSetting}, + {name: "set_config pinned rejected", sql: "SELECT set_config('centaur.slack_channel_id', 'C999', false)", pinned: pinned, reason: RejectSetConfig}, {name: "set pinned buried in batch rejected", sql: "SELECT 1; SET centaur.slack_channel_id = 'C999'", pinned: pinned, reason: RejectPinnedSetting}, {name: "reset all rejected even with pin", sql: "RESET ALL", pinned: pinned, reason: RejectResetAll}, // Reading a pinned setting is fine; only writes are blocked. @@ -64,13 +67,72 @@ func TestClassifyClientStatement(t *testing.T) { // Without a pin, the same SET is allowed. {name: "set formerly-pinned allowed without pin", sql: "SET centaur.slack_channel_id = 'C999'", allowed: true}, + // Inspectable SQL-language routine bodies are checked recursively. Calls + // to routines already installed by the database remain the operator's + // responsibility; definitions in opaque languages are rejected. + { + name: "benign sql function allowed", + sql: "CREATE FUNCTION f() RETURNS integer LANGUAGE sql AS $$ SELECT 1 $$", + allowed: true, + }, + { + name: "sql function cannot change pinned guc", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE sql AS $$ SELECT set_config('centaur.slack_user', 'U999', false) $$", + pinned: pinned, + reason: RejectSetConfig, + }, + { + name: "sql function set clause cannot change pinned guc", + sql: "CREATE FUNCTION f() RETURNS integer LANGUAGE sql SET centaur.slack_user = 'U999' AS $$ SELECT 1 $$", + pinned: pinned, + reason: RejectPinnedSetting, + }, + { + name: "sql procedure cannot reset settings", + sql: "CREATE PROCEDURE p() LANGUAGE sql AS $$ RESET ALL $$", + reason: RejectResetAll, + }, + { + name: "benign plpgsql function allowed", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN 'ok'; END $$", + allowed: true, + }, + { + name: "plpgsql function cannot change pinned guc", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN set_config('centaur.slack_user', 'U999', false); END $$", + pinned: pinned, + reason: RejectSetConfig, + }, + { + name: "plpgsql procedure cannot set pinned guc", + sql: "CREATE PROCEDURE p() LANGUAGE plpgsql AS $$ BEGIN SET centaur.slack_user = 'U999'; END $$", + pinned: pinned, + reason: RejectPinnedSetting, + }, + { + name: "plpgsql dynamic sql rejected", + sql: "CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN EXECUTE 'SELECT 1'; END $$", + reason: RejectUninspectableRoutine, + }, + { + name: "sql function may call existing routine", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE sql AS $$ SELECT existing_wrapper() $$", + allowed: true, + }, + // Function-call bypass attempts — caught by AST walker. - {name: "set_config role rejected", sql: "SELECT set_config('role', 'admin', false)", reason: RejectClientRoleChange}, - {name: "pg_catalog set_config role rejected", sql: "SELECT pg_catalog.set_config('role', 'admin', false)", reason: RejectClientRoleChange}, - {name: "set_config session_authorization rejected", sql: "SELECT set_config('session_authorization', 'admin', false)", reason: RejectClientRoleChange}, - {name: "set_config role in cte rejected", sql: "WITH x AS (SELECT set_config('role', 'admin', false)) SELECT * FROM x", reason: RejectClientRoleChange}, + {name: "set_config role rejected", sql: "SELECT set_config('role', 'admin', false)", reason: RejectSetConfig}, + {name: "pg_catalog set_config role rejected", sql: "SELECT pg_catalog.set_config('role', 'admin', false)", reason: RejectSetConfig}, + {name: "set_config session_authorization rejected", sql: "SELECT set_config('session_authorization', 'admin', false)", reason: RejectSetConfig}, + {name: "set_config role in cte rejected", sql: "WITH x AS (SELECT set_config('role', 'admin', false)) SELECT * FROM x", reason: RejectSetConfig}, + {name: "set_config non-role setting rejected", sql: "SELECT set_config('search_path', 'public', false)", reason: RejectSetConfig}, + {name: "set_config parameter target rejected", sql: "SELECT set_config($1, $2, false)", reason: RejectSetConfig}, + {name: "set_config cast target rejected", sql: "SELECT set_config('role'::text, 'admin', false)", reason: RejectSetConfig}, {name: "current_setting role allowed", sql: "SELECT current_setting('role')", allowed: true}, - {name: "set_config search_path allowed", sql: "SELECT set_config('search_path', 'public', false)", allowed: true}, + {name: "update pg_settings rejected", sql: "UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'", reason: RejectSettingsCatalogWrite}, + {name: "view over pg_settings rejected", sql: "CREATE TEMP VIEW v AS SELECT * FROM pg_settings", reason: RejectSettingsCatalogWrite}, + {name: "aggregate aliasing set_config rejected", sql: "CREATE AGGREGATE pg_temp.a(text, boolean) (SFUNC = pg_catalog.set_config, STYPE = text)", reason: RejectCallbackDefinition}, + {name: "select pg_settings allowed", sql: "SELECT name, setting FROM pg_settings", allowed: true}, // DO blocks rejected outright. {name: "do block rejected", sql: "DO $$ BEGIN END $$", reason: RejectDoBlock}, diff --git a/internal/postgres/relay_test.go b/internal/postgres/relay_test.go new file mode 100644 index 0000000..617c70e --- /dev/null +++ b/internal/postgres/relay_test.go @@ -0,0 +1,242 @@ +package postgres + +import ( + "encoding/binary" + "io" + "log/slog" + "net" + "testing" + "time" + + "github.com/jackc/pgx/v5/pgproto3" + "github.com/stretchr/testify/require" +) + +func TestRelayRejectsFunctionCall(t *testing.T) { + client, relayClient := net.Pipe() + relayUpstream, upstream := net.Pipe() + clientClosed := false + t.Cleanup(func() { + if !clientClosed { + require.NoError(t, client.Close()) + } + require.NoError(t, relayClient.Close()) + require.NoError(t, relayUpstream.Close()) + require.NoError(t, upstream.Close()) + }) + + backend := newClientBackend(relayClient) + frontend := pgproto3.NewFrontend(relayUpstream, relayUpstream) + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + r := newRelay(relayClient, relayUpstream, backend, frontend, &Upstream{}, logger) + r.txStatus = 'T' + + done := make(chan struct{}) + go func() { + defer close(done) + r.clientToServer() + }() + + clientProtocol := pgproto3.NewFrontend(client, client) + clientProtocol.Send(&pgproto3.FunctionCall{ + Function: 1, + ArgFormatCodes: []uint16{0}, + Arguments: [][]byte{[]byte("role"), []byte("other_role"), []byte("false")}, + ResultFormatCode: 0, + }) + require.NoError(t, clientProtocol.Flush()) + + msg, err := clientProtocol.Receive() + require.NoError(t, err) + pgErr, ok := msg.(*pgproto3.ErrorResponse) + require.True(t, ok) + require.Equal(t, "0A000", pgErr.Code) + require.Contains(t, pgErr.Message, "FunctionCall") + + msg, err = clientProtocol.Receive() + require.NoError(t, err) + ready, ok := msg.(*pgproto3.ReadyForQuery) + require.True(t, ok) + require.Equal(t, byte('T'), ready.TxStatus) + + require.NoError(t, upstream.SetReadDeadline(time.Now().Add(50*time.Millisecond))) + _, err = upstream.Read(make([]byte, 1)) + var netErr net.Error + require.ErrorAs(t, err, &netErr) + require.True(t, netErr.Timeout()) + require.NoError(t, upstream.SetReadDeadline(time.Time{})) + + clientProtocol.Send(&pgproto3.Query{String: "SELECT 1"}) + require.NoError(t, clientProtocol.Flush()) + upstreamProtocol := pgproto3.NewBackend(upstream, upstream) + upstreamMsg, err := upstreamProtocol.Receive() + require.NoError(t, err) + query, ok := upstreamMsg.(*pgproto3.Query) + require.True(t, ok) + require.Equal(t, "SELECT 1", query.String) + + require.NoError(t, client.Close()) + clientClosed = true + select { + case <-done: + case <-time.After(time.Second): + require.FailNow(t, "client-to-server relay did not stop") + } +} + +func TestRelayClosesConnectionForOversizedFrontendMessage(t *testing.T) { + client, relayClient := net.Pipe() + relayUpstream, upstream := net.Pipe() + clientClosed := false + upstreamClosed := false + t.Cleanup(func() { + if !clientClosed { + require.NoError(t, client.Close()) + } + if !upstreamClosed { + require.NoError(t, upstream.Close()) + } + }) + + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + r := newRelay( + relayClient, + relayUpstream, + newClientBackend(relayClient), + pgproto3.NewFrontend(relayUpstream, relayUpstream), + &Upstream{}, + logger, + ) + + done := make(chan struct{}) + go func() { + defer close(done) + r.run() + }() + + header := make([]byte, 5) + header[0] = 'Q' + // The protocol length includes its own four bytes, but not the message type. + binary.BigEndian.PutUint32(header[1:], uint32(maxFrontendMessageBodyLen+5)) + _, err := client.Write(header) + require.NoError(t, err) + + require.NoError(t, client.SetReadDeadline(time.Now().Add(time.Second))) + _, err = client.Read(make([]byte, 1)) + require.ErrorIs(t, err, io.EOF) + + select { + case <-done: + case <-time.After(time.Second): + require.FailNow(t, "relay did not stop after oversized frontend message") + } + + require.NoError(t, client.Close()) + clientClosed = true + require.NoError(t, upstream.Close()) + upstreamClosed = true +} + +func TestRelayPreservesUpstreamTransactionStateOnReject(t *testing.T) { + statuses := []struct { + name string + value byte + }{ + {name: "transaction", value: 'T'}, + {name: "failed transaction", value: 'E'}, + } + protocols := []struct { + name string + send func(*pgproto3.Frontend) error + sync bool + }{ + { + name: "simple query", + send: func(client *pgproto3.Frontend) error { + client.Send(&pgproto3.Query{String: "SET ROLE other_role"}) + return client.Flush() + }, + }, + { + name: "extended query", + send: func(client *pgproto3.Frontend) error { + client.Send(&pgproto3.Parse{Query: "SET ROLE other_role"}) + return client.Flush() + }, + sync: true, + }, + } + + for _, status := range statuses { + for _, protocol := range protocols { + t.Run(status.name+"/"+protocol.name, func(t *testing.T) { + client, relayClient := net.Pipe() + relayUpstream, upstream := net.Pipe() + clientClosed := false + upstreamClosed := false + t.Cleanup(func() { + if !clientClosed { + require.NoError(t, client.Close()) + } + if !upstreamClosed { + require.NoError(t, upstream.Close()) + } + }) + + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + r := newRelay( + relayClient, + relayUpstream, + newClientBackend(relayClient), + pgproto3.NewFrontend(relayUpstream, relayUpstream), + &Upstream{}, + logger, + ) + + done := make(chan struct{}) + go func() { + defer close(done) + r.run() + }() + + upstreamProtocol := pgproto3.NewBackend(upstream, upstream) + upstreamProtocol.Send(&pgproto3.ReadyForQuery{TxStatus: status.value}) + require.NoError(t, upstreamProtocol.Flush()) + + clientProtocol := pgproto3.NewFrontend(client, client) + msg, err := clientProtocol.Receive() + require.NoError(t, err) + ready, ok := msg.(*pgproto3.ReadyForQuery) + require.True(t, ok) + require.Equal(t, status.value, ready.TxStatus) + + require.NoError(t, protocol.send(clientProtocol)) + + msg, err = clientProtocol.Receive() + require.NoError(t, err) + _, ok = msg.(*pgproto3.ErrorResponse) + require.True(t, ok) + if protocol.sync { + clientProtocol.Send(&pgproto3.Sync{}) + require.NoError(t, clientProtocol.Flush()) + } + + msg, err = clientProtocol.Receive() + require.NoError(t, err) + ready, ok = msg.(*pgproto3.ReadyForQuery) + require.True(t, ok) + require.Equal(t, status.value, ready.TxStatus) + + require.NoError(t, client.Close()) + clientClosed = true + select { + case <-done: + case <-time.After(time.Second): + require.FailNow(t, "relay did not stop") + } + require.NoError(t, upstream.Close()) + upstreamClosed = true + }) + } + } +} diff --git a/internal/postgres/session.go b/internal/postgres/session.go index 0f87a88..151292c 100644 --- a/internal/postgres/session.go +++ b/internal/postgres/session.go @@ -14,6 +14,11 @@ import ( "github.com/jackc/pgx/v5/pgproto3" ) +// maxFrontendMessageBodyLen caps every client message after the startup packet. +// pgproto3 otherwise accepts bodies up to the protocol's multi-gigabyte length +// field and allocates the declared size before decoding the message. +const maxFrontendMessageBodyLen = 16 << 20 + // runSession owns a single client connection from accept through close. It // performs the proxy-side handshake under a deadline, selects a route from the // startup database name, opens an upstream connection through pgconn, issues @@ -24,7 +29,7 @@ func runSession(ctx context.Context, clientConn net.Conn, listener *Listener, lo _ = clientConn.SetDeadline(time.Now().Add(handshakeTimeout)) - backend := pgproto3.NewBackend(clientConn, clientConn) + backend := newClientBackend(clientConn) startup, err := receiveStartup(clientConn, backend) if err != nil { @@ -111,6 +116,12 @@ func runSession(ctx context.Context, clientConn net.Conn, listener *Listener, lo relay.run() } +func newClientBackend(conn net.Conn) *pgproto3.Backend { + backend := pgproto3.NewBackend(conn, conn) + backend.SetMaxBodyLen(maxFrontendMessageBodyLen) + return backend +} + // receiveStartup handles the optional SSLRequest / GSSEncRequest preludes and // returns the StartupMessage that follows. Returns (nil, nil) when the client // sent a CancelRequest (handled by closing the connection) since the proxy @@ -267,9 +278,8 @@ func completeClientHandshake(backend *pgproto3.Backend, hj *pgconn.HijackedConn) // relay drives the bidirectional message pump between client and upstream // after the role has been set on the upstream session. // -// Two goroutines do socket I/O: c2s reads client messages and rejects -// role-changing statements (and DO blocks) before forwarding everything else; -// s2c is a pure passthrough. +// Two goroutines do socket I/O: c2s reads client messages and applies the +// statement and protocol policy before forwarding; s2c is a pure passthrough. // // Writes to the client serialize through clientWriteMu since both goroutines // may write — c2s when synthesizing a reject, s2c when forwarding the normal @@ -285,6 +295,10 @@ type relay struct { logger *slog.Logger clientWriteMu sync.Mutex + // txStatus is the last ReadyForQuery status received from upstream. It is + // protected by clientWriteMu so synthetic ReadyForQuery messages preserve + // the upstream transaction state. + txStatus byte // skipExtended, when true, drops client Bind/Describe/Execute/Close // messages until a Sync is observed. Set when a Parse is rejected: per the @@ -304,6 +318,7 @@ func newRelay(clientConn, upstreamConn net.Conn, backend *pgproto3.Backend, fron frontend: frontend, upstream: upstream, logger: logger, + txStatus: 'I', } } @@ -327,8 +342,8 @@ func (r *relay) run() { <-done } -// clientToServer reads frontend messages from the client and forwards them -// upstream, rejecting only role-changing statements and DO blocks. +// clientToServer reads frontend messages from the client and forwards those +// permitted by the statement and protocol policy upstream. func (r *relay) clientToServer() { for { msg, err := r.backend.Receive() @@ -370,13 +385,23 @@ func (r *relay) clientToServer() { return } + case *pgproto3.FunctionCall: + if r.skipExtended { + continue + } + // FunctionCall invokes a function by OID without carrying SQL text, + // so the proxy cannot apply its statement policy. The protocol is a + // legacy fast path; reject it rather than forwarding an opaque call. + r.writeReject(RejectFunctionCall, true) + continue + case *pgproto3.Sync: if r.skipExtended { // We rejected an earlier Parse in this extended-query batch. // Synthesize a ReadyForQuery for the client and consume the // Sync without forwarding upstream. r.skipExtended = false - r.writeClient(&pgproto3.ReadyForQuery{TxStatus: 'I'}) + r.writeReadyForQuery() continue } r.frontend.Send(m) @@ -406,17 +431,29 @@ func (r *relay) writeReject(reason RejectReason, withReadyForQuery bool) { r.backend.Send(rejectError(reason)) if withReadyForQuery { - r.backend.Send(&pgproto3.ReadyForQuery{TxStatus: 'I'}) + r.backend.Send(&pgproto3.ReadyForQuery{TxStatus: r.txStatus}) } _ = r.backend.Flush() } +// writeReadyForQuery writes a synthetic ReadyForQuery carrying the most recent +// transaction status observed from upstream. +func (r *relay) writeReadyForQuery() { + r.clientWriteMu.Lock() + defer r.clientWriteMu.Unlock() + r.backend.Send(&pgproto3.ReadyForQuery{TxStatus: r.txStatus}) + _ = r.backend.Flush() +} + // writeClient writes one or more BackendMessages to the client under the // shared write mutex. func (r *relay) writeClient(msgs ...pgproto3.BackendMessage) { r.clientWriteMu.Lock() defer r.clientWriteMu.Unlock() for _, m := range msgs { + if ready, ok := m.(*pgproto3.ReadyForQuery); ok { + r.txStatus = ready.TxStatus + } r.backend.Send(m) } _ = r.backend.Flush() @@ -435,7 +472,7 @@ func rejectError(reason RejectReason) *pgproto3.ErrorResponse { return &pgproto3.ErrorResponse{ Severity: "ERROR", Code: "42501", - Message: "blocked by iron-proxy policy: this session setting is managed by the proxy; clients may not SET / RESET / set_config it", + Message: "blocked by iron-proxy policy: this session setting is managed by the proxy; clients may not SET or RESET it", } case RejectResetAll: return &pgproto3.ErrorResponse{ @@ -449,13 +486,43 @@ func rejectError(reason RejectReason) *pgproto3.ErrorResponse { Code: "42501", Message: "blocked by iron-proxy policy: DISCARD ALL would reset the proxy-managed role and session settings", } + case RejectSetConfig: + return &pgproto3.ErrorResponse{ + Severity: "ERROR", + Code: "42501", + Message: "blocked by iron-proxy policy: set_config is not supported because session settings are managed by the proxy", + } + case RejectSettingsCatalogWrite: + return &pgproto3.ErrorResponse{ + Severity: "ERROR", + Code: "42501", + Message: "blocked by iron-proxy policy: writing to pg_settings, or defining a view or rule over it, is not supported because session settings are managed by the proxy", + } + case RejectCallbackDefinition: + return &pgproto3.ErrorResponse{ + Severity: "ERROR", + Code: "0A000", + Message: "blocked by iron-proxy policy: definitions that bind functions as callbacks (aggregates, operators, types, casts, and similar) are not supported", + } + case RejectFunctionCall: + return &pgproto3.ErrorResponse{ + Severity: "ERROR", + Code: "0A000", + Message: "blocked by iron-proxy policy: the legacy FunctionCall protocol is not supported", + } + case RejectUninspectableRoutine: + return &pgproto3.ErrorResponse{ + Severity: "ERROR", + Code: "0A000", + Message: "blocked by iron-proxy policy: function and procedure definitions must use inspectable SQL or PL/pgSQL without dynamic SQL", + } case RejectClientRoleChange: fallthrough default: return &pgproto3.ErrorResponse{ Severity: "ERROR", Code: "42501", - Message: "blocked by iron-proxy policy: role is managed by the proxy; clients may not issue SET ROLE / SET SESSION AUTHORIZATION / RESET ROLE / set_config('role',...) / set_config('session_authorization',...)", + Message: "blocked by iron-proxy policy: role is managed by the proxy; clients may not issue SET ROLE / SET SESSION AUTHORIZATION / RESET ROLE", } } } diff --git a/internal/postgres/sql.go b/internal/postgres/sql.go index c6ebbd2..17a57bc 100644 --- a/internal/postgres/sql.go +++ b/internal/postgres/sql.go @@ -1,13 +1,14 @@ package postgres import ( + "encoding/json" "strings" // The wasilibs package is a pure-Go (wazero-based) drop-in for pg_query_go's // Parse function, sparing us from a CGO build dependency. AST node types // still come from the pganalyze package; wasilibs re-uses them. - pgparse "github.com/wasilibs/go-pgquery" pg_query "github.com/pganalyze/pg_query_go/v6" + pgparse "github.com/wasilibs/go-pgquery" "google.golang.org/protobuf/reflect/protoreflect" lru "github.com/hashicorp/golang-lru/v2" @@ -49,8 +50,7 @@ const ( // OpOther is any statement not matched by a more specific case. OpOther OpKind = iota // OpSetRole is `SET ROLE ` or anything anywhere in the AST that - // changes the `role` GUC — including SET LOCAL, SET SESSION, - // and function-call bypasses like `set_config('role', ...)`. + // changes the `role` GUC through SET, including SET LOCAL and SET SESSION. OpSetRole // OpSetSessionAuthorization is the equivalent for `session_authorization`. OpSetSessionAuthorization @@ -69,6 +69,28 @@ const ( // upstream so Postgres produces the canonical syntax error; the proxy // doesn't try to second-guess. OpParseError + // OpSetConfig is any call to set_config, regardless of schema qualification + // or whether its target setting is a string literal. Dynamic targets cannot + // be proven safe at Parse time, so the policy rejects the function wholesale. + OpSetConfig + // OpUninspectableRoutine is a CREATE FUNCTION / CREATE PROCEDURE statement + // whose body cannot be inspected. Dynamic PL/pgSQL and unsupported languages + // can hide role or GUC changes, so the policy rejects their definitions. + OpUninspectableRoutine + // OpSettingsCatalogWrite is an UPDATE or INSERT targeting pg_settings, or a + // view or rule definition referencing it. pg_settings' update rule calls + // set_config with the row's name, and an auto-updatable view over it + // forwards updates the same way, so these reach any listed setting — + // including pinned built-in ones — without a set_config call in the SQL. + OpSettingsCatalogWrite + // OpCallbackDefinition is a statement that binds existing functions by + // name into catalog metadata that PostgreSQL later invokes implicitly: + // CREATE AGGREGATE (SFUNC, FINALFUNC, ...), CREATE OPERATOR, CREATE TYPE, + // CREATE CAST WITH FUNCTION, operator class support functions, and + // similar. Such definitions create executable aliases (e.g. an aggregate + // whose SFUNC is pg_catalog.set_config) that the FuncCall-based + // classifier cannot see at call time, so the policy rejects them. + OpCallbackDefinition ) // GUC names whose mutation we treat as a role change for policy purposes. @@ -78,13 +100,6 @@ var roleGUCs = map[string]OpKind{ "session_authorization": OpSetSessionAuthorization, } -// dangerous set_config / current_setting target GUCs. We don't reject reads -// (current_setting), only writes (set_config). -var roleGUCsForSetConfig = map[string]OpKind{ - "role": OpSetRole, - "session_authorization": OpSetSessionAuthorization, -} - // classifyCache memoizes Classify results so repeated queries (the common // case in ORM workloads where the same parameterized SQL fires hundreds of // times per connection) don't re-parse on the hot path. Bounded so a @@ -135,7 +150,7 @@ func classifyUncached(sql string) Op { if root == nil { continue } - m := classifyStmt(root) + m := classifyStmt(root, rawStatementSQL(sql, s)) if op.Kind == OpOther { op.Kind = m.kind } @@ -157,9 +172,9 @@ type mutations struct { } // classifyStmt classifies a single statement's root node. -func classifyStmt(root *pg_query.Node) mutations { +func classifyStmt(root *pg_query.Node, statementSQL string) mutations { // Top-level shape gives us a fast path for the common DDL/DML cases. - switch root.Node.(type) { + switch n := root.Node.(type) { case *pg_query.Node_TransactionStmt: // BEGIN / COMMIT / ROLLBACK / SAVEPOINT etc. — no GUC mutation. return mutations{kind: OpOther} @@ -167,10 +182,209 @@ func classifyStmt(root *pg_query.Node) mutations { // The plpgsql body is opaque to the SQL AST walker; we can't see GUC // writes inside it, so the statement is rejected wholesale via Kind. return mutations{kind: OpDoBlock} + case *pg_query.Node_CreateFunctionStmt: + return classifyRoutineDefinition(root, n.CreateFunctionStmt, statementSQL) } return scanMutations(root) } +func rawStatementSQL(sql string, stmt *pg_query.RawStmt) string { + start := int(stmt.GetStmtLocation()) + if start < 0 || start > len(sql) { + return sql + } + end := len(sql) + if stmt.GetStmtLen() > 0 { + end = start + int(stmt.GetStmtLen()) + if end > len(sql) { + return sql + } + } + return sql[start:end] +} + +// classifyRoutineDefinition inspects CREATE FUNCTION and CREATE PROCEDURE +// bodies submitted by clients. SQL-standard bodies are already represented as +// AST nodes under SqlBody. String-form LANGUAGE sql bodies need a second parse. +// Other languages are rejected because their bodies are opaque to the SQL AST. +func classifyRoutineDefinition(root *pg_query.Node, stmt *pg_query.CreateFunctionStmt, statementSQL string) mutations { + m := scanMutations(root) + if m.kind != OpOther { + return m + } + + language, ok := routineStringOption(stmt.GetOptions(), "language") + if !ok { + m.kind = OpUninspectableRoutine + return m + } + if strings.EqualFold(language, "plpgsql") { + return mergeMutations(m, classifyPLpgSQL(statementSQL)) + } + if !strings.EqualFold(language, "sql") { + m.kind = OpUninspectableRoutine + return m + } + + if stmt.GetSqlBody() != nil { + return m + } + + body, ok := routineStringOption(stmt.GetOptions(), "as") + if !ok { + m.kind = OpUninspectableRoutine + return m + } + bodyOp := Classify(body) + if bodyOp.Kind == OpParseError || bodyOp.Kind == OpEmpty { + m.kind = OpUninspectableRoutine + return m + } + return mergeMutations(m, mutationsFromOp(bodyOp)) +} + +// classifyPLpgSQL parses a PL/pgSQL definition, recursively classifies every +// static SQL statement and expression, and rejects dynamic execution. Dynamic +// SQL cannot be resolved at Parse time, even when its current text is a literal. +func classifyPLpgSQL(statementSQL string) mutations { + parsed, err := pgparse.ParsePlPgSqlToJSON(statementSQL) + if err != nil { + return mutations{kind: OpUninspectableRoutine} + } + var tree any + if err := json.Unmarshal([]byte(parsed), &tree); err != nil { + return mutations{kind: OpUninspectableRoutine} + } + m := mutations{kind: OpOther} + if !scanPLpgSQL(tree, &m) { + m.kind = OpUninspectableRoutine + } + return m +} + +func scanPLpgSQL(value any, m *mutations) bool { + switch value := value.(type) { + case []any: + for _, item := range value { + if !scanPLpgSQL(item, m) { + return false + } + } + case map[string]any: + for key, item := range value { + switch key { + case "PLpgSQL_stmt_dynexecute", "PLpgSQL_stmt_dynfors", "dynquery": + return false + case "PLpgSQL_expr": + expr, ok := item.(map[string]any) + if !ok { + return false + } + op, ok := classifyPLpgSQLExpression(expr) + if !ok { + return false + } + *m = mergeMutations(*m, mutationsFromOp(op)) + default: + if !scanPLpgSQL(item, m) { + return false + } + } + } + } + return true +} + +func classifyPLpgSQLExpression(expr map[string]any) (Op, bool) { + query, ok := expr["query"].(string) + if !ok || strings.TrimSpace(query) == "" { + return Op{}, false + } + parseMode, ok := expr["parseMode"].(float64) + if !ok { + parseMode = 0 + } + + switch { + case parseMode == 0: + // A complete SQL statement such as SELECT, PERFORM's synthesized + // SELECT, CALL, or SET. + case parseMode == 2: + query = "SELECT " + query + case parseMode >= 3: + _, rhs, ok := strings.Cut(query, ":=") + if !ok || strings.TrimSpace(rhs) == "" { + return Op{}, false + } + query = "SELECT " + rhs + default: + // Type-name parse modes cannot execute SQL or mutate a setting. + return Op{Kind: OpOther}, true + } + + op := Classify(query) + if op.Kind == OpParseError || op.Kind == OpEmpty { + return Op{}, false + } + return op, true +} + +// routineStringOption returns a single string value from a CREATE FUNCTION / +// CREATE PROCEDURE option. LANGUAGE stores a String directly; AS stores its +// body in a one-item List. Native functions use two AS strings and are not +// considered inspectable. +func routineStringOption(options []*pg_query.Node, name string) (string, bool) { + for _, option := range options { + defNode, ok := option.Node.(*pg_query.Node_DefElem) + if !ok || !strings.EqualFold(defNode.DefElem.GetDefname(), name) { + continue + } + if value, ok := nodeString(defNode.DefElem.GetArg()); ok { + return value, true + } + arg := defNode.DefElem.GetArg() + if arg == nil { + return "", false + } + listNode, ok := arg.Node.(*pg_query.Node_List) + if !ok || len(listNode.List.GetItems()) != 1 { + return "", false + } + return nodeString(listNode.List.GetItems()[0]) + } + return "", false +} + +func nodeString(node *pg_query.Node) (string, bool) { + if node == nil { + return "", false + } + strNode, ok := node.Node.(*pg_query.Node_String_) + if !ok { + return "", false + } + return strNode.String_.GetSval(), true +} + +func mutationsFromOp(op Op) mutations { + return mutations{ + kind: op.Kind, + setGUCs: op.SetGUCs, + resetAll: op.ResetAll, + discard: op.Discard, + } +} + +func mergeMutations(dst, src mutations) mutations { + if dst.kind == OpOther { + dst.kind = src.kind + } + dst.setGUCs = append(dst.setGUCs, src.setGUCs...) + dst.resetAll = dst.resetAll || src.resetAll + dst.discard = dst.discard || src.discard + return dst +} + // scanMutations walks the AST rooted at node and reports every GUC-affecting // construct it finds: SET / RESET / RESET ALL, DISCARD ALL, and set_config(...) // calls, wherever they're nested (CTEs, subqueries, PREPARE bodies, function @@ -203,11 +417,39 @@ func scanMutations(node *pg_query.Node) mutations { } case *pg_query.VariableShowStmt: // SHOW is read-only; ignore. + case *pg_query.UpdateStmt: + if isPgSettings(n.GetRelation()) && m.kind == 0 { + m.kind = OpSettingsCatalogWrite + } + case *pg_query.InsertStmt: + if isPgSettings(n.GetRelation()) && m.kind == 0 { + m.kind = OpSettingsCatalogWrite + } + case *pg_query.ViewStmt, *pg_query.RuleStmt: + if referencesPgSettings(msg) && m.kind == 0 { + m.kind = OpSettingsCatalogWrite + } + case *pg_query.DefineStmt: + if definesCallbacks(n.GetKind()) && m.kind == 0 { + m.kind = OpCallbackDefinition + } + case *pg_query.CreateRangeStmt, *pg_query.CreateCastStmt, + *pg_query.CreateOpClassStmt, *pg_query.AlterOpFamilyStmt, + *pg_query.AlterOperatorStmt, *pg_query.AlterTypeStmt, + *pg_query.CreateConversionStmt, *pg_query.CreatePLangStmt, + *pg_query.CreateFdwStmt, *pg_query.AlterFdwStmt, + *pg_query.CreateAmStmt, *pg_query.CreateTransformStmt, + *pg_query.CreateEventTrigStmt: + if m.kind == 0 { + m.kind = OpCallbackDefinition + } case *pg_query.FuncCall: - if name, kind := setConfigTarget(n); name != "" { - m.setGUCs = append(m.setGUCs, name) - if kind != 0 && m.kind == 0 { - m.kind = kind + if name, ok := setConfigTarget(n); ok { + if name != "" { + m.setGUCs = append(m.setGUCs, name) + } + if m.kind == 0 { + m.kind = OpSetConfig } } } @@ -216,6 +458,40 @@ func scanMutations(node *pg_query.Node) mutations { return m } +// definesCallbacks reports whether a DefineStmt of the given kind can bind +// functions by name. Collations and text search dictionaries and +// configurations reference no functions directly; every other kind +// (aggregates, operators, types, text search parsers and templates) does. +func definesCallbacks(kind pg_query.ObjectType) bool { + switch kind { + case pg_query.ObjectType_OBJECT_COLLATION, + pg_query.ObjectType_OBJECT_TSDICTIONARY, + pg_query.ObjectType_OBJECT_TSCONFIGURATION: + return false + } + return true +} + +// isPgSettings reports whether rv names pg_settings. Any schema qualifier is +// matched: a user relation of that name is a harmless false positive, while +// schema-matching would have to track search_path. +func isPgSettings(rv *pg_query.RangeVar) bool { + return rv != nil && rv.GetRelname() == "pg_settings" +} + +// referencesPgSettings reports whether any relation reference in the tree +// rooted at m names pg_settings. +func referencesPgSettings(m protoreflect.Message) bool { + found := false + walkProto(m, func(msg protoreflect.Message) bool { + if rv, ok := msg.Interface().(*pg_query.RangeVar); ok && isPgSettings(rv) { + found = true + } + return !found + }) + return found +} + // roleKindFor returns the role-policy OpKind for a SET/RESET of the named GUC, // or 0 when the GUC is not role-affecting. kind distinguishes a write // (OpSetRole) from a reset (OpResetRole). @@ -233,46 +509,42 @@ func roleKindFor(name string, kind pg_query.VariableSetKind) OpKind { return base } -// setConfigTarget inspects fc and, when it is a call to set_config (or -// pg_catalog.set_config) whose first argument is a string-literal GUC name, -// returns that lowercased name and the role-policy OpKind for it (0 when the -// target is not role-affecting). Returns ("", 0) when fc is not such a call. -func setConfigTarget(fc *pg_query.FuncCall) (name string, kind OpKind) { +// setConfigTarget inspects fc and reports whether it is a call to set_config, +// including schema-qualified forms. When the first argument is a string +// literal, name is its lowercased value; dynamic targets return an empty name +// with ok=true so the policy can still reject the call. +func setConfigTarget(fc *pg_query.FuncCall) (name string, ok bool) { names := fc.GetFuncname() if len(names) == 0 { - return "", 0 + return "", false } // Funcname is a list of String nodes: ["set_config"] for unqualified // or ["pg_catalog", "set_config"] for schema-qualified. We accept any // schema qualifier and trust that user-defined set_config functions // are also suspicious — better a rare false positive than a bypass. last := names[len(names)-1] - lastStr, ok := last.Node.(*pg_query.Node_String_) - if !ok { - return "", 0 + lastStr, isString := last.Node.(*pg_query.Node_String_) + if !isString { + return "", false } if !strings.EqualFold(lastStr.String_.GetSval(), "set_config") { - return "", 0 + return "", false } args := fc.GetArgs() if len(args) < 1 { - return "", 0 + return "", true } - param, ok := stringConst(args[0]) - if !ok { - return "", 0 + param, isConst := stringConst(args[0]) + if !isConst { + return "", true } - lower := strings.ToLower(param) - // roleGUCsForSetConfig returns the zero OpKind (0) for non-role names, which - // is exactly the "not role-affecting" signal callers expect. - return lower, roleGUCsForSetConfig[lower] + return strings.ToLower(param), true } // stringConst returns the string value of node if it's an A_Const with a // string value, else ("", false). set_config's first argument is the GUC -// name; we only flag calls where it's a literal we can read at parse time. -// `set_config(my_var, ...)` where my_var is a variable evaluates at run -// time and is invisible to us — accepted limitation. +// name. Literal targets are retained for mutation metadata; non-literal +// targets are still classified as OpSetConfig and rejected by policy. func stringConst(node *pg_query.Node) (string, bool) { if node == nil { return "", false diff --git a/internal/postgres/sql_test.go b/internal/postgres/sql_test.go index 4d28265..355e894 100644 --- a/internal/postgres/sql_test.go +++ b/internal/postgres/sql_test.go @@ -52,35 +52,162 @@ func TestClassify(t *testing.T) { {name: "multi select then select allowed", sql: "SELECT 1; SELECT 2", want: OpOther}, {name: "multi benign then set role", sql: "SELECT 1; SET ROLE tenant", want: OpSetRole}, {name: "multi benign then do block", sql: "SELECT 1; DO $$ BEGIN END $$", want: OpDoBlock}, - {name: "multi benign then set_config role", sql: "SELECT 1; SELECT set_config('role', 'admin', false)", want: OpSetRole}, + {name: "multi benign then set_config role", sql: "SELECT 1; SELECT set_config('role', 'admin', false)", want: OpSetConfig}, {name: "multi non-role sets allowed", sql: "SET search_path = public; SELECT 1", want: OpOther}, // Trailing semicolon alone is *not* multi (pg_query collapses it). {name: "trailing semicolon not multi", sql: "SET ROLE tenant;", want: OpSetRole}, // set_config function-call bypass attempts - {name: "set_config role top level", sql: "SELECT set_config('role', 'admin', false)", want: OpSetRole}, - {name: "set_config role local flag", sql: "SELECT set_config('role', 'admin', true)", want: OpSetRole}, - {name: "pg_catalog set_config role", sql: "SELECT pg_catalog.set_config('role', 'admin', false)", want: OpSetRole}, - {name: "set_config session_authorization", sql: "SELECT set_config('session_authorization', 'admin', false)", want: OpSetSessionAuthorization}, - {name: "set_config case insensitive name", sql: "SELECT set_config('ROLE', 'admin', false)", want: OpSetRole}, - {name: "set_config nested in select", sql: "SELECT 1, set_config('role', 'admin', false), 2", want: OpSetRole}, - {name: "set_config in subquery", sql: "SELECT * FROM (SELECT set_config('role', 'admin', false)) s", want: OpSetRole}, - {name: "set_config in cte", sql: "WITH x AS (SELECT set_config('role', 'admin', false)) SELECT * FROM x", want: OpSetRole}, - {name: "set_config in where clause", sql: "SELECT 1 WHERE set_config('role', 'admin', false) = 'admin'", want: OpSetRole}, - {name: "set_config inside insert", sql: "INSERT INTO t SELECT set_config('role', 'admin', false)", want: OpSetRole}, - - // set_config for safe parameters — allowed. - {name: "set_config search_path allowed", sql: "SELECT set_config('search_path', 'public', false)", want: OpOther}, + {name: "set_config role top level", sql: "SELECT set_config('role', 'admin', false)", want: OpSetConfig}, + {name: "set_config role local flag", sql: "SELECT set_config('role', 'admin', true)", want: OpSetConfig}, + {name: "pg_catalog set_config role", sql: "SELECT pg_catalog.set_config('role', 'admin', false)", want: OpSetConfig}, + {name: "set_config session_authorization", sql: "SELECT set_config('session_authorization', 'admin', false)", want: OpSetConfig}, + {name: "set_config case insensitive name", sql: "SELECT set_config('ROLE', 'admin', false)", want: OpSetConfig}, + {name: "set_config nested in select", sql: "SELECT 1, set_config('role', 'admin', false), 2", want: OpSetConfig}, + {name: "set_config in subquery", sql: "SELECT * FROM (SELECT set_config('role', 'admin', false)) s", want: OpSetConfig}, + {name: "set_config in cte", sql: "WITH x AS (SELECT set_config('role', 'admin', false)) SELECT * FROM x", want: OpSetConfig}, + {name: "set_config in where clause", sql: "SELECT 1 WHERE set_config('role', 'admin', false) = 'admin'", want: OpSetConfig}, + {name: "set_config inside insert", sql: "INSERT INTO t SELECT set_config('role', 'admin', false)", want: OpSetConfig}, + {name: "set_config non-role setting", sql: "SELECT set_config('search_path', 'public', false)", want: OpSetConfig}, + {name: "set_config parameter target", sql: "SELECT set_config($1, $2, false)", want: OpSetConfig}, + {name: "set_config cast target", sql: "SELECT set_config('role'::text, 'admin', false)", want: OpSetConfig}, + {name: "set_config computed target", sql: "SELECT set_config(concat('ro', 'le'), 'admin', false)", want: OpSetConfig}, {name: "current_setting role is read only", sql: "SELECT current_setting('role')", want: OpOther}, // PREPARE wrapping a SELECT that calls set_config — caught by the // walker through the nested SelectStmt. - {name: "prepare wrapping set_config caught", sql: "PREPARE p AS SELECT set_config('role', 'admin', false)", want: OpSetRole}, + {name: "prepare wrapping set_config caught", sql: "PREPARE p AS SELECT set_config('role', 'admin', false)", want: OpSetConfig}, + + // pg_settings writes reach set_config through its update rule; an + // auto-updatable view or a rule over it forwards writes the same way. + {name: "update pg_settings", sql: "UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'", want: OpSettingsCatalogWrite}, + {name: "update qualified pg_settings", sql: "UPDATE pg_catalog.pg_settings SET setting = '0' WHERE name = 'statement_timeout'", want: OpSettingsCatalogWrite}, + {name: "update pg_settings in cte", sql: "WITH x AS (UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout') SELECT 1", want: OpSettingsCatalogWrite}, + {name: "prepare update pg_settings", sql: "PREPARE p AS UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'", want: OpSettingsCatalogWrite}, + {name: "insert pg_settings", sql: "INSERT INTO pg_settings (name, setting) VALUES ('statement_timeout', '0')", want: OpSettingsCatalogWrite}, + {name: "view over pg_settings", sql: "CREATE TEMP VIEW v AS SELECT * FROM pg_settings", want: OpSettingsCatalogWrite}, + {name: "view over pg_settings subquery", sql: "CREATE VIEW v AS SELECT * FROM (SELECT name, setting FROM pg_catalog.pg_settings) s", want: OpSettingsCatalogWrite}, + {name: "rule updating pg_settings", sql: "CREATE RULE r AS ON INSERT TO t DO ALSO UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'", want: OpSettingsCatalogWrite}, + {name: "multi benign then update pg_settings", sql: "SELECT 1; UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'", want: OpSettingsCatalogWrite}, + {name: "select pg_settings is other", sql: "SELECT setting FROM pg_settings WHERE name = 'statement_timeout'", want: OpOther}, + {name: "update reading pg_settings is other", sql: "UPDATE t SET x = (SELECT setting FROM pg_settings WHERE name = 'work_mem')", want: OpOther}, + {name: "view not over pg_settings is other", sql: "CREATE VIEW v AS SELECT * FROM t", want: OpOther}, + + // Definitions that bind existing functions as implicitly invoked + // callbacks create executable aliases the FuncCall check cannot see. + {name: "aggregate with set_config sfunc", sql: "CREATE AGGREGATE pg_temp.a(text, boolean) (SFUNC = pg_catalog.set_config, STYPE = text)", want: OpCallbackDefinition}, + {name: "aggregate with benign sfunc", sql: "CREATE AGGREGATE a(integer) (SFUNC = int4pl, STYPE = integer)", want: OpCallbackDefinition}, + {name: "operator definition", sql: "CREATE OPERATOR === (LEFTARG = text, RIGHTARG = text, FUNCTION = texteq)", want: OpCallbackDefinition}, + {name: "base type definition", sql: "CREATE TYPE t (INPUT = t_in, OUTPUT = t_out)", want: OpCallbackDefinition}, + {name: "range type definition", sql: "CREATE TYPE r AS RANGE (SUBTYPE = integer, SUBTYPE_DIFF = f)", want: OpCallbackDefinition}, + {name: "cast with function", sql: "CREATE CAST (text AS t) WITH FUNCTION f(text)", want: OpCallbackDefinition}, + {name: "operator class support function", sql: "CREATE OPERATOR CLASS c FOR TYPE t USING btree AS FUNCTION 1 f(t, t)", want: OpCallbackDefinition}, + {name: "alter operator family add function", sql: "ALTER OPERATOR FAMILY fam USING btree ADD FUNCTION 1 f(t, t)", want: OpCallbackDefinition}, + {name: "alter operator restrict", sql: "ALTER OPERATOR === (text, text) SET (RESTRICT = f)", want: OpCallbackDefinition}, + {name: "alter type set callbacks", sql: "ALTER TYPE t SET (SEND = f)", want: OpCallbackDefinition}, + {name: "conversion definition", sql: "CREATE CONVERSION c FOR 'UTF8' TO 'LATIN1' FROM f", want: OpCallbackDefinition}, + {name: "multi benign then aggregate", sql: "SELECT 1; CREATE AGGREGATE a(text, boolean) (SFUNC = set_config, STYPE = text)", want: OpCallbackDefinition}, + {name: "composite type is other", sql: "CREATE TYPE t AS (a integer, b text)", want: OpOther}, + {name: "enum type is other", sql: "CREATE TYPE e AS ENUM ('a', 'b')", want: OpOther}, + {name: "collation is other", sql: "CREATE COLLATION c (provider = icu, locale = 'und')", want: OpOther}, // DO blocks — rejected regardless of contents. {name: "do block empty", sql: "DO $$ BEGIN END $$", want: OpDoBlock}, {name: "do block with set role", sql: "DO $$ BEGIN EXECUTE 'SET ROLE admin'; END $$", want: OpDoBlock}, + // SQL function and procedure bodies are parsed recursively. Other + // languages remain opaque and are rejected fail-closed. + { + name: "benign sql function", + sql: "CREATE FUNCTION f() RETURNS integer LANGUAGE sql AS $$ SELECT 1 $$", + want: OpOther, + }, + { + name: "sql function calls set_config", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE sql AS $$ SELECT set_config('centaur.slack_user', 'U999', false) $$", + want: OpSetConfig, + }, + { + name: "sql function changes role", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE sql AS $$ SET ROLE none; SELECT current_role::text $$", + want: OpSetRole, + }, + { + name: "sql procedure resets all", + sql: "CREATE PROCEDURE p() LANGUAGE sql AS $$ RESET ALL $$", + want: OpOther, + }, + { + name: "sql standard body calls set_config", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE sql RETURN set_config('centaur.slack_user', 'U999', false)", + want: OpSetConfig, + }, + { + name: "sql function delegates to existing function", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE sql AS $$ SELECT existing_wrapper() $$", + want: OpOther, + }, + { + name: "benign plpgsql function", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN 'ok'; END $$", + want: OpOther, + }, + { + name: "plpgsql return calls set_config", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN set_config('centaur.slack_user', 'U999', false); END $$", + want: OpSetConfig, + }, + { + name: "plpgsql assignment calls set_config", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE plpgsql AS $$ DECLARE x text; BEGIN x := set_config('centaur.slack_user', 'U999', false); RETURN x; END $$", + want: OpSetConfig, + }, + { + name: "plpgsql perform calls set_config", + sql: "CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN PERFORM set_config('centaur.slack_user', 'U999', false); END $$", + want: OpSetConfig, + }, + { + name: "sql function updates pg_settings", + sql: "CREATE FUNCTION f() RETURNS void LANGUAGE sql AS $$ UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout' $$", + want: OpSettingsCatalogWrite, + }, + { + name: "plpgsql function updates pg_settings", + sql: "CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'; END $$", + want: OpSettingsCatalogWrite, + }, + { + name: "plpgsql procedure sets pinned guc", + sql: "CREATE PROCEDURE p() LANGUAGE plpgsql AS $$ BEGIN SET centaur.slack_user = 'U999'; END $$", + want: OpOther, + }, + { + name: "plpgsql dynamic execute is uninspectable", + sql: "CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN EXECUTE 'SET centaur.slack_user = ''U999'''; END $$", + want: OpUninspectableRoutine, + }, + { + name: "plpgsql dynamic cursor is uninspectable", + sql: "CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ DECLARE c refcursor; BEGIN OPEN c FOR EXECUTE 'SELECT 1'; END $$", + want: OpUninspectableRoutine, + }, + { + name: "plpgsql routine after another statement uses its own source", + sql: "SELECT 1; CREATE FUNCTION f() RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN set_config('centaur.slack_user', 'U999', false); END $$", + want: OpSetConfig, + }, + { + name: "native function is uninspectable", + sql: "CREATE FUNCTION f(integer) RETURNS integer AS 'module', 'symbol' LANGUAGE C STRICT", + want: OpUninspectableRoutine, + }, + { + name: "invalid sql body is uninspectable", + sql: "CREATE FUNCTION f() RETURNS integer LANGUAGE sql AS $$ this is not sql $$", + want: OpUninspectableRoutine, + }, + // Parse errors — forwarded. {name: "syntax error", sql: "INSERT FROM WHERE", want: OpParseError}, } @@ -114,6 +241,21 @@ func TestClassifyMutationFacts(t *testing.T) { {name: "reset all flagged", sql: "RESET ALL", resetAll: true}, {name: "discard all flagged", sql: "DISCARD ALL", discard: true}, {name: "discard plans not flagged", sql: "DISCARD PLANS"}, + { + name: "sql procedure body reset all", + sql: "CREATE PROCEDURE p() LANGUAGE sql AS $$ RESET ALL $$", + resetAll: true, + }, + { + name: "sql function set clause collected", + sql: "CREATE FUNCTION f() RETURNS integer LANGUAGE sql SET centaur.slack_user = 'U999' AS $$ SELECT 1 $$", + setGUCs: []string{"centaur.slack_user"}, + }, + { + name: "plpgsql procedure body set collected", + sql: "CREATE PROCEDURE p() LANGUAGE plpgsql AS $$ BEGIN SET centaur.slack_user = 'U999'; END $$", + setGUCs: []string{"centaur.slack_user"}, + }, {name: "select writes nothing", sql: "SELECT 1"}, {name: "current_setting writes nothing", sql: "SELECT current_setting('centaur.slack_channel_id')"}, } diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index 73bde1f..463a748 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -17,7 +17,6 @@ import ( "net/url" "strconv" "strings" - "sync" "time" "github.com/ironsh/iron-proxy/internal/certcache" @@ -822,35 +821,10 @@ func (p *Proxy) handleWebSocket(w http.ResponseWriter, r *http.Request, scheme, return } - // Proxy bidirectionally - var wg sync.WaitGroup - wg.Add(2) - - go func() { - defer wg.Done() - if _, err := io.Copy(upstreamConn, clientBuf); err != nil { - p.logger.Debug("websocket client->upstream copy error", slog.String("error", err.Error())) - } - // Signal upstream we're done writing - if tc, ok := upstreamConn.(*net.TCPConn); ok { - tc.CloseWrite() - } - }() - - go func() { - defer wg.Done() - if _, err := io.Copy(clientConn, upstreamConn); err != nil { - p.logger.Debug("websocket upstream->client copy error", slog.String("error", err.Error())) - } - // Signal client we're done writing - if tc, ok := clientConn.(*net.TCPConn); ok { - tc.CloseWrite() - } - }() - - wg.Wait() - clientConn.Close() - upstreamConn.Close() + // Relay until either side closes or the proxy shuts down. The hijacked + // bufio.Reader may hold bytes already read from the client, so wrap the + // conn to drain it first. + proxyBidi(p.shutdownCtx, newPeekedConn(clientConn, clientBuf.Reader), upstreamConn, p.logger, "websocket") p.logger.Debug("websocket connection closed", slog.String("host", host)) } diff --git a/internal/proxy/sni_passthrough.go b/internal/proxy/sni_passthrough.go index 9beeb44..850e958 100644 --- a/internal/proxy/sni_passthrough.go +++ b/internal/proxy/sni_passthrough.go @@ -2,6 +2,7 @@ package proxy import ( "context" + "errors" "fmt" "io" "log/slog" @@ -135,14 +136,14 @@ func (p *Proxy) serveSNIPassthrough(clientConn net.Conn) error { result.StatusCode = http.StatusOK // Proxy bidirectionally until either side closes or the proxy shuts down. - proxyBidi(p.shutdownCtx, clientConn, upstream, p.logger) + proxyBidi(p.shutdownCtx, clientConn, upstream, p.logger, "sni passthrough") return nil } // proxyBidi copies bytes between two connections in both directions. When // either direction ends (EOF, error, or ctx cancellation), both connections -// are closed so the other direction unblocks. -func proxyBidi(ctx context.Context, a, b net.Conn, logger *slog.Logger) { +// are closed so the other direction unblocks. label names the relay in logs. +func proxyBidi(ctx context.Context, a, b net.Conn, logger *slog.Logger, label string) { ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -152,22 +153,28 @@ func proxyBidi(ctx context.Context, a, b net.Conn, logger *slog.Logger) { _ = b.Close() }() + // The direction that loses the race sees ErrClosed when the winner closes + // its conn; that is the normal teardown, not a copy failure. + logCopyErr := func(dir string, err error) { + if err != nil && !errors.Is(err, net.ErrClosed) { + logger.Debug(label+" "+dir+" copy error", slog.String("error", err.Error())) + } + } + var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() - if _, err := io.Copy(b, a); err != nil { - logger.Debug("sni passthrough a->b copy error", slog.String("error", err.Error())) - } + _, err := io.Copy(b, a) + logCopyErr("a->b", err) cancel() }() go func() { defer wg.Done() - if _, err := io.Copy(a, b); err != nil { - logger.Debug("sni passthrough b->a copy error", slog.String("error", err.Error())) - } + _, err := io.Copy(a, b) + logCopyErr("b->a", err) cancel() }() diff --git a/internal/proxy/tunnel_test.go b/internal/proxy/tunnel_test.go index 01de9fa..04bab51 100644 --- a/internal/proxy/tunnel_test.go +++ b/internal/proxy/tunnel_test.go @@ -578,10 +578,70 @@ func (t *tunnelInfoTransform) TransformResponse(_ context.Context, _ *transform. return &transform.TransformResult{Action: transform.ActionContinue}, nil } -// startWebSocketEchoUpstream starts a raw TCP server that accepts a single -// connection, answers a WebSocket upgrade request with 101 Switching -// Protocols, then echoes all subsequent bytes back to the client. -func startWebSocketEchoUpstream(t *testing.T) string { +// dialCONNECT dials the tunnel listener, issues CONNECT for target, and +// returns the connection plus a reader positioned after the 200 reply. +func dialCONNECT(t *testing.T, tunnelAddr, target string) (net.Conn, *bufio.Reader) { + t.Helper() + + conn, err := net.DialTimeout("tcp", tunnelAddr, 5*time.Second) + require.NoError(t, err) + t.Cleanup(func() { _ = conn.Close() }) + + _, err = fmt.Fprintf(conn, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n", target, target) + require.NoError(t, err) + + br := bufio.NewReader(conn) + resp, err := http.ReadResponse(br, nil) + require.NoError(t, err) + _ = resp.Body.Close() + require.Equal(t, http.StatusOK, resp.StatusCode) + return conn, br +} + +// dialSOCKS5 dials the tunnel listener and completes a no-auth SOCKS5 +// CONNECT to target, which must be an IPv4 host:port. +func dialSOCKS5(t *testing.T, tunnelAddr, target string) (net.Conn, *bufio.Reader) { + t.Helper() + + conn, err := net.DialTimeout("tcp", tunnelAddr, 5*time.Second) + require.NoError(t, err) + t.Cleanup(func() { _ = conn.Close() }) + + targetHost, targetPortStr, err := net.SplitHostPort(target) + require.NoError(t, err) + ip := net.ParseIP(targetHost).To4() + require.NotNil(t, ip) + var port uint16 + _, err = fmt.Sscanf(targetPortStr, "%d", &port) + require.NoError(t, err) + + // Auth negotiation: version 5, 1 method (no auth) + _, err = conn.Write([]byte{0x05, 0x01, 0x00}) + require.NoError(t, err) + authResp := make([]byte, 2) + _, err = io.ReadFull(conn, authResp) + require.NoError(t, err) + require.Equal(t, []byte{0x05, 0x00}, authResp) + + connectReq := []byte{0x05, 0x01, 0x00, 0x01} // ver, cmd=connect, rsv, atyp=IPv4 + connectReq = append(connectReq, ip...) + connectReq = binary.BigEndian.AppendUint16(connectReq, port) + _, err = conn.Write(connectReq) + require.NoError(t, err) + + connectResp := make([]byte, 10) + _, err = io.ReadFull(conn, connectResp) + require.NoError(t, err) + require.Equal(t, byte(0x05), connectResp[0]) // version + require.Equal(t, byte(0x00), connectResp[1]) // success + + return conn, bufio.NewReader(conn) +} + +// startWebSocketUpstream starts a raw TCP server that accepts a single +// connection, answers the upgrade request with 101, then hands the +// connection to serve. The connection is closed when serve returns. +func startWebSocketUpstream(t *testing.T, serve func(conn net.Conn, br *bufio.Reader)) string { t.Helper() ln, err := net.Listen("tcp", "127.0.0.1:0") @@ -599,57 +659,53 @@ func startWebSocketEchoUpstream(t *testing.T) string { if _, err := http.ReadRequest(br); err != nil { return } - - resp := "HTTP/1.1 101 Switching Protocols\r\n" + - "Upgrade: websocket\r\n" + - "Connection: Upgrade\r\n\r\n" - if _, err := conn.Write([]byte(resp)); err != nil { + _, err = io.WriteString(conn, "HTTP/1.1 101 Switching Protocols\r\n"+ + "Upgrade: websocket\r\nConnection: Upgrade\r\n\r\n") + if err != nil { return } - - buf := make([]byte, 4096) - for { - n, err := br.Read(buf) - if err != nil { - return - } - if _, err := conn.Write(buf[:n]); err != nil { - return - } - } + serve(conn, br) }() return ln.Addr().String() } -// wsUpgradeAndEcho sends a WebSocket upgrade request for target over conn, -// verifies the 101 response, then checks that payload bytes echo back. -func wsUpgradeAndEcho(t *testing.T, conn net.Conn, br *bufio.Reader, target string) { +// startWebSocketEchoUpstream is startWebSocketUpstream with an echo loop. +func startWebSocketEchoUpstream(t *testing.T) string { + t.Helper() + return startWebSocketUpstream(t, func(conn net.Conn, br *bufio.Reader) { + _, _ = io.Copy(conn, br) + }) +} + +// wsUpgrade sends a WebSocket upgrade request for target over conn and +// verifies the 101 response, leaving br positioned at the relayed stream. +func wsUpgrade(t *testing.T, conn net.Conn, br *bufio.Reader, target string) { t.Helper() - upgradeReq := fmt.Sprintf("GET /ws HTTP/1.1\r\n"+ + _, err := fmt.Fprintf(conn, "GET /ws HTTP/1.1\r\n"+ "Host: %s\r\n"+ "Upgrade: websocket\r\n"+ "Connection: Upgrade\r\n"+ "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ "Sec-WebSocket-Version: 13\r\n\r\n", target) - _, err := conn.Write([]byte(upgradeReq)) require.NoError(t, err) require.NoError(t, conn.SetReadDeadline(time.Now().Add(5*time.Second))) - statusLine, err := br.ReadString('\n') + resp, err := http.ReadResponse(br, nil) require.NoError(t, err) - require.Contains(t, statusLine, "101") - for { - line, err := br.ReadString('\n') - require.NoError(t, err) - if line == "\r\n" { - break - } - } + require.Equal(t, http.StatusSwitchingProtocols, resp.StatusCode) +} - _, err = conn.Write([]byte("hello websocket")) +// wsUpgradeAndEcho upgrades via wsUpgrade, then checks that payload bytes +// echo back. +func wsUpgradeAndEcho(t *testing.T, conn net.Conn, br *bufio.Reader, target string) { + t.Helper() + + wsUpgrade(t, conn, br, target) + + _, err := conn.Write([]byte("hello websocket")) require.NoError(t, err) buf := make([]byte, 4096) @@ -726,6 +782,75 @@ func TestTunnel_SOCKS5_WebSocket(t *testing.T) { wsUpgradeAndEcho(t, conn, bufio.NewReader(conn), target) } +// wsEntryPaths are the ways a client reaches the WebSocket relay with a +// plaintext client connection. Each opens a connection ready for the +// upgrade request to target. The TLS-terminated paths (HTTPS listener, +// CONNECT MITM) are absent: they dial upstream with wss verified against +// system roots, which a test upstream cannot satisfy. +var wsEntryPaths = []struct { + name string + open func(t *testing.T, target string) (net.Conn, *bufio.Reader) +}{ + {"http listener", func(t *testing.T, _ string) (net.Conn, *bufio.Reader) { + _, httpAddr, _, _ := startProxy(t) + conn, err := net.DialTimeout("tcp", httpAddr, 5*time.Second) + require.NoError(t, err) + t.Cleanup(func() { _ = conn.Close() }) + return conn, bufio.NewReader(conn) + }}, + {"connect tunnel", func(t *testing.T, target string) (net.Conn, *bufio.Reader) { + _, tunnelAddr, _ := startTunnelProxy(t, nil) + return dialCONNECT(t, tunnelAddr, target) + }}, + {"socks5 tunnel", func(t *testing.T, target string) (net.Conn, *bufio.Reader) { + _, tunnelAddr, _ := startTunnelProxy(t, nil) + return dialSOCKS5(t, tunnelAddr, target) + }}, +} + +// The relay must close the client when the upstream hangs up; the +// pre-fix code only did so for bare *net.TCPConn clients, so tunnelled +// clients hung until they gave up. +func TestWebSocket_UpstreamClosePropagatesToClient(t *testing.T) { + for _, path := range wsEntryPaths { + t.Run(path.name, func(t *testing.T) { + // Upstream hangs up right after the handshake. + target := startWebSocketUpstream(t, func(net.Conn, *bufio.Reader) {}) + conn, br := path.open(t, target) + wsUpgrade(t, conn, br, target) + + require.NoError(t, conn.SetReadDeadline(time.Now().Add(5*time.Second))) + _, err := br.ReadByte() + require.ErrorIs(t, err, io.EOF) + }) + } +} + +// The relay must close the upstream when the client hangs up, or every +// abandoned client leaks an upstream socket. +func TestWebSocket_ClientClosePropagatesToUpstream(t *testing.T) { + for _, path := range wsEntryPaths { + t.Run(path.name, func(t *testing.T) { + upstreamRead := make(chan error, 1) + target := startWebSocketUpstream(t, func(conn net.Conn, br *bufio.Reader) { + _ = conn.SetReadDeadline(time.Now().Add(5 * time.Second)) + _, err := br.ReadByte() + upstreamRead <- err + }) + conn, br := path.open(t, target) + wsUpgrade(t, conn, br, target) + + require.NoError(t, conn.Close()) + select { + case err := <-upstreamRead: + require.ErrorIs(t, err, io.EOF) + case <-time.After(5 * time.Second): + t.Fatal("upstream did not observe the client close") + } + }) + } +} + // TestServeOneHTTPConn_HijackWaitsForHandler pins the contract that lets // hijacking handlers (WebSocket relays, CONNECT tunnels) survive their // callers' deferred conn.Close(): serveOneHTTPConn must not return until a diff --git a/internal/sshproxy/README.md b/internal/sshproxy/README.md new file mode 100644 index 0000000..08740af --- /dev/null +++ b/internal/sshproxy/README.md @@ -0,0 +1,55 @@ +# Iron SSH listener + +SSH runs as an optional listener inside the main `iron-proxy` process. Build and +test it with the normal repository commands; no separate executable or module +is needed. + +Configure a top-level `ssh:` block in the regular YAML file, or set +`IRON_SSH_CONFIG_JSON` to the equivalent JSON object for managed deployments. +SSH is disabled when configuration is absent. Pass `--ssh` to require a configured +listener at startup. Listener settings apply at startup. + +The SSH configuration contains `listen`, `host_key` (an existing Iron +secret source), `client_public_key` (registered by the trusted control plane), +`authorize_url`, and `token_env`. The environment variable named by `token_env` +is a proxy-only bearer token. Configuration and host keys must never be mounted +in the sandbox. Upstream dials honor the proxy-wide `proxy.upstream_deny_cidrs`. +Connections, authorization decisions, and session requests emit the standard +audit entries (JSON and OTEL) with `mode: ssh`. + +The authorizer receives `{route, public_key}` and an optional `destination` for +per-call targets. A client names these using SSH username +`ee:`; the proxy decodes this to route `ee` and passes +the URL to the authorizer. This is only a request: the authorizer must enforce +deployment-owned host/path policy and must never infer a user from this input. +It must derive the principal from +the proxy token and return a grant containing `id`, `principal`, `generation`, +`destination` (`tcp://host:port` or a full `wss://host/path`), `username`, +`host_key` (authorized-key format), `private_key` (Iron secret source), and +`expires_at` (RFC3339, at most 15 minutes away). It must deny ambiguous or +unauthorized routes with HTTP 401 or 403; other non-200 statuses are audited as +authorizer errors. Responses are limited to 1 MiB; errors never expose their +bodies or credential values. Redirects are not followed. + +The client must prove possession of the paired key before authorization or +any upstream dial. Exec and SFTP channels are supported; forwarding is blocked. +Global requests are refused locally, and environment/end-of-write requests are +discarded. Other channel types and requests are refused. Grants normally require a pinned SSH host key and verified WSS TLS. +An authorizer may explicitly return `tls_identity: true` with an empty +`host_key` for deployment-allowed WSS gateways. This authenticates the exact +destination using the TLS certificate and hostname, allowing the SSH key inside +that authenticated tunnel to rotate. It is rejected for plain TCP or combined +with a host pin. TLS verification, redirect rejection, IP guards, disposable +client keys, and per-request authorization remain mandatory. WSS implements +the wstunnel v11 default unmasked framing and routing handshake with a fixed remote target `127.0.0.1:22`. +The JWT is protocol metadata, not an authentication credential. + +The lease is renewed every five seconds. Expiry, authorization failure, or a +change to any grant field except expiry closes the active connection. Authorizer +failures fail closed. Both peers are probed with keepalives every 15 seconds, +and a peer that misses three intervals closes the connection. Reconnection never retries a remote command automatically. +A terminated channel does not guarantee detached remote descendants have exited. + +Run `go test -race ./internal/sshproxy`. No SSH functionality is enabled merely +by installing this binary; the embedding platform must supply the paired key, +authenticated authorizer, destination grants, and lifecycle wiring. diff --git a/internal/sshproxy/config.go b/internal/sshproxy/config.go new file mode 100644 index 0000000..4567e6e --- /dev/null +++ b/internal/sshproxy/config.go @@ -0,0 +1,143 @@ +// Package sshproxy implements an SSH credential boundary within iron-proxy. +package sshproxy + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "log/slog" + "net" + "net/http" + "os" + "time" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "github.com/ironsh/iron-proxy/internal/transform" + "github.com/ironsh/iron-proxy/internal/transform/secrets" + "golang.org/x/crypto/ssh" + "gopkg.in/yaml.v3" +) + +// Grant is delivered only over the authenticated proxy control channel. +type Grant struct { + ID string `json:"id" yaml:"id"` + Principal string `json:"principal" yaml:"principal"` + Destination string `json:"destination" yaml:"destination"` + Generation string `json:"generation" yaml:"generation"` + Username string `json:"username" yaml:"username"` + HostKey string `json:"host_key" yaml:"host_key"` + TLSIdentity bool `json:"tls_identity,omitempty" yaml:"tls_identity"` + PrivateKey yaml.Node `json:"-" yaml:"private_key"` + ExpiresAt time.Time `json:"expires_at" yaml:"expires_at"` +} + +// Authorization identifies a sandbox key and a configured route, never a user. +type Authorization struct { + Route string `json:"route"` + PublicKey string `json:"public_key"` + Destination string `json:"destination,omitempty"` +} + +// errAccessDenied marks policy rejections, as opposed to operational errors. +var errAccessDenied = errors.New("SSH access denied") + +// Authorizer is called again during active connections; errors revoke access. +type Authorizer func(context.Context, Authorization) (*Grant, error) + +// HTTPAuthorizer uses a proxy-only bearer token and does not follow redirects. +func HTTPAuthorizer(endpoint, token string) Authorizer { + client := &http.Client{Timeout: 5 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} + return func(ctx context.Context, a Authorization) (*Grant, error) { + body, err := json.Marshal(a) + if err != nil { + return nil, errors.New("authorization encoding failed") + } + req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body)) + if err != nil { + return nil, errors.New("authorization URL invalid") + } + req.Header.Set("Authorization", "Bearer "+token) + req.Header.Set("Content-Type", "application/json") + resp, err := client.Do(req) + if err != nil { + return nil, errors.New("authorization unavailable") + } + defer resp.Body.Close() + switch resp.StatusCode { + case http.StatusOK: + case http.StatusUnauthorized, http.StatusForbidden: + return nil, errAccessDenied + default: + // Outages are operational errors, not policy denials. + return nil, fmt.Errorf("authorization unavailable: status %d", resp.StatusCode) + } + // JSON is decoded as YAML so source nodes retain the existing resolver contract. + data, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) + if err != nil { + return nil, errors.New("authorization response unavailable") + } + var g Grant + if yaml.Unmarshal(data, &g) != nil { + return nil, errors.New("invalid authorization response") + } + return &g, nil + } +} + +func signer(ctx context.Context, node yaml.Node, logger *slog.Logger) (ssh.Signer, error) { + source, err := secrets.BuildSource(node, logger) + if err != nil { + return nil, errors.New("SSH credential source invalid") + } + value, err := source.Get(ctx) + if err != nil { + return nil, errors.New("SSH credential unavailable") + } + key, err := ssh.ParsePrivateKey([]byte(value)) + if err != nil { + return nil, errors.New("SSH credential invalid") + } + return key, nil +} + +// LoadFromNode prepares the optional SSH listener before other services start. +// An absent block disables SSH. The real key and bearer token stay in the proxy. +// Upstream dials use the proxy-wide guard and audit events the shared sinks. +func LoadFromNode(ctx context.Context, node yaml.Node, guard *dnsguard.Guard, audit transform.AuditFunc, logger *slog.Logger) (*Server, net.Listener, error) { + if node.Kind == 0 || node.Tag == "!!null" { + return nil, nil, nil + } + var cfg struct { + Listen string `yaml:"listen"` + HostKey yaml.Node `yaml:"host_key"` + ClientPublicKey string `yaml:"client_public_key"` + AuthorizeURL string `yaml:"authorize_url"` + TokenEnv string `yaml:"token_env"` + } + if node.Decode(&cfg) != nil || cfg.Listen == "" || cfg.AuthorizeURL == "" { + return nil, nil, errors.New("SSH listen address and authorization URL required") + } + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + host, err := ParseHostKey(ctx, cfg.HostKey, logger) + if err != nil { + return nil, nil, err + } + key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(cfg.ClientPublicKey)) + if err != nil { + return nil, nil, errors.New("sandbox SSH public key invalid") + } + token := os.Getenv(cfg.TokenEnv) + if token == "" { + return nil, nil, errors.New("SSH proxy token unavailable") + } + listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", cfg.Listen) + if err != nil { + return nil, nil, errors.New("SSH listener unavailable") + } + server := &Server{HostKey: host, ClientPublicKey: key, Authorize: HTTPAuthorizer(cfg.AuthorizeURL, token), Guard: guard, Audit: audit, Logger: logger} + return server, listener, nil +} diff --git a/internal/sshproxy/config_test.go b/internal/sshproxy/config_test.go new file mode 100644 index 0000000..92a3aa2 --- /dev/null +++ b/internal/sshproxy/config_test.go @@ -0,0 +1,58 @@ +package sshproxy + +import ( + "context" + "encoding/json" + "io" + "log/slog" + "testing" + "time" + + "github.com/stretchr/testify/require" + "golang.org/x/crypto/ssh" + "gopkg.in/yaml.v3" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "github.com/ironsh/iron-proxy/internal/transform" +) + +func noAudit(*transform.PipelineResult) {} + +func TestOptionalListenerLifecycle(t *testing.T) { + logger := slog.New(slog.NewJSONHandler(io.Discard, nil)) + server, listener, err := LoadFromNode(context.Background(), yaml.Node{}, &dnsguard.Guard{}, noAudit, logger) + require.NoError(t, err) + require.Nil(t, server) + require.Nil(t, listener) + + _, private := key(t) + client, _ := key(t) + t.Setenv("SSH_STARTUP_TEST_TOKEN", "synthetic-token") + data, err := json.Marshal(map[string]any{ + "listen": "127.0.0.1:0", "host_key": map[string]string{"type": "control_plane", "value": string(private)}, + "client_public_key": string(ssh.MarshalAuthorizedKey(client.PublicKey())), "authorize_url": "http://127.0.0.1/authorize", "token_env": "SSH_STARTUP_TEST_TOKEN", + }) + require.NoError(t, err) + var node yaml.Node + require.NoError(t, yaml.Unmarshal(data, &node)) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + server, listener, err = LoadFromNode(ctx, node, &dnsguard.Guard{}, noAudit, logger) + require.NoError(t, err) + defer listener.Close() + done := make(chan error, 1) + go func() { done <- server.Serve(ctx, listener) }() + cancel() + select { + case err := <-done: + require.NoError(t, err) + case <-time.After(time.Second): + t.Fatal("listener did not stop with the proxy context") + } + + t.Setenv("SSH_STARTUP_TEST_TOKEN", "") + server, listener, err = LoadFromNode(context.Background(), node, &dnsguard.Guard{}, noAudit, logger) + require.ErrorContains(t, err, "token unavailable") + require.Nil(t, server) + require.Nil(t, listener) +} diff --git a/internal/sshproxy/lifecycle_test.go b/internal/sshproxy/lifecycle_test.go new file mode 100644 index 0000000..88aba00 --- /dev/null +++ b/internal/sshproxy/lifecycle_test.go @@ -0,0 +1,123 @@ +package sshproxy + +import ( + "context" + "io" + "log/slog" + "net" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/require" + "golang.org/x/crypto/ssh" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "github.com/ironsh/iron-proxy/internal/transform" +) + +func startHandshake(t *testing.T, server *Server, clientKey ssh.Signer) (context.CancelFunc, <-chan struct{}) { + t.Helper() + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + client, err := net.Dial("tcp", listener.Addr().String()) + require.NoError(t, err) + downstream, err := listener.Accept() + require.NoError(t, err) + require.NoError(t, listener.Close()) + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(func() { cancel(); _ = client.Close(); _ = downstream.Close() }) + done := make(chan struct{}) + go func() { defer close(done); server.handle(ctx, downstream) }() + go func() { + // These fixtures intentionally fail or cancel before authentication completes. + conn, _, _, err := ssh.NewClientConn(client, "proxy", &ssh.ClientConfig{User: "ee", Auth: []ssh.AuthMethod{ssh.PublicKeys(clientKey)}, HostKeyCallback: ssh.FixedHostKey(server.HostKey.PublicKey())}) + if err == nil { + _ = conn.Close() + } + }() + return cancel, done +} + +func TestHandshakeDeadlineIncludesWSSNegotiation(t *testing.T) { + for _, stage := range []string{"TLS", "HTTP upgrade"} { + t.Run(stage, func(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + backendHost, private := key(t) + g := grant(t, "unused:22", backendHost, private) + reached := make(chan struct{}) + server := &Server{HostKey: proxyHost, ClientPublicKey: clientKey.PublicKey(), Authorize: func(context.Context, Authorization) (*Grant, error) { return g, nil }, Guard: &dnsguard.Guard{}, Audit: func(*transform.PipelineResult) {}, Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)), HandshakeTimeout: 500 * time.Millisecond} + if stage == "TLS" { + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + t.Cleanup(func() { _ = listener.Close() }) + go func() { + conn, err := listener.Accept() + if err != nil { + return + } + defer conn.Close() + close(reached) + _, _ = io.Copy(io.Discard, conn) + }() + g.Destination = "wss://" + listener.Addr().String() + "/events/events" + } else { + release := make(chan struct{}) + peer := httptest.NewTLSServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { close(reached); <-release })) + t.Cleanup(func() { close(release); peer.Close() }) + server.TLSRootCAs = peer.Client().Transport.(*http.Transport).TLSClientConfig.RootCAs + g.Destination = "wss" + strings.TrimPrefix(peer.URL, "https") + "/events/events" + } + _, done := startHandshake(t, server, clientKey) + select { + case <-reached: + case <-time.After(2 * time.Second): + t.Fatal("handshake never reached stalled peer") + } + select { + case <-done: + case <-time.After(2 * time.Second): + t.Fatal("WSS negotiation outlived the handshake deadline") + } + }) + } +} + +func TestCancellationClosesEstablishedUpstreamDuringSSHHandshake(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + backendHost, private := key(t) + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + t.Cleanup(func() { _ = listener.Close() }) + reached, closed := make(chan struct{}), make(chan struct{}) + go func() { + conn, err := listener.Accept() + if err != nil { + return + } + defer conn.Close() + close(reached) + _, _ = io.Copy(io.Discard, conn) + close(closed) + }() + g := grant(t, listener.Addr().String(), backendHost, private) + server := &Server{HostKey: proxyHost, ClientPublicKey: clientKey.PublicKey(), Authorize: func(context.Context, Authorization) (*Grant, error) { return g, nil }, Guard: &dnsguard.Guard{}, Audit: func(*transform.PipelineResult) {}, Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)), HandshakeTimeout: time.Minute} + cancel, done := startHandshake(t, server, clientKey) + select { + case <-reached: + case <-time.After(2 * time.Second): + t.Fatal("upstream was not dialed") + } + cancel() + for _, event := range []<-chan struct{}{closed, done} { + select { + case <-event: + case <-time.After(2 * time.Second): + t.Fatal("cancellation did not close the established upstream and handler") + } + } +} diff --git a/internal/sshproxy/policy.go b/internal/sshproxy/policy.go new file mode 100644 index 0000000..d47c42f --- /dev/null +++ b/internal/sshproxy/policy.go @@ -0,0 +1,42 @@ +package sshproxy + +import ( + "errors" + + "golang.org/x/crypto/ssh" +) + +var errCapabilityDenied = errors.New("SSH capability denied") + +type requestDecision int + +const ( + forwardRequest requestDecision = iota + // dropRequest refuses client bookkeeping without auditing it. + dropRequest + refuseRequest +) + +// sessionRequestPolicy is the default-deny allowlist for session channel +// requests. It returns the decision and the audited detail. +func sessionRequestPolicy(typ string, payload []byte) (requestDecision, string) { + switch typ { + case "exec": + var p struct{ Command string } + if ssh.Unmarshal(payload, &p) != nil { + return refuseRequest, "" + } + return forwardRequest, p.Command + case "subsystem", "signal": + var p struct{ Name string } + if ssh.Unmarshal(payload, &p) != nil || (typ == "subsystem" && p.Name != "sftp") { + return refuseRequest, p.Name + } + return forwardRequest, p.Name + case "env", "eow@openssh.com": + // OpenSSH sends these routinely; environment injection stays blocked. + return dropRequest, "" + default: + return refuseRequest, "" + } +} diff --git a/internal/sshproxy/policy_test.go b/internal/sshproxy/policy_test.go new file mode 100644 index 0000000..e69aaf6 --- /dev/null +++ b/internal/sshproxy/policy_test.go @@ -0,0 +1,37 @@ +package sshproxy + +import ( + "testing" + + "github.com/stretchr/testify/require" + "golang.org/x/crypto/ssh" +) + +func TestSessionRequestPolicy(t *testing.T) { + name := func(s string) []byte { return ssh.Marshal(struct{ Name string }{s}) } + cases := []struct { + typ string + payload []byte + decision requestDecision + detail string + }{ + {"exec", ssh.Marshal(struct{ Command string }{"ls -la"}), forwardRequest, "ls -la"}, + {"exec", append(ssh.Marshal(struct{ Command string }{"ls"}), 0), refuseRequest, ""}, + {"subsystem", name("sftp"), forwardRequest, "sftp"}, + {"subsystem", name("netconf"), refuseRequest, "netconf"}, + {"signal", name("TERM"), forwardRequest, "TERM"}, + {"env", nil, dropRequest, ""}, + {"eow@openssh.com", nil, dropRequest, ""}, + {"shell", nil, refuseRequest, ""}, + {"pty-req", nil, refuseRequest, ""}, + {"x11-req", nil, refuseRequest, ""}, + {"auth-agent-req@openssh.com", nil, refuseRequest, ""}, + } + for _, tc := range cases { + t.Run(tc.typ+"/"+tc.detail, func(t *testing.T) { + decision, detail := sessionRequestPolicy(tc.typ, tc.payload) + require.Equal(t, tc.decision, decision) + require.Equal(t, tc.detail, detail) + }) + } +} diff --git a/internal/sshproxy/server.go b/internal/sshproxy/server.go new file mode 100644 index 0000000..d78045f --- /dev/null +++ b/internal/sshproxy/server.go @@ -0,0 +1,451 @@ +package sshproxy + +import ( + "bytes" + "context" + "crypto/x509" + "encoding/base64" + "errors" + "fmt" + "io" + "log/slog" + "net" + "net/url" + "reflect" + "strings" + "sync" + "time" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "github.com/ironsh/iron-proxy/internal/transform" + "golang.org/x/crypto/ssh" + "gopkg.in/yaml.v3" +) + +// Server accepts only its paired sandbox's disposable public key. +type Server struct { + HostKey ssh.Signer + ClientPublicKey ssh.PublicKey + Authorize Authorizer + Guard *dnsguard.Guard + Audit transform.AuditFunc + TLSRootCAs *x509.CertPool + Logger *slog.Logger + RenewalInterval time.Duration + HandshakeTimeout time.Duration + // KeepaliveInterval paces liveness probes to both peers; 0 means 15s. + KeepaliveInterval time.Duration +} + +// session is one authorized upstream connection. +type session struct { + auth Authorization + grant *Grant + conn ssh.Conn + chans <-chan ssh.NewChannel + reqs <-chan *ssh.Request +} + +func (s *Server) Serve(parent context.Context, listener net.Listener) error { + if s.HostKey == nil || s.ClientPublicKey == nil || s.Authorize == nil || s.Guard == nil || s.Audit == nil || s.Logger == nil { + return errors.New("incomplete SSH configuration") + } + ctx, cancel := context.WithCancel(parent) + var wg sync.WaitGroup + defer wg.Wait() + defer cancel() + stop := context.AfterFunc(ctx, func() { _ = listener.Close() }) + defer stop() + for { + conn, err := listener.Accept() + if err != nil { + if ctx.Err() != nil { + return nil + } + return err + } + wg.Add(1) + go func() { defer wg.Done(); s.handle(ctx, conn) }() + } +} + +func (s *Server) handle(parent context.Context, conn net.Conn) { + ctx, cancel := context.WithCancel(parent) + defer cancel() + defer conn.Close() + stop := context.AfterFunc(ctx, func() { _ = conn.Close() }) + defer stop() + started := time.Now() + remote := conn.RemoteAddr().String() + timeout := s.HandshakeTimeout + if timeout == 0 { + timeout = 15 * time.Second + } + handshakeCtx, handshakeCancel := context.WithTimeout(ctx, timeout) + defer handshakeCancel() + handshakeDeadline, _ := handshakeCtx.Deadline() + if conn.SetDeadline(handshakeDeadline) != nil { + return + } + var sess *session + defer func() { + if sess != nil { + _ = sess.conn.Close() + } + }() + attempted := false + config := &ssh.ServerConfig{ + // Also runs for unsigned key queries, so it must only compare keys. + PublicKeyCallback: func(_ ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { + if !bytes.Equal(key.Marshal(), s.ClientPublicKey.Marshal()) { + return nil, errAccessDenied + } + return nil, nil + }, + // Runs only after the client proves possession of the paired key. + VerifiedPublicKeyCallback: func(meta ssh.ConnMetadata, key ssh.PublicKey, _ *ssh.Permissions, _ string) (*ssh.Permissions, error) { + attempted = true + attemptStarted := time.Now() + next, err := s.connect(handshakeCtx, meta.User(), key, handshakeDeadline) + s.audit(attemptStarted, remote, next, "auth", next.auth.Route, "ssh_authorization", err) + if err != nil { + return nil, err + } + sess = next + return nil, nil + }, + } + config.AddHostKey(s.HostKey) + downstream, chans, reqs, err := ssh.NewServerConn(conn, config) + if err != nil { + if !attempted { + s.audit(started, remote, nil, "auth", "", "ssh_authorization", fmt.Errorf("%w: %v", errAccessDenied, err)) + } + return + } + defer func() { _ = downstream.Close() }() // teardown; the connection is usually already closed + if conn.SetDeadline(time.Time{}) != nil { + return + } + handshakeCancel() + stopUpstream := context.AfterFunc(ctx, func() { _ = sess.conn.Close() }) + defer stopUpstream() + + var wg sync.WaitGroup + run := func(f func()) { + wg.Add(1) + go func() { defer wg.Done(); f() }() + } + // Wait's error only says why upstream closed; either way the session ends. + run(func() { _ = sess.conn.Wait(); cancel() }) + run(func() { s.renew(ctx, cancel, sess.auth, sess.grant) }) + run(func() { s.keepalive(ctx, cancel, downstream) }) + run(func() { s.keepalive(ctx, cancel, sess.conn) }) + // Global requests (forwarding, keepalives) are refused in both directions. + run(func() { ssh.DiscardRequests(reqs) }) + run(func() { ssh.DiscardRequests(sess.reqs) }) + run(func() { + for nc := range sess.chans { + _ = nc.Reject(ssh.Prohibited, "upstream channels are not permitted") // peer is being refused; a failed send changes nothing + } + }) + for nc := range chans { + if nc.ChannelType() != "session" { + s.audit(time.Now(), remote, sess, nc.ChannelType(), "", "ssh_policy", errCapabilityDenied) + _ = nc.Reject(ssh.Prohibited, "only session channels are permitted") // as above + continue + } + run(func() { s.bridge(nc, sess, remote) }) + } + cancel() + _ = sess.conn.Close() + wg.Wait() +} + +// bridge pairs one downstream session channel with a new upstream one. +func (s *Server) bridge(nc ssh.NewChannel, sess *session, remote string) { + up, upReqs, err := sess.conn.OpenChannel("session", nc.ExtraData()) + if err != nil { + _ = nc.Reject(ssh.ConnectionFailed, "upstream session unavailable") // refusal is best effort + return + } + down, downReqs, err := nc.Accept() + if err != nil { + // Keep upstream requests from stalling the connection until it closes. + go ssh.DiscardRequests(upReqs) + _ = up.Close() + return + } + // Upstream EOF is relayed once both streams drain, and the downstream + // channel closes only after exit-status requests are delivered. + var output sync.WaitGroup + output.Add(2) + go func() { defer output.Done(); _, _ = io.Copy(down, up) }() + go func() { defer output.Done(); _, _ = io.Copy(down.Stderr(), up.Stderr()) }() + requests := make(chan struct{}) + go func() { + defer close(requests) + for req := range upReqs { + ok, _ := down.SendRequest(req.Type, req.WantReply, req.Payload) // a send failure means the client is gone; report false + _ = req.Reply(ok, nil) // upstream channel may already be closed + } + }() + closed := make(chan struct{}) + go func() { + defer close(closed) + output.Wait() + _ = down.CloseWrite() // fails only if the channel is already closed + <-requests + _ = down.Close() + }() + input := make(chan struct{}) + go func() { defer close(input); _, _ = io.Copy(up, down); _ = up.CloseWrite() }() + + for req := range downReqs { + started := time.Now() + decision, detail := sessionRequestPolicy(req.Type, req.Payload) + if decision != forwardRequest { + if decision == refuseRequest { + s.audit(started, remote, sess, req.Type, detail, "ssh_policy", errCapabilityDenied) + } + _ = req.Reply(false, nil) // client may already be gone + continue + } + ok, err := up.SendRequest(req.Type, req.WantReply, req.Payload) + s.audit(started, remote, sess, req.Type, detail, "ssh_policy", err) + _ = req.Reply(ok, nil) // client may already be gone + } + _ = up.Close() + <-closed + <-input +} + +// connect authorizes the route and logs in upstream with the granted key. +// The returned session is never nil so failures can still be audited. +func (s *Server) connect(ctx context.Context, user string, key ssh.PublicKey, deadline time.Time) (*session, error) { + sess := &session{} + route, destination, err := parseRoute(user) + if err != nil { + return sess, fmt.Errorf("%w: %v", errAccessDenied, err) + } + sess.auth = Authorization{Route: route, Destination: destination, PublicKey: string(bytes.TrimSpace(ssh.MarshalAuthorizedKey(key)))} + grant, err := s.Authorize(ctx, sess.auth) + if err != nil { + return sess, err + } + if err = validateGrant(grant); err != nil { + return sess, err + } + sess.grant = grant + credential, err := signer(ctx, grant.PrivateKey, s.Logger) + if err != nil { + return sess, err + } + var expected ssh.PublicKey + var algorithms []string + if !grant.TLSIdentity { + expected, _, _, _, err = ssh.ParseAuthorizedKey([]byte(grant.HostKey)) + if err != nil { + return sess, errors.New("invalid upstream host identity") + } + algorithms = []string{expected.Type()} + if expected.Type() == ssh.KeyAlgoRSA { + algorithms = []string{ssh.KeyAlgoRSASHA512, ssh.KeyAlgoRSASHA256} + } + } + netConn, err := transport(ctx, grant.Destination, s.Guard, s.TLSRootCAs) + if err != nil { + return sess, fmt.Errorf("SSH upstream unavailable: %w", err) + } + if err = netConn.SetDeadline(deadline); err != nil { + _ = netConn.Close() + return sess, err + } + config := &ssh.ClientConfig{ + User: grant.Username, + Auth: []ssh.AuthMethod{ssh.PublicKeys(credential)}, + HostKeyAlgorithms: algorithms, + HostKeyCallback: func(_ string, _ net.Addr, actual ssh.PublicKey) error { + // TLS-identity grants are WSS-only (validated before dialing). The + // transport authenticates the exact URL's server certificate with no + // redirects or insecure TLS option; SSH remains inside that tunnel. + if expected != nil && !bytes.Equal(expected.Marshal(), actual.Marshal()) { + return errors.New("SSH host identity mismatch") + } + return nil + }, + } + // NewClientConn ignores ctx; shutdown or revocation must still interrupt it. + stop := context.AfterFunc(ctx, func() { _ = netConn.Close() }) + conn, chans, reqs, err := ssh.NewClientConn(netConn, grant.Destination, config) + if !stop() { + err = errors.Join(err, ctx.Err()) + } + if err != nil { + _ = netConn.Close() + return sess, fmt.Errorf("SSH upstream login failed: %w", err) + } + if err = netConn.SetDeadline(time.Time{}); err != nil { + _ = conn.Close() + return sess, err + } + sess.conn, sess.chans, sess.reqs = conn, chans, reqs + return sess, nil +} + +// audit emits one SSH event through the proxy's shared audit sinks. +func (s *Server) audit(started time.Time, remote string, sess *session, method, path, name string, err error) { + trace := transform.TransformTrace{Name: name, Duration: time.Since(started), Annotations: map[string]any{}} + result := &transform.PipelineResult{ + Method: method, + Path: path, + RemoteAddr: remote, + Mode: transform.ModeSSH, + StartedAt: started, + Duration: time.Since(started), + } + if sess != nil { + if sess.auth.Route != "" { + trace.Annotations["route"] = sess.auth.Route + } + if g := sess.grant; g != nil { + trace.Annotations["principal"] = g.Principal + trace.Annotations["grant_id"] = g.ID + if u, err := url.Parse(g.Destination); err == nil { + result.Host = u.Host + } + } + } + switch { + case errors.Is(err, errAccessDenied) || errors.Is(err, errCapabilityDenied): + result.Action, trace.Action = transform.ActionReject, transform.ActionReject + trace.Annotations["reason"] = err.Error() + case err != nil: + result.Err, trace.Err = err, err + } + result.RequestTransforms = []transform.TransformTrace{trace} + s.Audit(result) +} + +func validateGrant(g *Grant) error { + if g == nil || g.ID == "" || g.Principal == "" || g.Generation == "" || g.Username == "" || g.Destination == "" || !g.ExpiresAt.After(time.Now()) || time.Until(g.ExpiresAt) > 15*time.Minute { + return errors.New("invalid SSH grant") + } + if g.TLSIdentity { + u, err := url.Parse(g.Destination) + if err != nil || u.Scheme != "wss" || g.HostKey != "" { + return errors.New("invalid TLS identity grant") + } + } else if g.HostKey == "" { + return errors.New("SSH host key required") + } + return nil +} + +// A client may name a destination, but only the authorizer can approve it. +// The encoded value carries no credentials and is bounded before decoding. +func parseRoute(value string) (string, string, error) { + if strings.HasPrefix(value, "ee:") { + if len(value) > 4096 { + return "", "", errors.New("invalid SSH target") + } + data, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(value, "ee:")) + if err != nil || len(data) == 0 || len(data) > 2048 { + return "", "", errors.New("invalid SSH target") + } + return "ee", string(data), nil + } + if len(value) == 0 || len(value) > 128 { + return "", "", errors.New("invalid SSH route") + } + return value, "", nil +} + +// keepalive ends the session when conn stops answering global requests, so a +// silently dropped peer cannot hold the other side open. Any reply counts. +func (s *Server) keepalive(ctx context.Context, cancel context.CancelFunc, conn ssh.Conn) { + interval := s.KeepaliveInterval + if interval == 0 { + interval = 15 * time.Second + } + ticker := time.NewTicker(interval) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + } + reply := make(chan error, 1) + go func() { + _, _, err := conn.SendRequest("keepalive@openssh.com", true, nil) + reply <- err + }() + // Like OpenSSH's default ServerAliveCountMax, allow three intervals. + timer := time.NewTimer(3 * interval) + select { + case err := <-reply: + timer.Stop() + if err != nil { + cancel() + return + } + case <-timer.C: + s.Logger.Warn("SSH peer stopped responding", slog.String("remote_addr", conn.RemoteAddr().String())) + cancel() + return + case <-ctx.Done(): + timer.Stop() + return + } + } +} + +func (s *Server) renew(ctx context.Context, cancel context.CancelFunc, a Authorization, g *Grant) { + interval := s.RenewalInterval + if interval == 0 { + interval = 5 * time.Second + } + ticker := time.NewTicker(interval) + defer ticker.Stop() + expiry := time.AfterFunc(time.Until(g.ExpiresAt), cancel) + defer expiry.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + next, err := s.Authorize(ctx, a) + if err != nil || validateGrant(next) != nil || !sameGrant(g, next) { + if ctx.Err() == nil { + s.Logger.Warn("SSH lease revoked", slog.String("grant_id", g.ID), slog.String("principal", g.Principal)) + } + cancel() + return + } + expiry.Reset(time.Until(next.ExpiresAt)) + } + } +} +func sameGrant(a, b *Grant) bool { + if a == nil || b == nil { + return false + } + x, y := *a, *b + x.ExpiresAt = time.Time{} + y.ExpiresAt = time.Time{} + x.PrivateKey, y.PrivateKey = yaml.Node{}, yaml.Node{} + if !reflect.DeepEqual(x, y) { + return false + } + var sourceA, sourceB any + if a.PrivateKey.Decode(&sourceA) != nil || b.PrivateKey.Decode(&sourceB) != nil { + return false + } + return reflect.DeepEqual(sourceA, sourceB) +} + +// ParseHostKey resolves a proxy-only credential source. +func ParseHostKey(ctx context.Context, source yaml.Node, logger *slog.Logger) (ssh.Signer, error) { + return signer(ctx, source, logger) +} diff --git a/internal/sshproxy/server_test.go b/internal/sshproxy/server_test.go new file mode 100644 index 0000000..7734544 --- /dev/null +++ b/internal/sshproxy/server_test.go @@ -0,0 +1,765 @@ +package sshproxy + +import ( + "bytes" + "context" + "crypto/ecdsa" + "crypto/ed25519" + "crypto/elliptic" + "crypto/rand" + "crypto/x509" + "encoding/base64" + "encoding/json" + "encoding/pem" + "errors" + "io" + "log/slog" + "net" + "net/http" + "net/http/httptest" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/gobwas/ws" + "github.com/pkg/sftp" + "github.com/stretchr/testify/require" + "golang.org/x/crypto/ssh" + "gopkg.in/yaml.v3" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "github.com/ironsh/iron-proxy/internal/transform" +) + +func key(t *testing.T) (ssh.Signer, []byte) { + t.Helper() + _, private, err := ed25519.GenerateKey(rand.Reader) + require.NoError(t, err) + signer, err := ssh.NewSignerFromKey(private) + require.NoError(t, err) + block, err := ssh.MarshalPrivateKey(private, "") + require.NoError(t, err) + return signer, pem.EncodeToMemory(block) +} + +// testBackend is a real SSH server that accepts only userKey as user "dev". +// exec replies "ok:" with exit status 7; "hold" blocks on stdin, +// "eof-then-hold" sends output and EOF then blocks, and "stream-output" +// writes 12 MiB. The sftp subsystem serves a temp directory. +func testBackend(t *testing.T, host, userKey ssh.Signer) string { + t.Helper() + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, listener.Close()) }) + root := t.TempDir() + alternatePrivate, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + require.NoError(t, err) + alternateHost, err := ssh.NewSignerFromKey(alternatePrivate) + require.NoError(t, err) + go func() { + for { + conn, err := listener.Accept() + if err != nil { + return + } + go func() { + defer conn.Close() + cfg := &ssh.ServerConfig{PublicKeyCallback: func(meta ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { + if meta.User() != "dev" || !bytes.Equal(key.Marshal(), userKey.PublicKey().Marshal()) { + return nil, errors.New("wrong upstream identity") + } + return nil, nil + }} + // A second host key ensures the proxy negotiates the pinned one. + cfg.AddHostKey(alternateHost) + cfg.AddHostKey(host) + server, channels, requests, err := ssh.NewServerConn(conn, cfg) + if err != nil { + return // some tests provoke handshake failures on purpose + } + defer func() { _ = server.Close() }() + go ssh.DiscardRequests(requests) + for channel := range channels { + if channel.ChannelType() != "session" { + _ = channel.Reject(ssh.UnknownChannelType, "session required") + continue + } + stream, reqs, err := channel.Accept() + if err != nil { + return + } + go serveBackendSession(stream, reqs, root) + } + }() + } + }() + return listener.Addr().String() +} + +// Errors are ignored below: this is the test peer, and failures surface as +// assertion failures in the client. +func serveBackendSession(stream ssh.Channel, reqs <-chan *ssh.Request, root string) { + defer func() { _ = stream.Close() }() + for req := range reqs { + switch req.Type { + case "exec": + var cmd struct{ Command string } + if ssh.Unmarshal(req.Payload, &cmd) != nil || req.Reply(true, nil) != nil { + return + } + if cmd.Command == "hold" { + _, _ = io.Copy(io.Discard, stream) + return + } + if cmd.Command == "eof-then-hold" { + _, _ = stream.Write([]byte("partial")) + _ = stream.CloseWrite() + _, _ = io.Copy(io.Discard, stream) + return + } + output := []byte("ok:" + cmd.Command) + if cmd.Command == "stream-output" { + output = bytes.Repeat([]byte("output"), 1<<21) + } + if _, err := stream.Write(output); err != nil { + return + } + _, _ = stream.SendRequest("exit-status", false, ssh.Marshal(struct{ Status uint32 }{7})) + return + case "subsystem": + var sub struct{ Name string } + if ssh.Unmarshal(req.Payload, &sub) != nil || sub.Name != "sftp" { + _ = req.Reply(false, nil) + return + } + if req.Reply(true, nil) != nil { + return + } + server, err := sftp.NewServer(stream, sftp.WithServerWorkingDirectory(root)) + if err != nil { + return + } + _ = server.Serve() + _ = server.Close() + return + default: + _ = req.Reply(false, nil) + } + } +} + +func grant(t *testing.T, address string, host ssh.Signer, private []byte) *Grant { + t.Helper() + var source yaml.Node + require.NoError(t, yaml.Unmarshal([]byte("type: control_plane\nvalue: "+string(mustJSON(t, string(private)))), &source)) + return &Grant{ID: "grant-one", Principal: "user-a", Generation: "one", Destination: "tcp://" + address, Username: "dev", HostKey: strings.TrimSpace(string(ssh.MarshalAuthorizedKey(host.PublicKey()))), PrivateKey: source, ExpiresAt: time.Now().Add(time.Minute)} +} + +func mustJSON(t *testing.T, v any) []byte { + t.Helper() + data, err := json.Marshal(v) + require.NoError(t, err) + return data +} + +type auditLog struct { + mu sync.Mutex + events []*transform.PipelineResult +} + +func (l *auditLog) record(r *transform.PipelineResult) { + l.mu.Lock() + defer l.mu.Unlock() + l.events = append(l.events, r) +} + +func (l *auditLog) all() []*transform.PipelineResult { + l.mu.Lock() + defer l.mu.Unlock() + return append([]*transform.PipelineResult(nil), l.events...) +} + +// recordAuth returns g for every request and records what was asked. +type recordAuth struct { + mu sync.Mutex + g *Grant + calls []Authorization +} + +func (r *recordAuth) authorize(_ context.Context, a Authorization) (*Grant, error) { + r.mu.Lock() + defer r.mu.Unlock() + r.calls = append(r.calls, a) + return r.g, nil +} + +func (r *recordAuth) requests() []Authorization { + r.mu.Lock() + defer r.mu.Unlock() + return append([]Authorization(nil), r.calls...) +} + +// serveProxy runs server on a loopback listener. Unset Guard, Audit, and +// Logger default to an empty guard (the backends are on loopback), a +// recording audit sink, and a discarding logger. +func serveProxy(t *testing.T, server *Server) (string, *auditLog) { + t.Helper() + audits := &auditLog{} + if server.Guard == nil { + server.Guard = &dnsguard.Guard{} + } + if server.Audit == nil { + server.Audit = audits.record + } + if server.Logger == nil { + server.Logger = slog.New(slog.NewJSONHandler(io.Discard, nil)) + } + if server.RenewalInterval == 0 { + server.RenewalInterval = 20 * time.Millisecond + } + ctx, cancel := context.WithCancel(context.Background()) + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + done := make(chan error, 1) + go func() { done <- server.Serve(ctx, listener) }() + t.Cleanup(func() { cancel(); require.NoError(t, <-done) }) + return listener.Addr().String(), audits +} + +func testProxy(t *testing.T, clientKey, hostKey ssh.Signer, auth Authorizer, roots *x509.CertPool) (string, *auditLog) { + t.Helper() + return serveProxy(t, &Server{HostKey: hostKey, ClientPublicKey: clientKey.PublicKey(), Authorize: auth, TLSRootCAs: roots}) +} + +func connect(address string, key, host ssh.Signer) (*ssh.Client, error) { + return ssh.Dial("tcp", address, &ssh.ClientConfig{User: "ee", Auth: []ssh.AuthMethod{ssh.PublicKeys(key)}, HostKeyCallback: ssh.FixedHostKey(host.PublicKey()), Timeout: time.Second}) +} + +// backendFixture is a backend plus a grant that reaches it. +func backendFixture(t *testing.T) *Grant { + t.Helper() + backendHost, _ := key(t) + upstreamKey, private := key(t) + return grant(t, testBackend(t, backendHost, upstreamKey), backendHost, private) +} + +func TestExecSFTPAndKeyIsolation(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + backendHost, _ := key(t) + realKey, private := key(t) + target := testBackend(t, backendHost, realKey) + auth := &recordAuth{g: grant(t, target, backendHost, private)} + address, _ := testProxy(t, clientKey, proxyHost, auth.authorize, nil) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + output, err := session.Output("test-command") + var exit *ssh.ExitError + require.ErrorAs(t, err, &exit) + require.Equal(t, 7, exit.ExitStatus()) + require.Equal(t, "ok:test-command", string(output)) + files, err := sftp.NewClient(client) + require.NoError(t, err) + defer func() { _ = files.Close() }() + remote, err := files.Create("fixture.txt") + require.NoError(t, err) + _, err = remote.Write([]byte("edited")) + require.NoError(t, err) + require.NoError(t, remote.Close()) + remote, err = files.Open("fixture.txt") + require.NoError(t, err) + data, err := io.ReadAll(remote) + require.NoError(t, err) + require.Equal(t, "edited", string(data)) + require.NoError(t, remote.Close()) + require.Len(t, auth.requests(), 1) + require.Equal(t, "ee", auth.requests()[0].Route) + + wrong, _ := key(t) + _, err = connect(address, wrong, proxyHost) + require.Error(t, err) + _, err = connect(address, clientKey, backendHost) + require.Error(t, err, "client must pin the proxy's host key, not the backend's") +} + +func TestRefusalsAreAuditedAndKeepConnection(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + g := backendFixture(t) + target := strings.TrimPrefix(g.Destination, "tcp://") + auth := &recordAuth{g: g} + address, audits := testProxy(t, clientKey, proxyHost, auth.authorize, nil) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + + refused, err := client.NewSession() + require.NoError(t, err) + require.Error(t, refused.RequestPty("xterm", 24, 80, nil)) + require.NoError(t, refused.Close()) + _, err = client.Dial("tcp", "example.com:80") + require.Error(t, err) + + session, err := client.NewSession() + require.NoError(t, err) + out, err := session.Output("git-upload-pack 'repo'") + var exit *ssh.ExitError + require.ErrorAs(t, err, &exit) + require.Equal(t, "ok:git-upload-pack 'repo'", string(out)) + + events := audits.all() + methods := make([]string, len(events)) + for i, e := range events { + methods[i] = e.Method + require.Equal(t, transform.ModeSSH, e.Mode) + require.Equal(t, target, e.Host) + require.Len(t, e.RequestTransforms, 1) + require.Equal(t, "user-a", e.RequestTransforms[0].Annotations["principal"]) + require.Equal(t, "grant-one", e.RequestTransforms[0].Annotations["grant_id"]) + } + require.Equal(t, []string{"auth", "pty-req", "direct-tcpip", "exec"}, methods) + require.Equal(t, transform.ActionContinue, events[0].Action) + require.Equal(t, "ssh_authorization", events[0].RequestTransforms[0].Name) + for _, e := range events[1:3] { + require.Equal(t, transform.ActionReject, e.Action) + require.Equal(t, "ssh_policy", e.RequestTransforms[0].Name) + require.Equal(t, transform.ActionReject, e.RequestTransforms[0].Action) + } + require.Equal(t, transform.ActionContinue, events[3].Action) + require.Equal(t, "git-upload-pack 'repo'", events[3].Path) +} + +// publicOnlySigner offers a public key without holding its private key, as +// anyone who has seen the sandbox's public key could. +type publicOnlySigner struct{ key ssh.PublicKey } + +func (s publicOnlySigner) PublicKey() ssh.PublicKey { return s.key } +func (publicOnlySigner) Sign(io.Reader, []byte) (*ssh.Signature, error) { + return nil, errors.New("no private key") +} + +func TestAuthorizesOnlyVerifiedSandboxKey(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + other, _ := key(t) + cases := []struct { + name string + signer ssh.Signer + }{ + {"unpaired key", other}, + {"paired public key without proof", publicOnlySigner{clientKey.PublicKey()}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + auth := &recordAuth{g: backendFixture(t)} + address, audits := testProxy(t, clientKey, proxyHost, auth.authorize, nil) + _, err := connect(address, tc.signer, proxyHost) + require.Error(t, err) + + require.Eventually(t, func() bool { return len(audits.all()) == 1 }, 5*time.Second, 10*time.Millisecond) + event := audits.all()[0] + require.Equal(t, "auth", event.Method) + require.Equal(t, transform.ActionReject, event.Action) + require.Equal(t, "ssh_authorization", event.RequestTransforms[0].Name) + require.Empty(t, auth.requests()) + }) + } +} + +func TestUpstreamDialHonorsProxyDenyCIDRs(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + guard, err := dnsguard.New(dnsguard.DefaultDenyCIDRs) + require.NoError(t, err) + auth := &recordAuth{g: backendFixture(t)} + address, audits := serveProxy(t, &Server{HostKey: proxyHost, ClientPublicKey: clientKey.PublicKey(), Authorize: auth.authorize, Guard: guard}) + _, err = connect(address, clientKey, proxyHost) + require.Error(t, err) + + events := audits.all() + require.Len(t, events, 1) + require.Equal(t, "auth", events[0].Method) + require.ErrorContains(t, events[0].Err, "upstream_deny_cidrs") + require.Len(t, auth.requests(), 1) +} + +func TestWrongUpstreamHostKeyIsRejected(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + backendHost, _ := key(t) + realKey, private := key(t) + wrongHost, _ := key(t) + g := grant(t, testBackend(t, backendHost, realKey), wrongHost, private) + address, audits := testProxy(t, clientKey, proxyHost, func(context.Context, Authorization) (*Grant, error) { return g, nil }, nil) + _, err := connect(address, clientKey, proxyHost) + require.Error(t, err) + require.ErrorContains(t, audits.all()[0].Err, "SSH host identity mismatch") +} + +func TestRevocationClosesActiveConnection(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + g := backendFixture(t) + var denied atomic.Bool + address, _ := testProxy(t, clientKey, proxyHost, func(context.Context, Authorization) (*Grant, error) { + if denied.Load() { + return nil, errAccessDenied + } + return g, nil + }, nil) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + require.NoError(t, session.Start("hold")) + denied.Store(true) + done := make(chan error, 1) + go func() { done <- session.Wait() }() + select { + case err := <-done: + require.Error(t, err) + case <-time.After(2 * time.Second): + t.Fatal("revoked connection stayed open") + } +} + +func TestWstunnelV11Handshake(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + backendHost, _ := key(t) + realKey, private := key(t) + target := testBackend(t, backendHost, realKey) + wsServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/events/events" || !validTunnelProtocols(r.Header.Get("Sec-WebSocket-Protocol")) { + http.NotFound(w, r) + return + } + upgrader := ws.HTTPUpgrader{Protocol: func(value string) bool { return value == "v1" }} + conn, buffered, _, err := upgrader.Upgrade(r, w) + if err != nil { + return + } + stream := &websocketConn{Conn: conn, reader: buffered} + defer func() { _ = stream.Close() }() + upstream, err := net.Dial("tcp", target) + if err != nil { + return + } + defer upstream.Close() + go func() { _, _ = io.Copy(upstream, stream); _ = upstream.Close() }() + _, _ = io.Copy(stream, upstream) + })) + defer wsServer.Close() + roots := x509.NewCertPool() + roots.AddCert(wsServer.Certificate()) + g := grant(t, target, backendHost, private) + g.Destination = "wss" + strings.TrimPrefix(wsServer.URL, "https") + "/events/events" + address, _ := testProxy(t, clientKey, proxyHost, func(context.Context, Authorization) (*Grant, error) { return g, nil }, roots) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + output, err := session.Output("websocket") + var exit *ssh.ExitError + require.ErrorAs(t, err, &exit) + require.Equal(t, 7, exit.ExitStatus()) + require.Equal(t, "ok:websocket", string(output)) + + // Dynamic destinations use the WSS gateway's verified certificate instead + // of a per-pod SSH pin. The authorizer still approves each exact target. + tlsGrant := *g + tlsGrant.HostKey = "" + tlsGrant.TLSIdentity = true + route := "ee:" + base64.RawURLEncoding.EncodeToString([]byte(g.Destination)) + for _, test := range []struct { + name string + roots *x509.CertPool + allowed bool + }{ + {"verified TLS", roots, true}, {"untrusted TLS", x509.NewCertPool(), false}, + } { + t.Run(test.name, func(t *testing.T) { + auth := &recordAuth{g: &tlsGrant} + address, _ := testProxy(t, clientKey, proxyHost, auth.authorize, test.roots) + client, err := ssh.Dial("tcp", address, &ssh.ClientConfig{User: route, Auth: []ssh.AuthMethod{ssh.PublicKeys(clientKey)}, HostKeyCallback: ssh.FixedHostKey(proxyHost.PublicKey()), Timeout: time.Second}) + require.Len(t, auth.requests(), 1) + require.Equal(t, Authorization{Route: "ee", Destination: g.Destination, PublicKey: strings.TrimSpace(string(ssh.MarshalAuthorizedKey(clientKey.PublicKey())))}, auth.requests()[0]) + if !test.allowed { + require.Error(t, err) + return + } + require.NoError(t, err) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + output, err := session.Output("dynamic") + var exit *ssh.ExitError + require.ErrorAs(t, err, &exit) + require.Equal(t, 7, exit.ExitStatus()) + require.Equal(t, "ok:dynamic", string(output)) + }) + } +} + +// validTunnelProtocols checks the wstunnel v11 routing token targets 127.0.0.1:22. +func validTunnelProtocols(header string) bool { + protocols := strings.Split(header, ",") + if len(protocols) != 2 { + return false + } + parts := strings.Split(strings.TrimPrefix(strings.TrimSpace(protocols[1]), "authorization.bearer."), ".") + if len(parts) != 3 { + return false + } + claims, err := base64.RawURLEncoding.DecodeString(parts[1]) + if err != nil { + return false + } + var decoded map[string]any + return json.Unmarshal(claims, &decoded) == nil && decoded["r"] == "127.0.0.1" && decoded["rp"] == float64(22) +} + +func TestParseRouteRejectsMalformedTargets(t *testing.T) { + for _, value := range []string{"", "ee:", "ee:not*base64", "ee:" + strings.Repeat("a", 4096), strings.Repeat("a", 129)} { + _, _, err := parseRoute(value) + require.Error(t, err, value) + } + route, destination, err := parseRoute("ee:" + base64.RawURLEncoding.EncodeToString([]byte("wss://example.com/x"))) + require.NoError(t, err) + require.Equal(t, "ee", route) + require.Equal(t, "wss://example.com/x", destination) +} + +func TestValidateGrant(t *testing.T) { + _, private := key(t) + host, _ := key(t) + cases := []struct { + name string + mutate func(*Grant) + valid bool + }{ + {"pinned TCP", func(*Grant) {}, true}, + {"expired", func(g *Grant) { g.ExpiresAt = time.Now().Add(-time.Second) }, false}, + {"lease too long", func(g *Grant) { g.ExpiresAt = time.Now().Add(16 * time.Minute) }, false}, + {"missing host key", func(g *Grant) { g.HostKey = "" }, false}, + {"TLS identity over TCP", func(g *Grant) { g.TLSIdentity, g.HostKey = true, "" }, false}, + {"TLS identity over WSS", func(g *Grant) { g.TLSIdentity, g.HostKey, g.Destination = true, "", "wss://example.com/events" }, true}, + {"TLS identity with host pin", func(g *Grant) { g.TLSIdentity, g.Destination = true, "wss://example.com/events" }, false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + g := grant(t, "example.com:22", host, private) + tc.mutate(g) + if tc.valid { + require.NoError(t, validateGrant(g)) + } else { + require.Error(t, validateGrant(g)) + } + }) + } +} + +func TestSameGrant(t *testing.T) { + decode := func(expiry, generation, credential string) *Grant { + t.Helper() + var g Grant + require.NoError(t, yaml.Unmarshal([]byte(`{"id":"grant","generation":"`+generation+`","expires_at":"`+expiry+`","private_key":{"type":"control_plane","value":"`+credential+`"}}`), &g)) + return &g + } + a := decode("2099-01-01T00:00:00.123Z", "one", "same-key") + b := decode("2099-01-01T00:00:00.123456Z", "one", "same-key") + require.NotEqual(t, a.PrivateKey.Column, b.PrivateKey.Column, "fixture must shift YAML source positions") + require.True(t, sameGrant(a, b), "expiry formatting must not revoke an unchanged credential") + require.NoError(t, yaml.Unmarshal([]byte("value: 'same-key' # harmless comment\ntype: control_plane\n"), &b.PrivateKey)) + require.True(t, sameGrant(a, b), "mapping order, style and comments do not change the credential") + require.False(t, sameGrant(a, decode("2099-01-01T00:00:00Z", "one", "different-key")), "credential changes must revoke") + require.False(t, sameGrant(a, decode("2099-01-01T00:00:00Z", "two", "same-key")), "generation changes must revoke") + b.PrivateKey = yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: "invalid"} + require.False(t, sameGrant(a, b), "invalid sources must fail closed") +} + +func TestLeaseExpiryInterruptsBlockedRenewal(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + server := &Server{RenewalInterval: time.Millisecond, Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)), Authorize: func(ctx context.Context, _ Authorization) (*Grant, error) { <-ctx.Done(); return nil, ctx.Err() }} + done := make(chan struct{}) + go func() { + defer close(done) + server.renew(ctx, cancel, Authorization{}, &Grant{ExpiresAt: time.Now().Add(30 * time.Millisecond)}) + }() + select { + case <-done: + require.ErrorIs(t, ctx.Err(), context.Canceled) + case <-time.After(time.Second): + t.Fatal("blocked renewal outlived lease") + } +} + +func TestHTTPAuthorizationUsesProxyTokenAndDoesNotLeakErrors(t *testing.T) { + cases := []struct { + status int + denied bool + }{ + {http.StatusUnauthorized, true}, + {http.StatusForbidden, true}, + {http.StatusServiceUnavailable, false}, + {http.StatusNotFound, false}, + } + for _, tc := range cases { + t.Run(http.StatusText(tc.status), func(t *testing.T) { + var gotAuth atomic.Value + controller := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gotAuth.Store(r.Header.Get("Authorization")) + http.Error(w, "test-upstream-secret", tc.status) + })) + defer controller.Close() + _, err := HTTPAuthorizer(controller.URL, "test-only-token")(context.Background(), Authorization{Route: "ee"}) + require.Error(t, err) + require.Equal(t, tc.denied, errors.Is(err, errAccessDenied)) + require.NotContains(t, err.Error(), "test-upstream-secret") + require.Equal(t, "Bearer test-only-token", gotAuth.Load()) + }) + } +} + +func TestUpstreamEOFReachesClientBeforeClose(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + g := backendFixture(t) + address, _ := testProxy(t, clientKey, proxyHost, func(context.Context, Authorization) (*Grant, error) { return g, nil }, nil) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + defer func() { _ = session.Close() }() + stdout, err := session.StdoutPipe() + require.NoError(t, err) + // Hold stdin open so the backend keeps the channel open after its EOF. + stdin, err := session.StdinPipe() + require.NoError(t, err) + defer func() { _ = stdin.Close() }() + require.NoError(t, session.Start("eof-then-hold")) + read := make(chan []byte, 1) + go func() { + data, _ := io.ReadAll(stdout) // the assertion below covers the content + read <- data + }() + select { + case data := <-read: + require.Equal(t, "partial", string(data)) + case <-time.After(2 * time.Second): + t.Fatal("upstream EOF was not relayed while the channel stayed open") + } +} + +// deafConn stops delivering inbound bytes once deaf is set, like a peer whose +// network silently dropped. It still reports EOF when the proxy hangs up. +type deafConn struct { + net.Conn + deaf atomic.Bool +} + +func (c *deafConn) Read(p []byte) (int, error) { + n, err := c.Conn.Read(p) + for c.deaf.Load() && err == nil { + n, err = c.Conn.Read(p) + } + return n, err +} + +func TestUnresponsiveClientIsDisconnected(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + g := backendFixture(t) + address, _ := serveProxy(t, &Server{HostKey: proxyHost, ClientPublicKey: clientKey.PublicKey(), Authorize: func(context.Context, Authorization) (*Grant, error) { return g, nil }, KeepaliveInterval: 20 * time.Millisecond}) + raw, err := net.Dial("tcp", address) + require.NoError(t, err) + conn := &deafConn{Conn: raw} + c, chans, reqs, err := ssh.NewClientConn(conn, address, &ssh.ClientConfig{User: "ee", Auth: []ssh.AuthMethod{ssh.PublicKeys(clientKey)}, HostKeyCallback: ssh.FixedHostKey(proxyHost.PublicKey()), Timeout: time.Second}) + require.NoError(t, err) + client := ssh.NewClient(c, chans, reqs) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + require.NoError(t, session.Start("hold")) + + conn.deaf.Store(true) + done := make(chan error, 1) + go func() { done <- client.Wait() }() + select { + case err := <-done: + require.Error(t, err) + case <-time.After(2 * time.Second): + t.Fatal("proxy kept a silent client's session open") + } +} + +func TestOpenSSHBookkeepingDoesNotDisconnect(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + g := backendFixture(t) + address, _ := testProxy(t, clientKey, proxyHost, func(context.Context, Authorization) (*Grant, error) { return g, nil }, nil) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + for _, request := range []string{"keepalive@openssh.com", "tcpip-forward"} { + ok, _, err := client.SendRequest(request, true, nil) + require.NoError(t, err) + require.False(t, ok, "global requests must be refused locally") + } + channel, requests, err := client.OpenChannel("session", nil) + require.NoError(t, err) + defer func() { _ = channel.Close() }() + go ssh.DiscardRequests(requests) + for _, request := range []string{"env", "eow@openssh.com"} { + _, err := channel.SendRequest(request, false, nil) + require.NoError(t, err) + } + ok, err := channel.SendRequest("exec", true, ssh.Marshal(struct{ Command string }{"after-bookkeeping"})) + require.NoError(t, err) + require.True(t, ok) + output, err := io.ReadAll(channel) + require.NoError(t, err) + require.Equal(t, "ok:after-bookkeeping", string(output)) +} + +func TestKeepalivesDuringServerOutput(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + g := backendFixture(t) + address, _ := testProxy(t, clientKey, proxyHost, func(context.Context, Authorization) (*Grant, error) { return g, nil }, nil) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + defer func() { _ = session.Close() }() + output, err := session.StdoutPipe() + require.NoError(t, err) + require.NoError(t, session.Start("stream-output")) + keepalives := make(chan error, 1) + go func() { + for range 200 { + if _, _, err := client.SendRequest("keepalive@openssh.com", true, nil); err != nil { + keepalives <- err + return + } + } + keepalives <- nil + }() + data, err := io.ReadAll(output) + require.NoError(t, err) + require.NoError(t, <-keepalives) + var exit *ssh.ExitError + require.ErrorAs(t, session.Wait(), &exit) + require.Equal(t, 7, exit.ExitStatus()) + require.Equal(t, bytes.Repeat([]byte("output"), 1<<21), data) +} diff --git a/internal/sshproxy/transport.go b/internal/sshproxy/transport.go new file mode 100644 index 0000000..cdbde92 --- /dev/null +++ b/internal/sshproxy/transport.go @@ -0,0 +1,90 @@ +package sshproxy + +import ( + "bytes" + "context" + "crypto/hmac" + "crypto/rand" + "crypto/sha256" + "crypto/tls" + "crypto/x509" + "encoding/base64" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "net/url" + "time" + + "github.com/gobwas/ws" + "github.com/ironsh/iron-proxy/internal/dnsguard" +) + +func transport(ctx context.Context, target string, guard *dnsguard.Guard, roots *x509.CertPool) (net.Conn, error) { + // Bound TLS and HTTP upgrade negotiation too, not only the TCP dial. + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + u, err := url.Parse(target) + if err != nil || u.User != nil || u.Host == "" || u.RawQuery != "" || u.Fragment != "" { + return nil, errors.New("invalid SSH destination") + } + dialer := &net.Dialer{Timeout: 10 * time.Second, Control: guard.DialControl} + switch u.Scheme { + case "tcp": + if u.Path != "" { + return nil, errors.New("invalid TCP destination") + } + return dialer.DialContext(ctx, "tcp", u.Host) + case "wss": + // wstunnel v11 carries the fixed loopback SSH target in a JWT subprotocol. + token, err := tunnelToken() + if err != nil { + return nil, err + } + + client := ws.Dialer{NetDial: dialer.DialContext, TLSConfig: &tls.Config{RootCAs: roots, MinVersion: tls.VersionTLS12}, Timeout: 10 * time.Second, Protocols: []string{"v1", "authorization.bearer." + token}} + conn, buffered, handshake, err := client.Dial(ctx, target) + if err != nil { + return nil, fmt.Errorf("SSH WebSocket connection failed: %w", err) + } + if handshake.Protocol != "v1" { + _ = conn.Close() + return nil, errors.New("unsupported SSH WebSocket protocol") + } + stream := &websocketConn{Conn: conn, reader: conn} + if buffered != nil { + prefix := make([]byte, buffered.Buffered()) + if _, err := io.ReadFull(buffered, prefix); err != nil { + _ = conn.Close() + return nil, err + } + ws.PutReader(buffered) + stream.reader = io.MultiReader(bytes.NewReader(prefix), conn) + } + return stream, nil + + default: + return nil, errors.New("SSH transport must be tcp or wss") + } +} + +func tunnelToken() (string, error) { + nonce := make([]byte, 16) + if _, err := rand.Read(nonce); err != nil { + return "", errors.New("tunnel nonce unavailable") + } + payload, err := json.Marshal(map[string]any{"id": hex.EncodeToString(nonce), "p": map[string]any{"Tcp": map[string]any{"proxy_protocol": false}}, "r": "127.0.0.1", "rp": 22}) + if err != nil { + return "", err + } + enc := base64.RawURLEncoding + text := enc.EncodeToString([]byte(`{"alg":"HS256","typ":"JWT"}`)) + "." + enc.EncodeToString(payload) + // wstunnel treats this JWT as routing metadata, not as authentication. + mac := hmac.New(sha256.New, nonce) + if _, err := mac.Write([]byte(text)); err != nil { + return "", err + } + return text + "." + enc.EncodeToString(mac.Sum(nil)), nil +} diff --git a/internal/sshproxy/websocket.go b/internal/sshproxy/websocket.go new file mode 100644 index 0000000..602e8b5 --- /dev/null +++ b/internal/sshproxy/websocket.go @@ -0,0 +1,86 @@ +package sshproxy + +import ( + "errors" + "io" + "net" + "sync" + + "github.com/gobwas/ws" + "github.com/gobwas/ws/wsutil" +) + +// websocketConn matches wstunnel v11's default unmasked framing on both legs. +// TLS still authenticates and encrypts the external transport. +type websocketConn struct { + net.Conn + reader io.Reader + remaining int64 + fragmented bool + writeMu sync.Mutex +} + +func (c *websocketConn) Write(data []byte) (int, error) { + c.writeMu.Lock() + defer c.writeMu.Unlock() + if err := wsutil.WriteServerBinary(c.Conn, data); err != nil { + return 0, err + } + return len(data), nil +} + +func (c *websocketConn) Read(data []byte) (int, error) { + if len(data) == 0 { + return 0, nil + } + for c.remaining == 0 { + h, err := ws.ReadHeader(c.reader) + if err != nil { + return 0, err + } + if h.Masked || h.Rsv != 0 || h.Length < 0 || h.Length > 1<<20 { + return 0, errors.New("invalid wstunnel frame") + } + switch h.OpCode { + case ws.OpBinary: + if c.fragmented { + return 0, errors.New("invalid wstunnel fragmentation") + } + c.fragmented = !h.Fin + c.remaining = h.Length + case ws.OpContinuation: + if !c.fragmented { + return 0, errors.New("unexpected wstunnel continuation") + } + c.fragmented = !h.Fin + c.remaining = h.Length + case ws.OpPing, ws.OpPong, ws.OpClose: + if !h.Fin || h.Length > 125 { + return 0, errors.New("invalid wstunnel control frame") + } + payload := make([]byte, int(h.Length)) + if _, err = io.ReadFull(c.reader, payload); err != nil { + return 0, err + } + if h.OpCode == ws.OpClose { + return 0, io.EOF + } + if h.OpCode == ws.OpPing { + c.writeMu.Lock() + err = wsutil.WriteServerMessage(c.Conn, ws.OpPong, payload) + c.writeMu.Unlock() + if err != nil { + return 0, err + } + } + default: + return 0, errors.New("unsupported wstunnel frame") + } + } + if int64(len(data)) > c.remaining { + data = data[:int(c.remaining)] + } + n, err := c.reader.Read(data) + c.remaining -= int64(n) + return n, err +} diff --git a/internal/sshproxy/wstunnel_integration_test.go b/internal/sshproxy/wstunnel_integration_test.go new file mode 100644 index 0000000..a177cfb --- /dev/null +++ b/internal/sshproxy/wstunnel_integration_test.go @@ -0,0 +1,42 @@ +package sshproxy + +import ( + "bufio" + "bytes" + "context" + "crypto/x509" + "io" + "os" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/ironsh/iron-proxy/internal/dnsguard" +) + +func TestWstunnelIntegration(t *testing.T) { + target := os.Getenv("IRON_SSH_WSTUNNEL_TEST_URL") + caPath := os.Getenv("IRON_SSH_WSTUNNEL_TEST_CA") + if target == "" || caPath == "" { + t.Skip("set IRON_SSH_WSTUNNEL_TEST_URL and IRON_SSH_WSTUNNEL_TEST_CA for a local OpenSSH/wstunnel fixture") + } + cert, err := os.ReadFile(caPath) + require.NoError(t, err) + roots := x509.NewCertPool() + require.True(t, roots.AppendCertsFromPEM(cert)) + conn, err := transport(context.Background(), target, &dnsguard.Guard{}, roots) + require.NoError(t, err) + defer conn.Close() + require.NoError(t, conn.SetDeadline(time.Now().Add(3*time.Second))) + _, err = conn.Write([]byte("SSH-2.0-iron-test\r\n")) + require.NoError(t, err) + reader := bufio.NewReaderSize(conn, 256) + banner, err := reader.ReadSlice('\n') + require.NoError(t, err) + require.True(t, bytes.HasPrefix(banner, []byte("SSH-2.0-"))) + // Wait for the packet header even when the banner spans multiple reads. + var packetLength [4]byte + _, err = io.ReadFull(reader, packetLength[:]) + require.NoError(t, err) +} diff --git a/internal/transform/secrets/op_resolver.go b/internal/transform/secrets/op_resolver.go index fe197bb..c9aedf2 100644 --- a/internal/transform/secrets/op_resolver.go +++ b/internal/transform/secrets/op_resolver.go @@ -36,21 +36,27 @@ type opConfig struct { FailureTTL string `yaml:"failure_ttl,omitempty"` } +// opClients is shared by every 1Password source in the process. Each secrets +// transform, credential source, and config reload builds its own registry, but +// they must not create SDK clients concurrently: the SDK lazily loads its Wasm +// core without synchronization, so concurrent first calls each compile a +// separate copy (~150 MB of RSS apiece). +var opClients = &opClientCache{ + getenv: os.Getenv, + newClient: func(ctx context.Context, token string) (opClient, error) { + c, err := onepassword.NewClient(ctx, + onepassword.WithServiceAccountToken(token), + onepassword.WithIntegrationInfo("iron-proxy", version.Version), + ) + if err != nil { + return nil, err + } + return opSDKClient{c: c}, nil + }, +} + func newOPBuilder(logger *slog.Logger) *opBuilder { - cache := &opClientCache{ - getenv: os.Getenv, - newClient: func(ctx context.Context, token string) (opClient, error) { - c, err := onepassword.NewClient(ctx, - onepassword.WithServiceAccountToken(token), - onepassword.WithIntegrationInfo("iron-proxy", version.Version), - ) - if err != nil { - return nil, err - } - return opSDKClient{c: c}, nil - }, - } - return &opBuilder{clientFor: cache.get, logger: logger} + return &opBuilder{clientFor: opClients.get, logger: logger} } func (r *opBuilder) Build(raw yaml.Node) (secretSource, error) { @@ -86,7 +92,8 @@ func (r *opBuilder) fetchSecret(ctx context.Context, cfg opConfig) (string, erro // opClientCache lazily constructs a single SDK client. The 1P SDK loads a // Wasm module on NewClient, so reusing the client across multiple secret -// entries is significantly cheaper than creating one per entry. +// entries is significantly cheaper than creating one per entry. The mutex also +// serializes NewClient calls, which the SDK requires (see opClients). type opClientCache struct { mu sync.Mutex client opClient diff --git a/internal/transform/secrets/op_resolver_test.go b/internal/transform/secrets/op_resolver_test.go index 8b8c23b..43a5704 100644 --- a/internal/transform/secrets/op_resolver_test.go +++ b/internal/transform/secrets/op_resolver_test.go @@ -4,7 +4,10 @@ import ( "context" "fmt" "log/slog" + "sync" + "sync/atomic" "testing" + "time" "github.com/stretchr/testify/require" ) @@ -207,3 +210,43 @@ func TestOPClientCache_PropagatesNewClientError(t *testing.T) { require.Error(t, err) require.Contains(t, err.Error(), "sdk init failed") } + +func TestOPSources_ShareOneClientAcrossRegistries(t *testing.T) { + // Separate transforms each build their own registry. Concurrent first + // fetches across them must still create only one SDK client. + var calls atomic.Int32 + saved := opClients + opClients = &opClientCache{ + getenv: func(string) string { return "tok" }, + newClient: func(_ context.Context, _ string) (opClient, error) { + calls.Add(1) + time.Sleep(10 * time.Millisecond) // widen the window for overlapping creation + return staticOPClient("v", nil), nil + }, + } + t.Cleanup(func() { opClients = saved }) + + var sources []Source + for _, ref := range []string{"op://v/slack/token", "op://v/openai/key", "op://v/centaur/token"} { + src, err := BuildSource(yamlNode(t, map[string]string{"type": "1password", "secret_ref": ref}), slog.Default()) + require.NoError(t, err) + sources = append(sources, src) + } + + var wg sync.WaitGroup + errs := make(chan error, len(sources)) + for _, src := range sources { + wg.Add(1) + go func() { + defer wg.Done() + _, err := src.Get(context.Background()) + errs <- err + }() + } + wg.Wait() + close(errs) + for err := range errs { + require.NoError(t, err) + } + require.Equal(t, int32(1), calls.Load()) +} diff --git a/internal/transform/transform.go b/internal/transform/transform.go index fc0b009..ea48a2f 100644 --- a/internal/transform/transform.go +++ b/internal/transform/transform.go @@ -59,6 +59,10 @@ const ( // from a peeked TLS ClientHello SNI. Method, path, headers, and body are // all empty; transforms that depend on them cannot function. ModeSNIOnly + + // ModeSSH means the event came from the SSH listener. Method carries the + // SSH operation and Path its detail (route, command, or subsystem). + ModeSSH ) // String returns the canonical string form of the mode. @@ -68,6 +72,8 @@ func (m Mode) String() string { return "mitm" case ModeSNIOnly: return "sni-only" + case ModeSSH: + return "ssh" default: return "unknown" } diff --git a/iron-proxy.example.yaml b/iron-proxy.example.yaml index ae9786f..3140e30 100644 --- a/iron-proxy.example.yaml +++ b/iron-proxy.example.yaml @@ -699,8 +699,13 @@ transforms: # inside CTEs, subqueries, function arguments, etc.: # - SET ROLE / RESET ROLE / SET SESSION AUTHORIZATION / RESET SESSION # AUTHORIZATION — direct GUC mutation. -# - SELECT set_config('role', ...), SELECT pg_catalog.set_config(...) and -# the same for 'session_authorization' — function-call bypasses. +# - All calls to set_config, including schema-qualified and parameterized +# forms. Dynamic targets cannot be proven safe when the query is parsed. +# - The legacy wire-level FunctionCall protocol, which invokes functions by +# OID without carrying SQL text the proxy can inspect. +# - Function and procedure definitions whose bodies cannot be inspected. +# LANGUAGE sql and static LANGUAGE plpgsql bodies are parsed recursively; +# dynamic PL/pgSQL and unsupported languages are rejected. # - DO $$ ... $$ blocks — the plpgsql body is opaque to the SQL parser, so # we reject rather than risk an embedded role change. # - Multi-statement Simple Queries — kept as a single-statement guard. @@ -708,11 +713,11 @@ transforms: # Cannot be caught at the wire (defend at the database with role/grant config): # - User-defined SECURITY DEFINER functions that elevate internally. # - Dynamic SQL via plpgsql EXECUTE with a non-literal argument. -# - Functions that wrap set_config under a different name. +# - Calls to pre-existing functions that wrap set_config under another name. # # Limitations: client→proxy TLS is not supported (SSLRequest is refused); -# CancelRequest is not forwarded; one shared client credential per route -# (no per-user). +# CancelRequest is not forwarded; frontend messages are limited to 16 MiB; +# one shared client credential per route (no per-user). # # The proxy runs a single postgres listener: the postgres key is one object # with a bind address and a list of upstreams. A single listen address serves @@ -744,11 +749,12 @@ transforms: # # client issues runs as this role on the upstream database. # role: tenant_role # # Optional session variables (GUCs) the proxy SETs at session start, -# # before the SET ROLE. The proxy also pins these: a client may not SET, -# # RESET, or set_config them afterwards (nor RESET ALL / DISCARD ALL), so -# # a setting used as a security boundary (e.g. an RLS key) can't be -# # overridden. Names must be bare or dotted GUC identifiers; role and -# # session_authorization are reserved (use role above). +# # before the SET ROLE. The proxy also pins these: a client may not SET +# # or RESET them afterwards (nor RESET ALL / DISCARD ALL), so a setting +# # used as a security boundary (e.g. an RLS key) can't be overridden. +# # All set_config calls are blocked. Names must be bare or dotted GUC +# # identifiers; role and session_authorization are reserved (use role +# # above). # settings: # - name: app.tenant_id # value: centaur