bouncer

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package bouncer provides token validation, secret pattern matching, and streaming redaction for MCP proxy request/response filtering and security enforcement.

Index

Constants

View Source
const SecretRedacted = "[SECRET_REDACTED]"

Variables

View Source
var (
	// ErrInvalidToken indicates the provided token is invalid.
	ErrInvalidToken = errors.New("invalid token")
	// ErrExpiredToken indicates the provided token has expired.
	ErrExpiredToken = errors.New("expired token")
	// ErrMissingToken indicates no token was provided.
	ErrMissingToken = errors.New("missing token")
)
View Source
var BuiltInPatterns = []SecretPattern{
	{
		Name:        "aws-access-key",
		Pattern:     regexp.MustCompile(`AKIA[0-9A-Z]{16}`),
		Example:     "AKIAIOSFODNN7EXAMPLE",
		Description: "AWS Access Key ID (20 characters, starts with AKIA)",
	},
	{
		Name:        "github-classic-pat",
		Pattern:     regexp.MustCompile(`ghp_[A-Za-z0-9]{36}`),
		Example:     "ghp_abcdefghijklmnopqrstuvwxyz1234567890abcd",
		Description: "GitHub Classic Personal Access Token (starts with ghp_)",
	},
	{
		Name:        "github-fine-grained-pat",
		Pattern:     regexp.MustCompile(`github_pat_[A-Za-z0-9_]{22,}`),
		Example:     "github_pat_11abcdefghIJ9xsQ_xxxxxxxxxxxxxxxxx",
		Description: "GitHub Fine-grained PAT (starts with github_pat_)",
	},
	{
		Name:        "stripe-secret-key",
		Pattern:     regexp.MustCompile(`sk_live_[A-Za-z0-9]{24}`),
		Example:     "[Stripe Live Secret Key - 24 chars after sk_live_]",
		Description: "Stripe Live Secret Key (starts with sk_live_)",
	},
	{
		Name:        "stripe-publishable-key",
		Pattern:     regexp.MustCompile(`pk_live_[A-Za-z0-9]{24}`),
		Example:     "[Stripe Publishable Key - 24 chars after pk_live_]",
		Description: "Stripe Live Publishable Key (starts with pk_live_)",
	},
	{
		Name:        "generic-api-key",
		Pattern:     regexp.MustCompile(`(?i)(api[_-]?key)[_-]?[=]?[A-Za-z0-9]{16,}`),
		Example:     "api_key=abcdefghijklmnopqrstuvwx",
		Description: "Generic API key pattern (case-insensitive)",
	},
	{
		Name:        "bearer-token",
		Pattern:     regexp.MustCompile(`(?i)bearer\s+[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+`),
		Example:     "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
		Description: "JWT Bearer token (three base64url segments)",
	},
	{
		Name:        "env-var-value",
		Pattern:     regexp.MustCompile(`\$[A-Z_][A-Z0-9_]{0,30}=([^\s,}]+)`),
		Example:     "$API_KEY=secret123",
		Description: "Environment variable assignment",
	},
}
View Source
var (
	ErrDangerousPattern = errors.New("dangerous regex pattern detected")
)

Functions

func CompileCustomPatterns added in v0.2.0

func CompileCustomPatterns(configs []PatternConfig) ([]*regexp.Regexp, error)

func CompileCustomPatternsWithTimeout added in v0.2.0

func CompileCustomPatternsWithTimeout(configs []PatternConfig, timeout time.Duration) ([]*regexp.Regexp, error)

func CompilePatterns added in v0.2.0

func CompilePatterns(configs []PatternConfig) ([]*regexp.Regexp, error)

func DetectLanguage added in v0.2.0

func DetectLanguage(filename string) string

func FindDangerousPatterns added in v0.6.0

func FindDangerousPatterns(patterns []string) (unsafe []string, err error)

func FormatPatternList added in v0.2.0

func FormatPatternList() string

func GetBuffer added in v0.2.0

func GetBuffer() []byte

