// Package diffscan applies the pinned Preview scanner's embedded default rules // to complete bounded text. It never reads repository config, ignore files or // baselines, and never returns findings or writes scanner output. package diffscan import ( "context" "regexp" "strings" "sync" "amadan.net/oficina/equipo/internal/runnerproto" "amadan.net/oficina/equipo/internal/wire" "github.com/rs/zerolog" "github.com/spf13/viper" "github.com/zricethezav/gitleaks/v8/config" "github.com/zricethezav/gitleaks/v8/detect" "github.com/zricethezav/gitleaks/v8/detect/codec" "github.com/zricethezav/gitleaks/v8/logging" "github.com/zricethezav/gitleaks/v8/sources" ) // Gitleaks' trace logger can contain matches. Disable this library's logger // permanently, not process logging, before any scan. Findings stay in memory. func init() { logging.Logger = zerolog.Nop() } // Cheap whole-file refusal for URL userinfo containing a nonempty password. var passwordURL = regexp.MustCompile(`[A-Za-z][A-Za-z0-9+.-]*://[^\s/:@]*:[^\s/@]+@[^\s/]+`) var privateHeader = regexp.MustCompile(`-----BEGIN [A-Z ]*PRIVATE KEY-----`) var rules = sync.OnceValues(func() (config.Config, error) { v := viper.New() v.SetConfigType("toml") if err := v.ReadConfig(strings.NewReader(config.DefaultConfig)); err != nil { return config.Config{}, err } var raw config.ViperConfig if err := v.Unmarshal(&raw); err != nil { return config.Config{}, err } return raw.Translate() }) func Check(ctx context.Context, text string, known []string) (reason string) { // A scanner panic is a failed scan, never a clean result. defer func() { if recover() != nil { reason = wire.DiffUnavailable } }() if ctx.Err() != nil { return wire.DiffUnavailable } if len(text) > wire.MaxDiffBlobBytes { return wire.DiffTooLarge } decoded := text var segments []*codec.EncodedSegment decoder := codec.NewDecoder() for depth := 0; depth <= 5; depth++ { if ctx.Err() != nil { return wire.DiffUnavailable } if passwordURL.MatchString(decoded) || privateHeader.MatchString(decoded) || runnerproto.RedactText(decoded, known) != decoded { return wire.DiffWithheld } decoded, segments = decoder.Decode(decoded, segments) if len(segments) == 0 { break } } cfg, err := rules() if err != nil { return wire.DiffUnavailable } found, err := scanText(ctx, cfg, text) if err != nil || ctx.Err() != nil { return wire.DiffUnavailable } if found { return wire.DiffWithheld } return "" } // Separate the detector call so failures and panics are exercised at the // scanner boundary, in addition to configuration failures. var scanText = func(ctx context.Context, cfg config.Config, text string) (bool, error) { d := detect.NewDetectorContext(ctx, cfg) d.IgnoreGitleaksAllow = true d.MaxDecodeDepth = 5 d.Redact = 100 found, err := d.DetectSource(ctx, boundedText(text)) return len(found) > 0, err } // Yield one complete in-memory fragment, with no path-based exemptions or // filesystem/archive reader. The caller has already enforced the input bound. type boundedText string func (text boundedText) Fragments(ctx context.Context, yield sources.FragmentsFunc) error { if err := ctx.Err(); err != nil { return err } return yield(sources.Fragment{Raw: string(text)}, nil) }