From b345f88756ab90b2b40d496f00a2602fcabdb7f4 Mon Sep 17 00:00:00 2001 From: Matthew Slipper Date: Tue, 6 Oct 2026 11:45:21 -0600 Subject: [PATCH 1/8] fix(postgres): reject writes to pg_settings (#274) Harden the postgres policy by rejecting UPDATE and INSERT on pg_settings, along with view and rule definitions that reference it. --- integration_test/postgres_test.go | 16 +++++++++++++ internal/postgres/config.go | 3 ++- internal/postgres/policy.go | 13 ++++++++--- internal/postgres/policy_test.go | 3 +++ internal/postgres/session.go | 6 +++++ internal/postgres/sql.go | 38 +++++++++++++++++++++++++++++++ internal/postgres/sql_test.go | 25 ++++++++++++++++++++ 7 files changed, 100 insertions(+), 4 deletions(-) diff --git a/integration_test/postgres_test.go b/integration_test/postgres_test.go index 4ba9d6a..47292fa 100644 --- a/integration_test/postgres_test.go +++ b/integration_test/postgres_test.go @@ -373,6 +373,22 @@ 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("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/postgres/config.go b/internal/postgres/config.go index 4dd1d8f..867961d 100644 --- a/internal/postgres/config.go +++ b/internal/postgres/config.go @@ -20,7 +20,8 @@ // 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); DO blocks; uninspectable function and procedure definitions; and +// settings); UPDATE or INSERT on pg_settings and view or rule definitions over +// it; DO blocks; uninspectable function and procedure definitions; 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. diff --git a/internal/postgres/policy.go b/internal/postgres/policy.go index 998df27..a037a13 100644 --- a/internal/postgres/policy.go +++ b/internal/postgres/policy.go @@ -33,6 +33,10 @@ const ( // 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 ) // ClassifyClientStatement inspects sql and returns whether the relay should @@ -40,9 +44,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 every set_config call, 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). +// Beyond the role policy, the proxy rejects every set_config call, writes to +// pg_settings (directly or via a client-defined view or rule), 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; // Classify aggregates the batch, so a single offending statement anywhere @@ -64,6 +69,8 @@ func ClassifyClientStatement(sql string, pinned map[string]struct{}) (allowed bo return false, RejectDoBlock case OpUninspectableRoutine: return false, RejectUninspectableRoutine + case OpSettingsCatalogWrite: + return false, RejectSettingsCatalogWrite } // 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 c273669..e76f68d 100644 --- a/internal/postgres/policy_test.go +++ b/internal/postgres/policy_test.go @@ -129,6 +129,9 @@ func TestClassifyClientStatement(t *testing.T) { {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: "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: "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/session.go b/internal/postgres/session.go index 7011504..7ea8e6c 100644 --- a/internal/postgres/session.go +++ b/internal/postgres/session.go @@ -492,6 +492,12 @@ func rejectError(reason RejectReason) *pgproto3.ErrorResponse { 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 RejectFunctionCall: return &pgproto3.ErrorResponse{ Severity: "ERROR", diff --git a/internal/postgres/sql.go b/internal/postgres/sql.go index fa9469f..6ce4b04 100644 --- a/internal/postgres/sql.go +++ b/internal/postgres/sql.go @@ -77,6 +77,12 @@ const ( // 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 ) // GUC names whose mutation we treat as a role change for policy purposes. @@ -403,6 +409,18 @@ 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.FuncCall: if name, ok := setConfigTarget(n); ok { if name != "" { @@ -418,6 +436,26 @@ func scanMutations(node *pg_query.Node) mutations { return m } +// 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). diff --git a/internal/postgres/sql_test.go b/internal/postgres/sql_test.go index 6b23069..9ca1bf5 100644 --- a/internal/postgres/sql_test.go +++ b/internal/postgres/sql_test.go @@ -78,6 +78,21 @@ func TestClassify(t *testing.T) { // walker through the nested SelectStmt. {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}, + // 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}, @@ -134,6 +149,16 @@ func TestClassify(t *testing.T) { 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 $$", From 5bb325ace6c391cd5b2466d024ac3a2162162931 Mon Sep 17 00:00:00 2001 From: Matthew Slipper Date: Tue, 6 Oct 2026 12:32:09 -0600 Subject: [PATCH 2/8] fix(postgres): harden managed-session statement policy (#275) * fix(postgres): reject callback-binding definitions such as CREATE AGGREGATE * test(postgres): cover aggregate alias and harden fixture grants * test(postgres): keep fixture privileges default so tests exercise the proxy --- README.md | 9 ++++++-- integration_test/postgres_test.go | 12 +++++++++++ internal/postgres/config.go | 4 +++- internal/postgres/policy.go | 10 ++++++++- internal/postgres/policy_test.go | 1 + internal/postgres/session.go | 6 ++++++ internal/postgres/sql.go | 36 +++++++++++++++++++++++++++++++ internal/postgres/sql_test.go | 18 ++++++++++++++++ 8 files changed, 92 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8e5d8ac..23e99b0 100644 --- a/README.md +++ b/README.md @@ -48,9 +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`, - any `set_config` call, DO blocks, unsafe function definitions, etc.) via a + 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. Pairs with + 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 diff --git a/integration_test/postgres_test.go b/integration_test/postgres_test.go index 47292fa..f707932 100644 --- a/integration_test/postgres_test.go +++ b/integration_test/postgres_test.go @@ -389,6 +389,18 @@ func TestPostgresPolicy(t *testing.T) { } }) + 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/postgres/config.go b/internal/postgres/config.go index 867961d..e40edd3 100644 --- a/internal/postgres/config.go +++ b/internal/postgres/config.go @@ -21,7 +21,9 @@ // of a setting the upstream pins; the reset-everything statements // `RESET ALL` and `DISCARD ALL` (which would clear the managed role and pinned // settings); UPDATE or INSERT on pg_settings and view or rule definitions over -// it; DO blocks; uninspectable function and procedure definitions; and +// 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. diff --git a/internal/postgres/policy.go b/internal/postgres/policy.go index a037a13..e1ed529 100644 --- a/internal/postgres/policy.go +++ b/internal/postgres/policy.go @@ -37,6 +37,11 @@ const ( // 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 @@ -45,7 +50,8 @@ const ( // mutating; pass nil when the upstream pins nothing. // // Beyond the role policy, the proxy rejects every set_config call, writes to -// pg_settings (directly or via a client-defined view or rule), any SET or RESET +// 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). // @@ -71,6 +77,8 @@ func ClassifyClientStatement(sql string, pinned map[string]struct{}) (allowed bo 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 e76f68d..c8c3cf2 100644 --- a/internal/postgres/policy_test.go +++ b/internal/postgres/policy_test.go @@ -131,6 +131,7 @@ func TestClassifyClientStatement(t *testing.T) { {name: "current_setting role allowed", sql: "SELECT current_setting('role')", 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. diff --git a/internal/postgres/session.go b/internal/postgres/session.go index 7ea8e6c..151292c 100644 --- a/internal/postgres/session.go +++ b/internal/postgres/session.go @@ -498,6 +498,12 @@ func rejectError(reason RejectReason) *pgproto3.ErrorResponse { 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", diff --git a/internal/postgres/sql.go b/internal/postgres/sql.go index 6ce4b04..17a57bc 100644 --- a/internal/postgres/sql.go +++ b/internal/postgres/sql.go @@ -83,6 +83,14 @@ const ( // 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. @@ -421,6 +429,20 @@ func scanMutations(node *pg_query.Node) mutations { 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, ok := setConfigTarget(n); ok { if name != "" { @@ -436,6 +458,20 @@ 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. diff --git a/internal/postgres/sql_test.go b/internal/postgres/sql_test.go index 9ca1bf5..355e894 100644 --- a/internal/postgres/sql_test.go +++ b/internal/postgres/sql_test.go @@ -93,6 +93,24 @@ func TestClassify(t *testing.T) { {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}, From 7c6a6e9261f09aa4f4efdb0bc20bdbbfb306603d Mon Sep 17 00:00:00 2001 From: Leonardo Yvens Date: Fri, 16 Oct 2026 12:37:33 -0700 Subject: [PATCH 3/8] fix WS hang (#244) --- internal/proxy/proxy.go | 34 +----- internal/proxy/sni_passthrough.go | 25 ++-- internal/proxy/tunnel_test.go | 195 ++++++++++++++++++++++++------ 3 files changed, 180 insertions(+), 74 deletions(-) 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 From 60ed3566e5b39b1ef74d4854c62eb2d9fc4b8f42 Mon Sep 17 00:00:00 2001 From: Matthew Slipper Date: Wed, 21 Oct 2026 06:03:54 -0600 Subject: [PATCH 4/8] fix: require patched Go toolchain for emudhra CA compromise (#288) * fix: block compromised emudhra CA chains in proxy images * refactor: rely on Go's emudhra certificate rejection * test: remove redundant emudhra verifier fixtures * docs: remove emudhra README addition --- .github/workflows/ci.yml | 10 +++++----- .github/workflows/release.yml | 2 +- examples/docker-compose/Dockerfile | 2 +- examples/nftables/Dockerfile | 2 +- go.mod | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) 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/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..d3777b7 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 From fd1c8bc735619bee1f55f634f4a13987bca8a258 Mon Sep 17 00:00:00 2001 From: Matthew Slipper Date: Fri, 30 Oct 2026 22:29:27 -0600 Subject: [PATCH 5/8] fix(secrets): share one 1Password client across all sources (#299) Every secrets transform, credential source, and config reload builds its own source registry, and each registry had its own 1Password client cache. The 1Password SDK loads its Wasm core lazily without synchronization, so concurrent first fetches from different registries each compiled a separate copy of the core (~150 MB RSS apiece). Twenty requests needing three 1Password-backed secrets peaked at ~470 MB. Use a single process-wide client cache so SDK client creation is serialized and the core is loaded once. --- internal/transform/secrets/op_resolver.go | 37 +++++++++------- .../transform/secrets/op_resolver_test.go | 43 +++++++++++++++++++ 2 files changed, 65 insertions(+), 15 deletions(-) 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()) +} From 6eee95bfa430e2ee25205d59279f5a0354b076e3 Mon Sep 17 00:00:00 2001 From: goodwin-klaviyo Date: Wed, 4 Nov 2026 15:52:12 -0500 Subject: [PATCH 6/8] Add opt-in SSH credential proxying (#297) --- AGENTS.md | 5 +- README.md | 7 + cmd/iron-proxy/main.go | 41 +++++- go.mod | 17 ++- go.sum | 35 +++-- internal/config/config.go | 1 + internal/config/env.go | 8 + internal/sshproxy/README.md | 53 +++++++ internal/sshproxy/config.go | 136 +++++++++++++++++ internal/sshproxy/policy.go | 85 +++++++++++ internal/sshproxy/server.go | 262 +++++++++++++++++++++++++++++++++ internal/sshproxy/transport.go | 93 ++++++++++++ internal/sshproxy/websocket.go | 86 +++++++++++ 13 files changed, 805 insertions(+), 24 deletions(-) create mode 100644 internal/sshproxy/README.md create mode 100644 internal/sshproxy/config.go create mode 100644 internal/sshproxy/policy.go create mode 100644 internal/sshproxy/server.go create mode 100644 internal/sshproxy/transport.go create mode 100644 internal/sshproxy/websocket.go diff --git a/AGENTS.md b/AGENTS.md index 8c79f2d..3815eb1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -6,8 +6,9 @@ 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/`. +- The module uses sshpiper's crypto replacement for SSH transport support. - 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 23e99b0..58b80f8 100644 --- a/README.md +++ b/README.md @@ -1103,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 using sshpiper. 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..4d1937d 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() @@ -79,6 +81,22 @@ func main() { os.Exit(1) } + sshServer, sshListener, err := sshproxy.LoadFromNode(ctx, cfg.SSH, 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())) + } + }() + } + // CLI flag takes precedence over environment variable. proxyToken := *tokenFlag if proxyToken == "" { @@ -108,7 +126,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 @@ -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/go.mod b/go.mod index d3777b7..1a99c17 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ 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 @@ -27,6 +28,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.57.0 golang.org/x/net v0.58.0 golang.org/x/oauth2 v0.36.0 google.golang.org/grpc v1.83.0 @@ -74,6 +76,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 @@ -126,15 +130,16 @@ 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 - golang.org/x/text v0.41.0 // indirect + golang.org/x/mod v0.41.0 // indirect + golang.org/x/sync v0.23.0 // indirect + golang.org/x/sys v0.48.0 // indirect + golang.org/x/text v0.42.0 // indirect golang.org/x/time v0.12.0 // indirect - golang.org/x/tools v0.48.0 // indirect + golang.org/x/tools v0.49.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa // indirect ) replace github.com/1password/onepassword-sdk-go => github.com/ironsh/onepassword-sdk-go v0.0.0-20260430210150-17395b1aedb5 + +replace golang.org/x/crypto => github.com/tg123/sshpiper.crypto v0.57.0-sshpiper-20260929 diff --git a/go.sum b/go.sum index ce57f25..b4b3fdd 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= @@ -229,6 +235,8 @@ github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834 h1:ZF+QBjOI+tILZ github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834/go.mod h1:m9ymHTgNSEjuxvw8E7WWe4Pl4hZQHXONY8wE6dMLaRk= github.com/tetratelabs/wazero v1.11.0 h1:+gKemEuKCTevU4d7ZTzlsvgd1uaToIDtlQlmNbwqYhA= github.com/tetratelabs/wazero v1.11.0/go.mod h1:eV28rsN8Q+xwjogd7f4/Pp4xFxO7uOGbLcD/LzB1wiU= +github.com/tg123/sshpiper.crypto v0.57.0-sshpiper-20260929 h1:uIavevzg55D/oun/F7+ZsDZtiJTE0RAjh8DH7awZJBg= +github.com/tg123/sshpiper.crypto v0.57.0-sshpiper-20260929/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= github.com/tklauser/go-sysconf v0.4.0 h1:7H0uAN+7RkwWRaxhYXDLqa5V3LPrJeV8wmD9dRUgPQU= github.com/tklauser/go-sysconf v0.4.0/go.mod h1:8mTNWyog7H+MpKijp4VmKJAd2bbYQ2zuUwkYRbUArPI= github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4= @@ -277,30 +285,29 @@ go.opentelemetry.io/proto/otlp v1.10.0 h1:IQRWgT5srOCYfiWnpqUYz9CVmbO8bFmKcwYxpu go.opentelemetry.io/proto/otlp v1.10.0/go.mod h1:/CV4QoCR/S9yaPj8utp3lvQPoqMtxXdzn7ozvvozVqk= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -golang.org/x/crypto v0.55.0 h1:+KWHjbgOaAQ66dh/YlkZKHlz9ZUlq61AFirAR9ntP8M= -golang.org/x/crypto v0.55.0/go.mod h1:uq0V9dE/fzQuJtbnL+2EhWOE63vo164FY8xqEnV9xis= -golang.org/x/mod v0.38.0 h1:MECBjubtXD7yj4HrhIUcywNaGeNVUdfVnxmPajOk4yk= -golang.org/x/mod v0.38.0/go.mod h1:V6Xz0pq8TQ3dGqVQ1FVHuelZpAL0uNhSkk9ogYP3c40= +golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c= +golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o= golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To= golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU= golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs= golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q= -golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek= -golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= +golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk= +golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 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.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= -golang.org/x/term v0.45.0/go.mod h1:9aqxs0blBcrm/n0L9QW0aRVD+ktan8ssZromtqJC43w= -golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= -golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= +golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE= +golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc= +golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI= +golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE= golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= -golang.org/x/tools v0.48.0 h1:3+hClM1aLL5mjMKm5ovokw9epgRXPuu2tILgismM6RE= -golang.org/x/tools v0.48.0/go.mod h1:08xX0orndb/F7jJxGDicx061tyd5pcMto75YMAXr6lk= +golang.org/x/tools v0.49.0 h1:3NI7VXzL9+1WZD52Dx2ttoPwD5DWrFGpl9mFZDlmisI= +golang.org/x/tools v0.49.0/go.mod h1:SJNXV9DBKT0UbdttsQjbfJlAE/q+y36++zo3uL3N0Oo= gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4= gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E= google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa h1:Kjn0N0tCrDgiAFW+lGO4JZ3ck44CehvJQMAwj9QF0G8= 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/sshproxy/README.md b/internal/sshproxy/README.md new file mode 100644 index 0000000..cd4ee9a --- /dev/null +++ b/internal/sshproxy/README.md @@ -0,0 +1,53 @@ +# 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. The module uses sshpiper's crypto replacement for transport support. + +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_deny_cidrs` uses Iron's existing dial-time IP guard; +omission blocks loopback and cloud metadata. Explicit overrides are intended +for trusted operator configuration, including loopback-only integration tests. + +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. Responses are limited to 1 MiB; errors never expose their +bodies or credential values. Redirects are not followed. + +The paired public key is checked before authorization or any upstream dial. +Exec and SFTP channels are supported; forwarding is blocked. Global requests +are refused locally, and no-reply environment/end-of-write requests are discarded. +Other unsupported channel capabilities terminate the connection. 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. 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..1261cca --- /dev/null +++ b/internal/sshproxy/config.go @@ -0,0 +1,136 @@ +// Package sshproxy implements an SSH credential boundary within iron-proxy. +package sshproxy + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "io" + "log/slog" + "net" + "net/http" + "os" + "time" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "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"` +} + +// 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() + if resp.StatusCode != http.StatusOK { + return nil, errors.New("SSH access denied") + } + // 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. +func LoadFromNode(ctx context.Context, node yaml.Node, 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"` + DenyCIDRs []string `yaml:"upstream_deny_cidrs"` + } + 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") + } + if cfg.DenyCIDRs == nil { + cfg.DenyCIDRs = dnsguard.DefaultDenyCIDRs + } + 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), DenyCIDRs: cfg.DenyCIDRs, Logger: logger} + return server, listener, nil +} diff --git a/internal/sshproxy/policy.go b/internal/sshproxy/policy.go new file mode 100644 index 0000000..081c5a1 --- /dev/null +++ b/internal/sshproxy/policy.go @@ -0,0 +1,85 @@ +package sshproxy + +import ( + "errors" + "golang.org/x/crypto/ssh" +) + +var errCapabilityDenied = errors.New("SSH capability denied") + +func downstreamPolicy(packet []byte) (ssh.PipePacketHookMethod, []byte, error) { + deny := func() (ssh.PipePacketHookMethod, []byte, error) { + return ssh.PipePacketHookTransform, nil, errCapabilityDenied + } + if len(packet) == 0 { + return deny() + } + switch packet[0] { + case 80: + return refuseGlobalRequest(packet) + case 90: + var open struct { + Type string + Sender uint32 + Window uint32 + MaxPacket uint32 + Extra []byte `ssh:"rest"` + } + if ssh.Unmarshal(packet[1:], &open) != nil || open.Type != "session" { + return deny() + } + case 98: + var req struct { + Channel uint32 + Type string + WantReply bool + Payload []byte `ssh:"rest"` + } + if ssh.Unmarshal(packet[1:], &req) != nil { + return deny() + } + switch req.Type { + case "exec", "signal": + case "env", "eow@openssh.com": + if req.WantReply { + return deny() + } + // Ignore client bookkeeping without exposing environment injection. + return ssh.PipePacketHookTransform, nil, nil + case "subsystem": + var sub struct{ Name string } + if ssh.Unmarshal(req.Payload, &sub) != nil || sub.Name != "sftp" { + return deny() + } + default: + return deny() + } + } + return ssh.PipePacketHookTransform, packet, nil +} + +func upstreamPolicy(packet []byte) (ssh.PipePacketHookMethod, []byte, error) { + if len(packet) == 0 || packet[0] == 90 { + return ssh.PipePacketHookTransform, nil, errCapabilityDenied + } + if packet[0] == 80 { + return refuseGlobalRequest(packet) + } + return ssh.PipePacketHookTransform, packet, nil +} + +// Refuse globals locally, including forwarding and OpenSSH keepalives. +func refuseGlobalRequest(packet []byte) (ssh.PipePacketHookMethod, []byte, error) { + var req struct { + Type string + WantReply bool + Payload []byte `ssh:"rest"` + } + if ssh.Unmarshal(packet[1:], &req) != nil { + return ssh.PipePacketHookTransform, nil, errCapabilityDenied + } + if req.WantReply { + return ssh.PipePacketHookReply, []byte{82}, nil + } + return ssh.PipePacketHookTransform, nil, nil +} diff --git a/internal/sshproxy/server.go b/internal/sshproxy/server.go new file mode 100644 index 0000000..75d3333 --- /dev/null +++ b/internal/sshproxy/server.go @@ -0,0 +1,262 @@ +package sshproxy + +import ( + "bytes" + "context" + "crypto/x509" + "encoding/base64" + "errors" + "log/slog" + "net" + "net/url" + "reflect" + "strings" + "sync" + "time" + + "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 + DenyCIDRs []string + TLSRootCAs *x509.CertPool + Logger *slog.Logger + RenewalInterval time.Duration + HandshakeTimeout time.Duration +} + +func (s *Server) Serve(parent context.Context, listener net.Listener) error { + if s.HostKey == nil || s.ClientPublicKey == nil || s.Authorize == 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() + action, reason := "deny", "authentication_failed" + var route, principal, grantID string + defer func() { + rejected := "" + if action == "deny" { + rejected = "ssh_authorization" + if reason == "capability_denied" { + rejected = "ssh_policy" + } + } + s.Logger.Info("ssh_connection", "action", action, "reason", reason, "route", route, "principal", principal, "grant_id", grantID, "duration_ms", time.Since(started).Milliseconds(), "request_transforms", []string{"ssh_authorization"}, "rejected_by", rejected) + }() + 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 approved *Grant + var auth Authorization + var upstream net.Conn + defer func() { + if upstream != nil { + _ = upstream.Close() + } + }() + config := &ssh.PiperConfig{MaxAuthTries: 1} + config.PublicKeyCallback = func(meta ssh.ConnMetadata, key ssh.PublicKey, _ ssh.ChallengeContext) (*ssh.Upstream, error) { + if !bytes.Equal(key.Marshal(), s.ClientPublicKey.Marshal()) { + return nil, errors.New("SSH access denied") + } + parsedRoute, destination, err := parseRoute(meta.User()) + if err != nil { + return nil, err + } + route = parsedRoute + auth = Authorization{Route: route, Destination: destination, PublicKey: string(bytes.TrimSpace(ssh.MarshalAuthorizedKey(key)))} + grant, err := s.Authorize(handshakeCtx, auth) + if err != nil { + return nil, errors.New("SSH access denied") + } + if err = validateGrant(grant); err != nil { + return nil, err + } + credential, err := signer(handshakeCtx, grant.PrivateKey, s.Logger) + if err != nil { + return nil, err + } + var expected ssh.PublicKey + if !grant.TLSIdentity { + expected, _, _, _, err = ssh.ParseAuthorizedKey([]byte(grant.HostKey)) + if err != nil { + return nil, errors.New("invalid upstream host identity") + } + } + if upstream != nil { + _ = upstream.Close() + } + connected, err := transport(handshakeCtx, grant.Destination, s.DenyCIDRs, s.TLSRootCAs) + if err != nil { + return nil, errors.New("SSH upstream unavailable") + } + upstream = connected + // Capture this connection, not the mutable upstream variable. Revocation + // must interrupt blocked writes as well as downstream reads. + context.AfterFunc(ctx, func() { _ = connected.Close() }) + if err = upstream.SetDeadline(handshakeDeadline); err != nil { + return nil, err + } + var algorithms []string + if expected != nil { + algorithms = []string{expected.Type()} + if expected.Type() == ssh.KeyAlgoRSA { + algorithms = []string{ssh.KeyAlgoRSASHA512, ssh.KeyAlgoRSASHA256} + } + } + approved = grant + principal = grant.Principal + grantID = grant.ID + return &ssh.Upstream{Conn: upstream, Address: grant.Destination, ClientConfig: ssh.ClientConfig{User: grant.Username, HostKeyAlgorithms: algorithms, Auth: []ssh.AuthMethod{ssh.PublicKeys(credential)}, 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 + }}}, nil + } + config.AddHostKey(s.HostKey) + pipe, err := ssh.NewSSHPiperConn(conn, config) + if err != nil { + return + } + defer pipe.Close() + if approved == nil { + return + } + if conn.SetDeadline(time.Time{}) != nil || upstream.SetDeadline(time.Time{}) != nil { + return + } + handshakeCancel() + action, reason = "allow", "connection_closed" + leaseCtx, leaseCancel := context.WithCancel(ctx) + defer leaseCancel() + done := make(chan struct{}) + go func() { defer close(done); s.renew(leaseCtx, cancel, auth, approved) }() + if err := pipe.WaitWithHook(upstreamPolicy, downstreamPolicy); err != nil { + reason = "connection_terminated" + if errors.Is(err, errCapabilityDenied) { + action, reason = "deny", "capability_denied" + } + } + leaseCancel() + <-done +} + +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 +} + +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) { + 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/transport.go b/internal/sshproxy/transport.go new file mode 100644 index 0000000..f51cbb8 --- /dev/null +++ b/internal/sshproxy/transport.go @@ -0,0 +1,93 @@ +package sshproxy + +import ( + "bytes" + "context" + "crypto/hmac" + "crypto/rand" + "crypto/sha256" + "crypto/tls" + "crypto/x509" + "encoding/base64" + "encoding/hex" + "encoding/json" + "errors" + "io" + "net" + "net/url" + "time" + + "github.com/gobwas/ws" + "github.com/ironsh/iron-proxy/internal/dnsguard" +) + +func transport(ctx context.Context, target string, deny []string, 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") + } + guard, err := dnsguard.New(deny) + if err != nil { + return nil, errors.New("invalid upstream deny CIDRs") + } + 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, errors.New("SSH WebSocket connection failed") + } + 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 +} From f3745000206f31a2084d3ab135ff1ddc67f265a6 Mon Sep 17 00:00:00 2001 From: Matthew Slipper Date: Wed, 4 Nov 2026 13:53:46 -0700 Subject: [PATCH 7/8] Revert "Add opt-in SSH credential proxying (#297)" (#301) This reverts commit 6eee95bfa430e2ee25205d59279f5a0354b076e3. --- AGENTS.md | 5 +- README.md | 7 - cmd/iron-proxy/main.go | 41 +----- go.mod | 17 +-- go.sum | 35 ++--- internal/config/config.go | 1 - internal/config/env.go | 8 - internal/sshproxy/README.md | 53 ------- internal/sshproxy/config.go | 136 ----------------- internal/sshproxy/policy.go | 85 ----------- internal/sshproxy/server.go | 262 --------------------------------- internal/sshproxy/transport.go | 93 ------------ internal/sshproxy/websocket.go | 86 ----------- 13 files changed, 24 insertions(+), 805 deletions(-) delete mode 100644 internal/sshproxy/README.md delete mode 100644 internal/sshproxy/config.go delete mode 100644 internal/sshproxy/policy.go delete mode 100644 internal/sshproxy/server.go delete mode 100644 internal/sshproxy/transport.go delete mode 100644 internal/sshproxy/websocket.go diff --git a/AGENTS.md b/AGENTS.md index 3815eb1..8c79f2d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -6,9 +6,8 @@ tools. ## Repo at a glance -- 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/`. -- The module uses sshpiper's crypto replacement for SSH transport support. +- Single Go module: `github.com/ironsh/iron-proxy` (Go 1.26+). +- One binary: `cmd/iron-proxy` (the MITM egress proxy). - 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 58b80f8..23e99b0 100644 --- a/README.md +++ b/README.md @@ -1103,10 +1103,3 @@ 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 using sshpiper. 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 4d1937d..a06534a 100644 --- a/cmd/iron-proxy/main.go +++ b/cmd/iron-proxy/main.go @@ -1,4 +1,4 @@ -// Command iron-proxy runs HTTP/HTTPS, DNS, and optional Postgres and SSH listeners. +// Command iron-proxy runs the MITM HTTP/HTTPS proxy with built-in DNS server. package main import ( @@ -29,7 +29,6 @@ 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" @@ -61,7 +60,6 @@ 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() @@ -81,22 +79,6 @@ func main() { os.Exit(1) } - sshServer, sshListener, err := sshproxy.LoadFromNode(ctx, cfg.SSH, 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())) - } - }() - } - // CLI flag takes precedence over environment variable. proxyToken := *tokenFlag if proxyToken == "" { @@ -126,7 +108,7 @@ func main() { // errc collects fatal errors from all background goroutines: servers // and (in managed mode) the config poller. - errc := make(chan error, 6) + errc := make(chan error, 5) // The postgres listener comes from the local YAML postgres: block // (self-managed proxies, inline DSNs) and, in managed mode, has additional @@ -290,17 +272,6 @@ 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()) }() @@ -372,14 +343,6 @@ 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/go.mod b/go.mod index 1a99c17..d3777b7 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,6 @@ 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 @@ -28,7 +27,6 @@ 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.57.0 golang.org/x/net v0.58.0 golang.org/x/oauth2 v0.36.0 google.golang.org/grpc v1.83.0 @@ -76,8 +74,6 @@ 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 @@ -130,16 +126,15 @@ 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/mod v0.41.0 // indirect - golang.org/x/sync v0.23.0 // indirect - golang.org/x/sys v0.48.0 // indirect - golang.org/x/text v0.42.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 + golang.org/x/text v0.41.0 // indirect golang.org/x/time v0.12.0 // indirect - golang.org/x/tools v0.49.0 // indirect + golang.org/x/tools v0.48.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa // indirect ) replace github.com/1password/onepassword-sdk-go => github.com/ironsh/onepassword-sdk-go v0.0.0-20260430210150-17395b1aedb5 - -replace golang.org/x/crypto => github.com/tg123/sshpiper.crypto v0.57.0-sshpiper-20260929 diff --git a/go.sum b/go.sum index b4b3fdd..ce57f25 100644 --- a/go.sum +++ b/go.sum @@ -103,12 +103,6 @@ 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= @@ -235,8 +229,6 @@ github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834 h1:ZF+QBjOI+tILZ github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834/go.mod h1:m9ymHTgNSEjuxvw8E7WWe4Pl4hZQHXONY8wE6dMLaRk= github.com/tetratelabs/wazero v1.11.0 h1:+gKemEuKCTevU4d7ZTzlsvgd1uaToIDtlQlmNbwqYhA= github.com/tetratelabs/wazero v1.11.0/go.mod h1:eV28rsN8Q+xwjogd7f4/Pp4xFxO7uOGbLcD/LzB1wiU= -github.com/tg123/sshpiper.crypto v0.57.0-sshpiper-20260929 h1:uIavevzg55D/oun/F7+ZsDZtiJTE0RAjh8DH7awZJBg= -github.com/tg123/sshpiper.crypto v0.57.0-sshpiper-20260929/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= github.com/tklauser/go-sysconf v0.4.0 h1:7H0uAN+7RkwWRaxhYXDLqa5V3LPrJeV8wmD9dRUgPQU= github.com/tklauser/go-sysconf v0.4.0/go.mod h1:8mTNWyog7H+MpKijp4VmKJAd2bbYQ2zuUwkYRbUArPI= github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4= @@ -285,29 +277,30 @@ go.opentelemetry.io/proto/otlp v1.10.0 h1:IQRWgT5srOCYfiWnpqUYz9CVmbO8bFmKcwYxpu go.opentelemetry.io/proto/otlp v1.10.0/go.mod h1:/CV4QoCR/S9yaPj8utp3lvQPoqMtxXdzn7ozvvozVqk= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c= -golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o= +golang.org/x/crypto v0.55.0 h1:+KWHjbgOaAQ66dh/YlkZKHlz9ZUlq61AFirAR9ntP8M= +golang.org/x/crypto v0.55.0/go.mod h1:uq0V9dE/fzQuJtbnL+2EhWOE63vo164FY8xqEnV9xis= +golang.org/x/mod v0.38.0 h1:MECBjubtXD7yj4HrhIUcywNaGeNVUdfVnxmPajOk4yk= +golang.org/x/mod v0.38.0/go.mod h1:V6Xz0pq8TQ3dGqVQ1FVHuelZpAL0uNhSkk9ogYP3c40= golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To= golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU= golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs= golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q= -golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk= -golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek= +golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 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.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= -golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= -golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE= -golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc= -golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI= -golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= +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= +golang.org/x/term v0.45.0/go.mod h1:9aqxs0blBcrm/n0L9QW0aRVD+ktan8ssZromtqJC43w= +golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= +golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE= golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= -golang.org/x/tools v0.49.0 h1:3NI7VXzL9+1WZD52Dx2ttoPwD5DWrFGpl9mFZDlmisI= -golang.org/x/tools v0.49.0/go.mod h1:SJNXV9DBKT0UbdttsQjbfJlAE/q+y36++zo3uL3N0Oo= +golang.org/x/tools v0.48.0 h1:3+hClM1aLL5mjMKm5ovokw9epgRXPuu2tILgismM6RE= +golang.org/x/tools v0.48.0/go.mod h1:08xX0orndb/F7jJxGDicx061tyd5pcMto75YMAXr6lk= gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4= gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E= google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa h1:Kjn0N0tCrDgiAFW+lGO4JZ3ck44CehvJQMAwj9QF0G8= diff --git a/internal/config/config.go b/internal/config/config.go index 4efc442..31d7d1c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -52,7 +52,6 @@ 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 9fbdba3..9365034 100644 --- a/internal/config/env.go +++ b/internal/config/env.go @@ -7,20 +7,12 @@ 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/sshproxy/README.md b/internal/sshproxy/README.md deleted file mode 100644 index cd4ee9a..0000000 --- a/internal/sshproxy/README.md +++ /dev/null @@ -1,53 +0,0 @@ -# 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. The module uses sshpiper's crypto replacement for transport support. - -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_deny_cidrs` uses Iron's existing dial-time IP guard; -omission blocks loopback and cloud metadata. Explicit overrides are intended -for trusted operator configuration, including loopback-only integration tests. - -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. Responses are limited to 1 MiB; errors never expose their -bodies or credential values. Redirects are not followed. - -The paired public key is checked before authorization or any upstream dial. -Exec and SFTP channels are supported; forwarding is blocked. Global requests -are refused locally, and no-reply environment/end-of-write requests are discarded. -Other unsupported channel capabilities terminate the connection. 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. 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 deleted file mode 100644 index 1261cca..0000000 --- a/internal/sshproxy/config.go +++ /dev/null @@ -1,136 +0,0 @@ -// Package sshproxy implements an SSH credential boundary within iron-proxy. -package sshproxy - -import ( - "bytes" - "context" - "encoding/json" - "errors" - "io" - "log/slog" - "net" - "net/http" - "os" - "time" - - "github.com/ironsh/iron-proxy/internal/dnsguard" - "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"` -} - -// 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() - if resp.StatusCode != http.StatusOK { - return nil, errors.New("SSH access denied") - } - // 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. -func LoadFromNode(ctx context.Context, node yaml.Node, 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"` - DenyCIDRs []string `yaml:"upstream_deny_cidrs"` - } - 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") - } - if cfg.DenyCIDRs == nil { - cfg.DenyCIDRs = dnsguard.DefaultDenyCIDRs - } - 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), DenyCIDRs: cfg.DenyCIDRs, Logger: logger} - return server, listener, nil -} diff --git a/internal/sshproxy/policy.go b/internal/sshproxy/policy.go deleted file mode 100644 index 081c5a1..0000000 --- a/internal/sshproxy/policy.go +++ /dev/null @@ -1,85 +0,0 @@ -package sshproxy - -import ( - "errors" - "golang.org/x/crypto/ssh" -) - -var errCapabilityDenied = errors.New("SSH capability denied") - -func downstreamPolicy(packet []byte) (ssh.PipePacketHookMethod, []byte, error) { - deny := func() (ssh.PipePacketHookMethod, []byte, error) { - return ssh.PipePacketHookTransform, nil, errCapabilityDenied - } - if len(packet) == 0 { - return deny() - } - switch packet[0] { - case 80: - return refuseGlobalRequest(packet) - case 90: - var open struct { - Type string - Sender uint32 - Window uint32 - MaxPacket uint32 - Extra []byte `ssh:"rest"` - } - if ssh.Unmarshal(packet[1:], &open) != nil || open.Type != "session" { - return deny() - } - case 98: - var req struct { - Channel uint32 - Type string - WantReply bool - Payload []byte `ssh:"rest"` - } - if ssh.Unmarshal(packet[1:], &req) != nil { - return deny() - } - switch req.Type { - case "exec", "signal": - case "env", "eow@openssh.com": - if req.WantReply { - return deny() - } - // Ignore client bookkeeping without exposing environment injection. - return ssh.PipePacketHookTransform, nil, nil - case "subsystem": - var sub struct{ Name string } - if ssh.Unmarshal(req.Payload, &sub) != nil || sub.Name != "sftp" { - return deny() - } - default: - return deny() - } - } - return ssh.PipePacketHookTransform, packet, nil -} - -func upstreamPolicy(packet []byte) (ssh.PipePacketHookMethod, []byte, error) { - if len(packet) == 0 || packet[0] == 90 { - return ssh.PipePacketHookTransform, nil, errCapabilityDenied - } - if packet[0] == 80 { - return refuseGlobalRequest(packet) - } - return ssh.PipePacketHookTransform, packet, nil -} - -// Refuse globals locally, including forwarding and OpenSSH keepalives. -func refuseGlobalRequest(packet []byte) (ssh.PipePacketHookMethod, []byte, error) { - var req struct { - Type string - WantReply bool - Payload []byte `ssh:"rest"` - } - if ssh.Unmarshal(packet[1:], &req) != nil { - return ssh.PipePacketHookTransform, nil, errCapabilityDenied - } - if req.WantReply { - return ssh.PipePacketHookReply, []byte{82}, nil - } - return ssh.PipePacketHookTransform, nil, nil -} diff --git a/internal/sshproxy/server.go b/internal/sshproxy/server.go deleted file mode 100644 index 75d3333..0000000 --- a/internal/sshproxy/server.go +++ /dev/null @@ -1,262 +0,0 @@ -package sshproxy - -import ( - "bytes" - "context" - "crypto/x509" - "encoding/base64" - "errors" - "log/slog" - "net" - "net/url" - "reflect" - "strings" - "sync" - "time" - - "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 - DenyCIDRs []string - TLSRootCAs *x509.CertPool - Logger *slog.Logger - RenewalInterval time.Duration - HandshakeTimeout time.Duration -} - -func (s *Server) Serve(parent context.Context, listener net.Listener) error { - if s.HostKey == nil || s.ClientPublicKey == nil || s.Authorize == 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() - action, reason := "deny", "authentication_failed" - var route, principal, grantID string - defer func() { - rejected := "" - if action == "deny" { - rejected = "ssh_authorization" - if reason == "capability_denied" { - rejected = "ssh_policy" - } - } - s.Logger.Info("ssh_connection", "action", action, "reason", reason, "route", route, "principal", principal, "grant_id", grantID, "duration_ms", time.Since(started).Milliseconds(), "request_transforms", []string{"ssh_authorization"}, "rejected_by", rejected) - }() - 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 approved *Grant - var auth Authorization - var upstream net.Conn - defer func() { - if upstream != nil { - _ = upstream.Close() - } - }() - config := &ssh.PiperConfig{MaxAuthTries: 1} - config.PublicKeyCallback = func(meta ssh.ConnMetadata, key ssh.PublicKey, _ ssh.ChallengeContext) (*ssh.Upstream, error) { - if !bytes.Equal(key.Marshal(), s.ClientPublicKey.Marshal()) { - return nil, errors.New("SSH access denied") - } - parsedRoute, destination, err := parseRoute(meta.User()) - if err != nil { - return nil, err - } - route = parsedRoute - auth = Authorization{Route: route, Destination: destination, PublicKey: string(bytes.TrimSpace(ssh.MarshalAuthorizedKey(key)))} - grant, err := s.Authorize(handshakeCtx, auth) - if err != nil { - return nil, errors.New("SSH access denied") - } - if err = validateGrant(grant); err != nil { - return nil, err - } - credential, err := signer(handshakeCtx, grant.PrivateKey, s.Logger) - if err != nil { - return nil, err - } - var expected ssh.PublicKey - if !grant.TLSIdentity { - expected, _, _, _, err = ssh.ParseAuthorizedKey([]byte(grant.HostKey)) - if err != nil { - return nil, errors.New("invalid upstream host identity") - } - } - if upstream != nil { - _ = upstream.Close() - } - connected, err := transport(handshakeCtx, grant.Destination, s.DenyCIDRs, s.TLSRootCAs) - if err != nil { - return nil, errors.New("SSH upstream unavailable") - } - upstream = connected - // Capture this connection, not the mutable upstream variable. Revocation - // must interrupt blocked writes as well as downstream reads. - context.AfterFunc(ctx, func() { _ = connected.Close() }) - if err = upstream.SetDeadline(handshakeDeadline); err != nil { - return nil, err - } - var algorithms []string - if expected != nil { - algorithms = []string{expected.Type()} - if expected.Type() == ssh.KeyAlgoRSA { - algorithms = []string{ssh.KeyAlgoRSASHA512, ssh.KeyAlgoRSASHA256} - } - } - approved = grant - principal = grant.Principal - grantID = grant.ID - return &ssh.Upstream{Conn: upstream, Address: grant.Destination, ClientConfig: ssh.ClientConfig{User: grant.Username, HostKeyAlgorithms: algorithms, Auth: []ssh.AuthMethod{ssh.PublicKeys(credential)}, 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 - }}}, nil - } - config.AddHostKey(s.HostKey) - pipe, err := ssh.NewSSHPiperConn(conn, config) - if err != nil { - return - } - defer pipe.Close() - if approved == nil { - return - } - if conn.SetDeadline(time.Time{}) != nil || upstream.SetDeadline(time.Time{}) != nil { - return - } - handshakeCancel() - action, reason = "allow", "connection_closed" - leaseCtx, leaseCancel := context.WithCancel(ctx) - defer leaseCancel() - done := make(chan struct{}) - go func() { defer close(done); s.renew(leaseCtx, cancel, auth, approved) }() - if err := pipe.WaitWithHook(upstreamPolicy, downstreamPolicy); err != nil { - reason = "connection_terminated" - if errors.Is(err, errCapabilityDenied) { - action, reason = "deny", "capability_denied" - } - } - leaseCancel() - <-done -} - -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 -} - -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) { - 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/transport.go b/internal/sshproxy/transport.go deleted file mode 100644 index f51cbb8..0000000 --- a/internal/sshproxy/transport.go +++ /dev/null @@ -1,93 +0,0 @@ -package sshproxy - -import ( - "bytes" - "context" - "crypto/hmac" - "crypto/rand" - "crypto/sha256" - "crypto/tls" - "crypto/x509" - "encoding/base64" - "encoding/hex" - "encoding/json" - "errors" - "io" - "net" - "net/url" - "time" - - "github.com/gobwas/ws" - "github.com/ironsh/iron-proxy/internal/dnsguard" -) - -func transport(ctx context.Context, target string, deny []string, 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") - } - guard, err := dnsguard.New(deny) - if err != nil { - return nil, errors.New("invalid upstream deny CIDRs") - } - 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, errors.New("SSH WebSocket connection failed") - } - 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 deleted file mode 100644 index 602e8b5..0000000 --- a/internal/sshproxy/websocket.go +++ /dev/null @@ -1,86 +0,0 @@ -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 -} From 3d96ce8dd2ca093051b75ccb0c9e5e1db52d1c04 Mon Sep 17 00:00:00 2001 From: Matthew Slipper Date: Wed, 4 Nov 2026 13:54:48 -0700 Subject: [PATCH 8/8] feat(sshproxy): add opt-in SSH credential proxying on stock x/crypto (#300) * Add opt-in SSH credential proxying * fix(sshproxy): drop sshpiper fork, authorize only verified keys Rebuild the SSH listener on stock golang.org/x/crypto/ssh: - authorization, secret fetch, and upstream login run in VerifiedPublicKeyCallback, after the client proves key possession - typed default-deny channel/request policy; refusals no longer drop the connection - auth decisions and session requests emit the shared JSON/OTEL audit entries (mode ssh) with rejected_by and grant annotations - upstream dials use the proxy-wide upstream_deny_cidrs guard * fix(sshproxy): interrupt upstream SSH handshake on cancellation ssh.NewClientConn ignores context, so shutdown or revocation during the upstream login waited for the handshake deadline. * test(sshproxy): port SSH proxy tests from #298 Adapt the stacked test suite to the stock x/crypto implementation: exec/SFTP, revocation, wstunnel v11 and TLS identity, keepalives under load, handshake deadlines and cancellation, grant validation, and config loading. Packet-hook policy tests are replaced by a typed policy table. Co-authored-by: Jonathan Goodwin * fix(sshproxy): satisfy errcheck on Close calls * fix(sshproxy): detect dead peers, relay EOF, classify authorizer outages - probe both peers with keepalive@openssh.com and end the session after three silent intervals, so a dropped client cannot hold upstream open - relay upstream EOF once stdout and stderr drain, before channel close - treat only 401/403 from the authorizer as denials; other statuses are audited as errors - drain upstream requests when the downstream channel accept fails --------- Co-authored-by: Jonathan Goodwin --- AGENTS.md | 4 +- README.md | 7 + cmd/iron-proxy/main.go | 41 +- go.mod | 7 +- go.sum | 11 + internal/config/config.go | 1 + internal/config/env.go | 8 + internal/config/env_test.go | 15 + internal/sshproxy/README.md | 55 ++ internal/sshproxy/config.go | 143 ++++ internal/sshproxy/config_test.go | 58 ++ internal/sshproxy/lifecycle_test.go | 123 +++ internal/sshproxy/policy.go | 42 + internal/sshproxy/policy_test.go | 37 + internal/sshproxy/server.go | 451 +++++++++++ internal/sshproxy/server_test.go | 765 ++++++++++++++++++ internal/sshproxy/transport.go | 90 +++ internal/sshproxy/websocket.go | 86 ++ .../sshproxy/wstunnel_integration_test.go | 42 + internal/transform/transform.go | 6 + 20 files changed, 1987 insertions(+), 5 deletions(-) create mode 100644 internal/sshproxy/README.md create mode 100644 internal/sshproxy/config.go create mode 100644 internal/sshproxy/config_test.go create mode 100644 internal/sshproxy/lifecycle_test.go create mode 100644 internal/sshproxy/policy.go create mode 100644 internal/sshproxy/policy_test.go create mode 100644 internal/sshproxy/server.go create mode 100644 internal/sshproxy/server_test.go create mode 100644 internal/sshproxy/transport.go create mode 100644 internal/sshproxy/websocket.go create mode 100644 internal/sshproxy/wstunnel_integration_test.go 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 23e99b0..10bfce5 100644 --- a/README.md +++ b/README.md @@ -1103,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/go.mod b/go.mod index d3777b7..f585d9e 100644 --- a/go.mod +++ b/go.mod @@ -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/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/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/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" }