func GetPatternNames added in v0.2.0

func GetPatternNames() []string

func IsPatternSafe added in v0.6.0

func IsPatternSafe(pattern string) bool

func LoadPatternsWithLogging added in v0.2.0

func LoadPatternsWithLogging(customConfigs []PatternConfig) ([]*regexp.Regexp, []string)

func MatchSecret added in v0.2.0

func MatchSecret(input string) []string

func PatternsToRegexps added in v0.2.0

func PatternsToRegexps(patterns []SecretPattern) []*regexp.Regexp

func RedactSecrets added in v0.2.0

func RedactSecrets(input string) string

func RedactWithPatterns added in v0.2.0

func RedactWithPatterns(input string, patterns []*regexp.Regexp) string

func ReturnBuffer added in v0.2.0

func ReturnBuffer(buf []byte)

func SafeCompile added in v0.6.0

func SafeCompile(pattern string) (*regexp.Regexp, error)

func SafeCompileMust added in v0.6.0

func SafeCompileMust(pattern string) *regexp.Regexp

func StripComments added in v0.6.0

func StripComments(pattern string) string

func ValidatePattern added in v0.6.0

func ValidatePattern(pattern string) error

func ValidatePatterns added in v0.2.0

func ValidatePatterns() error

func ValidateReDoS added in v0.2.0

func ValidateReDoS(pattern string, timeout time.Duration) error

Types

type AlertManager added in v0.2.0

type AlertManager struct {
	// contains filtered or unexported fields
}

func NewAlertManager added in v0.2.0

func NewAlertManager(verbose bool) *AlertManager

func (*AlertManager) EmitSummary added in v0.2.0

func (am *AlertManager) EmitSummary(messageID string, method string)

func (*AlertManager) GetCounts added in v0.2.0

func (am *AlertManager) GetCounts() map[string]int

func (*AlertManager) RecordRedaction added in v0.2.0

func (am *AlertManager) RecordRedaction(event RedactionEvent)

func (*AlertManager) SetEnabled added in v0.2.0

func (am *AlertManager) SetEnabled(enabled bool)

func (*AlertManager) SetVerbose added in v0.2.0

func (am *AlertManager) SetVerbose(verbose bool)

type BoilerplateProcessor added in v0.2.0

type BoilerplateProcessor interface {
	Process(content []byte, language string) ([]byte, BoilerplateReport, error)
}

type BoilerplatePruner added in v0.2.0

type BoilerplatePruner struct {
	// contains filtered or unexported fields
}

func NewBoilerplatePruner added in v0.2.0

func NewBoilerplatePruner(enabled, redactImports, redactLicenses bool, customPatterns []PatternDef) *BoilerplatePruner

func (*BoilerplatePruner) Process added in v0.2.0

func (p *BoilerplatePruner) Process(content []byte, language string) ([]byte, BoilerplateReport, error)

func (*BoilerplatePruner) ProcessStream added in v0.2.0

func (p *BoilerplatePruner) ProcessStream(reader io.Reader, writer io.Writer, language string) (BoilerplateReport, error)

type BoilerplateReport added in v0.2.0

type BoilerplateReport struct {
	ImportsRedacted  int
	LicensesRedacted int
	OriginalSize     int
	ProcessedSize    int
	TokenSavings     int
}

type Bouncer

type Bouncer struct{}

Bouncer is a token validation and secret detection service.

func New

func New() *Bouncer

New creates a new Bouncer instance.

func (*Bouncer) ExtractClaims

func (b *Bouncer) ExtractClaims(ctx context.Context, token string) (Claims, error)

ExtractClaims extracts claims from a validated token.

func (*Bouncer) Validate

func (b *Bouncer) Validate(ctx context.Context, token string) error

Validate checks if the provided token is valid.

type Claims

type Claims map[string]interface{}

Claims represents the claims extracted from a validated token.

type Config added in v0.2.0

