From 291d253c81217db69059db76c0087ff41621a331 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 11 Nov 2026 22:53:33 -0500 Subject: [PATCH 01/11] feat(pam): output masking --- packages/api/model.go | 1 + packages/gateway-v2/capabilities.go | 2 + packages/gateway-v2/gateway.go | 1 + packages/pam/handlers/postgres.go | 18 ++ packages/pam/handlers/postgres_masking.go | 194 ++++++++++++++++++++++ packages/pam/handlers/ssh/proxy.go | 36 +++- packages/pam/pam-proxy.go | 9 + 7 files changed, 258 insertions(+), 3 deletions(-) create mode 100644 packages/pam/handlers/postgres_masking.go diff --git a/packages/api/model.go b/packages/api/model.go index 8b317be82..e156da424 100644 --- a/packages/api/model.go +++ b/packages/api/model.go @@ -1106,6 +1106,7 @@ type PAMSessionLogMaskingConfig struct { type PAMPolicyRules struct { CommandBlocking *PAMPolicyRuleConfig `json:"command-blocking,omitempty"` + OutputMasking *PAMPolicyRuleConfig `json:"output-masking,omitempty"` SessionLogMasking *PAMSessionLogMaskingConfig `json:"session-log-masking,omitempty"` } diff --git a/packages/gateway-v2/capabilities.go b/packages/gateway-v2/capabilities.go index 8b2b73825..22b645d6a 100644 --- a/packages/gateway-v2/capabilities.go +++ b/packages/gateway-v2/capabilities.go @@ -8,6 +8,8 @@ const CapabilitySupportedAccountTypes = "supported_account_types" const CapabilityClickhouseNativeProtocol = "clickhouseNativeProtocol" +const CapabilityOutputMasking = "outputMasking" + const CapabilityCertificateScan = "certificateScan" const CapabilityWindowsCertificateScan = "windowsCertificateScan" diff --git a/packages/gateway-v2/gateway.go b/packages/gateway-v2/gateway.go index f48b9596f..23cc04ea1 100644 --- a/packages/gateway-v2/gateway.go +++ b/packages/gateway-v2/gateway.go @@ -471,6 +471,7 @@ func (g *Gateway) registerHeartBeat(ctx context.Context, errCh chan error) { } capabilities[CapabilitySupportedAccountTypes] = pam.GetSupportedResourceTypes() capabilities[CapabilityClickhouseNativeProtocol] = true + capabilities[CapabilityOutputMasking] = true capabilities[CapabilityCertificateScan] = true capabilities[CapabilityWindowsCertificateScan] = true req := api.GatewayHeartbeatRequest{Capabilities: capabilities} diff --git a/packages/pam/handlers/postgres.go b/packages/pam/handlers/postgres.go index d38fde81f..4aa52427d 100644 --- a/packages/pam/handlers/postgres.go +++ b/packages/pam/handlers/postgres.go @@ -17,6 +17,7 @@ import ( "time" session "github.com/Infisical/infisical-merge/packages/pam/session" + "github.com/Infisical/infisical-merge/packages/pam/session/masking" "github.com/jackc/pgx/v5/pgproto3" "github.com/rs/zerolog/log" "golang.org/x/crypto/pbkdf2" // TODO: Remove this once we update to go 1.25.1 or later where it's already in the standard library @@ -31,6 +32,7 @@ type PostgresProxyConfig struct { TLSConfig *tls.Config SessionID string SessionLogger session.SessionLogger + OutputMasker masking.Masker } type PostgresProxy struct { @@ -39,6 +41,11 @@ type PostgresProxy struct { pendingRequests map[string]*PendingRequest preparedStatements map[string]string activePortal string + + describes []describeSource + rowColumns []pgColumnFormat + discarding bool + maskingCopy bool } type PendingRequest struct { @@ -679,6 +686,11 @@ func (p *PostgresProxy) proxyClientToServer(clientBackend *pgproto3.Backend, ser p.trackClientMessage(msg) + if p.masking() { + if describe := p.trackDescribe(msg); describe != nil { + serverFrontend.Send(describe) + } + } serverFrontend.Send(msg) err = serverFrontend.Flush() if err != nil { @@ -696,6 +708,12 @@ func (p *PostgresProxy) proxyServerToClient(serverFrontend *pgproto3.Frontend, c return } + if p.masking() { + if msg = p.maskServerMessage(msg); msg == nil { + continue + } + } + p.trackServerMessage(msg) clientBackend.Send(msg) diff --git a/packages/pam/handlers/postgres_masking.go b/packages/pam/handlers/postgres_masking.go new file mode 100644 index 000000000..8a8ff973a --- /dev/null +++ b/packages/pam/handlers/postgres_masking.go @@ -0,0 +1,194 @@ +package handlers + +import ( + "bytes" + "encoding/binary" + "net/netip" + "strconv" + + "github.com/Infisical/infisical-merge/packages/pam/session/masking" + "github.com/jackc/pgx/v5/pgproto3" + "github.com/jackc/pgx/v5/pgtype" +) + +// Values of these types are plain text in both wire formats, so a match is replaced in place. +var pgTextTypes = map[uint32]bool{ + pgtype.TextOID: true, + pgtype.VarcharOID: true, + pgtype.BPCharOID: true, + pgtype.NameOID: true, + pgtype.JSONOID: true, + pgtype.JSONBOID: true, + pgtype.XMLOID: true, + pgtype.UnknownOID: true, +} + +type pgColumnFormat struct { + typeOID uint32 + binary bool +} + +// Who receives the RowDescription or NoData answering a Describe; the server answers in request order. +type describeSource int + +const ( + describeForClient describeSource = iota + describeForMasking + syncPoint +) + +func (p *PostgresProxy) masking() bool { + return p.config.OutputMasker != nil +} + +// Each Execute gets a portal Describe sent ahead of it, so its rows always follow a RowDescription. +func (p *PostgresProxy) trackDescribe(msg pgproto3.FrontendMessage) *pgproto3.Describe { + p.mutex.Lock() + defer p.mutex.Unlock() + + switch m := msg.(type) { + case *pgproto3.Describe: + p.describes = append(p.describes, describeForClient) + case *pgproto3.Execute: + p.describes = append(p.describes, describeForMasking) + return &pgproto3.Describe{ObjectType: 'P', Name: m.Portal} + case *pgproto3.Sync, *pgproto3.Query, *pgproto3.FunctionCall: + p.describes = append(p.describes, syncPoint) + } + return nil +} + +// maskServerMessage returns the message to relay, or nil to drop it. +func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3.BackendMessage { + if _, ok := msg.(*pgproto3.ReadyForQuery); ok { + p.discarding = false + p.maskingCopy = false + p.popThroughSync() + return msg + } + if p.discarding { + return nil + } + + masker := p.config.OutputMasker + switch m := msg.(type) { + case *pgproto3.RowDescription: + p.rowColumns = p.rowColumns[:0] + for _, field := range m.Fields { + p.rowColumns = append(p.rowColumns, pgColumnFormat{field.DataTypeOID, field.Format == pgtype.BinaryFormatCode}) + } + return p.answerDescribe(msg) + case *pgproto3.NoData: + p.rowColumns = p.rowColumns[:0] + return p.answerDescribe(msg) + case *pgproto3.DataRow: + for i, value := range m.Values { + if value == nil { + continue + } + var column pgColumnFormat + if i < len(p.rowColumns) { + column = p.rowColumns[i] + } + text := value + if column.binary { + text = binaryValueText(column.typeOID, value) + } + masked := masker.Mask(text) + if bytes.Equal(masked, text) { + continue + } + if pgTextTypes[column.typeOID] { + m.Values[i] = masked + } else { + // A placeholder would not decode as this column's type. + m.Values[i] = nil + } + } + case *pgproto3.CopyOutResponse: + if m.OverallFormat == 0 { + p.maskingCopy = true + return msg + } + return p.blockCopy() + case *pgproto3.CopyBothResponse: + return p.blockCopy() + case *pgproto3.CopyData: + if p.maskingCopy { + m.Data = masker.Mask(m.Data) + } + case *pgproto3.FunctionCallResponse: + if masked := masker.Mask(m.Result); !bytes.Equal(masked, m.Result) { + m.Result = nil + } + case *pgproto3.NotificationResponse: + m.Payload = masker.MaskString(m.Payload) + case *pgproto3.ErrorResponse: + maskStrings(masker, &m.Message, &m.Detail, &m.Hint, &m.Where) + case *pgproto3.NoticeResponse: + maskStrings(masker, &m.Message, &m.Detail, &m.Hint, &m.Where) + } + return msg +} + +// Binary COPY rows have no text to match, so they cannot be masked. +func (p *PostgresProxy) blockCopy() pgproto3.BackendMessage { + p.discarding = true + return &pgproto3.ErrorResponse{ + Severity: "ERROR", + Code: "42501", + Message: "Binary COPY and replication are blocked because this account masks output, use COPY in text or CSV format instead", + } +} + +// Binary integers and addresses are matched as the text a text-format client would see, so a card +// number in a bigint column or an address in an inet column is caught whichever format was asked for. +func binaryValueText(typeOID uint32, value []byte) []byte { + switch { + case typeOID == pgtype.Int8OID && len(value) == 8: + return strconv.AppendInt(nil, int64(binary.BigEndian.Uint64(value)), 10) + case typeOID == pgtype.Int4OID && len(value) == 4: + return strconv.AppendInt(nil, int64(int32(binary.BigEndian.Uint32(value))), 10) + case (typeOID == pgtype.InetOID || typeOID == pgtype.CIDROID) && len(value) > 4 && int(value[3]) == len(value)-4: + if addr, ok := netip.AddrFromSlice(value[4:]); ok { + return []byte(addr.String()) + } + } + return value +} + +func maskStrings(masker masking.Masker, fields ...*string) { + for _, field := range fields { + *field = masker.MaskString(*field) + } +} + +// Outside an extended batch a RowDescription answers a simple query, so it goes to the client. +func (p *PostgresProxy) answerDescribe(msg pgproto3.BackendMessage) pgproto3.BackendMessage { + p.mutex.Lock() + defer p.mutex.Unlock() + + if len(p.describes) == 0 || p.describes[0] == syncPoint { + return msg + } + source := p.describes[0] + p.describes = p.describes[1:] + if source == describeForMasking { + return nil + } + return msg +} + +// popThroughSync drops what the batch ending at this ReadyForQuery left unanswered, as after an error. +func (p *PostgresProxy) popThroughSync() { + p.mutex.Lock() + defer p.mutex.Unlock() + + for len(p.describes) > 0 { + source := p.describes[0] + p.describes = p.describes[1:] + if source == syncPoint { + return + } + } +} diff --git a/packages/pam/handlers/ssh/proxy.go b/packages/pam/handlers/ssh/proxy.go index 4ead932f4..9a459fb28 100644 --- a/packages/pam/handlers/ssh/proxy.go +++ b/packages/pam/handlers/ssh/proxy.go @@ -12,6 +12,7 @@ import ( "time" "github.com/Infisical/infisical-merge/packages/pam/session" + "github.com/Infisical/infisical-merge/packages/pam/session/masking" "github.com/Infisical/infisical-merge/packages/util" "github.com/rs/zerolog/log" "golang.org/x/crypto/ssh" @@ -28,6 +29,7 @@ type SSHProxyConfig struct { SessionID string SessionLogger session.SessionLogger BlockedCommandPatterns []*regexp.Regexp // Regex patterns for command blocking (nil = no blocking) + OutputMasker masking.Masker // Applied to output before the user sees it (nil = no masking) OnActivity func() // Called when channel data flows } @@ -255,6 +257,12 @@ func (p *SSHProxy) handleChannel(ctx context.Context, newChannel ssh.NewChannel, Str("channelType", channelType). Msg("← CLIENT new channel request") + // A forwarded connection carries another protocol, which masking would corrupt rather than redact. + if p.config.OutputMasker != nil && channelType != "session" { + newChannel.Reject(ssh.Prohibited, "port forwarding is blocked because this account masks output") + return + } + // Open corresponding channel on server serverChannel, serverRequests, err := serverConn.OpenChannel(channelType, newChannel.ExtraData()) if err != nil { @@ -395,6 +403,10 @@ func (p *SSHProxy) handleChannelRequests(requests <-chan *ssh.Request, targetCha // Determine the type of operation isSCP := strings.HasPrefix(command, "scp ") + if isSCP && p.config.OutputMasker != nil { + p.rejectUnmaskableTransfer(req, sessionID) + continue + } chState.mutex.Lock() if isSCP { // Mark this channel as binary so we don't log the raw file data @@ -460,6 +472,11 @@ func (p *SSHProxy) handleChannelRequests(requests <-chan *ssh.Request, targetCha Str("subsystem", subsystem). Msg("SSH subsystem requested") + if subsystem == "sftp" && p.config.OutputMasker != nil { + p.rejectUnmaskableTransfer(req, sessionID) + continue + } + // Log SFTP sessions and set up SFTP parser for file operation logging if subsystem == "sftp" { chState.mutex.Lock() @@ -544,6 +561,11 @@ func (p *SSHProxy) proxyData(src io.Reader, dst io.Writer, direction string, ses channelType := chState.channelType chState.mutex.Unlock() + data := buf[:n] + if !logInput && !isBinary && p.config.OutputMasker != nil { + data = p.config.OutputMasker.Mask(data) + } + if isBinary && sftpParser != nil && logInput { // Parse SFTP packets from client->server direction to extract file operations operations := sftpParser.Parse(buf[:n]) @@ -570,16 +592,16 @@ func (p *SSHProxy) proxyData(src io.Reader, dst io.Writer, direction string, ses if logInput { p.bufferInput(buf[:n], sessionID, channelType, chState) } else { - p.bufferOutput(buf[:n], sessionID, channelType, chState) + p.bufferOutput(data, sessionID, channelType, chState) } } // Write to destination - written, writeErr := dst.Write(buf[:n]) + written, writeErr := dst.Write(data) if writeErr != nil { return writeErr } - if written != n { + if written != len(data) { return io.ErrShortWrite } } @@ -738,6 +760,14 @@ func (p *SSHProxy) logOutputLines(sessionID string, chState *channelState, lines } } +// File transfers carry raw bytes that masking would corrupt rather than redact. +func (p *SSHProxy) rejectUnmaskableTransfer(req *ssh.Request, sessionID string) { + log.Warn().Str("sessionID", sessionID).Str("requestType", req.Type).Msg("Blocked file transfer on an account with output masking") + if req.WantReply { + req.Reply(false, nil) + } +} + // matchBlockedCommand checks if a command matches any blocked pattern. func (p *SSHProxy) matchBlockedCommand(command string) bool { command = strings.TrimSpace(command) diff --git a/packages/pam/pam-proxy.go b/packages/pam/pam-proxy.go index d2156b6ae..0cb95609f 100644 --- a/packages/pam/pam-proxy.go +++ b/packages/pam/pam-proxy.go @@ -271,6 +271,13 @@ func HandlePAMProxy(ctx context.Context, conn *tls.Conn, pamConfig *GatewayPAMCo ) } + var outputMasker masking.Masker + if credentials.PolicyRules != nil { + if patterns := compilePolicyPatterns(rulePatterns(credentials.PolicyRules.OutputMasking), pamConfig.SessionId, "output-masking"); len(patterns) > 0 { + outputMasker = masking.New(patterns, false, nil, pamConfig.SessionId) + } + } + sessionLogger, err := session.NewSessionLogger(pamConfig.SessionId, encryptionKey, pamConfig.ExpiryTime, pamConfig.ResourceType, masker) if err != nil { return fmt.Errorf("failed to create session logger: %w", err) @@ -334,6 +341,7 @@ func HandlePAMProxy(ctx context.Context, conn *tls.Conn, pamConfig *GatewayPAMCo TLSConfig: tlsConfig, SessionID: pamConfig.SessionId, SessionLogger: sessionLogger, + OutputMasker: outputMasker, } proxy := handlers.NewPostgresProxy(proxyConfig) log.Info(). @@ -420,6 +428,7 @@ func HandlePAMProxy(ctx context.Context, conn *tls.Conn, pamConfig *GatewayPAMCo SessionID: pamConfig.SessionId, SessionLogger: sessionLogger, BlockedCommandPatterns: blockedCommandPatterns, + OutputMasker: outputMasker, OnActivity: pamConfig.OnActivity, } proxy := ssh.NewSSHProxy(sshConfig) From 002b83f7aa50c75c9adc8c675eba9ead39b8fe11 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 11 Nov 2026 23:25:29 -0500 Subject: [PATCH 02/11] address bot reviews + test --- packages/pam/handlers/postgres.go | 15 +- packages/pam/handlers/postgres_masking.go | 80 ++++--- .../pam/handlers/postgres_masking_test.go | 210 ++++++++++++++++++ .../pam/handlers/ssh/output_masking_test.go | 74 ++++++ packages/pam/handlers/ssh/proxy.go | 97 +++++++- packages/pam/pam-proxy.go | 13 +- 6 files changed, 441 insertions(+), 48 deletions(-) create mode 100644 packages/pam/handlers/postgres_masking_test.go create mode 100644 packages/pam/handlers/ssh/output_masking_test.go diff --git a/packages/pam/handlers/postgres.go b/packages/pam/handlers/postgres.go index 4aa52427d..03db50d7c 100644 --- a/packages/pam/handlers/postgres.go +++ b/packages/pam/handlers/postgres.go @@ -8,6 +8,7 @@ import ( "crypto/tls" "encoding/base64" "encoding/hex" + "errors" "fmt" "io" "net" @@ -19,6 +20,7 @@ import ( session "github.com/Infisical/infisical-merge/packages/pam/session" "github.com/Infisical/infisical-merge/packages/pam/session/masking" "github.com/jackc/pgx/v5/pgproto3" + "github.com/jackc/pgx/v5/pgtype" "github.com/rs/zerolog/log" "golang.org/x/crypto/pbkdf2" // TODO: Remove this once we update to go 1.25.1 or later where it's already in the standard library ) @@ -42,10 +44,11 @@ type PostgresProxy struct { preparedStatements map[string]string activePortal string - describes []describeSource - rowColumns []pgColumnFormat - discarding bool - maskingCopy bool + describes []describeSource + rowColumns []pgColumnFormat + typeMap *pgtype.Map + discarding bool + closeAfterReply bool } type PendingRequest struct { @@ -722,6 +725,10 @@ func (p *PostgresProxy) proxyServerToClient(serverFrontend *pgproto3.Frontend, c errChan <- err return } + if p.closeAfterReply { + errChan <- errors.New("replication refused because the account masks output") + return + } } } diff --git a/packages/pam/handlers/postgres_masking.go b/packages/pam/handlers/postgres_masking.go index 8a8ff973a..799e05019 100644 --- a/packages/pam/handlers/postgres_masking.go +++ b/packages/pam/handlers/postgres_masking.go @@ -2,9 +2,7 @@ package handlers import ( "bytes" - "encoding/binary" - "net/netip" - "strconv" + "encoding/json" "github.com/Infisical/infisical-merge/packages/pam/session/masking" "github.com/jackc/pgx/v5/pgproto3" @@ -62,7 +60,6 @@ func (p *PostgresProxy) trackDescribe(msg pgproto3.FrontendMessage) *pgproto3.De func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3.BackendMessage { if _, ok := msg.(*pgproto3.ReadyForQuery); ok { p.discarding = false - p.maskingCopy = false p.popThroughSync() return msg } @@ -90,15 +87,16 @@ func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3. if i < len(p.rowColumns) { column = p.rowColumns[i] } + isText := pgTextTypes[column.typeOID] text := value - if column.binary { - text = binaryValueText(column.typeOID, value) + if column.binary && !isText { + text = p.binaryValueText(column.typeOID, value) } masked := masker.Mask(text) if bytes.Equal(masked, text) { continue } - if pgTextTypes[column.typeOID] { + if isText && keepsValidJSON(column, masked) { m.Values[i] = masked } else { // A placeholder would not decode as this column's type. @@ -106,16 +104,21 @@ func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3. } } case *pgproto3.CopyOutResponse: - if m.OverallFormat == 0 { - p.maskingCopy = true - return msg + // COPY escapes its fields (CSV doubles quotes, text escapes control characters), so a pattern can + // miss a value there that it matches in a row. + p.discarding = true + return &pgproto3.ErrorResponse{ + Severity: "ERROR", + Code: "42501", + Message: "COPY TO STDOUT is blocked because this account masks output, use SELECT instead", } - return p.blockCopy() case *pgproto3.CopyBothResponse: - return p.blockCopy() - case *pgproto3.CopyData: - if p.maskingCopy { - m.Data = masker.Mask(m.Data) + // A replication stream never reaches ReadyForQuery, so only ending the connection stops it. + p.closeAfterReply = true + return &pgproto3.ErrorResponse{ + Severity: "FATAL", + Code: "42501", + Message: "Replication is blocked because this account masks output", } case *pgproto3.FunctionCallResponse: if masked := masker.Mask(m.Result); !bytes.Equal(masked, m.Result) { @@ -131,30 +134,39 @@ func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3. return msg } -// Binary COPY rows have no text to match, so they cannot be masked. -func (p *PostgresProxy) blockCopy() pgproto3.BackendMessage { - p.discarding = true - return &pgproto3.ErrorResponse{ - Severity: "ERROR", - Code: "42501", - Message: "Binary COPY and replication are blocked because this account masks output, use COPY in text or CSV format instead", +// A binary value is matched as the text a text-format client would see, so a card number in a numeric +// column is caught whichever format was asked for. A type pgx can't decode is matched as its raw bytes. +func (p *PostgresProxy) binaryValueText(typeOID uint32, value []byte) []byte { + if p.typeMap == nil { + p.typeMap = pgtype.NewMap() } + dataType, ok := p.typeMap.TypeForOID(typeOID) + if !ok { + return value + } + decoded, err := dataType.Codec.DecodeValue(p.typeMap, typeOID, pgtype.BinaryFormatCode, value) + if err != nil || decoded == nil { + return value + } + text, err := p.typeMap.Encode(typeOID, pgtype.TextFormatCode, decoded, nil) + if err != nil { + return value + } + return text } -// Binary integers and addresses are matched as the text a text-format client would see, so a card -// number in a bigint column or an address in an inet column is caught whichever format was asked for. -func binaryValueText(typeOID uint32, value []byte) []byte { - switch { - case typeOID == pgtype.Int8OID && len(value) == 8: - return strconv.AppendInt(nil, int64(binary.BigEndian.Uint64(value)), 10) - case typeOID == pgtype.Int4OID && len(value) == 4: - return strconv.AppendInt(nil, int64(int32(binary.BigEndian.Uint32(value))), 10) - case (typeOID == pgtype.InetOID || typeOID == pgtype.CIDROID) && len(value) > 4 && int(value[3]) == len(value)-4: - if addr, ok := netip.AddrFromSlice(value[4:]); ok { - return []byte(addr.String()) +// A match inside a JSON number or key leaves a placeholder the client can't parse. +func keepsValidJSON(column pgColumnFormat, masked []byte) bool { + switch column.typeOID { + case pgtype.JSONOID: + return json.Valid(masked) + case pgtype.JSONBOID: + if column.binary && len(masked) > 0 { + masked = masked[1:] // binary jsonb leads with a version byte } + return json.Valid(masked) } - return value + return true } func maskStrings(masker masking.Masker, fields ...*string) { diff --git a/packages/pam/handlers/postgres_masking_test.go b/packages/pam/handlers/postgres_masking_test.go new file mode 100644 index 000000000..6ffb7c34b --- /dev/null +++ b/packages/pam/handlers/postgres_masking_test.go @@ -0,0 +1,210 @@ +package handlers + +import ( + "regexp" + "testing" + + "github.com/Infisical/infisical-merge/packages/pam/session/masking" + "github.com/jackc/pgx/v5/pgproto3" + "github.com/jackc/pgx/v5/pgtype" +) + +func newMaskingProxy() *PostgresProxy { + return NewPostgresProxy(PostgresProxyConfig{ + OutputMasker: masking.New([]*regexp.Regexp{regexp.MustCompile(`secret`), regexp.MustCompile(`\b\d{16}\b`)}, false, nil, "test"), + }) +} + +// relay feeds the client messages, then the server's replies, and returns what reaches the client. +func relay(p *PostgresProxy, client []pgproto3.FrontendMessage, server []pgproto3.BackendMessage) (int, []pgproto3.BackendMessage) { + injected := 0 + for _, msg := range client { + if p.trackDescribe(msg) != nil { + injected++ + } + } + var forwarded []pgproto3.BackendMessage + for _, msg := range server { + if out := p.maskServerMessage(msg); out != nil { + forwarded = append(forwarded, out) + } + } + return injected, forwarded +} + +func rowDescription(format int16, oids ...uint32) *pgproto3.RowDescription { + desc := &pgproto3.RowDescription{} + for _, oid := range oids { + desc.Fields = append(desc.Fields, pgproto3.FieldDescription{Name: []byte("c"), DataTypeOID: oid, Format: format}) + } + return desc +} + +func dataRow(values ...[]byte) *pgproto3.DataRow { + return &pgproto3.DataRow{Values: values} +} + +func messageTypes(msgs []pgproto3.BackendMessage) []string { + var types []string + for _, msg := range msgs { + switch msg.(type) { + case *pgproto3.ParameterDescription: + types = append(types, "ParameterDescription") + case *pgproto3.RowDescription: + types = append(types, "RowDescription") + case *pgproto3.DataRow: + types = append(types, "DataRow") + case *pgproto3.PortalSuspended: + types = append(types, "PortalSuspended") + case *pgproto3.CommandComplete: + types = append(types, "CommandComplete") + case *pgproto3.ErrorResponse: + types = append(types, "ErrorResponse") + case *pgproto3.ReadyForQuery: + types = append(types, "ReadyForQuery") + default: + types = append(types, "other") + } + } + return types +} + +func assertTypes(t *testing.T, got []pgproto3.BackendMessage, want ...string) { + t.Helper() + gotTypes := messageTypes(got) + if len(gotTypes) != len(want) { + t.Fatalf("forwarded %v, want %v", gotTypes, want) + } + for i := range want { + if gotTypes[i] != want[i] { + t.Fatalf("forwarded %v, want %v", gotTypes, want) + } + } +} + +func TestMaskingDropsOnlyTheInsertedDescribe(t *testing.T) { + p := newMaskingProxy() + injected, forwarded := relay(p, + []pgproto3.FrontendMessage{&pgproto3.Describe{ObjectType: 'S'}, &pgproto3.Execute{}, &pgproto3.Sync{}}, + []pgproto3.BackendMessage{ + &pgproto3.ParameterDescription{}, + rowDescription(0, pgtype.TextOID), + rowDescription(0, pgtype.TextOID), + dataRow([]byte("a secret")), + &pgproto3.CommandComplete{CommandTag: []byte("SELECT 1")}, + &pgproto3.ReadyForQuery{TxStatus: 'I'}, + }) + if injected != 1 { + t.Fatalf("injected %d Describes, want 1", injected) + } + assertTypes(t, forwarded, "ParameterDescription", "RowDescription", "DataRow", "CommandComplete", "ReadyForQuery") + if got := string(forwarded[2].(*pgproto3.DataRow).Values[0]); got != "a [MASKED]" { + t.Fatalf("row value %q, want %q", got, "a [MASKED]") + } +} + +func TestMaskingClearsABatchThatFailedBeforeItsReply(t *testing.T) { + p := newMaskingProxy() + _, forwarded := relay(p, + []pgproto3.FrontendMessage{ + &pgproto3.Parse{}, &pgproto3.Bind{}, &pgproto3.Describe{ObjectType: 'P'}, &pgproto3.Execute{}, &pgproto3.Sync{}, + &pgproto3.Bind{}, &pgproto3.Execute{}, &pgproto3.Sync{}, + }, + []pgproto3.BackendMessage{ + &pgproto3.ErrorResponse{Severity: "ERROR", Message: "syntax error"}, + &pgproto3.ReadyForQuery{TxStatus: 'I'}, + rowDescription(0, pgtype.TextOID), + dataRow([]byte("secret")), + &pgproto3.CommandComplete{CommandTag: []byte("SELECT 1")}, + &pgproto3.ReadyForQuery{TxStatus: 'I'}, + }) + assertTypes(t, forwarded, "ErrorResponse", "ReadyForQuery", "DataRow", "CommandComplete", "ReadyForQuery") +} + +func TestMaskingDescribesAResumedPortalAgain(t *testing.T) { + p := newMaskingProxy() + injected, forwarded := relay(p, + []pgproto3.FrontendMessage{&pgproto3.Execute{MaxRows: 1}, &pgproto3.Execute{MaxRows: 1}, &pgproto3.Sync{}}, + []pgproto3.BackendMessage{ + rowDescription(0, pgtype.TextOID), + dataRow([]byte("one secret")), + &pgproto3.PortalSuspended{}, + rowDescription(0, pgtype.TextOID), + dataRow([]byte("two secret")), + &pgproto3.CommandComplete{CommandTag: []byte("SELECT 2")}, + &pgproto3.ReadyForQuery{TxStatus: 'I'}, + }) + if injected != 2 { + t.Fatalf("injected %d Describes, want 2", injected) + } + assertTypes(t, forwarded, "DataRow", "PortalSuspended", "DataRow", "CommandComplete", "ReadyForQuery") +} + +func TestMaskingForwardsASimpleQueryRowDescription(t *testing.T) { + p := newMaskingProxy() + _, forwarded := relay(p, + []pgproto3.FrontendMessage{&pgproto3.Query{String: "SELECT note FROM notes"}}, + []pgproto3.BackendMessage{ + rowDescription(0, pgtype.TextOID), + dataRow([]byte("secret")), + &pgproto3.CommandComplete{CommandTag: []byte("SELECT 1")}, + &pgproto3.ReadyForQuery{TxStatus: 'I'}, + }) + assertTypes(t, forwarded, "RowDescription", "DataRow", "CommandComplete", "ReadyForQuery") +} + +func TestMaskingMatchesBinaryValuesAsText(t *testing.T) { + typeMap := pgtype.NewMap() + var card pgtype.Numeric + if err := card.Scan("4111111111111111"); err != nil { + t.Fatal(err) + } + binaryCard, err := typeMap.Encode(pgtype.NumericOID, pgtype.BinaryFormatCode, card, nil) + if err != nil { + t.Fatal(err) + } + + p := newMaskingProxy() + _, forwarded := relay(p, + []pgproto3.FrontendMessage{&pgproto3.Execute{}, &pgproto3.Sync{}}, + []pgproto3.BackendMessage{rowDescription(1, pgtype.NumericOID), dataRow(binaryCard)}) + if value := forwarded[0].(*pgproto3.DataRow).Values[0]; value != nil { + t.Fatalf("binary numeric card number was forwarded as %v, want NULL", value) + } +} + +func TestMaskingKeepsJSONParseable(t *testing.T) { + p := newMaskingProxy() + _, forwarded := relay(p, + []pgproto3.FrontendMessage{&pgproto3.Query{String: "SELECT doc FROM docs"}}, + []pgproto3.BackendMessage{ + rowDescription(0, pgtype.JSONOID, pgtype.JSONOID), + dataRow([]byte(`{"note":"secret"}`), []byte(`{"card":4111111111111111}`)), + }) + row := forwarded[1].(*pgproto3.DataRow) + if got := string(row.Values[0]); got != `{"note":"[MASKED]"}` { + t.Fatalf("string match gave %q", got) + } + if row.Values[1] != nil { + t.Fatalf("number match gave %q, want NULL", row.Values[1]) + } +} + +func TestMaskingRefusesCopyAndReplication(t *testing.T) { + p := newMaskingProxy() + _, forwarded := relay(p, + []pgproto3.FrontendMessage{&pgproto3.Query{String: "COPY notes TO STDOUT"}}, + []pgproto3.BackendMessage{ + &pgproto3.CopyOutResponse{}, + &pgproto3.CopyData{Data: []byte("secret\n")}, + &pgproto3.CopyDone{}, + &pgproto3.CommandComplete{CommandTag: []byte("COPY 1")}, + &pgproto3.ReadyForQuery{TxStatus: 'I'}, + }) + assertTypes(t, forwarded, "ErrorResponse", "ReadyForQuery") + + _, forwarded = relay(p, nil, []pgproto3.BackendMessage{&pgproto3.CopyBothResponse{}}) + if err, ok := forwarded[0].(*pgproto3.ErrorResponse); !ok || err.Severity != "FATAL" || !p.closeAfterReply { + t.Fatalf("replication reply %+v, closeAfterReply %v", forwarded[0], p.closeAfterReply) + } +} diff --git a/packages/pam/handlers/ssh/output_masking_test.go b/packages/pam/handlers/ssh/output_masking_test.go new file mode 100644 index 000000000..3f193def8 --- /dev/null +++ b/packages/pam/handlers/ssh/output_masking_test.go @@ -0,0 +1,74 @@ +package ssh + +import ( + "bytes" + "io" + "regexp" + "sync" + "testing" + "time" + + "github.com/Infisical/infisical-merge/packages/pam/session/masking" +) + +type lockedBuffer struct { + mutex sync.Mutex + buf bytes.Buffer +} + +func (b *lockedBuffer) Write(p []byte) (int, error) { + b.mutex.Lock() + defer b.mutex.Unlock() + return b.buf.Write(p) +} + +func (b *lockedBuffer) String() string { + b.mutex.Lock() + defer b.mutex.Unlock() + return b.buf.String() +} + +func newOutputMaskingProxy() *SSHProxy { + return NewSSHProxy(SSHProxyConfig{ + SessionID: "sid", + SessionLogger: &recordingLogger{}, + OutputMasker: masking.New([]*regexp.Regexp{regexp.MustCompile(`token=\w+`)}, false, nil, "sid"), + }) +} + +func TestMaskedOutputMatchesAValueSplitAcrossReads(t *testing.T) { + src, w := io.Pipe() + out := &lockedBuffer{} + go func() { + _, _ = w.Write([]byte("value token=ab")) + time.Sleep(maskedOutputQuietPeriod / 5) + _, _ = w.Write([]byte("cd123 end\nprompt$ ")) + _ = w.Close() + }() + + if err := newOutputMaskingProxy().proxyData(src, out, "server→client", "sid", false, newChannelState()); err != nil { + t.Fatal(err) + } + if got := out.String(); got != "value [MASKED] end\nprompt$ " { + t.Fatalf("client saw %q", got) + } +} + +func TestMaskedOutputSendsAPartialLineOnceQuiet(t *testing.T) { + src, w := io.Pipe() + out := &lockedBuffer{} + done := make(chan error, 1) + go func() { + done <- newOutputMaskingProxy().proxyData(src, out, "server→client", "sid", false, newChannelState()) + }() + + _, _ = w.Write([]byte("prompt$ ")) + time.Sleep(4 * maskedOutputQuietPeriod) + if got := out.String(); got != "prompt$ " { + t.Fatalf("client saw %q before closing, want the prompt", got) + } + _ = w.Close() + if err := <-done; err != nil { + t.Fatal(err) + } +} diff --git a/packages/pam/handlers/ssh/proxy.go b/packages/pam/handlers/ssh/proxy.go index 9a459fb28..b0261acda 100644 --- a/packages/pam/handlers/ssh/proxy.go +++ b/packages/pam/handlers/ssh/proxy.go @@ -1,6 +1,7 @@ package ssh import ( + "bytes" "context" "fmt" "io" @@ -534,6 +535,10 @@ func (p *SSHProxy) handleChannelRequests(requests <-chan *ssh.Request, targetCha // proxyData proxies data between channels with optional logging func (p *SSHProxy) proxyData(src io.Reader, dst io.Writer, direction string, sessionID string, logInput bool, chState *channelState) error { + if !logInput && p.config.OutputMasker != nil { + return p.proxyMaskedOutput(src, dst, sessionID, chState) + } + buf := make([]byte, 32*1024) // 32KB buffer // Flush any remaining buffers on exit @@ -561,11 +566,6 @@ func (p *SSHProxy) proxyData(src io.Reader, dst io.Writer, direction string, ses channelType := chState.channelType chState.mutex.Unlock() - data := buf[:n] - if !logInput && !isBinary && p.config.OutputMasker != nil { - data = p.config.OutputMasker.Mask(data) - } - if isBinary && sftpParser != nil && logInput { // Parse SFTP packets from client->server direction to extract file operations operations := sftpParser.Parse(buf[:n]) @@ -592,16 +592,16 @@ func (p *SSHProxy) proxyData(src io.Reader, dst io.Writer, direction string, ses if logInput { p.bufferInput(buf[:n], sessionID, channelType, chState) } else { - p.bufferOutput(data, sessionID, channelType, chState) + p.bufferOutput(buf[:n], sessionID, channelType, chState) } } // Write to destination - written, writeErr := dst.Write(data) + written, writeErr := dst.Write(buf[:n]) if writeErr != nil { return writeErr } - if written != len(data) { + if written != n { return io.ErrShortWrite } } @@ -615,6 +615,87 @@ func (p *SSHProxy) proxyData(src io.Reader, dst io.Writer, direction string, ses } } +// How long a partial line waits for the rest of its output before it is masked and sent as is. +const maskedOutputQuietPeriod = 25 * time.Millisecond + +// A line longer than this is masked and sent before its end arrives, to bound what one channel holds. +const maxHeldOutput = 64 * 1024 + +// proxyMaskedOutput masks output a line at a time, so a value split across reads is still matched whole. +// A partial line is held until the rest arrives or the stream goes quiet, which keeps prompts and echo flowing. +func (p *SSHProxy) proxyMaskedOutput(src io.Reader, dst io.Writer, sessionID string, chState *channelState) error { + defer p.flushOutputBuffer(sessionID, chState) + + type chunk struct { + data []byte + err error + } + reads := make(chan chunk) + done := make(chan struct{}) + defer close(done) + go func() { + for { + buf := make([]byte, 32*1024) + n, err := src.Read(buf) + select { + case reads <- chunk{buf[:n], err}: + case <-done: + return + } + if err != nil { + return + } + } + }() + + emit := func(data []byte) error { + if len(data) == 0 { + return nil + } + masked := p.config.OutputMasker.Mask(data) + chState.mutex.Lock() + channelType := chState.channelType + chState.mutex.Unlock() + p.bufferOutput(masked, sessionID, channelType, chState) + _, err := dst.Write(masked) + return err + } + + var pending []byte + for { + var quiet <-chan time.Time + if len(pending) > 0 { + quiet = time.After(maskedOutputQuietPeriod) + } + select { + case c := <-reads: + if len(c.data) > 0 && p.config.OnActivity != nil { + p.config.OnActivity() + } + pending = append(pending, c.data...) + cut := bytes.LastIndexAny(pending, "\r\n") + 1 + if c.err != nil || len(pending)-cut > maxHeldOutput { + cut = len(pending) + } + if err := emit(pending[:cut]); err != nil { + return err + } + pending = append(pending[:0], pending[cut:]...) + if c.err == io.EOF { + return nil + } + if c.err != nil { + return c.err + } + case <-quiet: + if err := emit(pending); err != nil { + return err + } + pending = pending[:0] + } + } +} + // bufferInput accumulates input data and logs the effective command after processing edits. // It interprets control characters (backspace, Ctrl+C/U/W) so that the logged command // reflects what the user actually sent, not the raw keystrokes. diff --git a/packages/pam/pam-proxy.go b/packages/pam/pam-proxy.go index 0cb95609f..25846068e 100644 --- a/packages/pam/pam-proxy.go +++ b/packages/pam/pam-proxy.go @@ -272,8 +272,17 @@ func HandlePAMProxy(ctx context.Context, conn *tls.Conn, pamConfig *GatewayPAMCo } var outputMasker masking.Masker - if credentials.PolicyRules != nil { - if patterns := compilePolicyPatterns(rulePatterns(credentials.PolicyRules.OutputMasking), pamConfig.SessionId, "output-masking"); len(patterns) > 0 { + if credentials.PolicyRules != nil && credentials.PolicyRules.OutputMasking != nil { + var patterns []*regexp.Regexp + for _, pattern := range credentials.PolicyRules.OutputMasking.Patterns { + re, err := regexp.Compile(pattern) + if err != nil { + // Masking less than the policy asks for would show the user what it was meant to hide. + return fmt.Errorf("output masking pattern %q does not compile: %w", pattern, err) + } + patterns = append(patterns, re) + } + if len(patterns) > 0 { outputMasker = masking.New(patterns, false, nil, pamConfig.SessionId) } } From d1e332fee0da9da5fd9f26637bf38c027c066a94 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 11 Nov 2026 23:28:08 -0500 Subject: [PATCH 03/11] bump go --- .github/workflows/govulncheck.yml | 2 +- .github/workflows/release_build_infisical_cli.yml | 6 +++--- .github/workflows/run-cli-e2e-tests.yml | 8 ++++---- .github/workflows/run-cli-smoke-tests.yml | 2 +- .github/workflows/run-cli-tests.yml | 2 +- .github/workflows/test-update-instructions.yml | 8 ++++---- e2e/go.mod | 2 +- go.mod | 2 +- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.github/workflows/govulncheck.yml b/.github/workflows/govulncheck.yml index fb6ea78c9..3e48e1618 100644 --- a/.github/workflows/govulncheck.yml +++ b/.github/workflows/govulncheck.yml @@ -21,7 +21,7 @@ jobs: - name: Setup Go uses: actions/setup-go@7b8cf10d4e4a01d4992d18a89f4d7dc5a3e6d6f4 # v4.3.0 with: - go-version: "1.26.10" + go-version: "1.26.11" cache: true cache-dependency-path: go.sum - name: Install govulncheck diff --git a/.github/workflows/release_build_infisical_cli.yml b/.github/workflows/release_build_infisical_cli.yml index c778d8792..73fb8335a 100644 --- a/.github/workflows/release_build_infisical_cli.yml +++ b/.github/workflows/release_build_infisical_cli.yml @@ -169,7 +169,7 @@ jobs: - run: echo "Ref name ${{github.ref_name}}" - uses: actions/setup-go@be3c94b385c4f180051c996d336f57a34c397495 # v3.6.1 with: - go-version: "1.26.10" + go-version: "1.26.11" cache: true cache-dependency-path: go.sum - name: Setup for libssl1.0-dev @@ -513,7 +513,7 @@ jobs: - uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0 with: - go-version: "1.26.10" + go-version: "1.26.11" cache: true cache-dependency-path: go.sum @@ -593,7 +593,7 @@ jobs: - name: Set up Go uses: actions/setup-go@be3c94b385c4f180051c996d336f57a34c397495 # v3.6.1 with: - go-version: "1.26.10" + go-version: "1.26.11" cache: true cache-dependency-path: go.sum diff --git a/.github/workflows/run-cli-e2e-tests.yml b/.github/workflows/run-cli-e2e-tests.yml index c68778bbb..9377c6173 100644 --- a/.github/workflows/run-cli-e2e-tests.yml +++ b/.github/workflows/run-cli-e2e-tests.yml @@ -16,7 +16,7 @@ jobs: - name: Setup Go uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 with: - go-version: "1.26.10" + go-version: "1.26.11" - name: Install dependencies run: go get . - name: Build the CLI @@ -52,7 +52,7 @@ jobs: - name: Setup Go uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 with: - go-version: "1.26.10" + go-version: "1.26.11" - name: Install dependencies run: go get . - name: Build the CLI @@ -87,7 +87,7 @@ jobs: - name: Setup Go uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 with: - go-version: "1.26.10" + go-version: "1.26.11" - name: Install dependencies run: go get . - name: Cache cargo registry + target @@ -151,7 +151,7 @@ jobs: - name: Setup Go uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 with: - go-version: "1.26.10" + go-version: "1.26.11" - name: Build the CLI run: go build -o infisical-cli${{ runner.os == 'Windows' && '.exe' || '' }} # --exit-code 0 so leaks in test fixtures don't fail the job; a failed git scan still exits 1. diff --git a/.github/workflows/run-cli-smoke-tests.yml b/.github/workflows/run-cli-smoke-tests.yml index 58f06df84..a13d5274c 100644 --- a/.github/workflows/run-cli-smoke-tests.yml +++ b/.github/workflows/run-cli-smoke-tests.yml @@ -25,7 +25,7 @@ jobs: - name: Set up Go uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0 with: - go-version: "1.26.10" + go-version: "1.26.11" - name: Cache cargo registry + target uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 diff --git a/.github/workflows/run-cli-tests.yml b/.github/workflows/run-cli-tests.yml index ff3203bb4..4413252e3 100644 --- a/.github/workflows/run-cli-tests.yml +++ b/.github/workflows/run-cli-tests.yml @@ -35,7 +35,7 @@ jobs: - name: Setup Go uses: actions/setup-go@7b8cf10d4e4a01d4992d18a89f4d7dc5a3e6d6f4 # v4.3.0 with: - go-version: "1.26.10" + go-version: "1.26.11" - name: Install dependencies run: go get . - name: Race-check the Agent Vault packages diff --git a/.github/workflows/test-update-instructions.yml b/.github/workflows/test-update-instructions.yml index 751f258d4..f20cca461 100644 --- a/.github/workflows/test-update-instructions.yml +++ b/.github/workflows/test-update-instructions.yml @@ -11,7 +11,7 @@ jobs: - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - uses: actions/setup-go@7b8cf10d4e4a01d4992d18a89f4d7dc5a3e6d6f4 # v4.3.0 with: - go-version: "1.26.10" + go-version: "1.26.11" - name: Run unit tests run: go test ./packages/util/ -run TestGetUpdateInstructions -v @@ -21,7 +21,7 @@ jobs: - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - uses: actions/setup-go@7b8cf10d4e4a01d4992d18a89f4d7dc5a3e6d6f4 # v4.3.0 with: - go-version: "1.26.10" + go-version: "1.26.11" - name: Build test helper run: go build -o update-hint.exe ./test/update-hint @@ -70,7 +70,7 @@ jobs: - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - uses: actions/setup-go@7b8cf10d4e4a01d4992d18a89f4d7dc5a3e6d6f4 # v4.3.0 with: - go-version: "1.26.10" + go-version: "1.26.11" - name: Build test helper run: go build -o update-hint ./test/update-hint @@ -97,7 +97,7 @@ jobs: - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - uses: actions/setup-go@7b8cf10d4e4a01d4992d18a89f4d7dc5a3e6d6f4 # v4.3.0 with: - go-version: "1.26.10" + go-version: "1.26.11" - name: Build test helper run: go build -o update-hint ./test/update-hint diff --git a/e2e/go.mod b/e2e/go.mod index ef8f7ddf5..a6b363452 100644 --- a/e2e/go.mod +++ b/e2e/go.mod @@ -1,6 +1,6 @@ module github.com/infisical/cli/e2e-tests -go 1.26.10 +go 1.26.11 require ( github.com/Infisical/infisical-merge v0.0.0 diff --git a/go.mod b/go.mod index cce5f4c12..29e14a03b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/Infisical/infisical-merge -go 1.26.10 +go 1.26.11 require ( cloud.google.com/go/iam v1.1.11 From ff028c6759481468090924d30bb3eba6d8e34249 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 11 Nov 2026 23:33:30 -0500 Subject: [PATCH 04/11] revert version bump --- .github/workflows/govulncheck.yml | 2 +- .github/workflows/release_build_infisical_cli.yml | 6 +++--- .github/workflows/run-cli-e2e-tests.yml | 8 ++++---- .github/workflows/run-cli-smoke-tests.yml | 2 +- .github/workflows/run-cli-tests.yml | 2 +- .github/workflows/test-update-instructions.yml | 8 ++++---- e2e/go.mod | 2 +- go.mod | 2 +- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.github/workflows/govulncheck.yml b/.github/workflows/govulncheck.yml index 3e48e1618..fb6ea78c9 100644 --- a/.github/workflows/govulncheck.yml +++ b/.github/workflows/govulncheck.yml @@ -21,7 +21,7 @@ jobs: - name: Setup Go uses: actions/setup-go@7b8cf10d4e4a01d4992d18a89f4d7dc5a3e6d6f4 # v4.3.0 with: - go-version: "1.26.11" + go-version: "1.26.10" cache: true cache-dependency-path: go.sum - name: Install govulncheck diff --git a/.github/workflows/release_build_infisical_cli.yml b/.github/workflows/release_build_infisical_cli.yml index 73fb8335a..c778d8792 100644 --- a/.github/workflows/release_build_infisical_cli.yml +++ b/.github/workflows/release_build_infisical_cli.yml @@ -169,7 +169,7 @@ jobs: - run: echo "Ref name ${{github.ref_name}}" - uses: actions/setup-go@be3c94b385c4f180051c996d336f57a34c397495 # v3.6.1 with: - go-version: "1.26.11" + go-version: "1.26.10" cache: true cache-dependency-path: go.sum - name: Setup for libssl1.0-dev @@ -513,7 +513,7 @@ jobs: - uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0 with: - go-version: "1.26.11" + go-version: "1.26.10" cache: true cache-dependency-path: go.sum @@ -593,7 +593,7 @@ jobs: - name: Set up Go uses: actions/setup-go@be3c94b385c4f180051c996d336f57a34c397495 # v3.6.1 with: - go-version: "1.26.11" + go-version: "1.26.10" cache: true cache-dependency-path: go.sum diff --git a/.github/workflows/run-cli-e2e-tests.yml b/.github/workflows/run-cli-e2e-tests.yml index 9377c6173..c68778bbb 100644 --- a/.github/workflows/run-cli-e2e-tests.yml +++ b/.github/workflows/run-cli-e2e-tests.yml @@ -16,7 +16,7 @@ jobs: - name: Setup Go uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 with: - go-version: "1.26.11" + go-version: "1.26.10" - name: Install dependencies run: go get . - name: Build the CLI @@ -52,7 +52,7 @@ jobs: - name: Setup Go uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 with: - go-version: "1.26.11" + go-version: "1.26.10" - name: Install dependencies run: go get . - name: Build the CLI @@ -87,7 +87,7 @@ jobs: - name: Setup Go uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 with: - go-version: "1.26.11" + go-version: "1.26.10" - name: Install dependencies run: go get . - name: Cache cargo registry + target @@ -151,7 +151,7 @@ jobs: - name: Setup Go uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 with: - go-version: "1.26.11" + go-version: "1.26.10" - name: Build the CLI run: go build -o infisical-cli${{ runner.os == 'Windows' && '.exe' || '' }} # --exit-code 0 so leaks in test fixtures don't fail the job; a failed git scan still exits 1. diff --git a/.github/workflows/run-cli-smoke-tests.yml b/.github/workflows/run-cli-smoke-tests.yml index a13d5274c..58f06df84 100644 --- a/.github/workflows/run-cli-smoke-tests.yml +++ b/.github/workflows/run-cli-smoke-tests.yml @@ -25,7 +25,7 @@ jobs: - name: Set up Go uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0 with: - go-version: "1.26.11" + go-version: "1.26.10" - name: Cache cargo registry + target uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 diff --git a/.github/workflows/run-cli-tests.yml b/.github/workflows/run-cli-tests.yml index 4413252e3..ff3203bb4 100644 --- a/.github/workflows/run-cli-tests.yml +++ b/.github/workflows/run-cli-tests.yml @@ -35,7 +35,7 @@ jobs: - name: Setup Go uses: actions/setup-go@7b8cf10d4e4a01d4992d18a89f4d7dc5a3e6d6f4 # v4.3.0 with: - go-version: "1.26.11" + go-version: "1.26.10" - name: Install dependencies run: go get . - name: Race-check the Agent Vault packages diff --git a/.github/workflows/test-update-instructions.yml b/.github/workflows/test-update-instructions.yml index f20cca461..751f258d4 100644 --- a/.github/workflows/test-update-instructions.yml +++ b/.github/workflows/test-update-instructions.yml @@ -11,7 +11,7 @@ jobs: - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - uses: actions/setup-go@7b8cf10d4e4a01d4992d18a89f4d7dc5a3e6d6f4 # v4.3.0 with: - go-version: "1.26.11" + go-version: "1.26.10" - name: Run unit tests run: go test ./packages/util/ -run TestGetUpdateInstructions -v @@ -21,7 +21,7 @@ jobs: - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - uses: actions/setup-go@7b8cf10d4e4a01d4992d18a89f4d7dc5a3e6d6f4 # v4.3.0 with: - go-version: "1.26.11" + go-version: "1.26.10" - name: Build test helper run: go build -o update-hint.exe ./test/update-hint @@ -70,7 +70,7 @@ jobs: - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - uses: actions/setup-go@7b8cf10d4e4a01d4992d18a89f4d7dc5a3e6d6f4 # v4.3.0 with: - go-version: "1.26.11" + go-version: "1.26.10" - name: Build test helper run: go build -o update-hint ./test/update-hint @@ -97,7 +97,7 @@ jobs: - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - uses: actions/setup-go@7b8cf10d4e4a01d4992d18a89f4d7dc5a3e6d6f4 # v4.3.0 with: - go-version: "1.26.11" + go-version: "1.26.10" - name: Build test helper run: go build -o update-hint ./test/update-hint diff --git a/e2e/go.mod b/e2e/go.mod index a6b363452..ef8f7ddf5 100644 --- a/e2e/go.mod +++ b/e2e/go.mod @@ -1,6 +1,6 @@ module github.com/infisical/cli/e2e-tests -go 1.26.11 +go 1.26.10 require ( github.com/Infisical/infisical-merge v0.0.0 diff --git a/go.mod b/go.mod index 29e14a03b..cce5f4c12 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/Infisical/infisical-merge -go 1.26.11 +go 1.26.10 require ( cloud.google.com/go/iam v1.1.11 From fa34e7b6986efe75730bd2b80b63df7f7aa2d27a Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 11 Nov 2026 23:37:35 -0500 Subject: [PATCH 05/11] address veria --- packages/pam/handlers/postgres_masking.go | 25 ++++++++++++++--- .../pam/handlers/postgres_masking_test.go | 28 +++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/pam/handlers/postgres_masking.go b/packages/pam/handlers/postgres_masking.go index 799e05019..d1f884bf2 100644 --- a/packages/pam/handlers/postgres_masking.go +++ b/packages/pam/handlers/postgres_masking.go @@ -71,8 +71,10 @@ func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3. switch m := msg.(type) { case *pgproto3.RowDescription: p.rowColumns = p.rowColumns[:0] - for _, field := range m.Fields { + for i, field := range m.Fields { p.rowColumns = append(p.rowColumns, pgColumnFormat{field.DataTypeOID, field.Format == pgtype.BinaryFormatCode}) + // Dynamic SQL behind a cursor can name a column after a value. + m.Fields[i].Name = masker.Mask(field.Name) } return p.answerDescribe(msg) case *pgproto3.NoData: @@ -125,11 +127,16 @@ func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3. m.Result = nil } case *pgproto3.NotificationResponse: - m.Payload = masker.MaskString(m.Payload) + maskStrings(masker, &m.Channel, &m.Payload) + case *pgproto3.ParameterStatus: + // The one reported parameter a query can set to arbitrary text, through set_config. + if m.Name == "application_name" { + m.Value = masker.MaskString(m.Value) + } case *pgproto3.ErrorResponse: - maskStrings(masker, &m.Message, &m.Detail, &m.Hint, &m.Where) + maskErrorFields(masker, m) case *pgproto3.NoticeResponse: - maskStrings(masker, &m.Message, &m.Detail, &m.Hint, &m.Where) + maskErrorFields(masker, (*pgproto3.ErrorResponse)(m)) } return msg } @@ -169,6 +176,16 @@ func keepsValidJSON(column pgColumnFormat, masked []byte) bool { return true } +// Masks every field a query can fill (RAISE ... USING sets them freely); severity, code and source +// location come from the server and clients act on them. +func maskErrorFields(masker masking.Masker, e *pgproto3.ErrorResponse) { + maskStrings(masker, &e.Message, &e.Detail, &e.Hint, &e.Where, &e.InternalQuery, &e.SchemaName, &e.TableName, + &e.ColumnName, &e.DataTypeName, &e.ConstraintName) + for key, value := range e.UnknownFields { + e.UnknownFields[key] = masker.MaskString(value) + } +} + func maskStrings(masker masking.Masker, fields ...*string) { for _, field := range fields { *field = masker.MaskString(*field) diff --git a/packages/pam/handlers/postgres_masking_test.go b/packages/pam/handlers/postgres_masking_test.go index 6ffb7c34b..865a016a7 100644 --- a/packages/pam/handlers/postgres_masking_test.go +++ b/packages/pam/handlers/postgres_masking_test.go @@ -208,3 +208,31 @@ func TestMaskingRefusesCopyAndReplication(t *testing.T) { t.Fatalf("replication reply %+v, closeAfterReply %v", forwarded[0], p.closeAfterReply) } } + +func TestMaskingCoversProtocolMetadata(t *testing.T) { + p := newMaskingProxy() + _, forwarded := relay(p, + []pgproto3.FrontendMessage{&pgproto3.Query{String: "SELECT set_config('application_name', card::text, false) FROM cards"}}, + []pgproto3.BackendMessage{ + &pgproto3.RowDescription{Fields: []pgproto3.FieldDescription{{Name: []byte("4111111111111111"), DataTypeOID: pgtype.TextOID}}}, + &pgproto3.ParameterStatus{Name: "application_name", Value: "4111111111111111"}, + &pgproto3.ParameterStatus{Name: "server_version", Value: "16.4"}, + &pgproto3.ErrorResponse{Severity: "ERROR", Code: "P0001", Message: "raised", ColumnName: "secret", ConstraintName: "secret"}, + &pgproto3.NotificationResponse{Channel: "secret", Payload: "secret"}, + }) + if name := string(forwarded[0].(*pgproto3.RowDescription).Fields[0].Name); name != "[MASKED]" { + t.Fatalf("column name %q", name) + } + if value := forwarded[1].(*pgproto3.ParameterStatus).Value; value != "[MASKED]" { + t.Fatalf("application_name %q", value) + } + if value := forwarded[2].(*pgproto3.ParameterStatus).Value; value != "16.4" { + t.Fatalf("server_version %q", value) + } + if e := forwarded[3].(*pgproto3.ErrorResponse); e.ColumnName != "[MASKED]" || e.ConstraintName != "[MASKED]" || e.Code != "P0001" { + t.Fatalf("error fields %+v", e) + } + if n := forwarded[4].(*pgproto3.NotificationResponse); n.Channel != "[MASKED]" || n.Payload != "[MASKED]" { + t.Fatalf("notification %+v", n) + } +} From b534c15bc85275fe8cfe78e007f2f2b9e481df32 Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 12 Nov 2026 00:06:25 -0500 Subject: [PATCH 06/11] address bot reviews --- packages/pam/handlers/postgres.go | 1 + packages/pam/handlers/postgres_masking.go | 185 +++++++++++++++--- .../pam/handlers/postgres_masking_test.go | 90 ++++++++- packages/pam/handlers/ssh/output_masking.go | 175 +++++++++++++++++ .../pam/handlers/ssh/output_masking_test.go | 65 +++++- packages/pam/handlers/ssh/proxy.go | 98 +--------- packages/pam/pam-proxy.go | 16 +- 7 files changed, 506 insertions(+), 124 deletions(-) create mode 100644 packages/pam/handlers/ssh/output_masking.go diff --git a/packages/pam/handlers/postgres.go b/packages/pam/handlers/postgres.go index 03db50d7c..de06f73ed 100644 --- a/packages/pam/handlers/postgres.go +++ b/packages/pam/handlers/postgres.go @@ -690,6 +690,7 @@ func (p *PostgresProxy) proxyClientToServer(clientBackend *pgproto3.Backend, ser p.trackClientMessage(msg) if p.masking() { + msg = rejectCopyToClient(msg) if describe := p.trackDescribe(msg); describe != nil { serverFrontend.Send(describe) } diff --git a/packages/pam/handlers/postgres_masking.go b/packages/pam/handlers/postgres_masking.go index d1f884bf2..75ad1d81c 100644 --- a/packages/pam/handlers/postgres_masking.go +++ b/packages/pam/handlers/postgres_masking.go @@ -2,14 +2,17 @@ package handlers import ( "bytes" + "encoding/hex" "encoding/json" + "regexp" + "slices" + "strings" "github.com/Infisical/infisical-merge/packages/pam/session/masking" "github.com/jackc/pgx/v5/pgproto3" "github.com/jackc/pgx/v5/pgtype" ) -// Values of these types are plain text in both wire formats, so a match is replaced in place. var pgTextTypes = map[uint32]bool{ pgtype.TextOID: true, pgtype.VarcharOID: true, @@ -26,7 +29,7 @@ type pgColumnFormat struct { binary bool } -// Who receives the RowDescription or NoData answering a Describe; the server answers in request order. +// The server answers Describes in request order, so a queue of these says whose answer each one is. type describeSource int const ( @@ -39,6 +42,105 @@ func (p *PostgresProxy) masking() bool { return p.config.OutputMasker != nil } +// Fails on the server, so the batch a COPY to the client sits in aborts and nothing in it commits. +const blockedCopyStatement = `DO $$ BEGIN RAISE EXCEPTION USING ERRCODE = '42501', ` + + `MESSAGE = 'COPY TO STDOUT is blocked because this account masks output, use SELECT instead'; END $$` + +var copyToClientPattern = regexp.MustCompile(`(?is)^\s*copy\b.*\bto\s+stdout\b`) + +var dollarQuoteTag = regexp.MustCompile(`^\$([A-Za-z_][A-Za-z0-9_]*)?\$`) + +func rejectCopyToClient(msg pgproto3.FrontendMessage) pgproto3.FrontendMessage { + switch m := msg.(type) { + case *pgproto3.Query: + if copiesToClient(m.String) { + return &pgproto3.Query{String: blockedCopyStatement} + } + case *pgproto3.Parse: + if copiesToClient(m.Query) { + return &pgproto3.Parse{Name: m.Name, Query: blockedCopyStatement} + } + } + return msg +} + +func copiesToClient(sql string) bool { + return slices.ContainsFunc(strings.Split(blankQuoted(sql), ";"), copyToClientPattern.MatchString) +} + +func blankQuoted(sql string) string { + out := []byte(sql) + blank := func(from, to int) int { + to = min(to, len(out)) + for k := from; k < to; k++ { + out[k] = ' ' + } + return to + } + for i := 0; i < len(out); { + switch { + case out[i] == '\'': + backslashEscapes := i > 0 && (out[i-1] == 'E' || out[i-1] == 'e') + j := i + 1 + for ; j < len(out); j++ { + if backslashEscapes && out[j] == '\\' { + j++ + } else if out[j] == '\'' { + if j+1 < len(out) && out[j+1] == '\'' { + j++ + continue + } + break + } + } + i = blank(i, j+1) + case out[i] == '"': + j := i + 1 + for ; j < len(out); j++ { + if out[j] == '"' { + if j+1 < len(out) && out[j+1] == '"' { + j++ + continue + } + break + } + } + i = blank(i, j+1) + case out[i] == '-' && i+1 < len(out) && out[i+1] == '-': + j := bytes.IndexByte(out[i:], '\n') + if j < 0 { + j = len(out) - i + } + i = blank(i, i+j) + case out[i] == '/' && i+1 < len(out) && out[i+1] == '*': + depth, j := 1, i+2 + for ; j < len(out) && depth > 0; j++ { + if out[j] == '/' && j+1 < len(out) && out[j+1] == '*' { + depth, j = depth+1, j+1 + } else if out[j] == '*' && j+1 < len(out) && out[j+1] == '/' { + depth, j = depth-1, j+1 + } + } + i = blank(i, j) + case out[i] == '$' && (i == 0 || !isIdentifierByte(out[i-1])): + tag := dollarQuoteTag.Find(out[i:]) + if tag == nil { + i++ + continue + } + end := bytes.Index(out[i+len(tag):], tag) + if end < 0 { + i = blank(i, len(out)) + continue + } + i = blank(i, i+len(tag)+end+len(tag)) + default: + i++ + } + } + return string(out) +} + // Each Execute gets a portal Describe sent ahead of it, so its rows always follow a RowDescription. func (p *PostgresProxy) trackDescribe(msg pgproto3.FrontendMessage) *pgproto3.Describe { p.mutex.Lock() @@ -56,7 +158,6 @@ func (p *PostgresProxy) trackDescribe(msg pgproto3.FrontendMessage) *pgproto3.De return nil } -// maskServerMessage returns the message to relay, or nil to drop it. func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3.BackendMessage { if _, ok := msg.(*pgproto3.ReadyForQuery); ok { p.discarding = false @@ -90,10 +191,7 @@ func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3. column = p.rowColumns[i] } isText := pgTextTypes[column.typeOID] - text := value - if column.binary && !isText { - text = p.binaryValueText(column.typeOID, value) - } + text := p.matchText(column, value) masked := masker.Mask(text) if bytes.Equal(masked, text) { continue @@ -106,8 +204,7 @@ func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3. } } case *pgproto3.CopyOutResponse: - // COPY escapes its fields (CSV doubles quotes, text escapes control characters), so a pattern can - // miss a value there that it matches in a row. + // COPY escapes its fields, so patterns can miss values in it. p.discarding = true return &pgproto3.ErrorResponse{ Severity: "ERROR", @@ -123,7 +220,14 @@ func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3. Message: "Replication is blocked because this account masks output", } case *pgproto3.FunctionCallResponse: - if masked := masker.Mask(m.Result); !bytes.Equal(masked, m.Result) { + // The result's type isn't sent, so a hex bytea result is matched as the bytes it encodes. + text := m.Result + if hexText, ok := bytes.CutPrefix(m.Result, []byte(`\x`)); ok { + if decoded, err := hex.DecodeString(string(hexText)); err == nil { + text = decoded + } + } + if masked := masker.Mask(text); !bytes.Equal(masked, text) { m.Result = nil } case *pgproto3.NotificationResponse: @@ -141,28 +245,60 @@ func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3. return msg } -// A binary value is matched as the text a text-format client would see, so a card number in a numeric -// column is caught whichever format was asked for. A type pgx can't decode is matched as its raw bytes. -func (p *PostgresProxy) binaryValueText(typeOID uint32, value []byte) []byte { +// Matches the content a client reads out of a value, so binary encoding or bytea's hex can't hide a match. +func (p *PostgresProxy) matchText(column pgColumnFormat, value []byte) []byte { + isBytea := column.typeOID == pgtype.ByteaOID || column.typeOID == pgtype.ByteaArrayOID + if pgTextTypes[column.typeOID] || (!column.binary && !isBytea) { + return value + } if p.typeMap == nil { p.typeMap = pgtype.NewMap() } - dataType, ok := p.typeMap.TypeForOID(typeOID) + dataType, ok := p.typeMap.TypeForOID(column.typeOID) if !ok { return value } - decoded, err := dataType.Codec.DecodeValue(p.typeMap, typeOID, pgtype.BinaryFormatCode, value) + format := int16(pgtype.TextFormatCode) + if column.binary { + format = pgtype.BinaryFormatCode + } + decoded, err := dataType.Codec.DecodeValue(p.typeMap, column.typeOID, format, value) if err != nil || decoded == nil { return value } - text, err := p.typeMap.Encode(typeOID, pgtype.TextFormatCode, decoded, nil) - if err != nil { - return value + if content, ok := appendContent(nil, decoded); ok { + return content } - return text + if text, err := p.typeMap.Encode(column.typeOID, pgtype.TextFormatCode, decoded, nil); err == nil { + return text + } + return value +} + +func appendContent(buf []byte, value any) ([]byte, bool) { + switch v := value.(type) { + case []byte: + return append(buf, v...), true + case string: + return append(buf, v...), true + case []any: + for i, element := range v { + if i > 0 { + buf = append(buf, '\n') + } + if element == nil { + continue + } + var ok bool + if buf, ok = appendContent(buf, element); !ok { + return nil, false + } + } + return buf, true + } + return nil, false } -// A match inside a JSON number or key leaves a placeholder the client can't parse. func keepsValidJSON(column pgColumnFormat, masked []byte) bool { switch column.typeOID { case pgtype.JSONOID: @@ -176,8 +312,11 @@ func keepsValidJSON(column pgColumnFormat, masked []byte) bool { return true } -// Masks every field a query can fill (RAISE ... USING sets them freely); severity, code and source -// location come from the server and clients act on them. +func isIdentifierByte(b byte) bool { + return b == '_' || b == '$' || (b >= '0' && b <= '9') || (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b >= 0x80 +} + +// Severity, code and source location come from the server, and clients act on them. func maskErrorFields(masker masking.Masker, e *pgproto3.ErrorResponse) { maskStrings(masker, &e.Message, &e.Detail, &e.Hint, &e.Where, &e.InternalQuery, &e.SchemaName, &e.TableName, &e.ColumnName, &e.DataTypeName, &e.ConstraintName) @@ -208,7 +347,7 @@ func (p *PostgresProxy) answerDescribe(msg pgproto3.BackendMessage) pgproto3.Bac return msg } -// popThroughSync drops what the batch ending at this ReadyForQuery left unanswered, as after an error. +// After an error the server skips the rest of the batch, so its pending answers never arrive. func (p *PostgresProxy) popThroughSync() { p.mutex.Lock() defer p.mutex.Unlock() diff --git a/packages/pam/handlers/postgres_masking_test.go b/packages/pam/handlers/postgres_masking_test.go index 865a016a7..94f668730 100644 --- a/packages/pam/handlers/postgres_masking_test.go +++ b/packages/pam/handlers/postgres_masking_test.go @@ -15,7 +15,6 @@ func newMaskingProxy() *PostgresProxy { }) } -// relay feeds the client messages, then the server's replies, and returns what reaches the client. func relay(p *PostgresProxy, client []pgproto3.FrontendMessage, server []pgproto3.BackendMessage) (int, []pgproto3.BackendMessage) { injected := 0 for _, msg := range client { @@ -236,3 +235,92 @@ func TestMaskingCoversProtocolMetadata(t *testing.T) { t.Fatalf("notification %+v", n) } } + +func TestMaskingMatchesByteaContent(t *testing.T) { + typeMap := pgtype.NewMap() + encode := func(oid uint32, format int16, value any) []byte { + encoded, err := typeMap.Encode(oid, format, value, nil) + if err != nil { + t.Fatal(err) + } + return encoded + } + + for _, c := range []struct { + name string + oid uint32 + format int16 + value []byte + }{ + {"binary bytea", pgtype.ByteaOID, 1, encode(pgtype.ByteaOID, 1, []byte("a secret"))}, + {"hex bytea", pgtype.ByteaOID, 0, encode(pgtype.ByteaOID, 0, []byte("a secret"))}, + {"binary bytea array", pgtype.ByteaArrayOID, 1, encode(pgtype.ByteaArrayOID, 1, [][]byte{[]byte("x"), []byte("secret")})}, + {"text bytea array", pgtype.ByteaArrayOID, 0, encode(pgtype.ByteaArrayOID, 0, [][]byte{[]byte("secret")})}, + {"binary text array", pgtype.TextArrayOID, 1, encode(pgtype.TextArrayOID, 1, []string{"x", "secret"})}, + {"binary bigint array", pgtype.Int8ArrayOID, 1, encode(pgtype.Int8ArrayOID, 1, []int64{4111111111111111})}, + } { + t.Run(c.name, func(t *testing.T) { + p := newMaskingProxy() + _, forwarded := relay(p, + []pgproto3.FrontendMessage{&pgproto3.Execute{}, &pgproto3.Sync{}}, + []pgproto3.BackendMessage{rowDescription(c.format, c.oid), dataRow(c.value)}) + if value := forwarded[0].(*pgproto3.DataRow).Values[0]; value != nil { + t.Fatalf("forwarded %q, want NULL", value) + } + }) + } + + p := newMaskingProxy() + plain := encode(pgtype.ByteaOID, 0, []byte("nothing to hide")) + _, forwarded := relay(p, + []pgproto3.FrontendMessage{&pgproto3.Execute{}, &pgproto3.Sync{}}, + []pgproto3.BackendMessage{rowDescription(0, pgtype.ByteaOID), dataRow(plain)}) + if value := forwarded[0].(*pgproto3.DataRow).Values[0]; string(value) != string(plain) { + t.Fatalf("unmatched bytea forwarded as %q, want %q", value, plain) + } +} + +func TestMaskingChecksFunctionCallResults(t *testing.T) { + p := newMaskingProxy() + for _, result := range [][]byte{[]byte("a secret"), []byte(`\x6120736563726574`)} { + _, forwarded := relay(p, nil, []pgproto3.BackendMessage{&pgproto3.FunctionCallResponse{Result: result}}) + if got := forwarded[0].(*pgproto3.FunctionCallResponse).Result; got != nil { + t.Fatalf("function result %q forwarded as %q, want NULL", result, got) + } + } +} + +func TestCopiesToClient(t *testing.T) { + for sql, want := range map[string]bool{ + "COPY cards TO STDOUT": true, + "copy (select card from cards) to stdout with (format csv)": true, + "SELECT 1; /* note */ COPY cards TO STDOUT; INSERT INTO log VALUES (1)": true, + "COPY cards FROM STDIN": false, + "COPY cards TO '/tmp/cards.csv'": false, + "SELECT 'COPY cards TO STDOUT'": false, + "SELECT E'it\\'s; COPY cards TO STDOUT'": false, + "SELECT $$COPY cards TO STDOUT$$": false, + "SELECT $body$; COPY cards TO STDOUT$body$": false, + "SELECT \"COPY x TO STDOUT\" FROM t": false, + "-- COPY cards TO STDOUT\nSELECT 1": false, + "SELECT a$b$c FROM t; COPY cards TO STDOUT": true, + } { + if got := copiesToClient(sql); got != want { + t.Errorf("copiesToClient(%q) = %v, want %v", sql, got, want) + } + } +} + +func TestMaskingSwapsCopyToClientBeforeItRuns(t *testing.T) { + query := rejectCopyToClient(&pgproto3.Query{String: "COPY cards TO STDOUT; INSERT INTO log VALUES (1)"}) + if q := query.(*pgproto3.Query).String; q != blockedCopyStatement { + t.Fatalf("query forwarded as %q", q) + } + parse := rejectCopyToClient(&pgproto3.Parse{Name: "s1", Query: "COPY cards TO STDOUT"}).(*pgproto3.Parse) + if parse.Name != "s1" || parse.Query != blockedCopyStatement { + t.Fatalf("parse forwarded as %+v", parse) + } + if kept := rejectCopyToClient(&pgproto3.Query{String: "SELECT 1"}).(*pgproto3.Query); kept.String != "SELECT 1" { + t.Fatalf("plain query rewritten to %q", kept.String) + } +} diff --git a/packages/pam/handlers/ssh/output_masking.go b/packages/pam/handlers/ssh/output_masking.go new file mode 100644 index 000000000..049401e58 --- /dev/null +++ b/packages/pam/handlers/ssh/output_masking.go @@ -0,0 +1,175 @@ +package ssh + +import ( + "bytes" + "io" + "regexp" + "sort" + "time" + + "github.com/Infisical/infisical-merge/packages/pam/session/masking" +) + +func (p *SSHProxy) masksOutput() bool { + return len(p.config.OutputMaskingPatterns) > 0 +} + +const maskedOutputQuietPeriod = 25 * time.Millisecond + +const maxHeldOutput = 64 * 1024 + +// Masks a line at a time so a value split across reads still matches; a partial line waits until output goes quiet. +func (p *SSHProxy) proxyMaskedOutput(src io.Reader, dst io.Writer, sessionID string, chState *channelState) error { + defer p.flushOutputBuffer(sessionID, chState) + + type chunk struct { + data []byte + err error + } + reads := make(chan chunk) + done := make(chan struct{}) + defer close(done) + go func() { + for { + buf := make([]byte, 32*1024) + n, err := src.Read(buf) + select { + case reads <- chunk{buf[:n], err}: + case <-done: + return + } + if err != nil { + return + } + } + }() + + emit := func(data []byte) error { + if len(data) == 0 { + return nil + } + masked := maskTerminalOutput(p.config.OutputMaskingPatterns, data) + chState.mutex.Lock() + channelType := chState.channelType + chState.mutex.Unlock() + p.bufferOutput(masked, sessionID, channelType, chState) + _, err := dst.Write(masked) + return err + } + + var pending []byte + for { + var quiet <-chan time.Time + if len(pending) > 0 { + quiet = time.After(maskedOutputQuietPeriod) + } + select { + case c := <-reads: + if len(c.data) > 0 && p.config.OnActivity != nil { + p.config.OnActivity() + } + pending = append(pending, c.data...) + cut := bytes.LastIndexAny(pending, "\r\n") + 1 + if c.err != nil || len(pending)-cut > maxHeldOutput { + cut = len(pending) + } + if err := emit(pending[:cut]); err != nil { + return err + } + pending = append(pending[:0], pending[cut:]...) + chState.outputHeld.Store(int64(len(pending))) + if c.err == io.EOF { + return nil + } + if c.err != nil { + return c.err + } + case <-quiet: + if err := emit(pending); err != nil { + return err + } + pending = pending[:0] + chState.outputHeld.Store(0) + } + } +} + +// Skips escape sequences when matching so color can't split a value, and keeps them after the placeholder. +func maskTerminalOutput(patterns []*regexp.Regexp, data []byte) []byte { + plain := make([]byte, 0, len(data)) + offsets := make([]int, 0, len(data)) + for i := 0; i < len(data); { + if data[i] == 0x1b { + i += escapeLength(data, i) + continue + } + plain = append(plain, data[i]) + offsets = append(offsets, i) + i++ + } + + var spans [][2]int + for _, pattern := range patterns { + for _, match := range pattern.FindAllIndex(plain, -1) { + if match[1] > match[0] { + spans = append(spans, [2]int{match[0], match[1]}) + } + } + } + if len(spans) == 0 { + return data + } + sort.Slice(spans, func(a, b int) bool { return spans[a][0] < spans[b][0] }) + + out := make([]byte, 0, len(data)) + last := 0 + for i := 0; i < len(spans); { + start, end := spans[i][0], spans[i][1] + for i++; i < len(spans) && spans[i][0] <= end; i++ { + end = max(end, spans[i][1]) + } + from, to := offsets[start], offsets[end-1]+1 + out = append(out, data[last:from]...) + out = append(out, masking.Placeholder...) + for j := from; j < to; { + if data[j] == 0x1b { + n := escapeLength(data, j) + out = append(out, data[j:j+n]...) + j += n + continue + } + j++ + } + last = to + } + return append(out, data[last:]...) +} + +func escapeLength(data []byte, i int) int { + j := i + 1 + if j >= len(data) { + return 1 + } + switch data[j] { + case '[': + j++ + for j < len(data) && data[j] >= 0x20 && data[j] <= 0x3f { + j++ + } + if j < len(data) && data[j] >= 0x40 && data[j] <= 0x7e { + j++ + } + return j - i + case ']', 'P', 'X', '^', '_': + for j++; j < len(data); j++ { + if data[j] == 0x07 { + return j + 1 - i + } + if data[j] == 0x1b && j+1 < len(data) && data[j+1] == '\\' { + return j + 2 - i + } + } + return len(data) - i + } + return 2 +} diff --git a/packages/pam/handlers/ssh/output_masking_test.go b/packages/pam/handlers/ssh/output_masking_test.go index 3f193def8..977495ca5 100644 --- a/packages/pam/handlers/ssh/output_masking_test.go +++ b/packages/pam/handlers/ssh/output_masking_test.go @@ -4,11 +4,12 @@ import ( "bytes" "io" "regexp" + "strings" "sync" "testing" "time" - "github.com/Infisical/infisical-merge/packages/pam/session/masking" + "github.com/Infisical/infisical-merge/packages/pam/session" ) type lockedBuffer struct { @@ -32,7 +33,10 @@ func newOutputMaskingProxy() *SSHProxy { return NewSSHProxy(SSHProxyConfig{ SessionID: "sid", SessionLogger: &recordingLogger{}, - OutputMasker: masking.New([]*regexp.Regexp{regexp.MustCompile(`token=\w+`)}, false, nil, "sid"), + OutputMaskingPatterns: []*regexp.Regexp{ + regexp.MustCompile(`token=\w+`), + regexp.MustCompile(`[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}`), + }, }) } @@ -72,3 +76,60 @@ func TestMaskedOutputSendsAPartialLineOnceQuiet(t *testing.T) { t.Fatal(err) } } + +func TestMaskedOutputMatchesThroughColorCodes(t *testing.T) { + line := "1,Jane,jane\x1b[01;31m\x1b[K@\x1b[m\x1b[Kacme.com,123\n" + src, w := io.Pipe() + out := &lockedBuffer{} + go func() { + _, _ = w.Write([]byte(line)) + _ = w.Close() + }() + + if err := newOutputMaskingProxy().proxyData(src, out, "server→client", "sid", false, newChannelState()); err != nil { + t.Fatal(err) + } + if got, want := out.String(), "1,Jane,[MASKED]\x1b[01;31m\x1b[K\x1b[m\x1b[K,123\n"; got != want { + t.Fatalf("client saw %q, want %q", got, want) + } +} + +func TestMaskedOutputDoesNotRecordAHeldEchoTwice(t *testing.T) { + logger := &recordingLogger{} + proxy := NewSSHProxy(SSHProxyConfig{ + SessionID: "sid", + SessionLogger: logger, + OutputMaskingPatterns: []*regexp.Regexp{regexp.MustCompile(`token=\w+`)}, + }) + chState := newChannelState() + chState.channelType = session.SessionChannelShell + chState.inputChannelType = session.SessionChannelShell + + src, w := io.Pipe() + done := make(chan error, 1) + go func() { done <- proxy.proxyData(src, io.Discard, "server→client", "sid", false, chState) }() + + _, _ = w.Write([]byte("$ ")) + time.Sleep(4 * maskedOutputQuietPeriod) + proxy.bufferInput([]byte("ls"), "sid", session.SessionChannelShell, chState) + _, _ = w.Write([]byte("ls")) + time.Sleep(maskedOutputQuietPeriod / 5) + proxy.bufferInput([]byte{0x0D}, "sid", session.SessionChannelShell, chState) + _, _ = w.Write([]byte("\r\nfile.txt\r\n$ ")) + _ = w.Close() + if err := <-done; err != nil { + t.Fatal(err) + } + + recorded := 0 + logger.mutex.Lock() + for _, event := range logger.events { + if strings.Contains(string(event.Data), "ls") { + recorded++ + } + } + logger.mutex.Unlock() + if recorded != 1 { + t.Fatalf("command recorded %d times, want once: %+v", recorded, logger.events) + } +} diff --git a/packages/pam/handlers/ssh/proxy.go b/packages/pam/handlers/ssh/proxy.go index b0261acda..1b96d64a4 100644 --- a/packages/pam/handlers/ssh/proxy.go +++ b/packages/pam/handlers/ssh/proxy.go @@ -1,7 +1,6 @@ package ssh import ( - "bytes" "context" "fmt" "io" @@ -13,7 +12,6 @@ import ( "time" "github.com/Infisical/infisical-merge/packages/pam/session" - "github.com/Infisical/infisical-merge/packages/pam/session/masking" "github.com/Infisical/infisical-merge/packages/util" "github.com/rs/zerolog/log" "golang.org/x/crypto/ssh" @@ -30,7 +28,7 @@ type SSHProxyConfig struct { SessionID string SessionLogger session.SessionLogger BlockedCommandPatterns []*regexp.Regexp // Regex patterns for command blocking (nil = no blocking) - OutputMasker masking.Masker // Applied to output before the user sees it (nil = no masking) + OutputMaskingPatterns []*regexp.Regexp // Regex patterns for output masking (nil = no masking) OnActivity func() // Called when channel data flows } @@ -58,6 +56,7 @@ type channelState struct { outputParser *terminalTranscript // Renders the output stream into displayed lines outputChannelType session.SessionChannelType // Channel type for buffered output outputPending atomic.Int64 // Rendered length of the line still on screen + outputHeld atomic.Int64 // Output held back for masking, not yet rendered } func newChannelState() *channelState { @@ -259,7 +258,7 @@ func (p *SSHProxy) handleChannel(ctx context.Context, newChannel ssh.NewChannel, Msg("← CLIENT new channel request") // A forwarded connection carries another protocol, which masking would corrupt rather than redact. - if p.config.OutputMasker != nil && channelType != "session" { + if p.masksOutput() && channelType != "session" { newChannel.Reject(ssh.Prohibited, "port forwarding is blocked because this account masks output") return } @@ -404,7 +403,7 @@ func (p *SSHProxy) handleChannelRequests(requests <-chan *ssh.Request, targetCha // Determine the type of operation isSCP := strings.HasPrefix(command, "scp ") - if isSCP && p.config.OutputMasker != nil { + if isSCP && p.masksOutput() { p.rejectUnmaskableTransfer(req, sessionID) continue } @@ -473,7 +472,7 @@ func (p *SSHProxy) handleChannelRequests(requests <-chan *ssh.Request, targetCha Str("subsystem", subsystem). Msg("SSH subsystem requested") - if subsystem == "sftp" && p.config.OutputMasker != nil { + if subsystem == "sftp" && p.masksOutput() { p.rejectUnmaskableTransfer(req, sessionID) continue } @@ -535,7 +534,7 @@ func (p *SSHProxy) handleChannelRequests(requests <-chan *ssh.Request, targetCha // proxyData proxies data between channels with optional logging func (p *SSHProxy) proxyData(src io.Reader, dst io.Writer, direction string, sessionID string, logInput bool, chState *channelState) error { - if !logInput && p.config.OutputMasker != nil { + if !logInput && p.masksOutput() { return p.proxyMaskedOutput(src, dst, sessionID, chState) } @@ -615,87 +614,6 @@ func (p *SSHProxy) proxyData(src io.Reader, dst io.Writer, direction string, ses } } -// How long a partial line waits for the rest of its output before it is masked and sent as is. -const maskedOutputQuietPeriod = 25 * time.Millisecond - -// A line longer than this is masked and sent before its end arrives, to bound what one channel holds. -const maxHeldOutput = 64 * 1024 - -// proxyMaskedOutput masks output a line at a time, so a value split across reads is still matched whole. -// A partial line is held until the rest arrives or the stream goes quiet, which keeps prompts and echo flowing. -func (p *SSHProxy) proxyMaskedOutput(src io.Reader, dst io.Writer, sessionID string, chState *channelState) error { - defer p.flushOutputBuffer(sessionID, chState) - - type chunk struct { - data []byte - err error - } - reads := make(chan chunk) - done := make(chan struct{}) - defer close(done) - go func() { - for { - buf := make([]byte, 32*1024) - n, err := src.Read(buf) - select { - case reads <- chunk{buf[:n], err}: - case <-done: - return - } - if err != nil { - return - } - } - }() - - emit := func(data []byte) error { - if len(data) == 0 { - return nil - } - masked := p.config.OutputMasker.Mask(data) - chState.mutex.Lock() - channelType := chState.channelType - chState.mutex.Unlock() - p.bufferOutput(masked, sessionID, channelType, chState) - _, err := dst.Write(masked) - return err - } - - var pending []byte - for { - var quiet <-chan time.Time - if len(pending) > 0 { - quiet = time.After(maskedOutputQuietPeriod) - } - select { - case c := <-reads: - if len(c.data) > 0 && p.config.OnActivity != nil { - p.config.OnActivity() - } - pending = append(pending, c.data...) - cut := bytes.LastIndexAny(pending, "\r\n") + 1 - if c.err != nil || len(pending)-cut > maxHeldOutput { - cut = len(pending) - } - if err := emit(pending[:cut]); err != nil { - return err - } - pending = append(pending[:0], pending[cut:]...) - if c.err == io.EOF { - return nil - } - if c.err != nil { - return c.err - } - case <-quiet: - if err := emit(pending); err != nil { - return err - } - pending = pending[:0] - } - } -} - // bufferInput accumulates input data and logs the effective command after processing edits. // It interprets control characters (backspace, Ctrl+C/U/W) so that the logged command // reflects what the user actually sent, not the raw keystrokes. @@ -778,8 +696,8 @@ func (p *SSHProxy) flushInputBufferUnsafe(sessionID string, chState *channelStat switch { case onScreen > chState.echoBaseline: // Echoed while typing, so that line is the record. - case pasted: - // Delivered ahead of any echo; the next committed line decides. + case pasted || chState.outputHeld.Load() > 0: + // Delivered ahead of any echo, or the echo is still held for masking; the next committed line decides. chState.pendingEcho.hold(command, channel, onScreen) default: p.flushOutputBuffer(sessionID, chState) diff --git a/packages/pam/pam-proxy.go b/packages/pam/pam-proxy.go index 25846068e..3c1326642 100644 --- a/packages/pam/pam-proxy.go +++ b/packages/pam/pam-proxy.go @@ -271,19 +271,19 @@ func HandlePAMProxy(ctx context.Context, conn *tls.Conn, pamConfig *GatewayPAMCo ) } + var outputPatterns []*regexp.Regexp var outputMasker masking.Masker if credentials.PolicyRules != nil && credentials.PolicyRules.OutputMasking != nil { - var patterns []*regexp.Regexp - for _, pattern := range credentials.PolicyRules.OutputMasking.Patterns { + for i, pattern := range credentials.PolicyRules.OutputMasking.Patterns { re, err := regexp.Compile(pattern) if err != nil { - // Masking less than the policy asks for would show the user what it was meant to hide. - return fmt.Errorf("output masking pattern %q does not compile: %w", pattern, err) + // Fail closed, and keep the pattern out of the error since it can hold the value it masks. + return fmt.Errorf("output masking pattern %d does not compile", i+1) } - patterns = append(patterns, re) + outputPatterns = append(outputPatterns, re) } - if len(patterns) > 0 { - outputMasker = masking.New(patterns, false, nil, pamConfig.SessionId) + if len(outputPatterns) > 0 { + outputMasker = masking.New(outputPatterns, false, nil, pamConfig.SessionId) } } @@ -437,7 +437,7 @@ func HandlePAMProxy(ctx context.Context, conn *tls.Conn, pamConfig *GatewayPAMCo SessionID: pamConfig.SessionId, SessionLogger: sessionLogger, BlockedCommandPatterns: blockedCommandPatterns, - OutputMasker: outputMasker, + OutputMaskingPatterns: outputPatterns, OnActivity: pamConfig.OnActivity, } proxy := ssh.NewSSHProxy(sshConfig) From ad1716913ad28c1c072d7a2609e32e4a84ea505f Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 12 Nov 2026 00:08:11 -0500 Subject: [PATCH 07/11] simplify logic to be closer to formal --- packages/pam/handlers/postgres.go | 1 - packages/pam/handlers/postgres_masking.go | 106 ------------------ .../pam/handlers/postgres_masking_test.go | 35 ------ 3 files changed, 142 deletions(-) diff --git a/packages/pam/handlers/postgres.go b/packages/pam/handlers/postgres.go index de06f73ed..03db50d7c 100644 --- a/packages/pam/handlers/postgres.go +++ b/packages/pam/handlers/postgres.go @@ -690,7 +690,6 @@ func (p *PostgresProxy) proxyClientToServer(clientBackend *pgproto3.Backend, ser p.trackClientMessage(msg) if p.masking() { - msg = rejectCopyToClient(msg) if describe := p.trackDescribe(msg); describe != nil { serverFrontend.Send(describe) } diff --git a/packages/pam/handlers/postgres_masking.go b/packages/pam/handlers/postgres_masking.go index 75ad1d81c..a5b3dafb6 100644 --- a/packages/pam/handlers/postgres_masking.go +++ b/packages/pam/handlers/postgres_masking.go @@ -4,9 +4,6 @@ import ( "bytes" "encoding/hex" "encoding/json" - "regexp" - "slices" - "strings" "github.com/Infisical/infisical-merge/packages/pam/session/masking" "github.com/jackc/pgx/v5/pgproto3" @@ -42,105 +39,6 @@ func (p *PostgresProxy) masking() bool { return p.config.OutputMasker != nil } -// Fails on the server, so the batch a COPY to the client sits in aborts and nothing in it commits. -const blockedCopyStatement = `DO $$ BEGIN RAISE EXCEPTION USING ERRCODE = '42501', ` + - `MESSAGE = 'COPY TO STDOUT is blocked because this account masks output, use SELECT instead'; END $$` - -var copyToClientPattern = regexp.MustCompile(`(?is)^\s*copy\b.*\bto\s+stdout\b`) - -var dollarQuoteTag = regexp.MustCompile(`^\$([A-Za-z_][A-Za-z0-9_]*)?\$`) - -func rejectCopyToClient(msg pgproto3.FrontendMessage) pgproto3.FrontendMessage { - switch m := msg.(type) { - case *pgproto3.Query: - if copiesToClient(m.String) { - return &pgproto3.Query{String: blockedCopyStatement} - } - case *pgproto3.Parse: - if copiesToClient(m.Query) { - return &pgproto3.Parse{Name: m.Name, Query: blockedCopyStatement} - } - } - return msg -} - -func copiesToClient(sql string) bool { - return slices.ContainsFunc(strings.Split(blankQuoted(sql), ";"), copyToClientPattern.MatchString) -} - -func blankQuoted(sql string) string { - out := []byte(sql) - blank := func(from, to int) int { - to = min(to, len(out)) - for k := from; k < to; k++ { - out[k] = ' ' - } - return to - } - for i := 0; i < len(out); { - switch { - case out[i] == '\'': - backslashEscapes := i > 0 && (out[i-1] == 'E' || out[i-1] == 'e') - j := i + 1 - for ; j < len(out); j++ { - if backslashEscapes && out[j] == '\\' { - j++ - } else if out[j] == '\'' { - if j+1 < len(out) && out[j+1] == '\'' { - j++ - continue - } - break - } - } - i = blank(i, j+1) - case out[i] == '"': - j := i + 1 - for ; j < len(out); j++ { - if out[j] == '"' { - if j+1 < len(out) && out[j+1] == '"' { - j++ - continue - } - break - } - } - i = blank(i, j+1) - case out[i] == '-' && i+1 < len(out) && out[i+1] == '-': - j := bytes.IndexByte(out[i:], '\n') - if j < 0 { - j = len(out) - i - } - i = blank(i, i+j) - case out[i] == '/' && i+1 < len(out) && out[i+1] == '*': - depth, j := 1, i+2 - for ; j < len(out) && depth > 0; j++ { - if out[j] == '/' && j+1 < len(out) && out[j+1] == '*' { - depth, j = depth+1, j+1 - } else if out[j] == '*' && j+1 < len(out) && out[j+1] == '/' { - depth, j = depth-1, j+1 - } - } - i = blank(i, j) - case out[i] == '$' && (i == 0 || !isIdentifierByte(out[i-1])): - tag := dollarQuoteTag.Find(out[i:]) - if tag == nil { - i++ - continue - } - end := bytes.Index(out[i+len(tag):], tag) - if end < 0 { - i = blank(i, len(out)) - continue - } - i = blank(i, i+len(tag)+end+len(tag)) - default: - i++ - } - } - return string(out) -} - // Each Execute gets a portal Describe sent ahead of it, so its rows always follow a RowDescription. func (p *PostgresProxy) trackDescribe(msg pgproto3.FrontendMessage) *pgproto3.Describe { p.mutex.Lock() @@ -312,10 +210,6 @@ func keepsValidJSON(column pgColumnFormat, masked []byte) bool { return true } -func isIdentifierByte(b byte) bool { - return b == '_' || b == '$' || (b >= '0' && b <= '9') || (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b >= 0x80 -} - // Severity, code and source location come from the server, and clients act on them. func maskErrorFields(masker masking.Masker, e *pgproto3.ErrorResponse) { maskStrings(masker, &e.Message, &e.Detail, &e.Hint, &e.Where, &e.InternalQuery, &e.SchemaName, &e.TableName, diff --git a/packages/pam/handlers/postgres_masking_test.go b/packages/pam/handlers/postgres_masking_test.go index 94f668730..800d0053d 100644 --- a/packages/pam/handlers/postgres_masking_test.go +++ b/packages/pam/handlers/postgres_masking_test.go @@ -289,38 +289,3 @@ func TestMaskingChecksFunctionCallResults(t *testing.T) { } } } - -func TestCopiesToClient(t *testing.T) { - for sql, want := range map[string]bool{ - "COPY cards TO STDOUT": true, - "copy (select card from cards) to stdout with (format csv)": true, - "SELECT 1; /* note */ COPY cards TO STDOUT; INSERT INTO log VALUES (1)": true, - "COPY cards FROM STDIN": false, - "COPY cards TO '/tmp/cards.csv'": false, - "SELECT 'COPY cards TO STDOUT'": false, - "SELECT E'it\\'s; COPY cards TO STDOUT'": false, - "SELECT $$COPY cards TO STDOUT$$": false, - "SELECT $body$; COPY cards TO STDOUT$body$": false, - "SELECT \"COPY x TO STDOUT\" FROM t": false, - "-- COPY cards TO STDOUT\nSELECT 1": false, - "SELECT a$b$c FROM t; COPY cards TO STDOUT": true, - } { - if got := copiesToClient(sql); got != want { - t.Errorf("copiesToClient(%q) = %v, want %v", sql, got, want) - } - } -} - -func TestMaskingSwapsCopyToClientBeforeItRuns(t *testing.T) { - query := rejectCopyToClient(&pgproto3.Query{String: "COPY cards TO STDOUT; INSERT INTO log VALUES (1)"}) - if q := query.(*pgproto3.Query).String; q != blockedCopyStatement { - t.Fatalf("query forwarded as %q", q) - } - parse := rejectCopyToClient(&pgproto3.Parse{Name: "s1", Query: "COPY cards TO STDOUT"}).(*pgproto3.Parse) - if parse.Name != "s1" || parse.Query != blockedCopyStatement { - t.Fatalf("parse forwarded as %+v", parse) - } - if kept := rejectCopyToClient(&pgproto3.Query{String: "SELECT 1"}).(*pgproto3.Query); kept.String != "SELECT 1" { - t.Fatalf("plain query rewritten to %q", kept.String) - } -} From f3bc7ff7448b608cc10570b5744601fab2e4acc3 Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 12 Nov 2026 00:20:17 -0500 Subject: [PATCH 08/11] final tweaks --- packages/pam/handlers/postgres_masking.go | 10 +---- .../pam/handlers/postgres_masking_test.go | 2 +- packages/pam/handlers/ssh/output_masking.go | 30 ++++--------- .../pam/handlers/ssh/output_masking_test.go | 45 ++++--------------- packages/pam/handlers/ssh/proxy.go | 5 +-- 5 files changed, 21 insertions(+), 71 deletions(-) diff --git a/packages/pam/handlers/postgres_masking.go b/packages/pam/handlers/postgres_masking.go index a5b3dafb6..3d008a505 100644 --- a/packages/pam/handlers/postgres_masking.go +++ b/packages/pam/handlers/postgres_masking.go @@ -2,7 +2,6 @@ package handlers import ( "bytes" - "encoding/hex" "encoding/json" "github.com/Infisical/infisical-merge/packages/pam/session/masking" @@ -118,14 +117,7 @@ func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3. Message: "Replication is blocked because this account masks output", } case *pgproto3.FunctionCallResponse: - // The result's type isn't sent, so a hex bytea result is matched as the bytes it encodes. - text := m.Result - if hexText, ok := bytes.CutPrefix(m.Result, []byte(`\x`)); ok { - if decoded, err := hex.DecodeString(string(hexText)); err == nil { - text = decoded - } - } - if masked := masker.Mask(text); !bytes.Equal(masked, text) { + if masked := masker.Mask(m.Result); !bytes.Equal(masked, m.Result) { m.Result = nil } case *pgproto3.NotificationResponse: diff --git a/packages/pam/handlers/postgres_masking_test.go b/packages/pam/handlers/postgres_masking_test.go index 800d0053d..7cc6f86b6 100644 --- a/packages/pam/handlers/postgres_masking_test.go +++ b/packages/pam/handlers/postgres_masking_test.go @@ -282,7 +282,7 @@ func TestMaskingMatchesByteaContent(t *testing.T) { func TestMaskingChecksFunctionCallResults(t *testing.T) { p := newMaskingProxy() - for _, result := range [][]byte{[]byte("a secret"), []byte(`\x6120736563726574`)} { + for _, result := range [][]byte{[]byte("a secret"), []byte("card 4111111111111111")} { _, forwarded := relay(p, nil, []pgproto3.BackendMessage{&pgproto3.FunctionCallResponse{Result: result}}) if got := forwarded[0].(*pgproto3.FunctionCallResponse).Result; got != nil { t.Fatalf("function result %q forwarded as %q, want NULL", result, got) diff --git a/packages/pam/handlers/ssh/output_masking.go b/packages/pam/handlers/ssh/output_masking.go index 049401e58..0bd2e745a 100644 --- a/packages/pam/handlers/ssh/output_masking.go +++ b/packages/pam/handlers/ssh/output_masking.go @@ -77,7 +77,6 @@ func (p *SSHProxy) proxyMaskedOutput(src io.Reader, dst io.Writer, sessionID str return err } pending = append(pending[:0], pending[cut:]...) - chState.outputHeld.Store(int64(len(pending))) if c.err == io.EOF { return nil } @@ -89,7 +88,6 @@ func (p *SSHProxy) proxyMaskedOutput(src io.Reader, dst io.Writer, sessionID str return err } pending = pending[:0] - chState.outputHeld.Store(0) } } } @@ -145,31 +143,19 @@ func maskTerminalOutput(patterns []*regexp.Regexp, data []byte) []byte { return append(out, data[last:]...) } +// Only CSI sequences (color, cursor) are skipped whole, so a title's or link's text is still matched. func escapeLength(data []byte, i int) int { j := i + 1 if j >= len(data) { return 1 } - switch data[j] { - case '[': + if data[j] != '[' { + return 2 + } + for j++; j < len(data) && data[j] >= 0x20 && data[j] <= 0x3f; j++ { + } + if j < len(data) && data[j] >= 0x40 && data[j] <= 0x7e { j++ - for j < len(data) && data[j] >= 0x20 && data[j] <= 0x3f { - j++ - } - if j < len(data) && data[j] >= 0x40 && data[j] <= 0x7e { - j++ - } - return j - i - case ']', 'P', 'X', '^', '_': - for j++; j < len(data); j++ { - if data[j] == 0x07 { - return j + 1 - i - } - if data[j] == 0x1b && j+1 < len(data) && data[j+1] == '\\' { - return j + 2 - i - } - } - return len(data) - i } - return 2 + return j - i } diff --git a/packages/pam/handlers/ssh/output_masking_test.go b/packages/pam/handlers/ssh/output_masking_test.go index 977495ca5..063425f2a 100644 --- a/packages/pam/handlers/ssh/output_masking_test.go +++ b/packages/pam/handlers/ssh/output_masking_test.go @@ -4,12 +4,9 @@ import ( "bytes" "io" "regexp" - "strings" "sync" "testing" "time" - - "github.com/Infisical/infisical-merge/packages/pam/session" ) type lockedBuffer struct { @@ -94,42 +91,18 @@ func TestMaskedOutputMatchesThroughColorCodes(t *testing.T) { } } -func TestMaskedOutputDoesNotRecordAHeldEchoTwice(t *testing.T) { - logger := &recordingLogger{} - proxy := NewSSHProxy(SSHProxyConfig{ - SessionID: "sid", - SessionLogger: logger, - OutputMaskingPatterns: []*regexp.Regexp{regexp.MustCompile(`token=\w+`)}, - }) - chState := newChannelState() - chState.channelType = session.SessionChannelShell - chState.inputChannelType = session.SessionChannelShell - +func TestMaskedOutputMatchesInsideTerminalTitles(t *testing.T) { src, w := io.Pipe() - done := make(chan error, 1) - go func() { done <- proxy.proxyData(src, io.Discard, "server→client", "sid", false, chState) }() + out := &lockedBuffer{} + go func() { + _, _ = w.Write([]byte("\x1b]0;token=abcd123\x07\n")) + _ = w.Close() + }() - _, _ = w.Write([]byte("$ ")) - time.Sleep(4 * maskedOutputQuietPeriod) - proxy.bufferInput([]byte("ls"), "sid", session.SessionChannelShell, chState) - _, _ = w.Write([]byte("ls")) - time.Sleep(maskedOutputQuietPeriod / 5) - proxy.bufferInput([]byte{0x0D}, "sid", session.SessionChannelShell, chState) - _, _ = w.Write([]byte("\r\nfile.txt\r\n$ ")) - _ = w.Close() - if err := <-done; err != nil { + if err := newOutputMaskingProxy().proxyData(src, out, "server→client", "sid", false, newChannelState()); err != nil { t.Fatal(err) } - - recorded := 0 - logger.mutex.Lock() - for _, event := range logger.events { - if strings.Contains(string(event.Data), "ls") { - recorded++ - } - } - logger.mutex.Unlock() - if recorded != 1 { - t.Fatalf("command recorded %d times, want once: %+v", recorded, logger.events) + if got, want := out.String(), "\x1b]0;[MASKED]\x07\n"; got != want { + t.Fatalf("client saw %q, want %q", got, want) } } diff --git a/packages/pam/handlers/ssh/proxy.go b/packages/pam/handlers/ssh/proxy.go index 1b96d64a4..6bb6c503b 100644 --- a/packages/pam/handlers/ssh/proxy.go +++ b/packages/pam/handlers/ssh/proxy.go @@ -56,7 +56,6 @@ type channelState struct { outputParser *terminalTranscript // Renders the output stream into displayed lines outputChannelType session.SessionChannelType // Channel type for buffered output outputPending atomic.Int64 // Rendered length of the line still on screen - outputHeld atomic.Int64 // Output held back for masking, not yet rendered } func newChannelState() *channelState { @@ -696,8 +695,8 @@ func (p *SSHProxy) flushInputBufferUnsafe(sessionID string, chState *channelStat switch { case onScreen > chState.echoBaseline: // Echoed while typing, so that line is the record. - case pasted || chState.outputHeld.Load() > 0: - // Delivered ahead of any echo, or the echo is still held for masking; the next committed line decides. + case pasted: + // Delivered ahead of any echo; the next committed line decides. chState.pendingEcho.hold(command, channel, onScreen) default: p.flushOutputBuffer(sessionID, chState) From c7f90ac96e1b9cfea59fd26e87a3e455e36ddca5 Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 12 Nov 2026 00:32:50 -0500 Subject: [PATCH 09/11] fix veria comment --- packages/pam/handlers/postgres_masking.go | 13 ++++++++++--- packages/pam/handlers/postgres_masking_test.go | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/pam/handlers/postgres_masking.go b/packages/pam/handlers/postgres_masking.go index 3d008a505..3f174b592 100644 --- a/packages/pam/handlers/postgres_masking.go +++ b/packages/pam/handlers/postgres_masking.go @@ -136,7 +136,14 @@ func (p *PostgresProxy) maskServerMessage(msg pgproto3.BackendMessage) pgproto3. } // Matches the content a client reads out of a value, so binary encoding or bytea's hex can't hide a match. -func (p *PostgresProxy) matchText(column pgColumnFormat, value []byte) []byte { +func (p *PostgresProxy) matchText(column pgColumnFormat, value []byte) (text []byte) { + // pgx panics on some malformed values, and a panic here would take down the whole gateway. + defer func() { + if recover() != nil { + text = value + } + }() + isBytea := column.typeOID == pgtype.ByteaOID || column.typeOID == pgtype.ByteaArrayOID if pgTextTypes[column.typeOID] || (!column.binary && !isBytea) { return value @@ -159,8 +166,8 @@ func (p *PostgresProxy) matchText(column pgColumnFormat, value []byte) []byte { if content, ok := appendContent(nil, decoded); ok { return content } - if text, err := p.typeMap.Encode(column.typeOID, pgtype.TextFormatCode, decoded, nil); err == nil { - return text + if encoded, err := p.typeMap.Encode(column.typeOID, pgtype.TextFormatCode, decoded, nil); err == nil { + return encoded } return value } diff --git a/packages/pam/handlers/postgres_masking_test.go b/packages/pam/handlers/postgres_masking_test.go index 7cc6f86b6..a2695dcc0 100644 --- a/packages/pam/handlers/postgres_masking_test.go +++ b/packages/pam/handlers/postgres_masking_test.go @@ -1,6 +1,7 @@ package handlers import ( + "encoding/binary" "regexp" "testing" @@ -289,3 +290,19 @@ func TestMaskingChecksFunctionCallResults(t *testing.T) { } } } + +func TestMaskingSurvivesAMalformedBinaryArray(t *testing.T) { + header := make([]byte, 20) + binary.BigEndian.PutUint32(header[0:], 1) + binary.BigEndian.PutUint32(header[8:], pgtype.Int4OID) + binary.BigEndian.PutUint32(header[12:], 1) + binary.BigEndian.PutUint32(header[16:], 1) + + p := newMaskingProxy() + _, forwarded := relay(p, + []pgproto3.FrontendMessage{&pgproto3.Execute{}, &pgproto3.Sync{}}, + []pgproto3.BackendMessage{rowDescription(1, pgtype.Int4ArrayOID), dataRow(header)}) + if len(forwarded) != 1 { + t.Fatalf("forwarded %d messages, want the row", len(forwarded)) + } +} From d7edd11ffca6386102cabfa327ea2f43505142d6 Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 12 Nov 2026 16:40:55 -0500 Subject: [PATCH 10/11] two new mask presets --- packages/api/model.go | 7 ++++++- packages/pam/pam-proxy.go | 5 +++++ packages/pam/session/masking/credentials.go | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/api/model.go b/packages/api/model.go index e156da424..75b117925 100644 --- a/packages/api/model.go +++ b/packages/api/model.go @@ -1104,9 +1104,14 @@ type PAMSessionLogMaskingConfig struct { BuiltInDetection bool `json:"builtInDetection"` } +type PAMOutputMaskingConfig struct { + Patterns []string `json:"patterns"` + AccountCredentials bool `json:"accountCredentials"` +} + type PAMPolicyRules struct { CommandBlocking *PAMPolicyRuleConfig `json:"command-blocking,omitempty"` - OutputMasking *PAMPolicyRuleConfig `json:"output-masking,omitempty"` + OutputMasking *PAMOutputMaskingConfig `json:"output-masking,omitempty"` SessionLogMasking *PAMSessionLogMaskingConfig `json:"session-log-masking,omitempty"` } diff --git a/packages/pam/pam-proxy.go b/packages/pam/pam-proxy.go index 3c1326642..0f7e9dc10 100644 --- a/packages/pam/pam-proxy.go +++ b/packages/pam/pam-proxy.go @@ -282,6 +282,11 @@ func HandlePAMProxy(ctx context.Context, conn *tls.Conn, pamConfig *GatewayPAMCo } outputPatterns = append(outputPatterns, re) } + if credentials.PolicyRules.OutputMasking.AccountCredentials { + for _, secret := range masking.CredentialSecrets(credentialValues(credentials)) { + outputPatterns = append(outputPatterns, regexp.MustCompile(regexp.QuoteMeta(secret))) + } + } if len(outputPatterns) > 0 { outputMasker = masking.New(outputPatterns, false, nil, pamConfig.SessionId) } diff --git a/packages/pam/session/masking/credentials.go b/packages/pam/session/masking/credentials.go index ae119aeab..0ff40e81c 100644 --- a/packages/pam/session/masking/credentials.go +++ b/packages/pam/session/masking/credentials.go @@ -56,6 +56,14 @@ func (m *credentialMasker) Mask(data []byte) []byte { return []byte(masked) } +// CredentialSecrets is what newCredentialMasker would redact, for callers that match with patterns. +func CredentialSecrets(values []string) []string { + if m := newCredentialMasker(values); m != nil { + return m.secrets + } + return nil +} + // newCredentialMasker returns nil when nothing is worth redacting, so callers can skip the stage. func newCredentialMasker(values []string) *credentialMasker { seen := make(map[string]struct{}, len(values)) From eb9b35e2f804d58898e7d11c5fc87e9550159457 Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 12 Nov 2026 16:57:01 -0500 Subject: [PATCH 11/11] checksum for card numbers --- packages/api/model.go | 1 + packages/pam/handlers/ssh/output_masking.go | 3 +- .../pam/handlers/ssh/output_masking_test.go | 4 +- packages/pam/handlers/ssh/proxy.go | 7 +- packages/pam/pam-proxy.go | 20 +++-- packages/pam/session/masking/output.go | 90 +++++++++++++++++++ packages/pam/session/masking/output_test.go | 31 +++++++ 7 files changed, 142 insertions(+), 14 deletions(-) create mode 100644 packages/pam/session/masking/output.go create mode 100644 packages/pam/session/masking/output_test.go diff --git a/packages/api/model.go b/packages/api/model.go index 75b117925..1e511c5a9 100644 --- a/packages/api/model.go +++ b/packages/api/model.go @@ -1107,6 +1107,7 @@ type PAMSessionLogMaskingConfig struct { type PAMOutputMaskingConfig struct { Patterns []string `json:"patterns"` AccountCredentials bool `json:"accountCredentials"` + CreditCards bool `json:"creditCards"` } type PAMPolicyRules struct { diff --git a/packages/pam/handlers/ssh/output_masking.go b/packages/pam/handlers/ssh/output_masking.go index 0bd2e745a..1ca2e34fb 100644 --- a/packages/pam/handlers/ssh/output_masking.go +++ b/packages/pam/handlers/ssh/output_masking.go @@ -3,7 +3,6 @@ package ssh import ( "bytes" "io" - "regexp" "sort" "time" @@ -93,7 +92,7 @@ func (p *SSHProxy) proxyMaskedOutput(src io.Reader, dst io.Writer, sessionID str } // Skips escape sequences when matching so color can't split a value, and keeps them after the placeholder. -func maskTerminalOutput(patterns []*regexp.Regexp, data []byte) []byte { +func maskTerminalOutput(patterns []masking.Matcher, data []byte) []byte { plain := make([]byte, 0, len(data)) offsets := make([]int, 0, len(data)) for i := 0; i < len(data); { diff --git a/packages/pam/handlers/ssh/output_masking_test.go b/packages/pam/handlers/ssh/output_masking_test.go index 063425f2a..c2a23f8e0 100644 --- a/packages/pam/handlers/ssh/output_masking_test.go +++ b/packages/pam/handlers/ssh/output_masking_test.go @@ -7,6 +7,8 @@ import ( "sync" "testing" "time" + + "github.com/Infisical/infisical-merge/packages/pam/session/masking" ) type lockedBuffer struct { @@ -30,7 +32,7 @@ func newOutputMaskingProxy() *SSHProxy { return NewSSHProxy(SSHProxyConfig{ SessionID: "sid", SessionLogger: &recordingLogger{}, - OutputMaskingPatterns: []*regexp.Regexp{ + OutputMaskingPatterns: []masking.Matcher{ regexp.MustCompile(`token=\w+`), regexp.MustCompile(`[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}`), }, diff --git a/packages/pam/handlers/ssh/proxy.go b/packages/pam/handlers/ssh/proxy.go index 6bb6c503b..3a9325639 100644 --- a/packages/pam/handlers/ssh/proxy.go +++ b/packages/pam/handlers/ssh/proxy.go @@ -12,6 +12,7 @@ import ( "time" "github.com/Infisical/infisical-merge/packages/pam/session" + "github.com/Infisical/infisical-merge/packages/pam/session/masking" "github.com/Infisical/infisical-merge/packages/util" "github.com/rs/zerolog/log" "golang.org/x/crypto/ssh" @@ -27,9 +28,9 @@ type SSHProxyConfig struct { InjectCertificate string SessionID string SessionLogger session.SessionLogger - BlockedCommandPatterns []*regexp.Regexp // Regex patterns for command blocking (nil = no blocking) - OutputMaskingPatterns []*regexp.Regexp // Regex patterns for output masking (nil = no masking) - OnActivity func() // Called when channel data flows + BlockedCommandPatterns []*regexp.Regexp // Regex patterns for command blocking (nil = no blocking) + OutputMaskingPatterns []masking.Matcher // Matchers for output masking (nil = no masking) + OnActivity func() // Called when channel data flows } // SSHProxy handles proxying SSH connections with credential injection diff --git a/packages/pam/pam-proxy.go b/packages/pam/pam-proxy.go index 0f7e9dc10..9f7de19db 100644 --- a/packages/pam/pam-proxy.go +++ b/packages/pam/pam-proxy.go @@ -271,24 +271,28 @@ func HandlePAMProxy(ctx context.Context, conn *tls.Conn, pamConfig *GatewayPAMCo ) } - var outputPatterns []*regexp.Regexp + var outputMatchers []masking.Matcher var outputMasker masking.Masker if credentials.PolicyRules != nil && credentials.PolicyRules.OutputMasking != nil { - for i, pattern := range credentials.PolicyRules.OutputMasking.Patterns { + rule := credentials.PolicyRules.OutputMasking + for i, pattern := range rule.Patterns { re, err := regexp.Compile(pattern) if err != nil { // Fail closed, and keep the pattern out of the error since it can hold the value it masks. return fmt.Errorf("output masking pattern %d does not compile", i+1) } - outputPatterns = append(outputPatterns, re) + outputMatchers = append(outputMatchers, re) } - if credentials.PolicyRules.OutputMasking.AccountCredentials { + if rule.AccountCredentials { for _, secret := range masking.CredentialSecrets(credentialValues(credentials)) { - outputPatterns = append(outputPatterns, regexp.MustCompile(regexp.QuoteMeta(secret))) + outputMatchers = append(outputMatchers, regexp.MustCompile(regexp.QuoteMeta(secret))) } } - if len(outputPatterns) > 0 { - outputMasker = masking.New(outputPatterns, false, nil, pamConfig.SessionId) + if rule.CreditCards { + outputMatchers = append(outputMatchers, masking.CardNumbers()) + } + if len(outputMatchers) > 0 { + outputMasker = masking.NewMatcherMasker(outputMatchers) } } @@ -442,7 +446,7 @@ func HandlePAMProxy(ctx context.Context, conn *tls.Conn, pamConfig *GatewayPAMCo SessionID: pamConfig.SessionId, SessionLogger: sessionLogger, BlockedCommandPatterns: blockedCommandPatterns, - OutputMaskingPatterns: outputPatterns, + OutputMaskingPatterns: outputMatchers, OnActivity: pamConfig.OnActivity, } proxy := ssh.NewSSHProxy(sshConfig) diff --git a/packages/pam/session/masking/output.go b/packages/pam/session/masking/output.go new file mode 100644 index 000000000..7484c3de2 --- /dev/null +++ b/packages/pam/session/masking/output.go @@ -0,0 +1,90 @@ +package masking + +import ( + "slices" + "regexp" + "sort" +) + +// Matcher finds the spans to mask. *regexp.Regexp is one. +type Matcher interface { + FindAllIndex(b []byte, n int) [][]int +} + +var cardNumber = regexp.MustCompile(`\b(?:\d{4}[ -]?){3}\d{4}\b|\b3[47]\d{2}[ -]?\d{6}[ -]?\d{5}\b`) + +type cardMatcher struct{} + +// CardNumbers matches card-shaped numbers that pass the Luhn checksum, so other long digit runs stay visible. +func CardNumbers() Matcher { + return cardMatcher{} +} + +func (cardMatcher) FindAllIndex(b []byte, n int) [][]int { + var valid [][]int + for _, loc := range cardNumber.FindAllIndex(b, n) { + if luhnValid(b[loc[0]:loc[1]]) { + valid = append(valid, loc) + } + } + return valid +} + +func luhnValid(number []byte) bool { + sum, digits := 0, 0 + for _, n := range slices.Backward(number) { + if n < '0' || n > '9' { + continue + } + digit := int(n - '0') + if digits%2 == 1 { + if digit *= 2; digit > 9 { + digit -= 9 + } + } + sum += digit + digits++ + } + return sum%10 == 0 +} + +type matcherMasker struct { + matchers []Matcher +} + +// NewMatcherMasker masks every span any matcher finds, merging overlaps, so the order of matchers doesn't matter. +func NewMatcherMasker(matchers []Matcher) Masker { + return matcherMasker{matchers: matchers} +} + +func (m matcherMasker) Mask(data []byte) []byte { + var spans [][2]int + for _, matcher := range m.matchers { + for _, loc := range matcher.FindAllIndex(data, -1) { + if loc[1] > loc[0] { + spans = append(spans, [2]int{loc[0], loc[1]}) + } + } + } + if len(spans) == 0 { + return data + } + sort.Slice(spans, func(a, b int) bool { return spans[a][0] < spans[b][0] }) + + out := make([]byte, 0, len(data)) + last := 0 + for i := 0; i < len(spans); { + start, end := spans[i][0], spans[i][1] + for i++; i < len(spans) && spans[i][0] <= end; i++ { + end = max(end, spans[i][1]) + } + out = append(out, data[last:start]...) + out = append(out, Placeholder...) + last = end + } + return append(out, data[last:]...) +} + +func (m matcherMasker) MaskString(s string) string { + return string(m.Mask([]byte(s))) +} diff --git a/packages/pam/session/masking/output_test.go b/packages/pam/session/masking/output_test.go new file mode 100644 index 000000000..ba6dc9931 --- /dev/null +++ b/packages/pam/session/masking/output_test.go @@ -0,0 +1,31 @@ +package masking + +import ( + "regexp" + "testing" +) + +func TestCardNumbersNeedAValidChecksum(t *testing.T) { + masker := NewMatcherMasker([]Matcher{CardNumbers()}) + for input, want := range map[string]string{ + "card 4111 1111 1111 1111": "card [MASKED]", + "card 4111-1111-1111-1111": "card [MASKED]", + "amex 3782 822463 10005": "amex [MASKED]", + "order 4111111111111112": "order 4111111111111112", + "order 1234 5678 9012 3456": "order 1234 5678 9012 3456", + } { + if got := masker.MaskString(input); got != want { + t.Errorf("MaskString(%q) = %q, want %q", input, got, want) + } + } +} + +func TestMatcherMaskerMergesOverlappingMatches(t *testing.T) { + masker := NewMatcherMasker([]Matcher{ + regexp.MustCompile(`[A-Za-z][A-Za-z0-9+.-]*://[^\s:/@]*:\S*@`), + regexp.MustCompile(`\b(?:\d{1,3}\.){3}\d{1,3}\b`), + }) + if got := masker.MaskString("postgres://svc:hunter2@10.0.4.12:5432/app"); got != "[MASKED]:5432/app" { + t.Fatalf("got %q", got) + } +}