type Config struct {
	Enabled        bool         `yaml:"enabled"`
	CustomPatterns []PatternDef `yaml:"custom_patterns"`
}

Config defines the bouncer configuration, including whether the bouncer is enabled and any custom regex patterns for secret detection.

func LoadConfig added in v0.2.0

func LoadConfig(r io.Reader) (*Config, error)

LoadConfig reads and parses a bouncer YAML configuration from the provided reader.

func LoadConfigFile added in v0.2.0

func LoadConfigFile(path string) (*Config, error)

LoadConfigFile reads and parses a bouncer YAML configuration from the given file path.

func (*Config) CompilePatterns added in v0.2.0

func (c *Config) CompilePatterns() (*LoadedPatterns, error)

CompilePatterns compiles built-in and custom patterns into a LoadedPatterns struct.

type LoadedPatterns added in v0.2.0

type LoadedPatterns struct {
	BuiltIn []SecretPattern
	Custom  []SecretPattern
	All     []*regexp.Regexp
}

LoadedPatterns holds the compiled built-in and custom regex patterns.

type PatternConfig added in v0.2.0

type PatternConfig struct {
	Name    string `yaml:"name"`
	Pattern string `yaml:"pattern"`
}

func (PatternConfig) Validate added in v0.2.0

func (pc PatternConfig) Validate() error

type PatternDef added in v0.2.0

type PatternDef struct {
	Name    string `yaml:"name"`
	Pattern string `yaml:"pattern"`
}

PatternDef defines a custom regex pattern for secret detection.

type RedactionEvent added in v0.2.0

type RedactionEvent struct {
	PatternName string
	Count       int
	Timestamp   time.Time
	MessageID   string
	Method      string
}

type RedactionMeta added in v0.2.0

type RedactionMeta struct {
	MessageID string
	Method    string
}

type Redactor added in v0.1.2

type Redactor struct {
	// contains filtered or unexported fields
}

func NewRedactor added in v0.1.2

func NewRedactor(patterns []*regexp.Regexp) *Redactor

func NewRedactorFromLoaded added in v0.2.0

func NewRedactorFromLoaded(loaded *LoadedPatterns) *Redactor

func NewRedactorWithAlerts added in v0.2.0

func NewRedactorWithAlerts(patterns []*regexp.Regexp, alertManager *AlertManager) *Redactor

func (*Redactor) RedactJSON added in v0.1.2

func (r *Redactor) RedactJSON(data []byte) ([]byte, error)

func (*Redactor) RedactStream added in v0.1.2

func (r *Redactor) RedactStream(reader io.Reader, writer io.Writer, meta ...*RedactionMeta) error

type SecretPattern added in v0.2.0

type SecretPattern struct {
	Name        string
	Pattern     *regexp.Regexp
	Example     string
	Description string
}

func GetBuiltInPatterns added in v0.2.0

func GetBuiltInPatterns() []SecretPattern

func GetPatternByName added in v0.2.0

func GetPatternByName(name string) *SecretPattern

type StreamingRedactor added in v0.2.0

type StreamingRedactor struct {
	// contains filtered or unexported fields
}

func NewStreamingRedactor added in v0.2.0

func NewStreamingRedactor(patterns []*regexp.Regexp) *StreamingRedactor

func NewStreamingRedactorWithAlerts added in v0.2.0

func NewStreamingRedactorWithAlerts(patterns []*regexp.Regexp, am *AlertManager) *StreamingRedactor

func (*StreamingRedactor) RedactStream added in v0.2.0

func (sr *StreamingRedactor) RedactStream(r io.Reader, w io.Writer, meta ...*RedactionMeta) error

func (*StreamingRedactor) RedactToWriter added in v0.2.0

func (sr *StreamingRedactor) RedactToWriter(r io.Reader, w io.Writer) error

type TokenValidator

type TokenValidator interface {
	Validate(ctx context.Context, token string) error
	ExtractClaims(ctx context.Context, token string) (Claims, error)
}

TokenValidator validates tokens and extracts claims from them.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL